package com.qmth.distributed.print.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.teachcloud.common.service.BasicSemesterService;
import com.qmth.teachcloud.common.bean.params.BasicSemesterParams;
import com.qmth.teachcloud.common.contant.SystemConstant;
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 javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * <p>
 * 学期字典表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-10-28
 */
@Api(tags = "学期管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.basic}/semester")
public class BasicSemesterController {
    @Resource
    private BasicSemesterService basicSemesterService;

    @ApiOperation(value = "学期管理-查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findBasicSemesterList(@ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                        @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
        return ResultUtil.ok(basicSemesterService.basicSemesterPage(schoolId,pageNumber,pageSize));
    }

    /**
     * 模糊查询
     * @param inUsed 1:只查询当前使用的学期  0：只查询不是当前使用的学期  null:查询所有
     * @return
     */
    @ApiOperation(value = "学期模糊查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result list(@RequestParam(value = "inUsed", required = false) Boolean inUsed) {
        Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
        return ResultUtil.ok(basicSemesterService.list(schoolId, inUsed));
    }

    @ApiOperation(value = "学期管理-新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicSemester(@Valid @RequestBody BasicSemesterParams basicSemesterParams, BindingResult bindingResult) throws IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(basicSemesterService.saveBasicSemester(basicSemesterParams));
    }

    @ApiOperation(value = "学期管理-删除（逻辑）")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteBasicSemester(@ApiParam(value = "学期id", required = true) @RequestParam String id) {
        return ResultUtil.ok(basicSemesterService.deleteBasicSemesterById(SystemConstant.convertIdToLong(id)));
    }

    /**
     * 设置为当前使用学期
     * @param id
     * @return
     */
    @ApiOperation(value = "学期管理-设置为当前使用学期")
    @RequestMapping(value = "/set_in_used", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicSemester(@RequestParam("id") Long id) {
        return ResultUtil.ok(basicSemesterService.setInUsed(id));
    }
}
