package com.greenorange.promotion.controller.course;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.annotation.SysLog;
import com.greenorange.promotion.common.BaseResponse;
import com.greenorange.promotion.common.ResultUtils;
import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.model.dto.CommonBatchRequest;
import com.greenorange.promotion.model.dto.CommonRequest;
import com.greenorange.promotion.model.dto.courseChapter.CourseChapterAddRequest;
import com.greenorange.promotion.model.dto.courseChapter.CourseChapterQueryRequest;
import com.greenorange.promotion.model.dto.courseChapter.CourseChapterUpdateRequest;
import com.greenorange.promotion.model.entity.CourseChapter;
import com.greenorange.promotion.model.vo.courseChapter.CourseChapterVO;
import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.course.CourseChapterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 课程章节 控制器
 */
@RestController
@RequestMapping("courseChapter")
@Slf4j
@Tag(name = "课程章节模块")
public class CourseChapterController {

    @Resource
    private CourseChapterService courseChapterService;

    @Resource
    private CommonService commonService;

    /**
     * web端管理员添加课程章节
     * @param courseChapterAddRequest 课程章节添加请求体
     * @return 是否添加成功
     */
    @PostMapping("add")
    @Operation(summary = "web端管理员添加课程章节", description = "参数：课程章节添加请求体，权限：管理员，方法名：addCourseChapter")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "课程章节管理", content = "web端管理员添加课程章节")
    public BaseResponse<Long> addCourseChapter(@Valid @RequestBody CourseChapterAddRequest courseChapterAddRequest) {
        CourseChapter courseChapter = commonService.copyProperties(courseChapterAddRequest, CourseChapter.class);
        courseChapterService.save(courseChapter);
        return ResultUtils.success(courseChapter.getId());
    }

    /**
     * web端管理员根据id修改课程章节信息
     * @param courseChapterUpdateRequest 课程章节更新请求体
     * @return 是否更新成功
     */
    @PostMapping("update")
    @Operation(summary = "web端管理员根据id修改课程章节", description = "参数：课程章节更新请求体，权限：管理员，方法名：updateCourseChapter")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "课程章节管理", content = "web端管理员根据id修改课程章节信息")
    public BaseResponse<Boolean> updateCourseChapter(@Valid @RequestBody CourseChapterUpdateRequest courseChapterUpdateRequest) {
        CourseChapter courseChapter = commonService.copyProperties(courseChapterUpdateRequest, CourseChapter.class);
        courseChapterService.updateById(courseChapter);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id删除课程章节
     * @param commonRequest 课程章节删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delete")
    @Operation(summary = "web端管理员根据id删除课程章节", description = "参数：课程章节删除请求体，权限：管理员，方法名：delCourseChapter")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "课程章节管理", content = "web端管理员根据id删除课程章节")
    public BaseResponse<Boolean> delCourseChapter(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        courseChapterService.removeById(id);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员批量删除课程章节
     * @param commonBatchRequest 课程章节批量删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delBatch")
    @Operation(summary = "web端管理员批量删除课程章节", description = "参数：课程章节批量删除请求体，权限：管理员，方法名：delBatchCourseChapter")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "课程章节管理", content = "web端管理员批量删除课程章节")
    public BaseResponse<Boolean> delBatchCourseChapter(@Valid @RequestBody CommonBatchRequest commonBatchRequest) {
        List<Long> ids = commonBatchRequest.getIds();
        courseChapterService.removeByIds(ids);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id查询课程章节
     * @param commonRequest 课程章节查询请求体
     * @return 课程章节信息
     */
    @PostMapping("queryById")
    @Operation(summary = "web端管理员根据id查询课程章节", description = "参数：课程章节查询请求体，权限：管理员，方法名：queryCourseChapterById")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "课程章节管理", content = "web端管理员根据id查询课程章节")
    public BaseResponse<CourseChapterVO> queryCourseChapterById(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        CourseChapter courseChapter = courseChapterService.getById(id);
        CourseChapterVO courseChapterVO = commonService.copyProperties(courseChapter, CourseChapterVO.class);
        return ResultUtils.success(courseChapterVO);
    }

    /**
     * Web端管理员根据课程id分页查询课程章节
     * @param courseChapterQueryRequest 课程章节查询请求体
     * @return 课程章节列表
     */
    @PostMapping("page")
    @Operation(summary = "Web端管理员根据课程id分页查询课程章节", description = "参数：课程章节查询请求体，权限：管理员，方法名：listCourseChapterByPageByCourseId")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "课程章节管理", content = "Web端管理员根据课程id分页查询课程章节")
    public BaseResponse<Page<CourseChapterVO>> listCourseChapterByPageByCourseId(@Valid @RequestBody CourseChapterQueryRequest courseChapterQueryRequest) {
        long current = courseChapterQueryRequest.getCurrent();
        long pageSize = courseChapterQueryRequest.getPageSize();
        QueryWrapper<CourseChapter> queryWrapper = courseChapterService.getQueryWrapper(courseChapterQueryRequest);
        Page<CourseChapter> page = courseChapterService.page(new Page<>(current, pageSize), queryWrapper);
        List<CourseChapter> courseChapterList = page.getRecords();
        List<CourseChapterVO> courseChapterVOList = commonService.convertList(courseChapterList, CourseChapterVO.class);
        Page<CourseChapterVO> voPage = new Page<>(current, pageSize);
        voPage.setRecords(courseChapterVOList);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        return ResultUtils.success(voPage);
    }
}