package com.hc.web.controller.browser.front;

import com.hc.business.dto.ZccmPaperCommitDTO;
import com.hc.business.model.ZccmClassConfig;
import com.hc.business.model.ZccmPaper;
import com.hc.business.model.ZccmUserStatus;
import com.hc.business.model.ZccmUserTestHistory;
import com.hc.business.service.ZccmClassConfigService;
import com.hc.business.service.ZccmPaperService;
import com.hc.business.service.ZccmUserStatusService;
import com.hc.business.service.ZccmUserTestHistoryService;
import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * 用户考试记录表(UserTestHistory)表控制层
 *
 * @author java
 * @since 2020-12-07 11:28:22
 */

@RestController
@RequestMapping("/new/api/front/userTestHistory")
public class FrontUserTestHistoryController {

    /**
     * 服务对象
     */
    private final ZccmUserTestHistoryService userTestHistoryService;

    private final ZccmPaperService paperService;

    private final ZccmClassConfigService classConfigService;

    private final ZccmUserStatusService userStatusService;

    public FrontUserTestHistoryController(ZccmUserTestHistoryService userTestHistoryService,
                                          ZccmPaperService paperService,
                                          ZccmClassConfigService classConfigService,
                                          ZccmUserStatusService userStatusService) {
        this.userTestHistoryService = userTestHistoryService;
        this.paperService = paperService;
        this.classConfigService = classConfigService;
        this.userStatusService = userStatusService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 UserTestHistory", response = Result.class)
    public Result selectFrontUserTestHistory(@PathVariable("id") Integer id) {
        return Result.success(this.userTestHistoryService.queryById(id));
    }

    @PostMapping("/commit")
    @ApiOperation(value = "提交试卷,获取成绩", response = Result.class)
    public Result paperCommit(@Valid @RequestBody ZccmPaperCommitDTO zccmPaperCommitDTO) {
        return paperService.paperCommit(zccmPaperCommitDTO);
    }

    @GetMapping("/max")
    @ApiOperation(value = "获取用户当前批次最高成绩", response = Result.class)
    public Result selectUserMaxTestHistory(@ApiParam(value = "用户id") @RequestParam Integer userId,
                                           @RequestParam Integer classId) {
        return Result.success(userTestHistoryService.queryMaxScore(userId, classId));
    }

    @GetMapping("/pass")
    @ApiOperation(value = "获取用户成绩，和及格线", response = Result.class)
    public Result selectByCard(@RequestParam Integer statusId) {
        Map<String, Object> map = new HashMap<>(2);
        try {
            ZccmUserStatus userStatus
                    = userStatusService.queryById(statusId);
            Integer userId = userStatus.getUserId();
            Integer classId = userStatus.getClassId();
            ZccmClassConfig classConfig
                    = classConfigService.queryById(classId);
            Integer paperId = classConfig.getPaperId();
            ZccmPaper paper = paperService.queryById(paperId);
            ZccmUserTestHistory userTestHistory
                    = userTestHistoryService.queryMaxScore(userId, classId);
            Integer score = userTestHistory.getScore();
            map.put("score", score);
            map.put("scorePass", paper.getPaperScorePass());
            map.put("time",userTestHistory.getCreateTime());
            return Result.success(map);
        } catch (Exception e) {
           throw new ResponseException(-1,"暂无考试成绩");
        }

    }


}