<?php
/**
 * @package merchant
 *
 * @author xaboy
 * @day 2020-05-07
 *
 * 
 */

namespace app\common\repositories\user;


use app\common\dao\user\UserIdentityDao;
use app\common\repositories\BaseRepository;
use FormBuilder\Exception\FormBuilderException;
use FormBuilder\Factory\Elm;
use FormBuilder\Form;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Route; 
use think\facade\Db;

/**
 * Class UserAwardRepository
 * @package app\common\repositories\user
 * @author xaboy
 * @day 2020-05-07
 * @mixin UserIdentityDao
 */
class UserAwardRepository extends BaseRepository
{
    /**
     * @var UserIdentityDao
     */
    protected $dao;

    /**
     * UserGroupRepository constructor.
     * @param UserIdentityDao $dao
     */
    public function __construct(UserIdentityDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author xaboy
     * @day 2020-05-07
     */
    public function getList(array $where, $page, $limit)
    {
        $count = 0;
        $list = [];


        $brokerage_fugou_rate = Db::name("system_config_value")->where(["config_key"=>"brokerage_fugou_rate"])->value("value");
        $brokerage_fugou_rate = json_decode($brokerage_fugou_rate, true);

        $user_group_rate = Db::name("system_config_value")->where(["config_key"=>"user_group_rate"])->value("value");
        $user_group_rate = json_decode($user_group_rate, true);

        $user_group_fugou_rate = Db::name("system_config_value")->where(["config_key"=>"user_group_fugou_rate"])->value("value");
        $user_group_fugou_rate = json_decode($user_group_fugou_rate, true);

        $brokerage_guquan_rate = Db::name("system_config_value")->where(["config_key"=>"brokerage_guquan_rate"])->value("value");
        $fugou_guquan_rate = Db::name("system_config_value")->where(["config_key"=>"fugou_guquan_rate"])->value("value");

        $brokerage_lianchuang_rate = Db::name("system_config_value")->where(["config_key"=>"brokerage_lianchuang_rate"])->value("value");
        $fugou_lianchuang_rate = Db::name("system_config_value")->where(["config_key"=>"fugou_lianchuang_rate"])->value("value");

        $list[] = [
            'name' => '佣金',
            'key' => 'brokerage',
            'number_rate' => $brokerage_fugou_rate['brokerage_rate']??0,
            'guquan_rate' => $brokerage_guquan_rate??0,
            'lianchuang_rate' => $brokerage_lianchuang_rate??0,
            'user_groub_1' => $user_group_rate[0]['rate']??0,
            'user_groub_2' => $user_group_rate[1]['rate']??0,
            'user_groub_3' => $user_group_rate[2]['rate']??0,
            'user_groub_4' => $user_group_rate[3]['rate']??0,
            'user_groub_5' => $user_group_rate[4]['rate']??0,
            'user_groub_6' => $user_group_rate[5]['rate']??0,
            'user_groub_7' => $user_group_rate[6]['rate']??0,
        ];

        $list[] = [
            'name' => '复购金',
            'key' => 'fugou',
            'number_rate' => $brokerage_fugou_rate['fugou_rate']??0,
            'lianchuang_rate' => $fugou_lianchuang_rate??0,
            'guquan_rate' => $fugou_guquan_rate??0,
            'user_groub_1' => $user_group_fugou_rate[0]['rate']??0,
            'user_groub_2' => $user_group_fugou_rate[1]['rate']??0,
            'user_groub_3' => $user_group_fugou_rate[2]['rate']??0,
            'user_groub_4' => $user_group_fugou_rate[3]['rate']??0,
            'user_groub_5' => $user_group_fugou_rate[4]['rate']??0,
            'user_groub_6' => $user_group_fugou_rate[5]['rate']??0,
            'user_groub_7' => $user_group_fugou_rate[6]['rate']??0,
        ];

        $count = count($list);
        return compact('count', 'list');
    }

    /**
     * @param array $formData
     * @return Form
     * @throws FormBuilderException
     * @author xaboy
     * @day 2020-05-07
     */
    public function form(array $formData = [])
    {
        $action = Route::buildUrl('configAwardUpdate' , $formData)->build();
        return Elm::createForm($action, [
            Elm::number('number_rate', $formData['name'].'占比(%)')->min(0)->size('large'),
            Elm::number('lianchuang_rate', '联创占比(%)')->min(0)->size('large'),
            Elm::number('guquan_rate', '股权占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_1', '消费者占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_2', '业务员占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_3', '业务经理占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_4', '初级合伙人占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_5', '中级合伙人占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_6', '高级合伙人占比(%)')->min(0)->size('large'),
            Elm::number('user_groub_7', '董事占比(%)')->min(0)->size('large'),
        ])->setTitle($formData['name'].'设置')->formData($formData);
    }

    /**
     * @param $id
     * @return Form
     * @throws FormBuilderException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author xaboy
     * @day 2020-05-07
     */
    public function updateForm($data)
    {
        return $this->form($data);
    }

    /**
     * @param $id
     * @param data
     * @return array
     * @day 2020-05-07
     */
    public function updateAward($key, $data)
    {
        if ($key == 'brokerage') {
            //修改佣金比例
            $brokerage_fugou_rate = Db::name("system_config_value")->where(["config_key"=>"brokerage_fugou_rate"])->value("value");
            $brokerage_fugou_rate = json_decode($brokerage_fugou_rate, true);
            $brokerage_fugou_rate['brokerage_rate'] = $data['number_rate'];
            Db::name("system_config_value")->where(["config_key"=>"brokerage_fugou_rate"])->where('mer_id', 0)->update([
                'value' => json_encode($brokerage_fugou_rate)
            ]);

            //修改佣金等级分成比例
            $user_group_rate = Db::name("system_config_value")->where(["config_key"=>"user_group_rate"])->value("value");
            $user_group_rate = json_decode($user_group_rate, true);
            $user_group_rate[0]['rate'] = $data['user_groub_1'];
            $user_group_rate[1]['rate'] = $data['user_groub_2'];
            $user_group_rate[2]['rate'] = $data['user_groub_3'];
            $user_group_rate[3]['rate'] = $data['user_groub_4'];
            $user_group_rate[4]['rate'] = $data['user_groub_5'];
            $user_group_rate[5]['rate'] = $data['user_groub_6'];
            $user_group_rate[6]['rate'] = $data['user_groub_7'];
            Db::name("system_config_value")->where(["config_key"=>"user_group_rate"])->where('mer_id', 0)->update(['value' =>json_encode($user_group_rate)]);

            //修改佣金股权比例
            Db::name("system_config_value")->where(["config_key"=>"brokerage_guquan_rate"])->where('mer_id', 0)->update(['value' =>$data['guquan_rate']]);
            //修改佣金联创比例
            Db::name("system_config_value")->where(["config_key"=>"brokerage_lianchuang_rate"])->where('mer_id', 0)->update(['value' =>$data['lianchuang_rate']]);
        }

        if ($key == 'fugou') {
            //修改复购金比例
            $brokerage_fugou_rate = Db::name("system_config_value")->where(["config_key"=>"brokerage_fugou_rate"])->value("value");
            $brokerage_fugou_rate = json_decode($brokerage_fugou_rate, true);
            $brokerage_fugou_rate['fugou_rate'] = $data['number_rate'];
            Db::name("system_config_value")->where(["config_key"=>"brokerage_fugou_rate"])->where('mer_id', 0)->update([
                'value' => json_encode($brokerage_fugou_rate)
            ]);
            //修改复购金等级分成比例
            $user_group_fugou_rate = Db::name("system_config_value")->where(["config_key"=>"user_group_fugou_rate"])->value("value");
            $user_group_fugou_rate = json_decode($user_group_fugou_rate, true);
            $user_group_fugou_rate[0]['rate'] = $data['user_groub_1'];
            $user_group_fugou_rate[1]['rate'] = $data['user_groub_2'];
            $user_group_fugou_rate[2]['rate'] = $data['user_groub_3'];
            $user_group_fugou_rate[3]['rate'] = $data['user_groub_4'];
            $user_group_fugou_rate[4]['rate'] = $data['user_groub_5'];
            $user_group_fugou_rate[5]['rate'] = $data['user_groub_6'];
            $user_group_fugou_rate[6]['rate'] = $data['user_groub_7'];
            Db::name("system_config_value")->where(["config_key"=>"user_group_fugou_rate"])->where('mer_id', 0)->update([
                'value' => json_encode($user_group_fugou_rate)
            ]);
            //修改复购金股权比例
            Db::name("system_config_value")->where(["config_key"=>"fugou_guquan_rate"])->where('mer_id', 0)->update(['value' => $data['guquan_rate']]);
            //修改复购金联创比例
            Db::name("system_config_value")->where(["config_key"=>"fugou_lianchuang_rate"])->where('mer_id', 0)->update(['value' => $data['lianchuang_rate']]);
        }

        return true;
    }

    /**
     * @param array $where
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getIdentityList(array $where = [])
    {
        $query = $this->dao->search($where);
        return $query->select()->toArray();
    }
}