package com.zd.exam.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.exam.domain.ElPractise;
import com.zd.exam.domain.ElPractiseQu;
import com.zd.exam.service.IElPractiseQuService;
import com.zd.exam.service.IElPractiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 练习记录Controller
 *
 * @author liubo
 * @date 2021-12-18
 */
@RestController
@Api(tags = "【练习记录】")
@RequestMapping("/el_practise")
public class ElPractiseController extends BaseController {
    @Autowired
    private IElPractiseService elPractiseService;
    @Autowired
    private IElPractiseQuService practiseQuService;

    /**
     * 查询练习记录列表  "exam:el_practise:list"
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EL_PRACTISE+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询练习记录列表",notes="权限字符："+PerPrefix.EXAM_EL_PRACTISE+ PerFun.LIST)
    public TableDataInfo<ElPractise> list(ElPractise elPractise) {
        startPage();
        List<ElPractise> list = elPractiseService.selectElPractiseList(elPractise);
        return getDataTable(list);
    }


    /**
     * 获取练习记录详细信息  "exam:el_practise:query"
     */
    @ApiOperation(value = "获取练习记录详细信息",notes="权限字符："+PerPrefix.EXAM_EL_PRACTISE+ PerFun.QUERY)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_PRACTISE+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(elPractiseService.selectElPractiseById(id));
    }

    /**
     * 新增练习记录  "exam:el_practise:add"
     */
    @ApiOperation(value = "新增练习记录" ,notes="权限字符："+PerPrefix.EXAM_EL_PRACTISE+ PerFun.ADD)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_PRACTISE+ PerFun.ADD)
    @Log(title = "练习记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElPractise elPractise) {
        return AjaxResult.success(elPractiseService.insertElPractise(elPractise));
    }

    /**
     * 修改练习记录  "exam:el_practise:edit"
     */
    @ApiOperation(value = "修改练习记录" ,notes="权限字符："+PerPrefix.EXAM_EL_PRACTISE+ PerFun.EDIT)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_PRACTISE+ PerFun.EDIT)
    @Log(title = "练习记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElPractise elPractise) {
        return AjaxResult.success(elPractiseService.updateElPractise(elPractise));
    }

    /**
     * 删除练习记录  "exam:el_practise:remove"
     */
    @ApiOperation(value = "删除练习记录" ,notes="权限字符："+PerPrefix.EXAM_EL_PRACTISE+ PerFun.REMOVE)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_PRACTISE+ PerFun.REMOVE)
    @Log(title = "练习记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(elPractiseService.deleteElPractiseByIds(ids));
    }


    /**
     * 选择题目类型，开始练习
     */
    @ApiOperation(value = "选择题目类型，开始练习")
    @GetMapping(value = "/start/{ids}")
    public AjaxResult queryClassifyOption(@ApiParam("多个题目类型ID逗号分隔") @PathVariable("ids") String classifyIds) {
        Long practiseId = elPractiseService.createPractise(classifyIds);
        return AjaxResult.success(elPractiseService.selectElPractiseById(practiseId));
    }

    /**
     * 答题过程根据 paperId 和 quId 查询问题和选项
     */
    @ApiOperation(value = "练习 查询问题和选项 - 一题一查")
    @PostMapping(value = "/paperDetail")
    public AjaxResult queryPractiseQuDetail(@RequestBody ElPractiseQu practiseQu) {
        if (practiseQu.getPractiseId() == null || practiseQu.getQuId() == null) {
            return error("参数无效");
        }
        practiseQu = practiseQuService.selectElPractiseQuDetail(practiseQu);
        return AjaxResult.success(practiseQu);
    }

    /**
     * 练习填充答案
     */
    @ApiOperation(value = "练习保存用户答案 - 一题一交")
    @PostMapping(value = "/fillAnswer")
    public AjaxResult fillPractiseAnswer(@RequestBody ElPractiseQu practiseQu) {
        if (practiseQu.getPractiseId() == null || practiseQu.getQuId() == null) {
            return error("参数异常！");
        }
        elPractiseService.fillPractiseAnswer(practiseQu);
        return success();
    }

    @ApiOperation(value = "选择题目类型，结束练习")
    @PostMapping(value = "/handPractise")
    public AjaxResult handPractise(@RequestBody ElPractise practise) {
        if(practise.getId() == null){
            return error("ID不能为空！");
        }
        return AjaxResult.success(elPractiseService.handPractise(practise));
    }
}
