package com.sundot.airport.web.controller.exam;

import cn.hutool.core.collection.CollectionUtil;
import com.sundot.airport.attendance.dto.AttendancePostRecordImportVO;
import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.common.exception.ServiceException;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.exam.common.HttpResult;
import com.sundot.airport.exam.common.PageData;
import com.sundot.airport.exam.dto.QuestionImportDTO;
import com.sundot.airport.exam.logic.manage.model.params.*;
import com.sundot.airport.exam.logic.manage.model.result.QuesAddResult;
import com.sundot.airport.exam.logic.manage.model.result.QuesGetResult;
import com.sundot.airport.exam.logic.manage.model.result.QuesPageResult;
import com.sundot.airport.exam.logic.manage.service.QuesBizService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <b>功能名：</b>QuesController<br>
 * <b>说明：</b> 题目操作管理 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-18 liangxiao
 */
@RestController
@RequestMapping("/v1/cs/manage/ques")
public class QuesController extends BaseController {

    @Resource
    private QuesBizService quesBizService;

    /**
     * <b>方法名: </b> page <br>
     * <b>说明: </b> 题目分页列表 <br>
     *
     * @param param com.sundot.airport.model.params.QuesPageParam 题目分页列表请求参数
     * @return com.huifanedu.kernel.model.response.HttpResult<com.huifanedu.kernel.model.pager.PageData < com.sundot.airport.model.result.QuesPageResult>>
     * <b>修改履历: </b>
     * @author 2021-06-18 liangxiao
     */
    @PostMapping("/list")
    public HttpResult<PageData<QuesPageResult>> page(@RequestBody QuesPageParam param) {
        return quesBizService.pageList(param);
    }

    /**
     * <b>方法名: </b> get <br>
     * <b>说明: </b> 题目详情 <br>
     *
     * @param quId java.lang.String 题目业务id
     * @return com.huifanedu.kernel.model.response.HttpResult<com.sundot.airport.model.result.QuesGetResult>
     * <b>修改履历: </b>
     * @author 2021-06-18 liangxiao
     */
    @GetMapping("")
    public HttpResult<QuesGetResult> get(@RequestParam("quId") String quId) {
        return quesBizService.get(quId);
    }

    /**
     * <b>方法名: </b> add <br>
     * <b>说明: </b> 题目添加 <br>
     *
     * @param param com.sundot.airport.model.params.QuesAddParam 新增题目添加请求参数
     * @return com.huifanedu.kernel.model.response.HttpResult<com.sundot.airport.model.result.QuesAddResult>
     * <b>修改履历: </b>
     * @author 2021-06-18 liangxiao
     */
    @PostMapping("")
    public HttpResult<QuesAddResult> add(@Valid @RequestBody QuesAddParam param) {
        return quesBizService.add(param);
    }

    /**
     * <b>方法名: </b> delete <br>
     * <b>说明: </b> 题目删除 <br>
     *
     * @param param com.sundot.airport.model.params.QuesDelParam 题目删除请求参数
     * @return com.huifanedu.kernel.model.response.HttpResult<java.lang.Boolean>
     * <b>修改履历: </b>
     * @author 2021-06-18 liangxiao
     */
    @DeleteMapping("")
    public HttpResult<Boolean> del(@Valid @RequestBody QuesDelParam param) {
        return quesBizService.delete(param);
    }

    /**
     * <b>方法名: </b> update <br>
     * <b>说明: </b> 题目修改 <br>
     *
     * @param param com.sundot.airport.model.params.QuesUpdParam QuesUpdParam
     * @return com.huifanedu.kernel.model.response.HttpResult<java.lang.Boolean>
     * <b>修改履历: </b>
     * @author 2021-06-18 liangxiao
     */
    @PutMapping("")
    public HttpResult<Boolean> update(@Valid @RequestBody QuesUpdParam param) {
        return quesBizService.update(param);
    }

    /**
     * <b>方法名: </b> update <br>
     * <b>说明: </b> 题目分类转移 <br>
     *
     * @param param com.sundot.airport.model.params.QuesUpdParam 题目修改分类请求参数
     * @return com.huifanedu.kernel.model.response.HttpResult<java.lang.Boolean>
     * <b>修改履历: </b>
     * @author 2021-06-18 liangxiao
     */
    @PutMapping("/cat")
    public HttpResult<Boolean> batchUpdCat(@Valid @RequestBody QuesUpdCatParam param) {
        return quesBizService.batchCatUpdate(param);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QuestionImportDTO> util = new ExcelUtil<QuestionImportDTO>(QuestionImportDTO.class);
        util.importTemplateExcel(response, "题目操作");
    }

    @Log(title = "题目操作管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<QuestionImportDTO> util = new ExcelUtil<>(QuestionImportDTO.class);
        List<QuestionImportDTO> list = util.importExcel(file.getInputStream());
        if (CollectionUtil.isEmpty(list)) {
            throw new ServiceException("导入用户数据不能为空");
        }
        quesBizService.importData(list);
        return AjaxResult.success();
    }

}
