package com.education.api.controller.admin.education;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.education.business.service.education.CourseInfoService;
import com.education.business.service.education.CourseSectionNodeService;
import com.education.business.service.education.CourseSectionService;
import com.education.common.annotation.Param;
import com.education.common.annotation.ParamsType;
import com.education.common.annotation.ParamsValidate;
import com.education.common.base.BaseController;
import com.education.common.utils.Result;
import com.education.model.dto.CourseInfoDto;
import com.education.model.entity.CourseInfo;
import com.education.model.entity.CourseSection;
import com.education.model.entity.CourseSectionNode;
import com.education.model.request.PageParam;
import com.education.model.vo.CourseSectionVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

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

/**
 * 课程管理接口
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/25 15:03
 */
@RestController
@RequestMapping("/system/course")
public class CourseInfoController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(CourseInfoController.class);
    @Resource
    private CourseSectionService courseSectionService;
    @Resource
    private CourseInfoService courseInfoService;
    @Resource
    private CourseSectionNodeService courseSectionNodeService;


    /**
     * 课程列表
     * @param pageParam
     * @param courseInfo
     * @return
     */
    @GetMapping
    //@RequiresPermissions("system:course:list")
    public Result list(PageParam pageParam, CourseInfoDto courseInfoDto) {
        return Result.success(courseInfoService.selectPageList(pageParam, courseInfoDto));
    }

    /**
     * 添加或修改课程
     * @param courseInfo
     * @return
     */
    @PostMapping("saveOrUpdate")
    @ParamsValidate(params = {
        @Param(name = "name", message = "请输入课程名称"),
        @Param(name = "headImg", message = "请上传课程封面"),
        @Param(name = "schoolType", message = "请选择课程阶段"),
        @Param(name = "gradeInfoId", message = "请选择年级"),
        @Param(name = "subjectId", message = "请选择所属科目")
    }, paramsType = ParamsType.JSON_DATA)
    //@RequiresPermissions(value = {"system:course:save", "system:course:update"}, logical = Logical.OR)
    public Result saveOrUpdate(@RequestBody CourseInfo courseInfo) {
        courseInfoService.saveOrUpdateCourse(courseInfo);
        return Result.success();
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    //@RequiresPermissions("system:course:deleteById")
    public Result deleteById(@PathVariable Integer id) {
        courseInfoService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加或修改课程章节
     * @param CourseSection
     * @return
     */
    @PostMapping("/section")
    @ParamsValidate(params = {
            @Param(name = "title", message = "请输入章节标题"),
            @Param(name = "courseId", message = "课程Id为空"),
    }, paramsType = ParamsType.JSON_DATA)
    //@RequiresPermissions(value = {"system:section:save", "system:section:update"}, logical = Logical.OR)
    public Result section(@RequestBody CourseSection section) {
        courseSectionService.saveOrUpdate(section);
        return Result.success();
    }

    @GetMapping("/{courseId}/section")
    //@RequiresPermissions("system:section:list")
    public Result section(@PathVariable Integer courseId) {
        List<CourseSection> list = courseSectionService.list(new LambdaQueryWrapper<CourseSection>().eq(CourseSection::getCourseId, courseId));
        List<CourseSectionVo> collect = list.stream().map(item -> {
            CourseSectionVo courseSectionVo = new CourseSectionVo(item);
            List<CourseSectionNode> nodeList = courseSectionNodeService.list(new LambdaQueryWrapper<CourseSectionNode>().eq(CourseSectionNode::getCourseSectionId, item.getId()));
            courseSectionVo.setCourseSectionNodeList(nodeList);
            return courseSectionVo;
        }).collect(Collectors.toList());
        return Result.success(collect);
    }
    @PostMapping("/section/node")
    @ParamsValidate(params = {
            @Param(name = "title", message = "请输入节点标题"),
            @Param(name = "courseId", message = "请选择所属课程"),
            @Param(name = "synopsis", message = "请输入节点简介"),
            @Param(name = "courseSectionId", message = "请选择所属章节"),
    }, paramsType = ParamsType.JSON_DATA)
    //@RequiresPermissions(value = {"system:section:node:save", "system:section:node:update"}, logical = Logical.OR)
    public Result sectionNode(@RequestBody CourseSectionNode sectionNode) {
        courseSectionNodeService.saveOrUpdate(sectionNode);
        return Result.success(sectionNode);
    }
    @DeleteMapping("/section/node/{id}")
    //@RequiresPermissions("system:section:node:deleteById")
    public Result delSectionNode(@PathVariable Integer id) {
        courseSectionNodeService.removeById(id);
        return Result.success();
    }
}
