<?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 think\db\Query;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\works\GroupServiceWork;
use app\common\model\goods_category\GoodsCategory;
use app\common\model\master_worker\MasterWorkerTemporary;


/**
 * ServiceWork列表
 * Class ServiceWorkLists
 * @package app\adminapi\listsworks
 */
class GroupServiceWorkLists 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', 'master_worker_id','third_type','group_order_id'],
            '%like%'=>[ 'work_sn','mobile','real_name', 'title', 'address']
        ];
    }

    public function queryWhere(){
        $where = [];
        if (!empty($this->params['service_status']) && $this->params['service_status'] == 1) {
            $where[] = ['service_status', '<>', 3];
        }
        if (!empty($this->params['service_status']) && $this->params['service_status'] == 2) {
            $where[] = ['service_status', '=', 3];
        }
        if (isset($this->params['master_worker_name']) && !empty($this->params['master_worker_name'])) {
            $master_worker_ids = MasterWorkerTemporary::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']];
            $where[] = ['work_status', 'NOT IN', [7,8,9]];
        }
        if (!empty($this->params['goods_category_id'])) {
            $goodsCategoryId = end($this->params['goods_category_id']);
            $goodsCategoryData = GoodsCategory::where(['status'=>1])->order(['pid' => 'asc','weigh' => 'desc', 'id' => 'desc'])
                ->select()->toArray();
            $ids =get_tree_ids($goodsCategoryData,$goodsCategoryId);
            $ids[] = $goodsCategoryId;
            $where[] = ['goods_category_id','in' ,$ids];
        }
        if(isset($this->params['work_status']) && (!empty($this->params['work_status']) || $this->params['work_status'] != '')){
            switch ($this->params['work_status']){
                case 0:
                    $where[] = ['service_status','<' ,3];
                    break;
            }
        }
        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
    {
        $lists = GroupServiceWork::with([
            'worker'=>function(Query $query) {
                $query->field("id,worker_number,real_name,mobile");
            },
            'goodsCategory' =>function (Query $query) {
                $query->field('id,name');
            },
            'serviceWorkLog' =>function(Query $query){
                $query->field('id,work_id,opera_log,create_time');
            },
        ])
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->field(['id', 'code','work_sn', 'real_name', 'mobile', 'address', 'title', 'category_type', 'goods_category_ids', 'goods_category_id', 'work_total','settlement_amount','work_status', 'service_status', 'dispatch_time', 'receive_time', 'appointment_time', 'work_images','finished_images', 'finished_time', 'master_worker_id', 'work_amount',  'create_time', 'finally_door_time','third_type','group_user_order_id'])
            ->append(['service_status_text','category_type_text','third_type_text'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        return $lists;
    }


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

    public function setExcelComplexFields(): array
    {
        $zh_cn_fields = [
            '客户名字','工单编号','下单时间','手机号','服务内容','工程师',
            '工单状态','消费金额','订单来源','完成时间','城市','服务类别'
        ];
        $data_fields = [
            'real_name','work_sn','create_time','mobile','title',function ($row) { return $row['worker']['real_name'] ?? ''; },
            'service_status_text','work_total','third_type_text','finished_time','area_name','category_type_text'
        ];
        return [
            'zh_cn_fields' => $zh_cn_fields,
            'data_fields' => $data_fields
        ];
    }

}