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

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.wqwt.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.wqwt.WqwtDO;
import com.jojubanking.boot.module.system.convert.wqwt.WqwtConvert;
import com.jojubanking.boot.module.system.service.wqwt.WqwtService;

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

    @Resource
    private WqwtService wqwtService;

    @PostMapping("/create")
    @ApiOperation("创建问卷调查问题")
    @PreAuthorize("@ss.hasPermission('system:wqwt:create')")
    public CommonResult<Integer> createWqwt(@Valid @RequestBody WqwtCreateReqVO createReqVO) {
        return success(wqwtService.createWqwt(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新问卷调查问题")
    @PreAuthorize("@ss.hasPermission('system:wqwt:update')")
    public CommonResult<Boolean> updateWqwt(@Valid @RequestBody WqwtUpdateReqVO updateReqVO) {
        wqwtService.updateWqwt(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得问卷调查问题")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:wqwt:query')")
    public CommonResult<WqwtRespVO> getWqwt(@RequestParam("id") Integer id) {
        WqwtDO wqwt = wqwtService.getWqwt(id);
        return success(WqwtConvert.INSTANCE.convert(wqwt));
    }

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

    @GetMapping("/page")
    @ApiOperation("获得问卷调查问题分页")
    @PreAuthorize("@ss.hasPermission('system:wqwt:query')")
    public CommonResult<PageResult<WqwtRespVO>> getWqwtPage(@Valid WqwtPageReqVO pageVO) {
        PageResult<WqwtDO> pageResult = wqwtService.getWqwtPage(pageVO);
        return success(WqwtConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出问卷调查问题 Excel")
    @PreAuthorize("@ss.hasPermission('system:wqwt:export')")
    @OperateLog(type = EXPORT)
    public void exportWqwtExcel(@Valid WqwtExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WqwtDO> list = wqwtService.getWqwtList(exportReqVO);
        // 导出 Excel
        List<WqwtExcelVO> datas = WqwtConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问卷调查问题.xls", "数据", WqwtExcelVO.class, datas);
    }

}
