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

import com.hc.business.dto.PaperCommitDTO;
import com.hc.business.model.ClassConfig;
import com.hc.business.model.Paper;
import com.hc.business.model.UserStatus;
import com.hc.business.model.UserTestHistory;
import com.hc.business.service.ClassConfigService;
import com.hc.business.service.PaperService;
import com.hc.business.service.UserStatusService;
import com.hc.business.service.UserTestHistoryService;
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 UserTestHistoryService userTestHistoryService;

    private final PaperService paperService;

    private final ClassConfigService classConfigService;

    private final UserStatusService userStatusService;

    public FrontUserTestHistoryController(UserTestHistoryService userTestHistoryService,
                                          PaperService paperService,
                                          ClassConfigService classConfigService,
                                          UserStatusService 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 PaperCommitDTO paperCommitDTO) {
        return paperService.paperCommit(paperCommitDTO);
    }

    @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 {
            UserStatus userStatus
                    = userStatusService.queryById(statusId);
            if(userStatus ==null) {
                return Result.exception(652, "数据不存在");
            }
            Integer userId = userStatus.getUserId();
            Integer classId = userStatus.getClassId();
            ClassConfig classConfig
                    = classConfigService.queryById(classId);
            Integer paperId = classConfig.getPaperId();
            Paper paper = paperService.queryById(paperId);
            UserTestHistory userTestHistory
                    = userTestHistoryService.queryMaxScore(userId, classId);
            if(userTestHistory ==null) {
                return Result.exception(652, "查询不到成绩");
            }
            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,"暂无考试成绩");
        }

    }


}