package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.CourseTargetParam;
import com.qmth.distributed.print.business.bean.result.CourseDimensionTree;
import com.qmth.distributed.print.business.bean.result.CourseTargetResult;
import com.qmth.distributed.print.business.service.CourseDimensionService;
import com.qmth.distributed.print.business.service.CourseTargetService;
import com.qmth.distributed.print.business.service.TeachCourseService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 课程目标表 前端控制器
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-02-22
 */
@Api(tags = "课程目标管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_TEACH + "/course_target")
public class CourseTargetController {

    private final static Logger log = LoggerFactory.getLogger(CourseTargetController.class);

    @Resource
    private CourseTargetService courseTargetService;

    @Resource
    private CourseDimensionService courseDimensionService;

    @Resource
    private TeachCourseService teachCourseService;

    @ApiOperation(value = "课程目标管理-列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CourseTargetResult.class) })
    public Result findCourseTargetList(@ApiParam(value = "教学课程id") @RequestParam(required = false) String teachCourseId,
            @ApiParam(value = "考试id") @RequestParam(required = false) String examId,
            @ApiParam(value = "课程编号") @RequestParam(required = false) String courseCode) {
        if (SystemConstant.strNotNull(examId) && SystemConstant.strNotNull(courseCode)) {
            if (SystemConstant.strNotNull(teachCourseId)) {
                // 要么传examId和courseCode 要么传teachCourseId
                throw ExceptionResultEnum.ERROR.exception("参数错误");
            }
            SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
            teachCourseId = String.valueOf(teachCourseService.findByExamIdCourseCodeAndUserId(SystemConstant.convertIdToLong(examId),
                    courseCode, requestUser.getId()).getId());
        }
        return ResultUtil.ok(courseTargetService.findCourseTargetList(SystemConstant.convertIdToLong(teachCourseId)));
    }

    @ApiOperation(value = "课程目标管理-同步题库知识点")
    @RequestMapping(value = "/dimension_sync_tiku", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = Result.class) })
    public Result syncTikuDimension(@ApiParam(value = "教学课程id") @RequestParam(required = false) String teachCourseId) {
        courseDimensionService.syncTikuCourseProperties(SystemConstant.convertIdToLong(teachCourseId));
        return ResultUtil.ok();
    }

    @ApiOperation(value = "课程目标管理-导入课程知识点")
    @RequestMapping(value = "/dimension_import", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Result.class) })
    @OperationLogDetail(operationType = OperationTypeEnum.IMPORT)
    public Result courseDimensionImportSync(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
            @ApiParam(value = "教学课程id") @RequestParam(required = false) String teachCourseId) throws Exception {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        courseDimensionService.importCourseDimension(requestUser, file, SystemConstant.convertIdToLong(teachCourseId));
        return ResultUtil.ok();
    }

    @ApiOperation(value = "课程目标管理-课程知识点树查询")
    @RequestMapping(value = "/dimension_tree", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CourseDimensionTree.class) })
    public Result findDimensionTree(@ApiParam(value = "教学课程id") @RequestParam(required = false) String teachCourseId) {
        try {
            courseDimensionService.syncTikuCourseProperties(SystemConstant.convertIdToLong(teachCourseId));
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return ResultUtil.ok(courseTargetService.findDimensionTree(SystemConstant.convertIdToLong(teachCourseId)));
    }

    @ApiOperation(value = "课程目标管理-新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "更新成功", response = Result.class) })
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE)
    public Result saveCourseTarget(@Valid @RequestBody CourseTargetParam courseTargetParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        courseTargetService.saveCourseTarget(courseTargetParam, sysUser);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "课程目标管理-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "删除成功", response = Result.class) })
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE)
    public Result deleteCourseTarget(@ApiParam(value = "选择要删除的课程目标id", required = true) @RequestParam String id) {
        courseTargetService.deleteCourseTarget(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok();
    }
}