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

import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.project.training.elearn.domain.TElQu;
import com.ruoyi.project.training.elearn.domain.TElQuRepo;
import com.ruoyi.project.training.elearn.domain.TElUserBook;
import com.ruoyi.project.training.elearn.mapper.TElQuMapper;
import com.ruoyi.project.training.elearn.mapper.TElQuRepoMapper;
import com.ruoyi.project.training.elearn.mapper.TElUserBookMapper;
import com.ruoyi.project.training.elearn.mapper.TElUserQuMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.TElUserQu;
import com.ruoyi.project.training.elearn.service.ITElUserQuService;
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-06-04
 */
@RestController
@RequestMapping("/elearn/userQu")
public class TElUserQuController extends BaseController
{
    @Autowired
    private ITElUserQuService tElUserQuService;
    @Resource
    private TElUserQuMapper tElUserQuMapper;
    @Resource
    private TElQuRepoMapper tElQuRepoMapper;
    @Resource
    private TElUserBookMapper tElUserBookMapper;
    @Resource
    private TElQuMapper tElQuMapper;
    /**
     * 查询答题记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TElUserQu tElUserQu)
    {
        startPage();
        List<TElUserQu> list = tElUserQuService.selectTElUserQuList(tElUserQu);
        return getDataTable(list);
    }

    /**
     * 导出答题记录列表
     */
    @Log(title = "答题记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TElUserQu tElUserQu)
    {
        List<TElUserQu> list = tElUserQuService.selectTElUserQuList(tElUserQu);
        ExcelUtil<TElUserQu> util = new ExcelUtil<TElUserQu>(TElUserQu.class);
        return util.exportExcel(list, "userQu");
    }

    /**
     * 获取答题记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tElUserQuService.selectTElUserQuById(id));
    }

    /**
     * 下一题
     */
    @PostMapping(value = "/nextQu")
    public AjaxResult nextQu(@RequestBody TElUserQu tElUserQu)
    {
        tElUserQu.setUserId(getUserId());

        if (tElUserQu.getQuId() == null) { //答题第一题
            TElUserQu last = tElUserQuMapper.selectTElUserQuLast(tElUserQu);
            if (last == null) {
                //首次答题
                TElQuRepo quRepo =  tElQuRepoMapper.selectTElQuRepoLast(tElUserQu);
                return   AjaxResult.success(quRepo.getQuId());
            }else {
                //继续上次答题
              return   AjaxResult.success(last.getQuId());
            }
        }
        //继续答题
        TElQuRepo quRepo =  tElQuRepoMapper.selectTElQuRepoLast(tElUserQu);
        if (quRepo == null) {
            return  AjaxResult.error("已经是最后一题");
        }
        return AjaxResult.success(quRepo.getQuId());
    }


    /**
     * 新增答题记录
     */
    @Log(title = "答题记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElUserQu tElUserQu)
    {
        long userId = getUserId();
        tElUserQu.setUserId(userId);
        if (0 == tElUserQu.getIsRight()) { //加入错题
            TElUserBook quey = new TElUserBook();
            quey.setUserId(userId);
            quey.setQuId(tElUserQu.getQuId());
            List<TElUserBook> books = tElUserBookMapper.selectTElUserBookList(quey);
            TElQu qu = tElQuMapper.selectTElQuById(tElUserQu.getQuId());
            if (books.size() == 0) {
                TElUserBook book = new TElUserBook();
                book.setUserId(userId);
                book.setTitle(qu.getContent());
                book.setQuId(tElUserQu.getQuId());
                book.setWrongCount(1l);
                book.setUpdatedate(new Date());
                tElUserBookMapper.insertTElUserBook(book);
            }else {
                TElUserBook  book =books.get(0);
                book.setUpdatedate(new Date());
                book.setWrongCount(book.getWrongCount()+1);
                tElUserBookMapper.updateTElUserBook(book);
            }
        }else {
            TElUserBook book = new TElUserBook();
            book.setUserId(userId);
            book.setQuId(tElUserQu.getQuId());
            tElUserBookMapper.deleteTElUserBookByQuId(book);
        }
        // 确保分页状态被清除
        PageHelper.clearPage();
        return toAjax(tElUserQuService.insertTElUserQu(tElUserQu));
    }

    /**
     * 修改答题记录
     */
    @Log(title = "答题记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElUserQu tElUserQu)
    {
        return toAjax(tElUserQuService.updateTElUserQu(tElUserQu));
    }

    /**
     * 删除答题记录
     */
    @Log(title = "答题记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElUserQuService.deleteTElUserQuByIds(ids));
    }
}
