<?php
namespace app\admin\service\auth;

use app\admin\validate\auth\AuthRoleValidate;
use app\admin\service\AdminBaseService;
use app\model\AuthRoleModel;
use think\exception\ValidateException;

class AuthRoleService extends AdminBaseService
{

    public function add($param): array
    {
        try {
            // 参数校验
            validate(AuthRoleValidate::class)->scene('add')->check($param);
            // 查找角色是否存在
            $authRole=new AuthRoleModel();
            $rule = $authRole->where('name',$param['name'])->find();
            if(!empty($rule)){
                return $this->toData('200100', '名称已存在', []);
            }
            $authRole->save($param);
            return $this->toData('0', '添加成功.', []);
        }catch (ValidateException $validateException){
            $message = $validateException->getError();
            return $this->toData('100400', $message);
        }catch (\Exception $exception){
            return $this->toData('100500', '系统繁忙.', [$exception->getMessage()]);
        }

    }


    public function list($param){
        try {
            $authRole=new AuthRoleModel();
            //查询条件
            if(isset($param['name'])&&!empty($param['name'])){
                $authRole=$authRole->where('name',$param['name']);
            }
            if(isset($param['satus'])&&!empty($param['status'])){
                $authRole=$authRole->where('status',$param['status']);
            }
            // 总数
            $total = $authRole->count();
            $list=$authRole->order('sort', 'asc')->page($param['page'], $param['limit'])->select();
            for($i=0;$i<count($list);$i++){
                if($list[$i]['rules']){
                    $rules=explode(",", $list[$i]['rules']);
                    //字符串转换为数字类型
                    $list[$i]['rules']=array_map(function ($value){
                        return (int)$value;
                    },$rules);
                }
            }
            return $this->toData('0', 'SUCCESS', ['total' => $total, 'list' => $list]);
        } catch (\Exception $exception) {
            return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
        }
    }


    public function updateStatus($id, $param): array
    {
        try {
            // 主键
            if(empty($id) || $id <= 0){
                return $this->toData('200403', 'id字段丢失', []);
            }
            // 参数校验
            validate(AuthRoleValidate::class)->scene('updateStatus')->check($param);
            // 查找用户信息
            $authRole = AuthRoleModel::find($id);
            if(empty($authRole)){
                return $this->toData('200403', '该角色不存在.', []);
            }
            $authRole->status=$param['status'];
            $authRole->save();
            // 返回
            return $this->toData('0', 'Modification successful.', []);
        }catch (ValidateException $validateException){
            $message = $validateException->getError();
            return $this->toData('200400', $message);
        }catch (\Exception $exception){
            return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
        }

    }
    public function allList($param){
        try {
            $authRole=new AuthRoleModel();
            //查询条件
            if(isset($param['name'])&&!empty($param['name'])){
                $authRole=$authRole->where('name',$param['name']);
            }
            if(isset($param['satus'])&&!empty($param['status'])){
                $authRole=$authRole->where('status',$param['status']);
            }
            $list=$authRole->order('sort', 'asc')->column('name,id');
            return $this->toData('0', 'SUCCESS', $list);
        } catch (\Exception $exception) {
            return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
        }
    }

    public function edit($id, $param): array
    {
        try {
            // 主键
            if(empty($id) || $id <= 0){
                return $this->toData('200403', 'id字段丢失', []);
            }
            // 参数校验
            validate(AuthRoleValidate::class)->scene('edit')->check($param);
            // 查找用户信息
            $authRole = AuthRoleModel::find($id);
            if(empty($authRole)){
                return $this->toData('200403', '该规则不存在.', []);
            }
            $authRole->withoutField('id')->save($param);
            // 返回
            return $this->toData('0', 'Modification successful.', []);
        }catch (ValidateException $validateException){
            $message = $validateException->getError();
            return $this->toData('200400', $message);
        }catch (\Exception $exception){
            return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
        }

    }

    public function del($id): array
    {
        try {
            $authRole = AuthRoleModel::find($id);
            $authRole->delete();
            // 返回
            return $this->toData('0', 'Modification successful.', []);
        }catch (ValidateException $validateException){
            $message = $validateException->getError();
            return $this->toData('200400', $message);
        }catch (\Exception $exception){
            return $this->toData('200500', 'The system is busy.', [$exception->getMessage()]);
        }

    }
}