/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.disk.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.disk.domain.CourseAuditRecord;
import vip.xiaonuo.disk.domain.CourseInfo;
import vip.xiaonuo.disk.dto.courseInfo.CourseInfoAddParam;
import vip.xiaonuo.disk.dto.courseInfo.CourseInfoEditParam;
import vip.xiaonuo.disk.dto.courseInfo.CourseInfoIdParam;
import vip.xiaonuo.disk.dto.courseInfo.CourseInfoPageParam;
import vip.xiaonuo.disk.service.CourseAuditRecordService;
import vip.xiaonuo.disk.service.CourseInfoService;
import vip.xiaonuo.sys.modular.user.entity.SysUser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.*;

/**
 * 课程信息表控制器
 *
 * @author honorfire
 * @date  2025/06/20 15:00
 */
@Api(tags = "课程信息表控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class CourseInfoController {

    @Resource
    private CourseInfoService courseInfoService;
    @Resource
    private CourseAuditRecordService courseAuditRecordService;

    /**
     * 课程信息-分页列表
     *
     * @author honorfire
     * @date  2025/06/20 15:00
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("课程信息-分页列表")
    @SaCheckPermission("/disk/courseinfo/page")
    @GetMapping("/disk/courseinfo/page")
    public CommonResult<Page<Map<String,Object>>> page(CourseInfoPageParam courseInfoPageParam, HttpServletRequest req) {
        Map param =new HashMap();
        param.put("courseName", req.getParameter("courseName"));
        param.put("collegeId", req.getParameter("collegeId"));
        param.put("majorId", req.getParameter("majorId"));
        param.put("courseType", req.getParameter("courseType"));
        //排序类型，0最新，1热门
        param.put("sortflag", req.getParameter("sortflag"));
        //格式类型，待定
        param.put("suffixType", req.getParameter("suffixType"));

        Page<Map<String,Object>> list=courseInfoService.queryList(param);
        return CommonResult.data(list);
    }

    /**
     * 课程信息-全量列表
     *
     * @author honorfire
     * @date  2025/06/20 15:00
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("课程信息-全量列表")
    @SaCheckPermission("/disk/courseinfo/allList")
    @GetMapping("/disk/courseinfo/allList")
    public CommonResult<List<CourseInfo>> allList(CourseInfoPageParam courseInfoPageParam, HttpServletRequest req) {
        Map param =new HashMap();
        List<CourseInfo> list=courseInfoService.queryCourseInfoList(param);
        return CommonResult.data(list);
    }

    /**
     * 课程信息-添加
     *
     * @author honorfire
     * @date  2025/06/20 15:00
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("课程信息-添加")
    @CommonLog("课程信息-添加")
    @SaCheckPermission("/disk/courseinfo/add")
    @PostMapping("/disk/courseinfo/add")
    public CommonResult<String> add(@RequestBody @Valid CourseInfoAddParam courseInfoAddParam) {
        //1.先添加课程信息
        CourseInfo courseInfo = BeanUtil.toBean(courseInfoAddParam, CourseInfo.class);
        //设置审核状态变成待审核（2025.6.26废弃，现在只要资源发布就有课程，只是最后审关联课程一个都没通过，没有关联的资源会查不出来）
//        courseInfo.setVerifyStatus("1");
        //设置初始关联课程（2025.6.27废弃）
//        courseInfo.setHaveresource("0");
        courseInfo.setPublishTime(new Date());
        courseInfoService.addOne(courseInfo);
//        //2.要查回关联的多条课程资源审核记录，然后进行绑定2025.6.26(废弃2025.6.27，课程和资源解耦，不在强绑定)
//        String courseAuditIdsStr=courseInfoAddParam.getCourseAuditIds();
//        String[] courseAuditIdList = courseAuditIdsStr.split(",");
//        List<CourseAuditRecord> courseAuditRecordList = new ArrayList<>();
//        for(String courseAuditId : courseAuditIdList)
//        {
//            CourseAuditRecord courseAuditRecord=courseAuditRecordService.queryEntity(courseAuditId);
//            courseAuditRecord.setCourseId(courseInfo.getCourseId());
//            //设置审核状态变成待审核
//            courseAuditRecord.setVerifyStatus("1");
//            courseAuditRecordList.add(courseAuditRecord);
//        }
//
//        courseAuditRecordService.editBatch(courseAuditRecordList);
        return CommonResult.ok();
    }

    /**
     * 课程信息-编辑
     *
     * @author honorfire
     * @date  2025/06/20 15:00
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("课程信息-编辑")
    @CommonLog("课程信息-编辑")
    @SaCheckPermission("/disk/courseinfo/edit")
    @PostMapping("/disk/courseinfo/edit")
    public CommonResult<String> edit(@RequestBody @Valid CourseInfoEditParam courseInfoEditParam) {
        //1.先更新课程基础信息
        CourseInfo courseInfo = BeanUtil.toBean(courseInfoEditParam, CourseInfo.class);
        //设置审核状态重新变成待审核（2025.6.26废弃）
//        courseInfo.setVerifyStatus("1");
        courseInfoService.editOne(courseInfo);
        //2.要查回关联的那条课程审核记录，同样修改状态变成待审核（2025.6.26废弃，修改课程只修改课程信息）
//        CourseAuditRecord courseAuditRecord=courseAuditRecordService.queryEntity(courseInfoEditParam.getCourseAuditId());
//        courseAuditRecord.setVerifyStatus("1");
//        courseAuditRecordService.editOne(courseAuditRecord);
        return CommonResult.ok();
    }

    /**
     * 删除课程信息表
     *
     * @author honorfire
     * @date  2025/06/20 15:00
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除课程信息表")
    @CommonLog("删除课程信息表")
    @SaCheckPermission("/disk/courseinfo/delete")
    @PostMapping("/disk/courseinfo/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<CourseInfoIdParam> courseInfoIdParamList) {
        courseInfoService.delete(courseInfoIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取课程信息表详情
     *
     * @author honorfire
     * @date  2025/06/20 15:00
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取课程信息表详情")
    @SaCheckPermission("/disk/courseinfo/detail")
    @GetMapping("/disk/courseinfo/detail")
    public CommonResult<Map<String,Object>> detail(@Valid CourseInfoIdParam courseInfoIdParam, HttpServletRequest req) {
        Map param =new HashMap();
        param.put("courseId", req.getParameter("courseId"));
        Map<String,Object> result=courseInfoService.queryInfo(param);
        return CommonResult.data(result);
    }

}
