package com.zhongzheng.controller.bank;

import java.util.List;
import java.util.Arrays;

import com.zhongzheng.modules.bank.bo.*;
import com.zhongzheng.modules.bank.domain.QuestionBusiness;
import com.zhongzheng.modules.bank.service.IExamQuestionService;
import com.zhongzheng.modules.bank.service.IExamService;
import com.zhongzheng.modules.bank.service.IQuestionBusinessService;
import com.zhongzheng.modules.bank.vo.ExamQuestionVo;
import com.zhongzheng.modules.bank.vo.ExamVo;
import lombok.RequiredArgsConstructor;
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.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 试卷Controller
 *
 * @author hjl
 * @date 2021-10-22
 */
@Api(value = "试卷控制器", tags = {"试卷管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/bank/exam")
public class ExamController extends BaseController {

    private final IExamService iExamService;

    private final IQuestionBusinessService iQuestionBusinessService;

    private final IExamQuestionService iExamQuestionService;

    /**
     * 查询试卷列表
     */
    @ApiOperation("查询试卷列表")
    @PreAuthorize("@ss.hasPermi('system:exam:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamVo> list(ExamQueryBo bo) {
        startPage();
        List<ExamVo> list = iExamService.getList(bo);
        return getDataTable(list);
    }

    /**
     * 导出试卷列表
     */
   /* @ApiOperation("导出试卷列表")
    @PreAuthorize("@ss.hasPermi('system:exam:export')")
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ExamVo> export(ExamQueryBo bo) {
        List<ExamVo> list = iExamService.queryList(bo);
        ExcelUtil<ExamVo> util = new ExcelUtil<ExamVo>(ExamVo.class);
        return util.exportExcel(list, "试卷");
    }*/

    /**
     * 获取试卷详细信息
     */
    @ApiOperation("获取试卷详细信息")
    @PreAuthorize("@ss.hasPermi('system:exam:query')")
    @GetMapping("/{examId}")
    public AjaxResult<ExamVo> getInfo(@PathVariable("examId" ) Long examId) {
        return AjaxResult.success(iExamService.queryById(examId));
    }

    /**
     * 新增试卷
     */
    @ApiOperation("新增试卷")
    @PreAuthorize("@ss.hasPermi('system:exam:add')")
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<ExamVo> add(@RequestBody ExamAddBo bo) {
        return AjaxResult.success(iExamService.insertByAddBo(bo));
    }

    /**
     * 修改试卷
     */
    @ApiOperation("修改试卷")
    @PreAuthorize("@ss.hasPermi('system:exam:edit')")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody ExamEditBo bo) {
        return toAjax(iExamService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除试卷
     */
   /* @ApiOperation("删除试卷")
    @PreAuthorize("@ss.hasPermi('system:exam:remove')")
    @Log(title = "试卷" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{examIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] examIds) {
        return toAjax(iExamService.deleteWithValidByIds(Arrays.asList(examIds), true) ? 1 : 0);
    }*/
    /**
     * 查询题目业务层次关系列表
     */
    @ApiOperation("查询业务层次关系列表")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/business/list")
    public TableDataInfo<QuestionBusiness> businessList(QuestionBusinessQueryBo bo) {
        startPage();
        bo.setType(QuestionBusiness.TYPE_EXAM);
        List<QuestionBusiness> list = iQuestionBusinessService.getListById(bo);
        return getDataTable(list);
    }

    /**
     * 查询题目业务层次关系列表
     */
    @ApiOperation("查询题目列表")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/question/list")
    public AjaxResult<List<ExamQuestionVo>> questionList(ExamQuestionQueryBo bo) {
        List<ExamQuestionVo> list = iExamQuestionService.getList(bo);
        return AjaxResult.success(list);
    }
}
