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


use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\training\TrainingBlockConfigLogic;
use app\common\model\master_worker\MasterWorker;
use app\common\model\training\TrainingWorkerTask;
use app\common\lists\ListsSearchInterface;
use app\workerapi\service\MasterWokerTaskRequiredService;


/**
 * TrainingWorkerTask列表
 * Class TrainingWorkerTaskLists
 * @package app\adminapi\lists
 */
class TrainingWorkerTaskLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2025/02/16 13:49
     */
    public function setSearch(): array
    {
        return [
            '=' => ['master_worker_id', 'shop_goods_id', 'shop_orders_id', 'training_task_id', 'training_status', 'lead_master_worker_id', 'operate_status'],

        ];
    }

    public function queryWhere()
    {
        $where = [];
        $whereWorker = [];
        if(isset($this->params['province']) && !empty($this->params['province'])){
            $whereWorker[] = ['province','=' ,$this->params['province']];
        }
        if(isset($this->params['city']) && !empty($this->params['city'])){
            $whereWorker[] = ['city','=' ,$this->params['city']];
        }
        if(!empty($whereWorker)){
            $worker_ids = MasterWorker::where($whereWorker)->column('id')??[0];
            $where = [['master_worker_id','in' ,$worker_ids]];
        }
        return $where;
    }

    /**
     * 获取数据权限
     * $this->adminInfo['data_rules']
     * province city admin_id sale_group_id sale_id  property_head_id
     */
    public function queryDataWhere(){
        $where = [];
        $data_rules = $this->adminInfo['data_rules'];
        if (isset($data_rules['province']) && !empty($data_rules['province'])) {
            $where[] = ['province','in' ,$data_rules['province']];
        }
        if (isset($data_rules['city']) && !empty($data_rules['city'])) {
            $where[] = ['city','in' ,$data_rules['city']];
        }
        if(!empty($where)){
            $worker_ids = MasterWorker::where($where)->column('id')??[0];
            $where = [['master_worker_id','in' ,$worker_ids]];
        }
        return $where;
    }

    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2025/02/16 13:49
     */
    public function lists(): array
    {
        $lists = TrainingWorkerTask::with([
                'TrainingTask',
                'MasterWorker',
                'LeadMasterWorker',
                'ShopGoods',
                'ShopOrders',
            ])->where($this->searchWhere)->where($this->queryWhere())->where($this->queryDataWhere())
            ->field(['id', 'task_list','master_worker_id', 'shop_goods_id', 'shop_orders_id', 'training_task_id', 'training_status', 'lead_master_worker_id', 'operate_status'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        foreach ($lists as &$item) {
            $item['taskDetail'] = $this->taskDetail($item['master_worker_id'],$item['MasterWorker']['identity_source']);
        }
        return $lists;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2025/02/16 13:49
     */
    public function count(): int
    {
        return TrainingWorkerTask::where($this->searchWhere)->count();
    }

    public function taskDetail($master_worker_id,$identity_source)
    {
        $trainingWorkerTask = TrainingWorkerTask::where(['master_worker_id'=>$master_worker_id])->findOrEmpty();
        if($trainingWorkerTask->isEmpty()){
            return [];
        }
        $configs = TrainingBlockConfigLogic::getRequiredConfig($identity_source);
        $taskDetail = [];
        foreach ($configs as $item) {
            if(empty($item['execute_function'])) continue;
            if(!class_exists(\app\workerapi\service\MasterWokerTaskRequiredService::class)){
                continue;
            }
            if(!method_exists(\app\workerapi\service\MasterWokerTaskRequiredService::class,$item['execute_function'])){
                continue;
            }
            $execute_function = $item['execute_function'];
            $res = (new MasterWokerTaskRequiredService())::$execute_function($master_worker_id);

            $taskDetail[] = [
                'type_name'=>$item['type_name'],
                'is_must'=> $item['is_must']?'是':'否',
                'result'=> $res===false?'未完成':'已完成',
            ];
        }
        return $taskDetail;
    }




}