package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.analyze.GradeBatchParam;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.bean.result.analyze.GradeBatchResult;
import com.qmth.distributed.print.business.entity.GradeBatch;
import com.qmth.distributed.print.business.service.GradeBatchService;
import com.qmth.distributed.print.business.templete.execute.AsyncTeachCloudReportService;
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.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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分析-批次表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Api(tags = "分析批次管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.grade}/batch")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class GradeBatchController {
    @Resource
    private GradeBatchService gradeBatchService;

    @Resource
    private AsyncTeachCloudReportService asyncTeachCloudReportService;

    @ApiOperation(value = "成绩分析批次-查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradeBatchResult.class)})
    public Result findGradeBatchPage(@ApiParam(value = "分析批次名称") @RequestParam(required = false) String gradeBatchName,
                                     @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) {

        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(gradeBatchService.gradeBatchPage(gradeBatchName, pageNumber, pageSize, requestUser));
    }

    @ApiOperation(value = "成绩分析批次-新建")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    public Result saveGradeBatch(@Valid @RequestBody GradeBatchParam gradeBatchParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(gradeBatchService.editGradeBatch(gradeBatchParam, requestUser));
    }

    @ApiOperation(value = "成绩分析批次-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteGradeBatch(@ApiParam(value = "选择的要删除的成绩分析批次id集合", required = true) @RequestParam List<String> idList) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        gradeBatchService.deleteGradeBatch(idList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList()), requestUser);
        return ResultUtil.ok();
    }

    /**
     * 按批次下载考务数据
     *
     * @param batchId 批次ID
     */
    @ApiOperation(value = "成绩分析批次课程-考务数据下载")
    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "下载成功", response = EditResult.class)})
    public void downloadFile(@RequestParam Long batchId) throws IOException {
        gradeBatchService.downloadFile(batchId, ServletUtil.getResponse());
    }

    /**
     * 导入考务数据-包含任课老师信息
     *
     * @param batchId 批次ID
     * @param file    上传文件
     * @return
     */
    @ApiOperation(value = "成绩分析批次课程-考务数据导入")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "上传成功", response = EditResult.class)})
    public Result upload(@RequestParam Long batchId,
                         @RequestParam MultipartFile file) throws IOException, NoSuchFieldException {
        gradeBatchService.uploadFile(batchId, file);
        return ResultUtil.success(true);
    }

    /**
     * 导入考务数据-包含任课老师信息
     *
     * @param batchId 批次ID
     */
    @ApiOperation(value = "成绩分析批次课程-批次同步")
    @RequestMapping(value = "/push", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "同步成功", response = EditResult.class)})
    public Result batchSync(@RequestParam Long batchId) {
        GradeBatch gradeBatch = gradeBatchService.getById(batchId);
        if(gradeBatch == null){
            throw ExceptionResultEnum.ERROR.exception("没有批次信息");
        }
        asyncTeachCloudReportService.syncGradeBatch(gradeBatch);
        return ResultUtil.success(true);
    }

    /**
     * 成绩分析批次课程-开始计算
     *
     * @param batchId 批次ID
     */
    @ApiOperation(value = "成绩分析批次课程-开始计算")
    @RequestMapping(value = "/start_calc", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "同步成功", response = EditResult.class)})
    public Result startCalc(@RequestParam Long batchId) {
        GradeBatch gradeBatch = gradeBatchService.getById(batchId);
        if(gradeBatch == null){
            throw ExceptionResultEnum.ERROR.exception("没有批次信息");
        }
        asyncTeachCloudReportService.startCalc(gradeBatch);
        return ResultUtil.success(true);
    }
}
