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.utils.poi.ExcelUtil;
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.ElQu;
import com.zd.exam.service.ElQuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 问题题目(ElQu)表控制层
 *
 * @author liubo
 * @since 2021-08-30 15:26:29
 */
@RestController
@RequestMapping("/qu")
@Api(tags = {"试题管理"})
public class ElQuController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ElQuService elQuService;

    /**
     * 主键查询 "exam:qu:query"
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_QU+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据主键查询试题明细" ,notes="权限字符："+PerPrefix.EXAM_QU+ PerFun.QUERY)
    public AjaxResult query(@PathVariable("id") Long id) {
        return AjaxResult.success(this.elQuService.queryById(id));
    }

    /**
     * 查询试题列表  "exam:qu:list"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_QU+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "分页查询试题列表" ,notes="权限字符："+PerPrefix.EXAM_QU+ PerFun.LIST)
    public TableDataInfo list(ElQu qu) {
        startPage();
        List<ElQu> list = elQuService.queryList(qu);
        return getDataTable(list);
    }

    /**
     * 添加题目  "exam:qu:add"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_QU+ PerFun.ADD)
    @Log(title = "问题题目", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "添加题库" ,notes="权限字符："+PerPrefix.EXAM_QU+ PerFun.ADD)
    public AjaxResult add(@RequestBody @Valid ElQu qu) {
        return toAjax(elQuService.insert(qu));
    }

    /**
     * 修改题库  "exam:qu:edit"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_QU+ PerFun.EDIT)
    @Log(title = "问题题目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改题库" ,notes="权限字符："+PerPrefix.EXAM_QU+ PerFun.EDIT)
    public AjaxResult edit(@RequestBody @Valid ElQu qu) {
        return toAjax(elQuService.update(qu));
    }

    /**
     * 批量删除  "exam:qu:remove"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_QU+ PerFun.REMOVE)
    @Log(title = "问题题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除" ,notes="权限字符："+PerPrefix.EXAM_QU+ PerFun.REMOVE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        //根据ID删除
        return toAjax(elQuService.deleteByIds(Arrays.asList(ids)));
    }

    /**
     * 导出问题题目列表  "exam:qu:export"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_QU+ PerFun.EXPORT)
    @Log(title = "问题题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElQu elQu) throws IOException {
        List<ElQu> list = elQuService.selectElQuList(elQu);
        ExcelUtil<ElQu> util = new ExcelUtil<ElQu>(ElQu.class);
        util.exportExcel(response, list, "问题题目数据");
    }

    /**
     * 下载导入模板  "exam:qu:export"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_QU+ PerFun.EXPORT)
    @Log(title = "问题题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export/model")
    public void exportModel(HttpServletResponse response, ElQu elQu) throws IOException {
        List<ElQu> list = new ArrayList<>();
        ExcelUtil<ElQu> util = new ExcelUtil<ElQu>(ElQu.class);
        util.exportExcel(response, list, "问题题目数据");
    }

    /**
     * 导出  "exam:qu:import"
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "问题题目", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = PerPrefix.EXAM_QU+ PerFun.EXPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ElQu> util = new ExcelUtil<ElQu>(ElQu.class);
        List<ElQu> quList = util.importExcel(file.getInputStream());
        String message = elQuService.importData(quList);
        return AjaxResult.success(message);
    }

}
