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.TbSubjectWarehouseClass;
import com.ruoyi.hezhi.service.ITbExamSubjectService;
import com.ruoyi.hezhi.service.ITbExamSubjectSimulateService;
import com.ruoyi.hezhi.service.ITbSubjectWarehouseClassService;
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-06
 */
@RestController
@RequestMapping("/hezhi/subjectWarehouseClass")
public class TbSubjectWarehouseClassController extends BaseController
{
    @Autowired
    private ITbSubjectWarehouseClassService tbSubjectWarehouseClassService;
    @Autowired
    private ITbExamSubjectService tbExamSubjectService;
    @Autowired
    private ITbExamSubjectSimulateService tbExamSubjectSimulateService;

    /**
     * 查询题库分类列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        startPage();
        List<TbSubjectWarehouseClass> list = tbSubjectWarehouseClassService.selectTbSubjectWarehouseClassList(tbSubjectWarehouseClass);
        return getDataTable(list);
    }

    /**
     * 导出题库分类列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:export')")
    @Log(title = "题库分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        List<TbSubjectWarehouseClass> list = tbSubjectWarehouseClassService.selectTbSubjectWarehouseClassList(tbSubjectWarehouseClass);
        ExcelUtil<TbSubjectWarehouseClass> util = new ExcelUtil<TbSubjectWarehouseClass>(TbSubjectWarehouseClass.class);
        util.exportExcel(response, list, "题库分类数据");
    }

    /**
     * 获取题库分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:query')")
    @GetMapping(value = "/{subjectWarehouseClassId}")
    public AjaxResult getInfo(@PathVariable("subjectWarehouseClassId") Long subjectWarehouseClassId)
    {
        return success(tbSubjectWarehouseClassService.selectTbSubjectWarehouseClassBySubjectWarehouseClassId(subjectWarehouseClassId));
    }

    /**
     * 新增题库分类
     */
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:add')")
    @Log(title = "题库分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        return toAjax(tbSubjectWarehouseClassService.insertTbSubjectWarehouseClass(tbSubjectWarehouseClass));
    }

    /**
     * 修改题库分类
     */
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:edit')")
    @Log(title = "题库分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        return toAjax(tbSubjectWarehouseClassService.updateTbSubjectWarehouseClass(tbSubjectWarehouseClass));
    }

    /**
     * 删除题库分类
     */
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:remove')")
    @Log(title = "题库分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subjectWarehouseClassIds}")
    public AjaxResult remove(@PathVariable Long[] subjectWarehouseClassIds)
    {
        return toAjax(tbSubjectWarehouseClassService.deleteTbSubjectWarehouseClassBySubjectWarehouseClassIds(subjectWarehouseClassIds));
    }

    /**
     * 查询题库分类列表
     */
    @GetMapping("/getSubjectWarehouseClassList")
    public AjaxResult getSubjectWarehouseClassList(TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        tbSubjectWarehouseClass.setDelFlag(0);
        return AjaxResult.success(tbSubjectWarehouseClassService.selectTbSubjectWarehouseClassList(tbSubjectWarehouseClass));
    }

    /**
     * 修改模拟考试状态
     */
    @PutMapping("/setSimulationStatus")
    public AjaxResult setSimulationStatus(@RequestBody TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        return toAjax(tbSubjectWarehouseClassService.updateTbSubjectWarehouseClass(tbSubjectWarehouseClass));
    }

    /**
     * 修改模拟考试设置
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('hezhi:subjectWarehouseClass:edit')")
    @Log(title = "修改模拟考试设置", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSimulateExam")
    public AjaxResult updateSimulateExam(@RequestBody TbSubjectWarehouseClass tbSubjectWarehouseClass)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("subjectWarehouseClassId", tbSubjectWarehouseClass.getSubjectWarehouseClassId());
        map.put("examType", '1');
        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 (tbSubjectWarehouseClass.getSingleSelectNum() > singleSelectNum) {
            return AjaxResult.error(tbSubjectWarehouseClass.getName() + "单选只有【" + singleSelectNum + "】题，请先添加足够题目！！！");
        }
        if (tbSubjectWarehouseClass.getManySelectNum() > manySelectNum) {
            return AjaxResult.error(tbSubjectWarehouseClass.getName() + "多选只有【" + manySelectNum + "】题，请先添加足够题目！！！");
        }
        if (tbSubjectWarehouseClass.getJudgeNum() > judgeNum) {
            return AjaxResult.error(tbSubjectWarehouseClass.getName() + "判断只有【" + judgeNum + "】题，请先添加足够题目！！！");
        }
        if (tbSubjectWarehouseClass.getDiscussNum() > discussNum) {
            return AjaxResult.error(tbSubjectWarehouseClass.getName() + "论述只有【" + discussNum + "】题，请先添加足够题目！！！");
        }

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

        // 重新设置模拟考试题
        tbExamSubjectSimulateService.batchInsertTbExamSubjectSimulateBy(tbSubjectWarehouseClass);

        return toAjax(tbSubjectWarehouseClassService.updateTbSubjectWarehouseClass(tbSubjectWarehouseClass));
    }

    /**
     * 获取模拟考试列表
     * @return 结果
     */
    @GetMapping("/getAllSubjectWarehouseCalssList")
    public AjaxResult getAllSubjectWarehouseCalssList(){
        List<TbSubjectWarehouseClass> allSubjectWarehouseCalssList = tbSubjectWarehouseClassService.getAllSubjectWarehouseCalssList();
        return AjaxResult.success(allSubjectWarehouseCalssList);
    }
}
