package com.jojubanking.boot.module.system.controller.admin.wqda;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.wqda.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.wqda.WqdaDO;
import com.jojubanking.boot.module.system.convert.wqda.WqdaConvert;
import com.jojubanking.boot.module.system.service.wqda.WqdaService;

@Api(tags = "管理后台 - 问卷调查答案")
@RestController
@RequestMapping("/system/wqda")
@Validated
public class WqdaController {

    @Resource
    private WqdaService wqdaService;

    @PostMapping("/create")
    @ApiOperation("创建问卷调查答案")
    @PreAuthorize("@ss.hasPermission('system:wqda:create')")
    public CommonResult<Integer> createWqda(@Valid @RequestBody WqdaCreateReqVO createReqVO) {
        return success(wqdaService.createWqda(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新问卷调查答案")
    @PreAuthorize("@ss.hasPermission('system:wqda:update')")
    public CommonResult<Boolean> updateWqda(@Valid @RequestBody WqdaUpdateReqVO updateReqVO) {
        wqdaService.updateWqda(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除问卷调查答案")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:wqda:delete')")
    public CommonResult<Boolean> deleteWqda(@RequestParam("id") Integer id) {
        wqdaService.deleteWqda(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得问卷调查答案")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:wqda:query')")
    public CommonResult<WqdaRespVO> getWqda(@RequestParam("id") Integer id) {
        WqdaDO wqda = wqdaService.getWqda(id);
        return success(WqdaConvert.INSTANCE.convert(wqda));
    }

    @GetMapping("/list")
    @ApiOperation("获得问卷调查答案列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:wqda:query')")
    public CommonResult<List<WqdaRespVO>> getWqdaList(@RequestParam("ids") Collection<Integer> ids) {
        List<WqdaDO> list = wqdaService.getWqdaList(ids);
        return success(WqdaConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得问卷调查答案分页")
    @PreAuthorize("@ss.hasPermission('system:wqda:query')")
    public CommonResult<PageResult<WqdaRespVO>> getWqdaPage(@Valid WqdaPageReqVO pageVO) {
        PageResult<WqdaDO> pageResult = wqdaService.getWqdaPage(pageVO);
        return success(WqdaConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出问卷调查答案 Excel")
    @PreAuthorize("@ss.hasPermission('system:wqda:export')")
    @OperateLog(type = EXPORT)
    public void exportWqdaExcel(@Valid WqdaExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WqdaDO> list = wqdaService.getWqdaList(exportReqVO);
        // 导出 Excel
        List<WqdaExcelVO> datas = WqdaConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问卷调查答案.xls", "数据", WqdaExcelVO.class, datas);
    }

    @GetMapping("/sum")
    @ApiOperation("导出问卷调查答案 Excel")
    @OperateLog
    public CommonResult<String> GetWqdaSum(WqSumReqVo wqSumReqVo) {

        String res = wqdaService.getWqdaSumList(wqSumReqVo.getQuestionType(), wqSumReqVo.getCreateTime()[0], wqSumReqVo.getCreateTime()[1]);
        return success(res);
        //return success(WqdaConvert.INSTANCE.convertList(list));
    }

}
