package cn.ey.biz.module.system.controller.admin.errorcode;

import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.errorcode.vo.*;
import cn.ey.biz.module.system.convert.errorcode.ErrorCodeConvert;
import cn.ey.biz.module.system.dal.dataobject.errorcode.ErrorCodeDO;
import cn.ey.biz.module.system.service.errorcode.ErrorCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 错误码")
@RestController
@RequestMapping("/system/error-code")
@Validated
public class ErrorCodeController {

    @Resource
    private ErrorCodeService errorCodeService;

    @PostMapping("/create")
    @ApiOperation("创建错误码")
    @PreAuthorize("@ss.hasPermission('system:error-code:create')")
    public R<Void> createErrorCode(@Valid @RequestBody ErrorCodeCreateReqVO createReqVO) {
        errorCodeService.createErrorCode(createReqVO);
        return success(null);
    }

    @PutMapping("/update")
    @ApiOperation("更新错误码")
    @PreAuthorize("@ss.hasPermission('system:error-code:update')")
    public R<Boolean> updateErrorCode(@Valid @RequestBody ErrorCodeUpdateReqVO updateReqVO) {
        errorCodeService.updateErrorCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除错误码")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:error-code:delete')")
    public R<Boolean> deleteErrorCode(@RequestParam("ids") List<Long> ids){
        errorCodeService.deleteErrorCode(ids);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得错误码")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:error-code:query')")
    public R<ErrorCodeRespVO> getErrorCode(@RequestParam("ids") List<Long> ids) {
        ErrorCodeRespVO errorCode = errorCodeService.getErrorCode(ids);
        return success(errorCode);
    }

    @GetMapping("/page")
    @ApiOperation("获得错误码分页")
    @PreAuthorize("@ss.hasPermission('system:error-code:query')")
    public R<PageResult<ErrorCodeRespVO>> getErrorCodePage(@Valid ErrorCodePageReqVO pageVO) {
        PageResult<ErrorCodeRespVO> pageResult = errorCodeService.getErrorCodePage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出错误码 Excel")
    @PreAuthorize("@ss.hasPermission('system:error-code:export')")
    @OperateLog(type = EXPORT)
    public void exportErrorCodeExcel(@Valid ErrorCodeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ErrorCodeDO> list = errorCodeService.getErrorCodeList(exportReqVO);
        // 导出 Excel
        List<ErrorCodeExcelVO> datas = ErrorCodeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "错误码.xls", "数据", ErrorCodeExcelVO.class, datas);
    }

}
