package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.dto.CourseWeightDto;
import com.qmth.distributed.print.business.bean.params.ObeCourseWeightParam;
import com.qmth.distributed.print.business.service.ObeCourseWeightService;
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;

/**
 * <p>
 * 课程权重设置 前端控制器
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-02-22
 */
@Api(tags = "课程大纲权重管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_OBE + "/course_weight")
public class ObeCourseWeightController {

    @Resource
    private ObeCourseWeightService obeCourseWeightService;

    @ApiOperation(value = "课程权重管理-列表查询")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = CourseWeightDto.class) })
    public Result findCourseWeightList(@ApiParam(value = "教学课程id") @RequestParam(required = false) String obeCourseOutlineId) {
        return ResultUtil.ok(obeCourseWeightService.findCourseWeight(SystemConstant.convertIdToLong(obeCourseOutlineId)));
    }

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