<?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\goods_category\GoodsCategory;
use app\common\model\master_worker\MasterWorker;
use app\common\model\master_worker\MasterWorkerInfo;
use app\common\model\master_worker_credential\MasterWorkerCredentialImages;
use app\common\model\master_worker_register\MasterWorkerRegister;
use app\common\model\property\PropertyOrder;
use app\common\model\works\ServiceWork;
use app\common\lists\ListsSearchInterface;
use think\db\Query;
use think\facade\Log;


/**
 * 导出ServiceWork列表
 * Class ServiceWorkLists
 * @package app\adminapi\listsworks
 */
class ServiceWorkMailLists extends BaseAdminDataLists implements ListsSearchInterface
{
    /**
     * 设置搜索条件
     */
    public function setSearch(): array
    {
        return [
            '=' => ['id']
        ];
    }

    public function queryWhere(){
        $where = [];
        $time = [strtotime(date("Y-m-d")), strtotime(date("Y-m-d 22:59:59"))];
        $where[] = ['finished_time', 'between', $time];
        return $where;
    }
    /**
     * 获取列表
     */
    public function lists(): array
    {
        $lists = ServiceWork::with([
                'worker'=>function(Query $query) {
                    $query->field("id,worker_number,real_name,mobile");
                },
                'goodsCategory' =>function (Query $query) {
                    $query->field('id,name');
                }
            ])
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->where('master_worker_id', '>', 0)
            ->field(['*'])
            ->append(['category_type_text'])
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        $serialNumber = 1;
        array_walk($lists, function(&$item) use (&$serialNumber) {
            $item['serial_number'] = $serialNumber++;
        });
        return $lists;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function count(): int
    {
        return 0;
    }


    public function setFileName(): string
    {
        return date('Ymd');
    }

    public function setExcelComplexFields(): array
    {
        $zh_cn_fields = [
            '序号','工单编号', '服务类别','维修家电类型','领单时间','预约上门时间','结单时间',
            '工程师','工程师身份证号','工程师手机号','资格证书号','获证日期','客户信息','客户手机号','客户地址'
        ];
        $data_fields = [
            'serial_number','work_sn','category_type_text',function ($row) { return isset($row['goodsCategory']['name'])?$row['goodsCategory']['name']:''; },function ($row) { return $row['receive_time']?:''; },function ($row) { return isset($row['finally_door_time'])?date('Y-m-d H:i:s',$row['finally_door_time']):''; },function ($row) { return $row['finished_time']?:''; },
            function ($row) { return isset($row['worker']['real_name'])?$row['worker']['real_name']:''; },
            function ($row) { return isset($row['worker']['id'])?MasterWorkerInfo::where(['worker_id'=>$row['worker']['id']])->value('id_card'):''; },
            function ($row) { return isset($row['worker']['mobile'])?$row['worker']['mobile']:''; },
            function ($row) {
                if(isset($row['worker']['id'])){
                    $worker_register_id = MasterWorkerRegister::where(['worker_id'=>$row['worker']['id']])->value('id')?:0;
                    return MasterWorkerCredentialImages::where(['audit_state'=>1, 'worker_register_id'=>$worker_register_id])->value('number')?:'';
                }else{
                    return '';
                }
            },
            function ($row) {
                if(isset($row['worker']['id'])){
                    $worker_register_id = MasterWorkerRegister::where(['worker_id'=>$row['worker']['id']])->value('id')?:0;
                    return MasterWorkerCredentialImages::where(['audit_state'=>1, 'worker_register_id'=>$worker_register_id])->value('start_time')?:'';
                }else{
                    return '';
                }
            },
            'real_name','mobile','address'
        ];
        return [
            'zh_cn_fields' => $zh_cn_fields,
            'data_fields' => $data_fields
        ];
    }
}