package com.qmth.teachcloud.report.api;

import com.qmth.boot.api.constant.ApiConstant;
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.report.business.enums.PublishStatusEnum;
import com.qmth.teachcloud.report.business.service.AnalyzeForReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @Description: 数据计算控制类
 * @Author: CaoZixuan
 * @Date: 2021-06-15
 */
@Api(tags = "数据源分析计算controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.analyze}")
public class DataAnalyzeController {
    @Resource
    private AnalyzeForReportService analyzeForReportService;

    @ApiOperation(value = "数据计算")
    @RequestMapping(value = "/calculate", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result dataCalculate(@RequestParam Long schoolId, @RequestParam Long examId, @RequestParam(required = false) String courseCode) throws Exception {
        analyzeForReportService.dataCalculate(examId,courseCode);
        analyzeForReportService.updateCoursePublishStatus(examId,courseCode, PublishStatusEnum.WILL_COMPUTE);
        return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
    }
}
