<?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)
    {
        $key = [
            ['name' => '直推收益', 'key' => 'user_group_rate'],
            ['name' => '锁客收益', 'key' => 'user_group_lock_rate'],
            ['name' => '推广店铺收益', 'key' => 'user_group_store_rate'],
            ['name' => '分红', 'key' => 'user_group_red_rate'],
        ];
        $list = [];
        foreach ($key as $item) {
            $user_group_rate = Db::name("system_config_value")->where(["config_key"=>$item['key']])->value("value");
            $user_group_rate = json_decode($user_group_rate, true);
            $list[] = [
                'name' => $item['name'],
                'key' => $item['key'],
                '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,
                'user_groub_8' => $user_group_rate[7]['rate']??0,
                'user_groub_9' => $user_group_rate[8]['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('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'),
            Elm::number('user_groub_8', '联创(%)')->min(0)->size('large'),
            Elm::number('user_groub_9', '股权(%)')->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)
    {
        $user_group_rate = Db::name("system_config_value")->where(["config_key"=>$key])->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'];
        $user_group_rate[7]['rate'] = $data['user_groub_8'];
        $user_group_rate[8]['rate'] = $data['user_groub_9'];
        Db::name("system_config_value")->where(["config_key"=>$key])->where('mer_id', 0)->update(['value' =>json_encode($user_group_rate)]);
    }

    /**
     * @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();
    }
}