package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.dto.CourseDimensionDto;
import com.qmth.distributed.print.business.bean.params.ObeCourseTargetExpectValueParam;
import com.qmth.distributed.print.business.bean.params.ObeCourseTargetParam;
import com.qmth.distributed.print.business.bean.result.CourseDimensionTree;
import com.qmth.distributed.print.business.bean.result.ObeCourseTargetResult;
import com.qmth.distributed.print.business.service.ObeCourseDimensionService;
import com.qmth.distributed.print.business.service.ObeCourseRequirementDimensionService;
import com.qmth.distributed.print.business.service.ObeCourseTargetService;
import com.qmth.distributed.print.business.service.ObeCultureProgramRequirementService;
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.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_OBE + "/course_target")
public class ObeCourseTargetController {

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

    @Resource
    private ObeCourseTargetService obeCourseTargetService;

    @Resource
    private ObeCourseDimensionService obeCourseDimensionService;

    @Resource
    private ObeCourseRequirementDimensionService obeCourseRequirementDimensionService;

    @Resource
    private ObeCultureProgramRequirementService obeCultureProgramRequirementService;

    @ApiOperation(value = "课程目标管理-列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = ObeCourseTargetResult.class) })
    public Result findCourseTargetList(@ApiParam(value = "课程大纲id") @RequestParam(required = false) String obeCourseOutlineId) {
        return ResultUtil.ok(obeCourseTargetService.findCourseTargetList(SystemConstant.convertIdToLong(obeCourseOutlineId)));
    }

    @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 obeCourseOutlineId) {
        obeCourseDimensionService.syncTikuCourseProperties(SystemConstant.convertIdToLong(obeCourseOutlineId));
        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 obeCourseOutlineId) throws Exception {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        obeCourseDimensionService.importCourseDimension(requestUser, file, SystemConstant.convertIdToLong(obeCourseOutlineId));
        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 obeCourseOutlineId) {
        try {
            obeCourseDimensionService.syncTikuCourseProperties(SystemConstant.convertIdToLong(obeCourseOutlineId));
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return ResultUtil.ok(obeCourseDimensionService.findDimensionTree(SystemConstant.convertIdToLong(obeCourseOutlineId)));
    }

    @ApiOperation(value = "课程目标管理-指标知识点关系查询")
    @RequestMapping(value = "/requirement_dimension", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CourseDimensionDto.class) })
    public Result findRequirementDimension(@ApiParam(value = "课程大纲id") @RequestParam(required = false) String obeCourseOutlineId,
            @ApiParam(value = "指标点id") @RequestParam(required = false) String obeCultureProgramRequirementId) {
        return ResultUtil.ok(obeCourseRequirementDimensionService.findByOutlineIdAndRequirementId(SystemConstant.convertIdToLong(obeCourseOutlineId),
                SystemConstant.convertIdToLong(obeCultureProgramRequirementId)));
    }

    @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 ObeCourseTargetParam obeCourseTargetParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        obeCourseTargetService.saveCourseTarget(obeCourseTargetParam, 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) {
        obeCourseTargetService.deleteCourseTarget(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok();
    }

    @ApiOperation(value = "课程目标管理-预期值设置")
    @RequestMapping(value = "/setting_expect_value", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "更新成功", response = Result.class) })
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE)
    public Result settingExpertValue(@Valid @RequestBody ObeCourseTargetExpectValueParam obeCourseTargetExpectValueParam,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        obeCourseTargetService.settingExpectValue(obeCourseTargetExpectValueParam, sysUser);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "根据培养方案、课程查询指标点")
    @RequestMapping(value = "/sub_requirement/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "分页查询", response = Result.class) })
    public Result listCourse(@ApiParam(value = "培养方案ID", required = true) @RequestParam String cultureProgramId,
            @ApiParam(value = "课程ID", required = true) @RequestParam String courseId) {
        return ResultUtil.ok(obeCultureProgramRequirementService.listSubRequirement(SystemConstant.convertIdToLong(cultureProgramId),
                SystemConstant.convertIdToLong(courseId)));
    }
}