package com.qmth.distributed.print.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.CourseEvaluationParam;
import com.qmth.distributed.print.business.bean.result.CourseEvaluationResult;
import com.qmth.distributed.print.business.enums.CourseEvaluationTypeEnum;
import com.qmth.distributed.print.business.service.CourseEvaluationService;
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.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 课程评价方式 前端控制器
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-02-22
 */
@Api(tags = "评价方式管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_TEACH + "/course_evaluation")
public class CourseEvaluationController {

    @Resource
    private CourseEvaluationService courseEvaluationService;

    @ApiOperation(value = "评价方式管理-列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CourseEvaluationResult.class) })
    public Result findCourseEvaluation(@ApiParam(value = "教学课程id") @RequestParam(required = false) String teachCourseId) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(
                courseEvaluationService.findCourseEvaluationList(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 saveCourseEvaluation(@Valid @RequestBody CourseEvaluationParam courseEvaluationParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (Objects.nonNull(courseEvaluationParam)) {
            // 设置为自定义评价方式
            courseEvaluationParam.setType(CourseEvaluationTypeEnum.CUSTOM);
        }
        courseEvaluationService.saveCourseEvaluation(courseEvaluationParam);
        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 deleteCourseEvaluation(@ApiParam(value = "选择要删除的评价方式id", required = true) @RequestParam String id) {
        courseEvaluationService.deleteCourseEvaluation(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok();
    }
}
