package com.zhongzheng.controller.bank;

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

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zhongzheng.modules.bank.bo.QuestionBankAddBo;
import com.zhongzheng.modules.bank.bo.QuestionBankEditBo;
import com.zhongzheng.modules.bank.bo.QuestionBankQueryBo;
import com.zhongzheng.modules.bank.service.IQuestionBankService;
import com.zhongzheng.modules.bank.vo.QuestionBankVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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-05-20
 */
@Api(value = "题库控制器", tags = {"题库管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/bank")
public class QuestionBankController extends BaseController {

    private final IQuestionBankService iQuestionBankService;

    /**
     * 查询题库列表
     */
    @ApiOperation("查询题库列表")
    @PreAuthorize("@ss.hasPermi('bank:list')")
    @GetMapping("/list")
    public TableDataInfo<QuestionBankVo> list(QuestionBankQueryBo bo) {
        startPage();
        List<QuestionBankVo> list = iQuestionBankService.selectBankList(bo);
        return getDataTable(list);
    }

    /**
     * 导出题库列表
     */
    /*@ApiOperation("导出题库列表")
    @PreAuthorize("@ss.hasPermi('system:bank:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<QuestionBankVo> export(QuestionBankQueryBo bo) {
        List<QuestionBankVo> list = iQuestionBankService.queryList(bo);
        ExcelUtil<QuestionBankVo> util = new ExcelUtil<QuestionBankVo>(QuestionBankVo.class);
        return util.exportExcel(list, "题库");
    }*/

    /**
     * 获取题库详细信息
     */
    @ApiOperation("获取题库详细信息")
    @PreAuthorize("@ss.hasPermi('bank:query')")
    @GetMapping("/{bankId}")
    public AjaxResult<QuestionBankVo> getInfo(@PathVariable("bankId" ) Long bankId) {
        return AjaxResult.success(iQuestionBankService.queryById(bankId));
    }

    /**
     * 新增题库
     */
    @ApiOperation("新增题库")
    @ApiOperationSupport(ignoreParameters = {"updateTime","createTime"})
    @PreAuthorize("@ss.hasPermi('bank:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody QuestionBankAddBo bo) {
        Long id = iQuestionBankService.insertByAddBo(bo);
        String content = "新增题库:"+bo.getBankName();
        return AjaxResult.success_log(id,content);
    }

    /**
     * 修改题库
     */
    @ApiOperation("修改题库")
    @ApiOperationSupport(ignoreParameters = {"updateTime","createTime"})
    @PreAuthorize("@ss.hasPermi('bank:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@Validated  @RequestBody QuestionBankEditBo bo) {
        return toAjax(iQuestionBankService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除题库
     */
    @ApiOperation("删除题库")
    @PreAuthorize("@ss.hasPermi('bank:remove')")
    @Log(title = "题库" , businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult<Void> remove(@RequestBody Long[] bankIds) {
        return toAjax(iQuestionBankService.deleteWithValidByIds(Arrays.asList(bankIds), true) ? 1 : 0);
    }
}
