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.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.bean.params.BasicCollegeParams;
import com.qmth.teachcloud.common.bean.result.BasicCollegeResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.service.BasicCollegeService;
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;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 基础学院前端控制器
 * @Author: CaoZixuan
 * @Date: 2022-08-17
 */
@Api(tags = "学生学院管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_BASIC + "/college")
public class BasicCollegeController {
    @Resource
    private BasicCollegeService basicCollegeService;

    @ApiOperation(value = "学生学院管理-查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = BasicCollegeResult.class)})
    public Result findBasicCollegeList(@ApiParam(value = "学生学院名称") @RequestParam(required = false) String collegeName,
                                       @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(basicCollegeService.basicCollegePage(schoolId, collegeName, pageNumber, pageSize));
    }

    @ApiOperation(value = "学生学院管理-新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE)
    public Result saveBasicCollege(@Valid @RequestBody BasicCollegeParams basicCollegeParams, BindingResult bindingResult) throws IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(basicCollegeService.saveBasicCollege(basicCollegeParams, sysUser));
    }

    @ApiOperation(value = "学生学院管理-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE)
    public Result deleteBasicCollege(@ApiParam(value = "学生学院id", required = true) @RequestParam List<String> idList) {
        if (idList.size() == 0) {
            throw ExceptionResultEnum.ERROR.exception("请选择要删除的学生学院");
        }
        List<Long> newIdList = idList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        return ResultUtil.ok(basicCollegeService.deleteBasicCollegeByIdList(newIdList));
    }
}
