<?php
namespace app\admin\server\index;

use app\admin\model\SystemConfigClassify;
use app\admin\model\SystemConfigValue;

class ConfigServer
{
    public static function getClassify()
    {
        $list = SystemConfigClassify::getClassifyList();
        return $list;
    }

    /**
     * Notes:新增设置分类
     * @param int $classify_pid
     * @param string $classify_name
     * @param string $classify_key
     * @param string $classify_info
     * @param int $classify_sort
     * @param int $classify_status
     * @param int $admin_id
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/10/10
     */
    public static function insertClassify(int $classify_pid, string $classify_name, string $classify_key, string $classify_info, int $classify_sort, int $classify_status, int $admin_id)
    {
        try {
            $insert = array(
                'classify_pid'  => $classify_pid,
                'classify_name' => $classify_name,
                'classify_key'  => $classify_key,
                'classify_info' => $classify_info,
                'classify_sort' => $classify_sort,
                'classify_status' => $classify_status,
                'classify_create_time' => date("Y-m-d H:i:s", time())
            );
            $result = SystemConfigClassify::insertData($insert);
            if($result)
            {
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加设置分类: ' . $classify_name . $result;
                plog('system-config-create', '系统设置-配置-新增配置', $msg);
                return true;
            }
            throw new \Exception('新增失败！');
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:系统设置分类编辑
     * @param int $classify_id
     * @param int $classify_pid
     * @param string $classify_name
     * @param string $classify_key
     * @param string $classify_info
     * @param int $classify_sort
     * @param int $classify_status
     * @param int $admin_id
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/10/10
     */
    public static function updateClassify(int $classify_id, int $classify_pid, string $classify_name, string $classify_key, string $classify_info, int $classify_sort, int $classify_status, int $admin_id)
    {
        try {
            //检测是否存在
            $info = SystemConfigClassify::getInfo($classify_id);
            if(empty($info))
            {
                throw new \Exception('查无此分类信息');
            }
            $update = array(
                'classify_pid'  => $classify_pid,
                'classify_name' => $classify_name,
                'classify_key'  => $classify_key,
                'classify_info' => $classify_info,
                'classify_sort' => $classify_sort,
                'classify_status' => $classify_status
            );
            $result = SystemConfigClassify::updateData($classify_id, $update);
            if($result !== false)
            {
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '编辑设置分类: ' . $classify_name . $classify_id;
                plog('system-config-update', '系统设置-配置-修改配置', $msg);
                return true;
            }
            throw new \Exception('编辑失败！');
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:配置分类删除或修改状态
     * @param int $classify_id
     * @param int $type
     * @param int $status
     * @param int $admin_id
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/10/10
     */
    public static function delUpClassify(int $classify_id, int $type, int $status, int $admin_id)
    {
        try {
            //删除
            if($type == 0)
            {
                //检测是否存在子集配置
                if(SystemConfigClassify::where(['classify_pid' => $classify_id])->exists())
                {
                    throw new \Exception('该配置分类存在子集，请先删除子集分类');
                }
                if(SystemConfigValue::where(['value_classify_id' => $classify_id])->exists())
                {
                    throw new \Exception('该配置分类存在配置内容，请先删除相关配置信息');
                }
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除设置分类: ' . $classify_id;
                $result = SystemConfigClassify::where(['classify_id' => $classify_id])->delete();
            }else{
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '编辑设置分类: ' . $classify_id . '状态：' . $status;
                //修改状态
                $result = SystemConfigClassify::updateData($classify_id, ['classify_status' => $status]);
            }
            plog('system-config-update', '系统设置-配置-修改配置', $msg);
            if($result !== false)
            {
                return true;
            }
            throw new \Exception('操作失败');
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:获取单个配置信息
     * @param int $classify_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/10/10
     */
    public static function getClassifyInfo(int $classify_id)
    {
        return SystemConfigClassify::getInfo($classify_id);
    }
}
