package jnpf.cultivate.controller.web.course;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateCourseService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.course.FtbCultivateCourseDTO;
import jnpf.model.cultivate.dto.course.FtbCultivateCourseQueryDTO;
import jnpf.model.cultivate.dto.course.FtbCultivateCourseUpdateDTO;
import jnpf.model.cultivate.dto.course.FtbCultivateShelvesDTO;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.vo.course.web.*;
import jnpf.model.cultivate.vo.offline.FtbCultivateOfflineTrainPageVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* web课程模块
*
* @author wcx
*/
@RestController
@RequestMapping("/web/ftb-cultivate-course")
public class FtbCultivateCourseController {
    @Resource
    private FtbCultivateCourseService ftbCultivateCourseService;

    /**
     * 添加课程，返回主键id，用于章节操作
     *
     * @param ftbCultivateCourseDTO 课程参数
     * @return {@link ActionResult}<{@link FtbCultivateCourse}>
     */
    @PostMapping("/add")
    public ActionResult<String> add(@Validated @RequestBody FtbCultivateCourseDTO ftbCultivateCourseDTO) {
        String courseId = ftbCultivateCourseService.add(ftbCultivateCourseDTO);
        return ActionResult.success(courseId);
    }

    /**
     * 删除课程
     *
     * @param id 课程主键id，必传
     * @return {@link ActionResult}
     */
    @DeleteMapping("/{id}/{flag}")
    public ActionResult delete(@PathVariable("id") String id) {
        if (StrUtil.isBlank(id)) {
            throw new IllegalArgumentException("参数错误");
        }
        ftbCultivateCourseService.delete(id);
        return ActionResult.success();
    }

    /**
     * 课程删除检查判断
     *
     * @param id 课程主键id，必传
     * @return {@link ActionResult}<{@link FtbCourseDeleteVO}>
     */
    @GetMapping("/course-deletion-verification/{id}")
    public ActionResult<FtbCourseDeleteVO> deleteCheck(@PathVariable("id") String id) {
        if (StrUtil.isBlank(id)) {
            throw new IllegalArgumentException("参数错误");
        }
        return ftbCultivateCourseService.deleteCheck(id);
    }

    /**
     * 更新课程信息
     *
     * @param ftbCultivateCourseUpdateDTO
     * @return {@link ActionResult}
     */
    @PutMapping("/update")
    public ActionResult update(@Validated @RequestBody FtbCultivateCourseUpdateDTO ftbCultivateCourseUpdateDTO) {
        ftbCultivateCourseService.updateCourse(ftbCultivateCourseUpdateDTO);
        return ActionResult.success();
    }

    /**
     * 课程上下架
     *
     * @param ftbCultivateShelvesDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/upper-lower-shelves")
    public ActionResult upperLower(@Validated @RequestBody FtbCultivateShelvesDTO ftbCultivateShelvesDTO) {
        ftbCultivateCourseService.upperLower(ftbCultivateShelvesDTO);
        return ActionResult.success();
    }

    /**
     * 课程管理分页查询
     *
     * @param cultivatePage 分页模型
     * @param ftbCultivateCourseQueryDTO 条件参数
     * @return {@link ActionResult}
     */
    @GetMapping("/query-list")
    public ActionResult<PageListVO<FtbCultivateCoursePageVO>> list(CultivatePage cultivatePage, FtbCultivateCourseQueryDTO ftbCultivateCourseQueryDTO) {
        Page<FtbCultivateCoursePageVO> page = cultivatePage.coverCultivatePage("a.F_Id");
        page = ftbCultivateCourseService.pagingQuery(page, ftbCultivateCourseQueryDTO);
        PageListVO<FtbCultivateCoursePageVO> result = CultivatePage.coverPageList(page);
        return ActionResult.success(result);
    }

    /**
     * 课程管理-分页上方的总数统计
     */
    @GetMapping("/query-list-count")
    public ActionResult<FtbCultivateCourseNumberVO> listCount(){
        FtbCultivateCourseNumberVO ftbCultivateCourseNumberVO = ftbCultivateCourseService.listCount();
        return ActionResult.success(ftbCultivateCourseNumberVO);
    }

    /**
     * 课程详情-编辑时回显调用
     *
     * @param id 课程主键id
     * @return {@link ActionResult}<{@link FtbCultivateCourse}>
     */
    @GetMapping("/details/{id}")
    public ActionResult<FtbCultivateCourseDetailsVO> courseDetails(@PathVariable("id") String id) {
        FtbCultivateCourseDetailsVO ftbCultivateCourse = ftbCultivateCourseService.courseDetails(id);
        return ActionResult.success(ftbCultivateCourse);
    }

    /**
     * 二次删除时，岗位学习分页列表
     *
     * @param cultivatePage 分页
     * @param courseId      课程id必填
     * @return {@link ActionResult}<{@link PageListVO}<{@link FtbCultivateOfflineTrainPageVO}>>
     */
    @GetMapping("/job-learning-paginated-list")
    public ActionResult<PageListVO<FtbCourseDeleteJobLearnVO>> deleteJobLearningList(CultivatePage cultivatePage, String courseId) {
        Page<FtbCourseDeleteJobLearnVO> page = cultivatePage.coverCultivatePage();
        page = ftbCultivateCourseService.deleteJobLearningList(page, courseId);
        PageListVO<FtbCourseDeleteJobLearnVO> result = CultivatePage.coverPageList(page);
        return ActionResult.success(result);
    }

    /**
     * 二次删除时，关联题库分页列表
     *
     * @param cultivatePage 分页
     * @param courseId      课程id必填
     * @return {@link ActionResult}<{@link Integer}>
     */
    @GetMapping("/query-list-offline-train-count")
    public ActionResult<PageListVO<FtbCourseDeleteQuestionBankVO>> deleteRelatedQuestionBankList(CultivatePage cultivatePage, String courseId) {
        Page<FtbCourseDeleteQuestionBankVO> page = cultivatePage.coverCultivatePage();
        page = ftbCultivateCourseService.deleteRelatedQuestionBankList(page, courseId);
        PageListVO<FtbCourseDeleteQuestionBankVO> result = CultivatePage.coverPageList(page);
        return ActionResult.success(result);
    }

    /**
     * 校验岗位是否建立岗位学习，返回不存在的岗位id信息
     *
     * @param postIds 待校验岗位id集合（必传）
     * @return {@link ActionResult }
     */
    @PostMapping("/query-job-binding-courses")
    public ActionResult<List<FtbCourseDeleteJobLearnVO.FtbCultivateJobBindingCoursesVO>> queryJobBindingCourses(@RequestBody List<String> postIds) {
        if (CollUtil.isEmpty(postIds)) {
            throw new RuntimeException("请选择学习岗位");
        }
        List<FtbCourseDeleteJobLearnVO.FtbCultivateJobBindingCoursesVO> ftbCultivateCoursePageVOS = ftbCultivateCourseService.queryJobBindingCourses(postIds);
        return ActionResult.success(ftbCultivateCoursePageVOS);
    }

}
