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


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


/**
 * 工程师服务工单列表
 * Class WorkerServiceWorkLists
 * @package app\adminapi\listsworks
 */
class WorkerServiceWorkLists extends BaseAdminDataLists implements ListsSearchInterface
{

    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function setSearch(): array
    {
        return [
            '=' => ['id','category_type', 'work_status', 'service_status', 'master_worker_id', 'work_type','city'],
        ];
    }

    public function queryWhere(){
        $where = [];
        if (isset($this->params['master_worker_name']) && !empty($this->params['master_worker_name'])) {
            $master_worker_ids = MasterWorker::where([['worker_number|mobile|real_name', 'like','%' .$this->params['master_worker_name'] . '%']])->column('id')??[0];
            $where[] = ['master_worker_id','in' ,$master_worker_ids];
        }
        if(isset($this->params['workid']) && !empty($this->params['workid'])){
            $where[] = ['master_worker_id', '=', $this->params['workid']];
        }
        return $where;
    }

    public function queryAndWhere(){
        $where = [];
        if (isset($this->params['work_sn']) && !empty($this->params['work_sn'])) {
            $where[] = ['work_sn','=' ,$this->params['work_sn']];
        }
        if(isset($this->params['mobile']) && !empty($this->params['mobile'])){
            $where[] = ['mobile', '=', $this->params['mobile']];
        }
        if(isset($this->params['real_name']) && !empty($this->params['real_name'])){
            $where[] = ['real_name', 'like', '%'.$this->params['real_name'].'%'];
        }
        return $where;
    }
    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function lists(): array
    {
        $params = $this->params;
        $list = MasterWorker::hasWhere('serviceWork', function($query) use ($params) {
                $query->where('work_type','in',[0,1]);
                if (isset($params['work_sn']) && !empty($params['work_sn'])) {
                    $query->where(['work_sn','=',trim($params['work_sn'])]);
                }
                if (isset($params['mobile']) && !empty($params['mobile'])) {
                    $query->where('mobile','=',trim($params['mobile']));
                }
                if (isset($params['real_name']) && !empty($params['real_name'])) {
                    $query->where('real_name','like','%'.trim($params['real_name']).'%');
                }
                if (isset($params['appointment_time']) && !empty($params['appointment_time'])) {
                    $query->where('appointment_time','between',[strtotime($params['appointment_time']), strtotime($params['appointment_time'].' 23:59:59')]);
                } 
                if (!empty($params['dispatch_time']) && is_array($params['dispatch_time'])) {
                    $query->where('dispatch_time','between',[strtotime($params['dispatch_time'][0]), strtotime($params['dispatch_time'][1])]);
                } 
                if (empty($params['appointment_time']) && empty($params['dispatch_time'])) {
                    $query->where('appointment_time','between',[strtotime(date('Y-m-d')), strtotime(date('Y-m-d 23:59:59'))]);
                }
            })->with(['serviceWork' => function($query) use($params) {
                $query->where('work_type','in',[0,1]);
                if (isset($params['work_sn']) && !empty($params['work_sn'])) {
                    $query->where(['work_sn','=',trim($params['work_sn'])]);
                }
                if (isset($params['mobile']) && !empty($params['mobile'])) {
                    $query->where('mobile','=',trim($params['mobile']));
                }
                if (isset($params['real_name']) && !empty($params['real_name'])) {
                    $query->where('real_name','like','%'.trim($params['real_name']).'%');
                }
                if (isset($params['appointment_time']) && !empty($params['appointment_time'])) {
                    $query->where('appointment_time','between',[strtotime($params['appointment_time']), strtotime($params['appointment_time'].' 23:59:59')]);
                } 
                if (!empty($params['dispatch_time']) && is_array($params['dispatch_time'])) {
                    $query->where('dispatch_time','between',[strtotime($params['dispatch_time'][0]), strtotime($params['dispatch_time'][1])]);
                } 
                if (empty($params['appointment_time']) && empty($params['dispatch_time'])) {
                    $query->where('appointment_time','between',[strtotime(date('Y-m-d')), strtotime(date('Y-m-d 23:59:59'))]);
                }
            }])
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();
        foreach($list as &$item) {
            $item = [
                'id' => $item['id'],
                'worker_number' => $item['worker_number'],
                'real_name' => $item['real_name'],
                'category_ids' => empty($item['category_ids']) ? [] : explode(",",$item['category_ids']),
                'serviceWork' => $item['serviceWork'],
            ];
        }
        return $list;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function count(): int
    {
        $params = $this->params;
        return MasterWorker::hasWhere('serviceWork', function($query) use ($params) {
            $query->where('work_type','in',[0,1]);
            if (isset($params['work_sn']) && !empty($params['work_sn'])) {
                $query->where('work_sn','=',trim($params['work_sn']));
            }
            if (isset($params['mobile']) && !empty($params['mobile'])) {
                $query->where('mobile','=',trim($params['mobile']));
            }
            if (isset($params['real_name']) && !empty($params['real_name'])) {
                $query->where('real_name','like','%'.trim($params['real_name']).'%');
            }
        })->with('serviceWork')
        ->where($this->searchWhere)
        ->where($this->queryWhere())
        ->count();
    }

}