<?php



namespace app\controller\admin\system\config;

use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\user\UserRepository;
use ln\jobs\ChangeMerchantStatusJob;
use think\App;
use ln\basic\BaseController;
use app\common\repositories\system\config\ConfigClassifyRepository;
use app\common\repositories\system\config\ConfigRepository as repository;
use app\common\repositories\system\config\ConfigValueRepository;
use think\facade\Db;
use think\facade\Queue;

class ConfigOthers extends BaseController
{

    public $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function lst()
    {
        $data = [
            'extension_status' => systemConfig('extension_status'),
            'extension_self' => systemConfig('extension_self'),
            'extension_limit' => systemConfig('extension_limit'),
            'extension_limit_day' => systemConfig('extension_limit_day'),
            'extension_one_rate' => bcmul(systemConfig('extension_one_rate'), 100, 3),
            'extension_two_rate' => bcmul(systemConfig('extension_two_rate'), 100, 3),
            'sys_extension_type' => systemConfig('sys_extension_type'),
        ];
        return app('json')->success($data);
    }

    public function update()
    {
        $data = $this->request->params(['extension_status', 'extension_two_rate', 'extension_one_rate', 'extension_self', 'extension_limit', 'extension_limit_day', 'sys_extension_type']);
        if ($data['extension_two_rate'] < 0 || $data['extension_one_rate'] < 0) return app('json')->fail('比例不能小于0');
        if (bccomp($data['extension_one_rate'], $data['extension_two_rate'], 4) == -1)
            return app('json')->fail('一级比例不能小于二级比例');
        if (bccomp(bcadd($data['extension_one_rate'], $data['extension_two_rate'], 3), 100, 3) == 1)
            return app('json')->fail('比例之和不能超过100%');
        if (!ctype_digit((string)$data['extension_limit_day']) || $data['extension_limit_day'] <= 0) {
            return app('json')->fail('分销绑定时间必须大于0');
        }
        $arr['extension_status'] = $data['extension_status'];
        $arr['extension_self'] = $data['extension_self'];
        $arr['extension_limit'] = $data['extension_limit'];
        $arr['extension_limit_day'] = $data['extension_limit_day'];
        $arr['extension_one_rate'] = bcdiv($data['extension_one_rate'], 100, 3);
        $arr['extension_two_rate'] = bcdiv($data['extension_two_rate'], 100, 3);
        $old = systemConfig(['extension_limit', 'extension_limit_day']);
        if (!$old['extension_limit'] && $data['extension_limit']) {
            app()->make(UserRepository::class)->initSpreadLimitDay(intval($data['extension_limit_day']));
        } else if ($old['extension_limit'] && !$data['extension_limit']) {
            app()->make(UserRepository::class)->clearSpreadLimitDay();
        } else if ($data['extension_limit_day'] != $old['extension_limit_day'] && $data['extension_limit']) {
            app()->make(UserRepository::class)->updateSpreadLimitDay(intval($data['extension_limit_day'] - $old['extension_limit_day']));
        }
        $arr['sys_extension_type'] = $data['sys_extension_type'];
        app()->make(ConfigValueRepository::class)->setFormData($arr, 0);

        return app('json')->success('修改成功');
    }

    public function getGroupBuying()
    {
        $data = [
            'ficti_status' => systemConfig('ficti_status'),
            'group_buying_rate' => systemConfig('group_buying_rate'),
        ];
        return app('json')->success($data);
    }

    public function setGroupBuying()
    {
        $data['ficti_status'] = $this->request->param('ficti_status') == 1 ? 1 : 0;
        $data['group_buying_rate'] = $this->request->param('group_buying_rate');
        if ($data['group_buying_rate'] < 0 || $data['group_buying_rate'] > 100)
            return app('json')->fail('请填写1～100之间的整数');
        app()->make(ConfigValueRepository::class)->setFormData($data, 0);

        return app('json')->success('修改成功');
    }

    public function getProfitsharing()
    {
        return app('json')->success(array_filter(systemConfig(['extract_maxmum_num', 'extract_minimum_line', 'extract_minimum_num', 'open_wx_combine', 'open_wx_sub_mch', 'mer_lock_time']), function ($val) {
                return $val !== '';
            }) + ['open_wx_sub_mch' => 0, 'open_wx_combine' => 0]);
    }

    public function setProfitsharing()
    {
        $data = $this->request->params(['extract_maxmum_num', 'extract_minimum_line', 'extract_minimum_num', 'open_wx_combine', 'open_wx_sub_mch', 'mer_lock_time']);
        $config = systemConfig(['open_wx_combine', 'wechat_service_merid', 'wechat_service_key', 'wechat_service_v3key', 'wechat_service_client_cert', 'wechat_service_client_key', 'wechat_service_serial_no']);
        $open_wx_combine = $config['open_wx_combine'];
        unset($config['open_wx_combine']);
        if (($data['open_wx_combine'] || $data['open_wx_sub_mch']) && count(array_filter($config)) < 6) {
            return app('json')->fail('请先配置微信服务器相关参数');
        }
        Db::transaction(function () use ($data, $open_wx_combine) {
            app()->make(ConfigValueRepository::class)->setFormData($data, 0);
            if (!$open_wx_combine && $data['open_wx_combine']) {
                $column = app()->make(MerchantRepository::class)->search([])->where('sub_mchid', '')->column('mer_id');
                app()->make(MerchantRepository::class)->search([])->where('sub_mchid', '')->save(['mer_state' => 0]);
                foreach ($column as $merId) {
                    Queue::push(ChangeMerchantStatusJob::class, $merId);
                }
            }
        });
        return app('json')->success('修改成功');
    }
}
