<?php

namespace app\admin\controller\sys_manage;

use app\admin\service\sys_manage\ConfigService;
use app\admin\validate\sys_manage\ConfigValidate;
use app\model\SysConfig;
use support\Request;

class ConfigController
{
    /**
     * @Desc 配置列表
     * @Author Gorden
     * @Date 2024/2/22 16:35
     *
     * @param Request $request
     * @return \support\Response
     */
    public function configList(Request $request)
    {
        return ConfigService::configList($request);
    }

    /**
     * @Desc 配置详情
     * @Author Gorden
     * @Date 2024/2/22 16:36
     *
     * @param $id
     * @return \support\Response
     */
    public function configInfo($id)
    {
        return ConfigService::configInfo($id);
    }

    /**
     * @Desc 添加配置
     * @Author Gorden
     * @Date 2024/2/22 15:57
     *
     * @param Request $request
     * @return \support\Response
     */
    public function addConfig(Request $request)
    {
        $validate = new ConfigValidate();
        if (!$validate->scene('add')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return ConfigService::addConfig($request->post());
    }

    /**
     * @Desc 修改配置
     * @Author Gorden
     * @Date 2024/2/22 16:24
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateConfig($id, Request $request)
    {
        $validate = new ConfigValidate();
        if (!$validate->scene('update')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return ConfigService::updateConfig($id, $request->post());
    }

    /**
     * @Desc 修改配置状态
     * @Author Gorden
     * @Date 2024/2/22 16:38
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateStatus($id, Request $request)
    {
        $validate = new ConfigValidate();
        if (!$validate->scene('update_status')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return ConfigService::updateStatus($id, $request->post());
    }

    /**
     * @Desc 删除配置
     * @Author Gorden
     * @Date 2024/2/22 16:39
     *
     * @param $id
     * @return \support\Response
     */
    public function delConfig(Request $request)
    {
        return ConfigService::delConfig($request);
    }

    public function bottomMenuList()
    {
        $config = SysConfig::where('config_name', 'APP底部菜单')->first()->toArray();

        return json_success('', json_decode($config['config_value_json']));
    }

    public function bottomMenuSave(Request $request)
    {
        try {
            SysConfig::where('config_name', 'APP底部菜单')->update(['config_value_json' => json_encode($request->post())]);

            return json_success('success');
        } catch (\Exception $e) {
            return json_fail('APP底部菜单保存失败');
        }
    }

    public function navMenuList()
    {
        $config = SysConfig::where('config_name', 'APP导航菜单')->first()->toArray();

        return json_success('', json_decode($config['config_value_json']));
    }

    public function navMenuSave(Request $request)
    {
        try {
            SysConfig::where('config_name', 'APP导航菜单')->update(['config_value_json' => json_encode($request->post())]);

            return json_success('success');
        } catch (\Exception $e) {
            return json_fail('APP底部菜单保存失败');
        }
    }

    public function paramsList(Request $request)
    {
        $type = $request->get('type', '');

        return ConfigService::getParams($this->getType($type));
    }

    public function paramsSave(Request $request)
    {
        return ConfigService::paramsSave($request, $this->getType($request->post('type', '')));
    }

    public function partnerParamsInfo()
    {
        $config = SysConfig::where('config_key', 'app-identity-partner-config')->first()->toArray();
        $config['config_value_json'] = json_decode($config['config_value_json'], true);
        if (!empty($config['config_value_json']['account']['added'])) {
            $config['config_value_json']['account']['added'] = round($config['config_value_json']['account']['added'] * 100, 2);
        }
        if (!empty($config['config_value_json']['commission'])) {
            $config['config_value_json']['commission']['direct'] = round($config['config_value_json']['commission']['direct'] * 100, 2);
            $config['config_value_json']['commission']['indirect'] = round($config['config_value_json']['commission']['indirect'] * 100, 2);
            $config['config_value_json']['commission']['relate_consum'] = round($config['config_value_json']['commission']['relate-consum'] * 100, 2);
            $config['config_value_json']['commission']['invite_non_identity'] = round($config['config_value_json']['commission']['invite-non-identity'] * 100, 2);
            $config['config_value_json']['commission']['agent_senior_join'] = round($config['config_value_json']['commission']['agent-senior-join'] * 100, 2);
            $config['config_value_json']['commission']['agent_senior_strategy'] = round($config['config_value_json']['commission']['agent-senior-strategy'] * 100, 2);
        }

        return json_success('success', $config);
    }

    public function setPartnerParams(Request $request)
    {
        $params = $request->post();
        if (!empty($params['account']['added'])) {
            $params['account']['added'] = $params['account']['added'] / 100;
        }
        if (!empty($params['commission'])) {
            $params['commission']['direct'] = $params['commission']['direct'] / 100;
            $params['commission']['indirect'] = $params['commission']['indirect'] / 100;
            $params['commission']['relate-consum'] = $params['commission']['relate_consum'] / 100;
            $params['commission']['invite-non-identity'] = $params['commission']['invite_non_identity'] / 100;
            $params['commission']['agent-senior-join'] = $params['commission']['agent_senior_join'] / 100;
            $params['commission']['agent-senior-strategy'] = $params['commission']['agent_senior_strategy'] / 100;

            unset($params['commission']['relate_consum'], $params['commission']['invite_non_identity'], $params['commission']['agent_senior_join'], $params['commission']['agent_senior_strategy']);
        }
        try {
            SysConfig::where('config_key', 'app-identity-partner-config')->update(['config_value_json' => json_encode($params)]);

            return json_success('success');
        } catch (\Exception $e) {
            return json_fail("保存失败");
        }
    }

    /**
     * @Desc 加盟合伙人参数
     * @Author Gorden
     * @Date 2024/11/12 11:29
     *
     * @return \support\Response
     */
    public function franchiseeParamsInfo()
    {
        $config = SysConfig::where('config_key', 'app-identity-franchisee-config')->first()->toArray();
        $config['config_value_json'] = json_decode($config['config_value_json'], true);
        if (!empty($config['config_value_json']['account']['added'])) {
            $config['config_value_json']['account']['added'] = round($config['config_value_json']['account']['added'] * 100, 2);
        }
        if (!empty($config['config_value_json']['commission'])) {
            $config['config_value_json']['commission']['direct'] = round($config['config_value_json']['commission']['direct'] * 100, 2);
            $config['config_value_json']['commission']['indirect'] = round($config['config_value_json']['commission']['indirect'] * 100, 2);
            $config['config_value_json']['commission']['relate_consum'] = round($config['config_value_json']['commission']['relate-consum'] * 100, 2);
            $config['config_value_json']['commission']['invite_non_identity'] = round($config['config_value_json']['commission']['invite-non-identity'] * 100, 2);
            $config['config_value_json']['commission']['agent_senior_join'] = round($config['config_value_json']['commission']['agent-senior-join'] * 100, 2);
            $config['config_value_json']['commission']['agent_senior_strategy'] = round($config['config_value_json']['commission']['agent-senior-strategy'] * 100, 2);
        }

        return json_success('success', $config);
    }

    /**
     * @Desc 设置加盟合伙人参数
     * @Author Gorden
     * @Date 2024/11/12 11:29
     *
     * @param Request $request
     * @return \support\Response
     */
    public function setFranchiseeParams(Request $request)
    {
        $params = $request->post();
        if (!empty($params['account']['added'])) {
            $params['account']['added'] = $params['account']['added'] / 100;
        }
        if (!empty($params['commission'])) {
            $params['commission']['direct'] = $params['commission']['direct'] / 100;
            $params['commission']['indirect'] = $params['commission']['indirect'] / 100;
            $params['commission']['relate-consum'] = $params['commission']['relate_consum'] / 100;
            $params['commission']['invite-non-identity'] = $params['commission']['invite_non_identity'] / 100;
            $params['commission']['agent-senior-join'] = $params['commission']['agent_senior_join'] / 100;
            $params['commission']['agent-senior-strategy'] = $params['commission']['agent_senior_strategy'] / 100;

            unset($params['commission']['relate_consum'], $params['commission']['invite_non_identity'], $params['commission']['agent_senior_join'], $params['commission']['agent_senior_strategy']);
        }
        try {
            SysConfig::where('config_key', 'app-identity-franchisee-config')->update(['config_value_json' => json_encode($params)]);

            return json_success('success');
        } catch (\Exception $e) {
            return json_fail("保存失败");
        }
    }

    /**
     * @Desc 获取推荐官参数
     * @Author Gorden
     * @Date 2024/9/29 15:05
     *
     * @return \support\Response
     */
    public function referrerParamsInfo()
    {
        $config = SysConfig::where('config_key', 'app-identity-referrer-config')->first()->toArray();
        $config['config_value_json'] = json_decode($config['config_value_json'], true);
        if (!empty($config['config_value_json']['account']['added'])) {
            $config['config_value_json']['account']['added'] = round($config['config_value_json']['account']['added'] * 100, 2);
        }
        if (!empty($config['config_value_json']['commission'])) {
            $config['config_value_json']['commission']['direct'] = round($config['config_value_json']['commission']['direct'] * 100, 2);
            $config['config_value_json']['commission']['indirect'] = round($config['config_value_json']['commission']['indirect'] * 100, 2);
            $config['config_value_json']['commission']['relate_consum'] = round($config['config_value_json']['commission']['relate-consum'] * 100, 2);
            $config['config_value_json']['commission']['invite_non_identity'] = round($config['config_value_json']['commission']['invite-non-identity'] * 100, 2);
            $config['config_value_json']['commission']['agent_senior_join'] = round($config['config_value_json']['commission']['agent-senior-join'] * 100, 2);
            $config['config_value_json']['commission']['agent_senior_strategy'] = round($config['config_value_json']['commission']['agent-senior-strategy'] * 100, 2);
        }

        return json_success('success', $config);
    }

    /**
     * @Desc 设置推荐官参数
     * @Author Gorden
     * @Date 2024/9/29 15:06
     *
     * @param Request $request
     * @return \support\Response
     */
    public function setReferrerParams(Request $request)
    {
        $params = $request->post();
        if (!empty($params['account']['added'])) {
            $params['account']['added'] = $params['account']['added'] / 100;
        }
        if (!empty($params['commission'])) {
            $params['commission']['direct'] = $params['commission']['direct'] / 100;
            $params['commission']['indirect'] = $params['commission']['indirect'] / 100;
            $params['commission']['relate-consum'] = $params['commission']['relate_consum'] / 100;
            $params['commission']['invite-non-identity'] = $params['commission']['invite_non_identity'] / 100;
            $params['commission']['agent-senior-join'] = $params['commission']['agent_senior_join'] / 100;
            $params['commission']['agent-senior-strategy'] = $params['commission']['agent_senior_strategy'] / 100;

            unset($params['commission']['relate_consum'], $params['commission']['invite_non_identity'], $params['commission']['agent_senior_join'], $params['commission']['agent_senior_strategy']);
        }
        try {
            SysConfig::where('config_key', 'app-identity-referrer-config')->update(['config_value_json' => json_encode($params)]);

            return json_success('success');
        } catch (\Exception $e) {
            return json_fail("保存失败");
        }
    }

    private function getType($type)
    {
        switch ($type) {
            case 'sys':
                return 'sys';
                break;
            case 'pay':
                return 'app-pay-unionpay';
                break;
            case 'msg':
                return 'app-sms-aliyun';
                break;
            case 'face':
                return 'app-face-aliyun';
                break;
            case 'ocr':
                return 'app-cert-ocr-aliyun';
                break;
            case 'ws':
                return 'app-websocket';
                break;
            case 'shortcutMenu':
                return 'app-menu-shortcut';
                break;
            case 'navigationMenu':
                return 'app-menu-navigation';
                break;
            case 'footerMenu':
                return 'app-menu-footer';
                break;
            default:
                return '';
                break;
        }
    }
}