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

import java.util.List;
import com.ruoyi.project.training.elearn.mapper.TElUserBookMapper;
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.TElUserBook;
import com.ruoyi.project.training.elearn.service.ITElUserBookService;
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-31
 */
@RestController
@RequestMapping("/elearn/userBook")
public class TElUserBookController extends BaseController
{
    @Autowired
    private ITElUserBookService tElUserBookService;
    @Resource
    private TElUserBookMapper tElUserBookMapper;
    /**
     * 查询错题本列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TElUserBook tElUserBook)
    {
        tElUserBook.setUserId(getUserId());
        startPage();
        List<TElUserBook> list = tElUserBookService.selectTElUserBookList(tElUserBook);
        return getDataTable(list);
    }

    /**
     * 导出错题本列表
     */
    @Log(title = "错题本", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TElUserBook tElUserBook)
    {
        List<TElUserBook> list = tElUserBookService.selectTElUserBookList(tElUserBook);
        ExcelUtil<TElUserBook> util = new ExcelUtil<TElUserBook>(TElUserBook.class);
        return util.exportExcel(list, "userBook");
    }

    /**
     * 获取错题本详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tElUserBookService.selectTElUserBookById(id));
    }

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

        if (book.getQuId() == null) { //答题第一题

        }else {
          List<TElUserBook> list =  tElUserBookMapper.selectTElUserBookList(book);
            if (list.size() >0) {
                book.setUpdatedate(list.get(0).getUpdatedate());
            }
        }
        //继续答题
        TElUserBook userbook =  tElUserBookMapper.selectTElUserBookByLast(book);
        if (userbook == null) {
            return  AjaxResult.error("已经是最后一题");
        }
        return AjaxResult.success(userbook.getQuId());
    }

    /**
     * 移除错题集
     */
    @PostMapping(value = "/removBookQu")
    public AjaxResult removBookQu(@RequestBody TElUserBook book)
    {
        book.setUserId(getUserId());
        //移除错题集
        return AjaxResult.success(tElUserBookMapper.deleteTElUserBookByQuId(book));
    }


    /**
     * 新增错题本
     */
    @Log(title = "错题本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElUserBook tElUserBook)
    {
        return toAjax(tElUserBookService.insertTElUserBook(tElUserBook));
    }

    /**
     * 修改错题本
     */
    @Log(title = "错题本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElUserBook tElUserBook)
    {
        return toAjax(tElUserBookService.updateTElUserBook(tElUserBook));
    }

    /**
     * 删除错题本
     */
    @Log(title = "错题本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElUserBookService.deleteTElUserBookByIds(ids));
    }
}
