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

import com.zt.plat.module.qms.business.config.controller.vo.ConfigUserSignaturePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigUserSignatureRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigUserSignatureSaveReqVO;
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 com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

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.dal.dataobject.ConfigUserSignatureDO;
import com.zt.plat.module.qms.business.config.service.ConfigUserSignatureService;

@Tag(name = "管理后台 - 手写签名配置")
@RestController
@RequestMapping("/qms/config-user-signature")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.configusersignature")
public class ConfigUserSignatureController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = ConfigUserSignatureController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private ConfigUserSignatureService configUserSignatureService;

    @PostMapping("/create")
    @Operation(summary = "创建手写签名配置")
    @PreAuthorize("@ss.hasPermission('qms:config-user-signature:create')")
    public CommonResult<ConfigUserSignatureRespVO> createConfigUserSignature(@Valid @RequestBody ConfigUserSignatureSaveReqVO createReqVO) {
        return success(configUserSignatureService.createConfigUserSignature(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新手写签名配置")
    @PreAuthorize("@ss.hasPermission('qms:config-user-signature:update')")
    public CommonResult<Boolean> updateConfigUserSignature(@Valid @RequestBody ConfigUserSignatureSaveReqVO updateReqVO) {
        configUserSignatureService.updateConfigUserSignature(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除手写签名配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:config-user-signature:delete')")
    public CommonResult<Boolean> deleteConfigUserSignature(@RequestParam("id") Long id) {
        configUserSignatureService.deleteConfigUserSignature(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除手写签名配置")
                @PreAuthorize("@ss.hasPermission('qms:config-user-signature:delete')")
    public CommonResult<Boolean> deleteConfigUserSignatureList(@RequestBody BatchDeleteReqVO req) {
        configUserSignatureService.deleteConfigUserSignatureListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得手写签名配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ConfigUserSignatureRespVO> getConfigUserSignature(@RequestParam("id") Long id) {
        ConfigUserSignatureDO configUserSignature = configUserSignatureService.getConfigUserSignature(id);
        return success(BeanUtils.toBean(configUserSignature, ConfigUserSignatureRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得手写签名配置分页")
    @PreAuthorize("@ss.hasPermission('qms:config-user-signature:query')")
    public CommonResult<PageResult<ConfigUserSignatureRespVO>> getConfigUserSignaturePage(@Valid ConfigUserSignaturePageReqVO pageReqVO) {
        PageResult<ConfigUserSignatureDO> pageResult = configUserSignatureService.getConfigUserSignaturePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigUserSignatureRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出手写签名配置 Excel")
    @PreAuthorize("@ss.hasPermission('qms:config-user-signature:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigUserSignatureExcel(@Valid ConfigUserSignaturePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigUserSignatureDO> list = configUserSignatureService.getConfigUserSignaturePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "手写签名配置.xls", "数据", ConfigUserSignatureRespVO.class,
                        BeanUtils.toBean(list, ConfigUserSignatureRespVO.class));
    }

}