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


use app\adminapi\logic\goods_category\GoodsCategoryLogic;
use app\common\model\training\TrainingCourse;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * TrainingCourse逻辑
 * Class TrainingCourseLogic
 * @package app\adminapi\logic
 */
class TrainingCourseLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 17:37
     */
    public static function add(array $params): bool
    {
        if($params['subclass']) $params['subclass'] = end($params['subclass']);
        Db::startTrans();
        try {
            TrainingCourse::create([
                'course_name' => $params['course_name'],
                'course_picture' => $params['course_picture'],
                'course_url' => $params['course_url'],
                'course_lock' => $params['course_lock'],
                'course_length' => $params['course_length'],
                'course_question_setting' => self::configureReservedField($params['course_question_setting']??[]),
                'course_question_score' => $params['course_question_score'],
                'subclass' => $params['subclass']??0,
                'randomly_question' => $params['randomly_question']??0,
                'randomly_num' => $params['randomly_num']??0,
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 17:37
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            TrainingCourse::where('id', $params['id'])->update([
                'course_name' => $params['course_name'],
                'course_picture' => $params['course_picture'],
                'course_url' => $params['course_url'],
                'course_lock' => $params['course_lock'],
                'course_length' => $params['course_length'],
                'course_question_setting' => json_encode(self::configureReservedField($params['course_question_setting']??[])),
                'course_question_score' => $params['course_question_score'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 17:37
     */
    public static function delete(array $params): bool
    {
        return TrainingCourse::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/02/14 17:37
     */
    public static function detail($params): array
    {
        return TrainingCourse::findOrEmpty($params['id'])->toArray();
    }

    public static function configureReservedField($data): array
    {
        foreach ($data as $key => &$item) {
            $data[$key]['block_key'] = $key+1;
            $item['select_value'] = (int)$item['select_value'];
        }
        return $data??[];
    }



    public static function classBatchCourses()
    {

        $dataCategory = GoodsCategoryLogic::getTreeData();
        $randomly_question = 1;
        $randomly_num = 4;
        $course_question_score = 70;

        Db::startTrans();
        try {

            foreach ($dataCategory as $item) {
                if($item['id'] == 18) {
                    $course_name = '清洗服务全流程';
                    $picture_url = 'https://cdnweixiu.kyjlkj.com/uploads/images/20250309/20250309152641ddf4a7638.png';
                    $mp4_url = 'https://alipreview.zhongdunanpei.com/00474565fa6371ef88d77511979f0102/a0927b14355944c2a1ab876309a7c0e8-08f7fd77e0d39a658cc82fa71dd50586-ld.mp4';
                }
                if($item['id'] == 6) {
                    $course_name = '维修服务全流程';
                    $picture_url = 'https://cdnweixiu.kyjlkj.com/uploads/images/20250309/20250309152650239af2955.png';
                    $mp4_url = 'https://alipreview.zhongdunanpei.com/a0cd4665fa6371ef8be00360d1d70102/889fc4a4b97645a4ae8ef0f368f7f08d-dcd5c6e3fbfcb59cd2a71c10984a321d-ld.mp4';
                }
                if(in_array($item['id'], [18,6])){
                    foreach ($item['children'] as $children) {
                        foreach ($children['children'] as $thr_children) {
                            TrainingCourse::create([
                                'course_name' => $course_name,
                                'course_picture' => $picture_url,
                                'course_url' => $mp4_url,
                                'course_lock' => 2,
                                'course_length' => 0,
                                'course_question_setting' => [],
                                'course_question_score' => $course_question_score,
                                'subclass' => $thr_children['id']??0,
                                'randomly_question' => $randomly_question,
                                'randomly_num' => $randomly_num,
                            ]);
                        }
                    }
                }
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
    }


}