<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\master_worker;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\master_worker\MasterWorker;
use app\common\lists\ListsSearchInterface;
use think\db\Query;

/**
 * MasterWorker列表
 * Class MasterWorkerLists
 * @package app\adminapi\listsmaster_worker
 */
class MasterWorkerLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/07/10 18:17
     */
    public function setSearch(): array
    {
        return [
            '=' => ['mw.sn', 'mw.real_name', 'mw.account', 'mw.password', 'mw.mobile', 'mw.sex', 'mw.channel', 'mw.is_disable', 'mw.is_new_user', 'mw.create_time', 'mw.update_time'],

        ];
    }

    public function queryWhere(){
        $where = [];
        // 用户昵称
        if (isset($this->params['nickname']) && !empty($this->params['nickname'])) {
            $where[] = ['mw.nickname|mw.worker_number', 'like','%' .$this->params['nickname'] . '%'];
        }
        if (isset($this->params['worker_exp_type']) && !empty($this->params['worker_exp_type'])) {
            $where[] = ['mwr.maintain_exp_type', '=', $this->params['worker_exp_type']];
        }

        return $where;
    }
    public function queryHaving()
    {
        $having = '';
        if(isset($this->params['min_work_number']) && is_numeric($this->params['min_work_number'])){
            $having .= 'work_total >= '.$this->params['min_work_number'];
        }
        if(isset($this->params['max_work_number']) && is_numeric($this->params['max_work_number'])){
            if(!empty($having)){
                $having .= ' and ';
            }
            $having .= 'work_total <= '.$this->params['max_work_number'];
        }
        return $having;
    }
    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/07/10 18:17
     */
    public function lists(): array
    {
         return MasterWorker::alias('mw')
            ->join('master_worker_register mwr', 'mwr.worker_id = mw.id')
             ->leftJoin('service_work sw','sw.master_worker_id = mw.id and sw.service_status !=4')
            ->where($this->searchWhere)
            ->where($this->queryWhere())
//            ->field(['mw.id', 'mw.sn', 'mw.avatar', 'mw.real_avatar','mw.worker_number', 'mw.real_name', 'mw.nickname', 'mw.account', 'mw.password', 'mw.mobile', 'mw.sex', 'mw.channel', 'mw.is_disable', 'mw.login_ip', 'mw.login_time', 'mw.is_new_user', 'mw.estimate_money', 'mw.user_money', 'mw.earnest_money', 'mw.exp'])
             ->field([
                 'mw.*',
                 'mwr.maintain_exp_type',
                 'mwr.other_exp_type',
                 'SUM(CASE WHEN sw.work_status NOT IN (7,8) THEN 1 ELSE 0 END) work_total'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['mw.id' => 'desc'])
            ->group(['sw.master_worker_id'])
            ->having($this->queryHaving())
            ->select()
            ->toArray();
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/07/10 18:17
     */
    public function count(): int
    {
        return MasterWorker::alias('mw')
            ->join('master_worker_register mwr', 'mwr.worker_id = mw.id')
            ->leftJoin('service_work sw','sw.master_worker_id = mw.id and sw.service_status !=4')
            ->field([
                'mw.*',
                'SUM(CASE WHEN sw.work_status NOT IN (7,8) THEN 1 ELSE 0 END) work_total'])
            ->where($this->searchWhere)->where($this->queryWhere())
            ->group(['sw.master_worker_id'])
            ->having($this->queryHaving())
            ->count();
    }

}