<?php
namespace app\admin\controller\base\config;
/**
 * @title: 系统配置项
 */
use app\admin\controller\Base;
use app\admin\validate\base\config\Systemconfig as SystemconfigValidate;
use app\common\model\base\config\ConfigClass as configClassModel;
use app\common\model\base\config\Systemconfig as systemConfigModel;
class Systemconfig extends Base
{
    protected $configModel = null;
    protected $groupModel = null;
    protected $noNeedLogin = ['gettypelist','getconfig'];
    protected $noNeedAuth = ['getList'];
    public function initialize()
    {
        parent::initialize();
        $this->classModel = new configClassModel;
        $this->configModel = new systemConfigModel;
    }
    private function createWhere()
    {
        $data = $this->request->param();
        $where = [];
        if(!empty($data['group_id'])){
            $group_code = $this->classModel->value('code');
            $where[] = ['group_code','=',$group_code];
        }
        if(!empty($data['group_code'])){
            $where[] = ['group_code','=',$data['group_code']];
        }
        if(!$this->userinfo['is_developer']){
            $where[] = ['is_developer','=',2];
        }
        return $where;
    }
    /**
     * @title: 获取配置项列表
     * @desc: 描述
     * @param {int} {pageNo} {0} {页码,传0不分页}
     * @param {int} {pageSize} {10} {每页数量}
     * @param {int} {return_json} {0} {是否返回json对象,0不返回,其他都返回}
     * @return {*}
     * @author: Rock
     * @method: POST
     * @Date: 2023-04-06 10:34:24
     */    
    public function getList(int $pageNo=0,int $pageSize=10,int $return_json=0)
    {
        $data = $this->request->param();
        $where = $this->createWhere();
        if(!empty($pageNo)){
            $list = $this->configModel->where($where)->order("weigh ASC")->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
            $total = $list->total();
            $list = $list->items();
        }else{
            $list = $this->configModel->where($where)->order("weigh ASC")->select()->toArray();
        }
        if(!empty($return_json)){
            $list = array_column($list,null,'key');
        }
        if(!empty($pageNo)){
            return pageRes(1,'获取成功',$total,$list);
        }else{
            return res(1,"获取成功",$list);
        }
    }
    /**
     * @title: 编辑配置项
     * @param array
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 10:02
     */
    public function doEdit()
    {
        $data = $this->request->param();
        $check = $this->validate($data,SystemconfigValidate::class);
        //验证字段
        if(true!==$check){
            return res(2,$check);
        }
        $this->configModel->replace()->save($data);
        slog(1,"编辑了系统配置项".$data['name']);
        return res(1,"保存成功");
    }
    /**
     * @title: 删除配置项
     * @param {int} {config_id} {必填} {id}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 10:02
     */
    public function doDelete(int $config_id)
    {
        $info = $this->configModel->where("config_id",$config_id)->find();
        $info->delete();
        slog(1,"删除了系统配置项".$info['name']);
        return res(1,"删除成功");
    }
    /**
     * @title: 保存配置
     * @desc:
     * @param {array} {} {} {}
     * @return {}
     * @Author: Rock
     * @Date: 2021-05-20 15:41:30
     * @LastEditTime: Do not edit
     */
    public function doSave()
    {
        $data = $this->request->param();
				if(isset($data['jwtData']))unset($data['jwtData']);
        foreach($data as $row){
            if(!empty($row['rule'])){
                $validateData = [];
                $validateData[$row['key']] = $row['value'];
                $rule = [];
                $rule[$row['key']] = $row['rule'];
                $check = $this->validate($validateData,$rule);
                if(true!==$check){
                    return res(2,$check);
                }else{
                    slog(1,"设置了系统配置项".$row['name']);
                }
            }
        }
        $this->configModel->replace()->saveAll($data);
        return res(1,"配置成功");
    }
    /**
     * @title: 获取已有的最大排序号,返回最大排序号加1的结果
     * @desc: 描述
     * @param {string} $group_code
     * @return {*}
     * @author: Rock
     * @method: POST
     * @Date: 2023-03-31 10:49:10
     */    
    public function getMaxWeigh(string $group_code=''){
        $where = [];
        if(!empty($group_code)){
            $where[] = ['group_code','=',$group_code];
        }
        $maxWeigh = $this->configModel->where($where)->max('weigh');
        return res(1,"获取成功",$maxWeigh + 1);
    }
    /**
     * @title: 批量操作
     * @desc: 描述
     * @param {array} {select} {} {选中的配置}
     * @param {int} {type} {0} {操作类型,1=启用,2=禁用,3=删除}
     * @return {*}
     * @author: Rock
     * @method: POST
     * @Date: 2023-03-31 11:10:12
     */    
    public function batchOperate(array $select=[],int $type=0){
        if(empty($select) || empty($type)){
            return res(2,"请选择操作项");
        }
        $ids = array_column($select,'config_id');
        if($type==1){
            $this->configModel->where('config_id','IN',$ids)->update(['status'=>1]);
        }elseif($type==2){
            $this->configModel->where('config_id','IN',$ids)->update(['status'=>2]);
        }elseif($type==3){
            $this->configModel->destroy(function($query)use($ids){
                $query->where('config_id','IN',$ids);
            });
        }
        return res(1,"操作成功");
    }
    /**
     * @title: 获取系统配置
     * @desc:
     * @param {string} {key} {} {配置名称,为空时获取全部生效配置}
     * @return {mixed} {} {} {有传key时,如果key包含".",表示获取分组下的配置,例如,group_code.key,不包含".",表示获取group_code等于key的所有配置;没传key则返回所有配置}
     * @Author: Rock
     * @Date: 2021-05-20 15:42:12
     * @LastEditTime: Do not edit
     */
    public function getConfig($key="base")
    {
        $result = sysconfig($key);
        return res(1,"获取成功",$result);
    }
    /**
     * @title: 获取可选项
     * @desc: 描述
     * @return {*}
     * @author: Rock
     * @method: POST
     * @Date: 2023-03-31 11:32:40
     */    
    public function getOptions()
    {
        $options = [
            'typeList'      =>  systemConfigModel::getTypeList(),
            'statusList'    =>  systemConfigModel::getStatusList()
        ];
        return res(1,"获取成功",$options);
    }
}