package com.qmth.distributed.print.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.bean.params.BasicMajorParams;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.BasicMajorService;
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;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 专业字典表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-10-28
 */
@Api(tags = "专业管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.basic}/major")
public class BasicMajorController {
    @Resource
    private BasicMajorService basicMajorService;

    @ApiOperation(value = "专业管理-查询")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findBasicMajorList(@ApiParam(value = "专业名称") @RequestParam(required = false) String majorName,
                                     @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(basicMajorService.basicMajorPage(schoolId, majorName, pageNumber, pageSize));
    }

    @ApiOperation(value = "专业管理-新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicMajor(@Valid @RequestBody BasicMajorParams basicMajorParams, BindingResult bindingResult) throws IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(basicMajorService.saveBasicMajor(basicMajorParams,sysUser));
    }

    @ApiOperation(value = "专业管理-删除（逻辑）")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteBasicMajor(@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(basicMajorService.deleteBasicMajorByIdList(newIdList));
    }
}
