package com.zhongzheng.controller.bank;

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

import cn.hutool.core.lang.Validator;
import cn.hutool.http.HttpStatus;
import com.github.pagehelper.PageInfo;
import com.zhongzheng.common.core.domain.entity.SysUser;
import com.zhongzheng.common.core.domain.model.LoginUser;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.framework.web.service.TokenService;
import com.zhongzheng.modules.bank.bo.QuestionAddBo;
import com.zhongzheng.modules.bank.bo.QuestionBusinessQueryBo;
import com.zhongzheng.modules.bank.bo.QuestionEditBo;
import com.zhongzheng.modules.bank.bo.QuestionQueryBo;
import com.zhongzheng.modules.bank.domain.Question;
import com.zhongzheng.modules.bank.domain.QuestionBusiness;
import com.zhongzheng.modules.bank.service.IQuestionBusinessService;
import com.zhongzheng.modules.bank.service.IQuestionService;
import com.zhongzheng.modules.bank.vo.QuestionBusinessVo;
import com.zhongzheng.modules.bank.vo.QuestionImport;
import com.zhongzheng.modules.bank.vo.QuestionVo;
import com.zhongzheng.modules.goods.vo.GoodsVo;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 题库题目Controller
 *
 * @author hjl
 * @date 2021-10-21
 */
@Api(value = "题库题目控制器", tags = {"题库题目管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/bank/question")
public class QuestionController extends BaseController {

    private final IQuestionService iQuestionService;

    private final IQuestionBusinessService iQuestionBusinessService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询题库题目列表
     */
    @ApiOperation("查询题库题目列表")
    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public TableDataInfo<QuestionVo> list(QuestionQueryBo bo) {
        startPage();
        List<QuestionVo> list = iQuestionService.selectList(bo);
        return getDataTable(list);
    }

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

    /**
     * 获取题库题目详细信息
     */
    @ApiOperation("获取题库题目详细信息")
    @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping("/{questionId}")
    public AjaxResult<QuestionVo> getInfo(@PathVariable("questionId" ) Long questionId) {
        return AjaxResult.success(iQuestionService.queryById(questionId));
    }

    /**
     * 新增题库题目
     */
    @ApiOperation("新增题库题目")
    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "题库题目", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Long> add(@RequestBody QuestionAddBo bo) {
        return AjaxResult.success(iQuestionService.insertByAddBo(bo));
    }

    /**
     * 修改题库题目
     */
    @ApiOperation("修改题库题目")
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "题库题目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody QuestionEditBo bo) {
        return toAjax(iQuestionService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除题库题目
     */
    /*@ApiOperation("删除题库题目")
    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "题库题目" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] questionIds) {
        return toAjax(iQuestionService.deleteWithValidByIds(Arrays.asList(questionIds), true) ? 1 : 0);
    }*/

    /**
     * 查询题目业务层次关系列表
     */
    @ApiOperation("查询题目业务层次关系列表")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/business/list")
    public TableDataInfo<QuestionBusiness> businessList(QuestionBusinessQueryBo bo) {
        startPage();
        bo.setType(QuestionBusiness.TYPE_QUESTION);
        List<QuestionBusiness> list = iQuestionBusinessService.getListById(bo);
        return getDataTable(list);
    }

    @Log(title = "题目管理", businessType = BusinessType.IMPORT)
    @ApiOperation("导入题目模板")
    @PreAuthorize("@ss.hasPermi('system:question:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QuestionImport> util = new ExcelUtil<QuestionImport>(QuestionImport.class);
        List<QuestionImport> questionList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = iQuestionService.importQuestion(questionList, updateSupport, operName);
        if(Validator.isNotEmpty(message)){
            return AjaxResult.error(message);
        }else{
            return AjaxResult.success(message);
        }

    }
}
