<?php
namespace app\admin\controller\base\config;
/**
 * @title: 系统设置分类
 */
use app\admin\controller\Base;
use app\admin\validate\base\config\ConfigClass as ConfigGroupValidate;
use app\common\model\base\config\ConfigClass as configClassModel;
use app\common\model\base\config\Systemconfig as systemConfigModel;
use think\facade\Db;
class ConfigClass extends Base
{
    protected $classModel = null;
    protected $configModel = null;
    public function initialize()
    {
        parent::initialize();
        $this->classModel = new configClassModel;
        $this->configModel = new systemConfigModel;
    }

    private function createWhere()
    {
        $data = $this->request->param();
        $group_id = !empty($data['group_id'])?$data['group_id']:0;
        $name = !empty($data['name'])?$data['name']:"";
        $where = [];
        if(!empty($group_id)){
            $where[] = ['group_id','=',$group_id];
        }
        if(!$this->userinfo['is_developer']){
            $where[] = ['is_developer','=',2];
        }
        if(!empty($name)){
            $where[] = ['name','LIKE',"%$name%"];
        }
        return $where;
    }
    /**
     * @title: 获取配置分类列表
     * @param {int} {group_id} {非必填} {分组id}
     * @param {int} {name} {非必填} {名称搜索}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:08
     */
    public function getList()
    {
        $where = $this->createWhere();
        $list = $this->classModel->where($where)->order("weigh ASC")->select();
        $list = FieldConverList($list);
        return res(1,"获取成功",$list);
    }
    /**
     * @title: 获取配置分类树形数据
     * @desc: 描述
     * @return {*}
     * @author: Rock
     * @method: POST
     * @Date: 2023-03-27 09:19:52
     */    
    public function getTree()
    {
        $where = $this->createWhere();
        $list = $this->classModel->where($where)->order('weigh ASC')->select()->toArray();
        $tree = array2tree($list,'pid','group_id');
        return res(1,"获取成功",$tree);
    }
    /**
     * @title: 配置分类编辑
     * @param array
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:10
     */
    public function doEdit()
    {
        $data = $this->request->param();
        $check = $this->validate($data,ConfigGroupValidate::class);
        //验证字段
        if(true!==$check){
            return res(2,$check);
        }
        if(!$this->userinfo['is_developer']){
            return res(2,"没有权限");
        }
        $res = $this->classModel->replace()->save($data);
        return res(1,"保存成功");
    }


    /**
     * @title: 配置分类删除
     * @param {int} {id} {必填} {分类id}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:11
     */
    public function doDelete(int $group_id = 0)
    {
        if(empty($group_id)){
            return res(2,'参数错误');
        }
        //检查分组下是否有配置项
        $info = $this->classModel->where('group_id',$group_id)->find();
        $configCount = $this->configModel->where([
            ['status','=',1],['group_code','=',$info->code]
        ])->count();
        if($configCount > 0){
            return res(2,'该分组下有使用中的配置,无法删除');
        }else{
            $this->classModel->where("group_id",$group_id)->delete();
            $this->configModel->destroy(function($query)use($info){
                $query->where("group_code",$info->code);
            });
            return res(1,"删除成功");
        }
    }
    /**
     * @title: 启用/禁用配置分类
     * @desc: 描述
     * @param {int} {group_id} {} {分类ID}
     * @return {*}
     * @author: Rock
     * @method: POST
     * @Date: 2023-03-27 09:19:01
     */    
    public function changeStatus(int $group_id=0){
        $this->classModel->where('group_id',$group_id)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]);
        return res(1,"操作成功");
    }
}