<?php
namespace app\workerapi\logic;

use app\common\logic\BaseLogic;
use app\common\model\shops\ShopOrderGoods;
use app\common\model\shops\ShopOrders;
use app\common\model\training\TrainingCourse;
use app\common\model\training\TrainingTask;
use app\common\model\training\TrainingWorkerCourse;
use app\common\model\training\TrainingWorkerQuestion;
use app\common\model\training\TrainingWorkerTask;
use think\facade\Db;


/**
 * TrainingWorkerTaskLogic逻辑
 * Class TrainingWorkerTaskLogic
 * @package app\workerapi\logic\works
 */
class TrainingWorkerTaskLogic extends BaseLogic
{




    /**
     * @notes 验证是否购买
     * @param array $params
     * @return bool
     * @author whitef
     * @date 2024/07/10 15:06
     */
    public static function upTaskOrder($masterWorkerId): bool
    {
        $taskWorker = TrainingWorkerTask::where('master_worker_id',$masterWorkerId)->findOrEmpty();
        if (!$taskWorker->isEmpty() && $taskWorker->shop_orders_id == 0) {
            //shop_goods_id  INT ( 10 ) DEFAULT 0  NOT NULL COMMENT '商品id(套餐)',
            //shop_orders_id int(10) NOT NULL DEFAULT '0' COMMENT '购买单号id',
            // 检查是否已经购买套餐
            $sn_orders = ShopOrders::where('worker_id',$masterWorkerId)->where('pay_status',1)->where('refund_status',0)->column('id','sn');
            if (!empty($sn_orders)) {
                $shopOrderGoods = ShopOrderGoods::whereIn('sn',array_keys($sn_orders))->where('shop_goods_id',$taskWorker->shop_goods_id)->findOrEmpty();
                if (!$shopOrderGoods->isEmpty()) {
                    $taskWorker->shop_orders_id = $sn_orders[$shopOrderGoods->sn];
                    $taskWorker->save();

                    // 暂时不限制
                    /*$training_course_ids  = json_decode(TrainingTask::where('id',$taskWorker->training_task_id)->value('training_course_id'),true);
                    // 添加该工程师的所有培训课程初始化记录
                    foreach ($training_course_ids as $course_id) {
                        $workerCourse = TrainingWorkerCourse::create([
                            'master_worker_id' => $masterWorkerId,
                            'training_task_id' => $taskWorker->training_task_id,
                            'training_course_id' => $course_id,
                            'play_time' => 0,
                            'exam_start_time' => 0,
                            'exam_end_time' => 0,
                            'study_status' => 0,
                            'exam_score' => 0,
                        ]);

                        // 添加该工程师该课程的所有试题初始化记录
                        //[{"block_key":1,"select_value":1,"score":"20"},{"block_key":2,"select_value":2,"score":"10"},{"block_key":3,"select_value":3,"score":"70"}]
                        $course_question = json_decode(TrainingCourse::where('id',$course_id)->value('course_question_setting'),true);
                        $course_question_ids = array_column($course_question,'select_value');
                        foreach ($course_question_ids as $question_id) {
                            $workerQuestion = TrainingWorkerQuestion::create([
                                'worker_course_id' => $workerCourse->id,
                                'questions_id' => $question_id,
                                'worker_answer' => '',
                                'exam_score' => 0
                            ]);
                        }
                    }*/

                    return true;
                }
            }
        }
        return false;
    }


}