package com.ruoyi.project.training.elearn.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.job.service.JobService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.elearn.domain.TElPaperQu;
import com.ruoyi.project.training.elearn.domain.TElPaperQuAnswer;
import com.ruoyi.project.training.elearn.domain.TElQu;
import com.ruoyi.project.training.elearn.job.BreakExamJob;
import com.ruoyi.project.training.elearn.mapper.TElPaperQuAnswerMapper;
import com.ruoyi.project.training.elearn.mapper.TElPaperQuMapper;
import com.ruoyi.project.training.elearn.mapper.TElQuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.elearn.domain.TElPaper;
import com.ruoyi.project.training.elearn.service.ITElPaperService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 在线考试Controller
 *
 * @author ssy
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/elearn/paper")
public class TElPaperController extends BaseController
{
    @Autowired
    private ITElPaperService tElPaperService;
    @Resource
    private TElPaperQuMapper tElPaperQuMapper;
    @Resource
    private TElPaperQuAnswerMapper tElPaperQuAnswerMapper;
    @Resource
    private TElQuMapper tElQuMapper;
    @Autowired
    ISysUserService sysUserService;
    /**
     * 查询在线考试列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TElPaper tElPaper)
    {
        if (tElPaper.getUserId() == null && tElPaper.getStaffId()!=null) {
            SysUser user = this.sysUserService.selectUserByStaffId(tElPaper.getStaffId()+"");
            tElPaper.setUserId(user.getUserId());
            tElPaper.setStaffId(null);
        }
        startPage();
        List<TElPaper> list = tElPaperService.selectTElPaperList(tElPaper);
        return getDataTable(list);
    }

    /**
     * 导出在线考试列表
     */
    @Log(title = "在线考试", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TElPaper tElPaper)
    {
        List<TElPaper> list = tElPaperService.selectTElPaperList(tElPaper);
        ExcelUtil<TElPaper> util = new ExcelUtil<TElPaper>(TElPaper.class);
        return util.exportExcel(list, "paper");
    }

    /**
     * 获取在线考试详细信息
     */
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId)
    {
        return AjaxResult.success(tElPaperService.selectTElPaperById(paperId));
    }

    /**
     * 新增在线考试
     */
    @Log(title = "在线考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElPaper tElPaper)
    {
        return toAjax(tElPaperService.insertTElPaper(tElPaper));
    }

    /**
     * 创建考试
     */
    @Log(title = "创建考试", businessType = BusinessType.INSERT)
    @PostMapping(value = "/createPaper")
    public AjaxResult createPaper(@RequestBody TElPaper tElPaper)
    {
        getUserId();
        return AjaxResult.success(tElPaperService.createPaper(tElPaper,getUserId()));
    }

    /**
     * 试卷详情
     */
    @GetMapping(value = "/paperDetail")
    public AjaxResult paperDetail(TElPaper tElPaper)
    {
        TElPaper paper = tElPaperService.selectTElPaperById(tElPaper.getPaperId());
        //查询题目列表
        TElPaperQu quey = new TElPaperQu();
        quey.setPaperId(tElPaper.getPaperId());
        List<TElPaperQu> list = tElPaperQuMapper.selectTElPaperQuList(quey);
        logger.info(JSON.toJSONString(list));
        List<TElPaperQu> radioList = new ArrayList<>();
        List<TElPaperQu> multiList = new ArrayList<>();
        List<TElPaperQu> judgeList = new ArrayList<>();
        for(TElPaperQu item: list){
            if("1".equals(item.getQuType().toString())){
                radioList.add(item);
            }
            if("2".equals(item.getQuType().toString())){
                multiList.add(item);
            }
            if("3".equals(item.getQuType().toString())){
                judgeList.add(item);
            }
        }
        paper.setRadioList(radioList);
        paper.setMultiList(multiList);
        paper.setJudgeList(judgeList);

        // 结束时间
        Calendar cl = Calendar.getInstance();
        cl.setTime(paper.getCreatedate());
        cl.add(Calendar.MINUTE, paper.getTotalTime().intValue());

        long seconds = (cl.getTimeInMillis() - System.currentTimeMillis()) / 1000;
        paper.setLeftSeconds(seconds);

        return AjaxResult.success(paper);
    }

    /**
     * 试卷题目详情
     */
    @GetMapping(value = "/quDetail")
    public AjaxResult quDetail(TElPaperQu paperQu)
    {
        TElPaperQu tElPaperQu = tElPaperQuMapper.selectTElPaperQuByPaperIdAndQuId(paperQu);

        // 问题
        TElQu qu = tElQuMapper.selectTElQuById(paperQu.getQuId());
        tElPaperQu.setContent(qu.getContent());
        tElPaperQu.setImage(qu.getImage());
        //答案
        TElPaperQuAnswer param = new TElPaperQuAnswer();
        param.setPaperId(paperQu.getPaperId());
        param.setQuId(qu.getQuId());
        List<TElPaperQuAnswer> answerList = tElPaperQuAnswerMapper.selectTElPaperQuAnswerListByExam(param);
        tElPaperQu.setAnswerList(answerList);

        return AjaxResult.success(tElPaperQu);
    }

    /**
     * 交卷
     */
    @Log(title = "交卷", businessType = BusinessType.INSERT)
    @GetMapping(value = "/handExam")
    public AjaxResult handExam(TElPaperQu paperQu)
    {
        tElPaperService.handExam(paperQu);
        return AjaxResult.success();
    }

    /**
     * 试卷详情
     */
    @GetMapping(value = "/paperResult")
    public AjaxResult paperResult(TElPaperQu paperQu)
    {
        return AjaxResult.success(tElPaperService.paperResult(paperQu));
    }

    /**
     * 填充答案
     */
    @PostMapping(value = "/fillAnswer")
    public AjaxResult fillAnswer(@RequestBody TElPaperQu paperQu)
    {
        logger.info("填充答案",paperQu);
        logger.info(JSON.toJSONString(paperQu));
        tElPaperService.fillAnswer(paperQu);
        return AjaxResult.success();
    }

    /**
     * 修改在线考试
     */
    @Log(title = "在线考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElPaper tElPaper)
    {
        return toAjax(tElPaperService.updateTElPaper(tElPaper));
    }

    /**
     * 删除在线考试
     */
    @Log(title = "在线考试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds)
    {
        return toAjax(tElPaperService.deleteTElPaperByIds(paperIds));
    }


}
