package com.qmth.paper.library.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.common.bean.params.BasicSemesterParams;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.service.BasicSemesterService;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import com.qmth.paper.library.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>
 */
@Api(tags = "学期管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_BASIC + "/semester")
public class BasicSemesterController {
    @Resource
    private BasicSemesterService basicSemesterService;

    @ApiOperation(value = "查询")
    @RequestMapping(value = "/page", 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));
    }

    @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 ids) {
        return ResultUtil.ok(basicSemesterService.deleteBasicSemesterById(ids));
    }

}
