<?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;

/**
 * Class UserIdentityRepository
 * @package app\common\repositories\user
 * @author xaboy
 * @day 2020-05-07
 * @mixin UserIdentityDao
 */
class UserIdentityRepository 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)
    {
        $query = $this->dao->search($where);
        $count = $query->count($this->dao->getPk());
        $list = $query->page($page, $limit)->select();
        return compact('count', 'list');
    }

    /**
     * @param null $id
     * @param array $formData
     * @return Form
     * @throws FormBuilderException
     * @author xaboy
     * @day 2020-05-07
     */
    public function form($id = null, array $formData = [])
    {
        $action = Route::buildUrl('configIdentityUpdate' , compact('id'))->build();
        return Elm::createForm($action, [
            Elm::number('arrive_num_own', '个人达标贡献值')->min(0)->size('large'),
            Elm::number('arrive_num_team', '团队达标贡献值')->min(0)->size('large')
        ])->setTitle('业务员升级设置')->formData($formData);
    }

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

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