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

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.wq.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.wq.WqDO;
import com.jojubanking.boot.module.system.convert.wq.WqConvert;
import com.jojubanking.boot.module.system.service.wq.WqService;

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

    @Resource
    private WqService wqService;

    @PostMapping("/create")
    @ApiOperation("创建问卷调查")
    @PreAuthorize("@ss.hasPermission('system:wq:create')")
    public CommonResult<Integer> createWq(@Valid @RequestBody WqCreateReqVO createReqVO) {
        return success(wqService.createWq(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新问卷调查")
    @PreAuthorize("@ss.hasPermission('system:wq:update')")
    public CommonResult<Boolean> updateWq(@Valid @RequestBody WqUpdateReqVO updateReqVO) {
        wqService.updateWq(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得问卷调查")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:wq:query')")
    public CommonResult<WqRespVO> getWq(@RequestParam("id") Integer id) {
        WqDO wq = wqService.getWq(id);
        return success(WqConvert.INSTANCE.convert(wq));
    }

    @GetMapping("/list")
    @ApiOperation("获得问卷调查列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:wq:query')")
    public CommonResult<List<WqRespVO>> getWqList(@RequestParam("ids") Collection<Integer> ids) {
        List<WqDO> list = wqService.getWqList(ids);
        return success(WqConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得问卷调查分页")
    @PreAuthorize("@ss.hasPermission('system:wq:query')")
    public CommonResult<PageResult<WqRespVO>> getWqPage(@Valid WqPageReqVO pageVO) {
        PageResult<WqDO> pageResult = wqService.getWqPage(pageVO);
        return success(WqConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出问卷调查 Excel")
    @PreAuthorize("@ss.hasPermission('system:wq:export')")
    @OperateLog(type = EXPORT)
    public void exportWqExcel(@Valid WqExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WqDO> list = wqService.getWqList(exportReqVO);
        // 导出 Excel
        List<WqExcelVO> datas = WqConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问卷调查.xls", "数据", WqExcelVO.class, datas);
    }

}
