package com.zt.plat.module.qms.business.config.controller.admin;

import com.zt.plat.module.qms.business.config.controller.vo.ConfigReportFieldPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigReportFieldRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigReportFieldSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.business.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportFieldDO;
import com.zt.plat.module.qms.business.config.service.ConfigReportFieldService;

@Tag(name = "管理后台 - 报表字段配置")
@RestController
@RequestMapping("/qms/config-report-field")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ConfigReportFieldController implements BusinessControllerMarker {


    @Resource
    private ConfigReportFieldService configReportFieldService;

    @PostMapping("/create")
    @Operation(summary = "创建报表字段配置")
    //@PreAuthorize("@ss.hasPermission('qms:config-report-field:create')")
    public CommonResult<ConfigReportFieldRespVO> createConfigReportField(@Valid @RequestBody ConfigReportFieldSaveReqVO createReqVO) {
        return success(configReportFieldService.createConfigReportField(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报表字段配置")
    //@PreAuthorize("@ss.hasPermission('qms:config-report-field:update')")
    public CommonResult<Boolean> updateConfigReportField(@Valid @RequestBody ConfigReportFieldSaveReqVO updateReqVO) {
        configReportFieldService.updateConfigReportField(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报表字段配置")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:config-report-field:delete')")
    public CommonResult<Boolean> deleteConfigReportField(@RequestParam("id") Long id) {
        configReportFieldService.deleteConfigReportField(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报表字段配置")
                //@PreAuthorize("@ss.hasPermission('qms:config-report-field:delete')")
    public CommonResult<Boolean> deleteConfigReportFieldList(@RequestBody BatchDeleteReqVO req) {
        configReportFieldService.deleteConfigReportFieldListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报表字段配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:config-report-field:query')")
    public CommonResult<ConfigReportFieldRespVO> getConfigReportField(@RequestParam("id") Long id) {
        ConfigReportFieldDO configReportField = configReportFieldService.getConfigReportField(id);
        return success(BeanUtils.toBean(configReportField, ConfigReportFieldRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报表字段配置分页")
    //@PreAuthorize("@ss.hasPermission('qms:config-report-field:query')")
    public CommonResult<PageResult<ConfigReportFieldRespVO>> getConfigReportFieldPage(@Valid ConfigReportFieldPageReqVO pageReqVO) {
        PageResult<ConfigReportFieldDO> pageResult = configReportFieldService.getConfigReportFieldPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigReportFieldRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报表字段配置 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:config-report-field:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigReportFieldExcel(@Valid ConfigReportFieldPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigReportFieldDO> list = configReportFieldService.getConfigReportFieldPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报表字段配置.xls", "数据", ConfigReportFieldRespVO.class,
                        BeanUtils.toBean(list, ConfigReportFieldRespVO.class));
    }

}