<?php
namespace app\workerapi\lists;

use app\common\model\master_worker\MasterWorker;
use app\common\model\works\ServiceWork;
use app\common\lists\ListsSearchInterface;
use DateTime;

/**
 * ServiceWork列表
 * Class ServiceWorkLists
 * @package app\workerapi\listsworks
 */
class TeamServiceWorkLists extends BaseWorkerDataLists
{
    protected $count = 0;
    public function queryWhere(){
        $where = [
            'approval'=>1,
            'master_worker_id'=>['in',implode(',',MasterWorker::where('team_id', $this->userInfo['team_id'])->where('team_role', 2)->column('id')?:[-1])]
        ];

        // 待领单、待联系、待上门、已上门、服务中、待结算、已完结、已评价、已取消
        // 1=待领单,2=待联系,3=待上门,4=已上门,5=服务中,6=待结算,7=已完结,8=已评价,9=已退
        if(isset($this->params['work_status']) && !empty($this->params['work_status'])){
            $where['work_status'] = ['=', $this->params['work_status']];
        }
        if(isset($this->params['create_time']) && !empty($this->params['create_time'])){
            $where[] = ['create_time', 'between', [strtotime($this->params['create_time']), strtotime($this->params['create_time'])+86400-1]];
        }
        if(isset($this->params['team_work_type']) && !empty($this->params['team_work_type'])){
            /*  挂起订单=主管工程师未联系的工单(未分配、待领单)
                超时未领单=分配给其他工程师后未领单的工单(已分配、待领单)
                异常预约=其他工程师未联系的工单(已分配、待联系)
                超时未服务=其他工程师未上门的工单(已分配、待上门)
            */
            switch ($this->params['team_work_type']){
                case 1: // 挂起订单
                    $where['master_worker_id'] = $this->userInfo['user_id'];
                    $where['work_status'] = ['=',1];
                    break;
                case 2: // 超时未领单
                    $where['work_status'] = ['=',1];
                    break;
                case 3: // 异常预约
                    $where['work_status'] = ['=',2];
                    break;
                case 4: // 超时未服务
                    $where['work_status'] = ['=',3];
                    break;
            }
        }
        return $where;
    }
    /**
     * @notes 获取列表
     */
    public function lists(): array
    {
        $list = ServiceWork::where($this->queryWhere())
            ->field('*')
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['appointment_time' => 'asc'])
            ->select()
            ->toArray();
        $this->count = ServiceWork::where($this->queryWhere())->count();
        return $list;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author whitef
     * @date 2024/07/10 15:06
     */
    public function count(): int
    {
        return $this->count;
    }

}