package com.ruoyi.hezhi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hezhi.domain.TbExam;
import com.ruoyi.hezhi.service.ITbExamService;
import com.ruoyi.hezhi.service.ITbExamSubjectFormalService;
import com.ruoyi.hezhi.service.ITbExamSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考试Controller
 *
 * @author ruoyi
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/hezhi/exam")
public class TbExamController extends BaseController
{
    @Autowired
    private ITbExamService tbExamService;
    @Autowired
    private ITbExamSubjectService tbExamSubjectService;
    @Autowired
    private ITbExamSubjectFormalService tbExamSubjectFormalService;

    /**
     * 查询考试列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbExam tbExam)
    {
        startPage();
        List<TbExam> list = tbExamService.selectTbExamList(tbExam);
        return getDataTable(list);
    }

    /**
     * 导出考试列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:exam:export')")
    @Log(title = "考试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbExam tbExam)
    {
        List<TbExam> list = tbExamService.selectTbExamList(tbExam);
        ExcelUtil<TbExam> util = new ExcelUtil<TbExam>(TbExam.class);
        util.exportExcel(response, list, "考试数据");
    }

    /**
     * 获取考试详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:exam:query')")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return success(tbExamService.selectTbExamByExamId(examId));
    }

    /**
     * 新增考试
     */
    @PreAuthorize("@ss.hasPermi('hezhi:exam:add')")
    @Log(title = "考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbExam tbExam)
    {
        return toAjax(tbExamService.insertTbExam(tbExam));
    }

    /**
     * 修改考试
     */
    @PreAuthorize("@ss.hasPermi('hezhi:exam:edit')")
    @Log(title = "考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbExam tbExam)
    {
        return toAjax(tbExamService.updateTbExam(tbExam));
    }

    /**
     * 删除考试
     */
    @PreAuthorize("@ss.hasPermi('hezhi:exam:remove')")
    @Log(title = "考试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(tbExamService.deleteTbExamByExamIds(examIds));
    }

    /**
     * 修改考试设置
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('hezhi:exam:edit')")
    @Log(title = "修改考试设置", businessType = BusinessType.UPDATE)
    @PostMapping("/updateExamConfig")
    public AjaxResult updateExamConfig(@RequestBody TbExam tbExam)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("examId", tbExam.getExamId());
        map.put("level", tbExam.getLevel());
        map.put("examType", '0');
        Map<String, Object> examSubjectNum = tbExamSubjectService.getExamSubjectNum(map);

        int singleSelectNum = Integer.parseInt(examSubjectNum.get("singleSelectNum").toString());
        int manySelectNum = Integer.parseInt(examSubjectNum.get("manySelectNum").toString());
        int judgeNum = Integer.parseInt(examSubjectNum.get("judgeNum").toString());
        int discussNum = Integer.parseInt(examSubjectNum.get("discussNum").toString());

        if (tbExam.getSingleSelectNum() > singleSelectNum) {
            return AjaxResult.error("单选只有【" + singleSelectNum + "】题，请先添加足够题目！！！");
        }
        if (tbExam.getManySelectNum() > manySelectNum) {
            return AjaxResult.error("多选只有【" + manySelectNum + "】题，请先添加足够题目！！！");
        }
        if (tbExam.getJudgeNum() > judgeNum) {
            return AjaxResult.error("判断只有【" + judgeNum + "】题，请先添加足够题目！！！");
        }
        if (tbExam.getDiscussNum() > discussNum) {
            return AjaxResult.error("论述只有【" + discussNum + "】题，请先添加足够题目！！！");
        }

        BigDecimal singleSelectScoreTotal = tbExam.getSingleSelectScore().multiply(new BigDecimal(tbExam.getSingleSelectNum()));
        BigDecimal manySelectScoreTotal = tbExam.getManySelectScore().multiply(new BigDecimal(tbExam.getManySelectNum()));
        BigDecimal judgeScoreTotal = tbExam.getJudgeScore().multiply(new BigDecimal(tbExam.getJudgeNum()));
        BigDecimal discussScoreTotal = tbExam.getDiscussScore().multiply(new BigDecimal(tbExam.getDiscussNum()));
        BigDecimal totalScore = singleSelectScoreTotal.add(manySelectScoreTotal).add(judgeScoreTotal).add(discussScoreTotal);
        tbExam.setTotalScore(totalScore);

        // 重新设置考试题
        tbExamSubjectFormalService.batchInsertTbExamSubjectFormalBy(tbExam);

        return toAjax(tbExamService.updateTbExam(tbExam));
    }

    /**
     * 所有考试列表
     * @return
     */
    @GetMapping("/getAllExamList")
    public AjaxResult getAllExamList() {
        TbExam exam = new TbExam();
        exam.setDelFlag(0);
        return AjaxResult.success(tbExamService.selectTbExamList(exam));
    }
}
