package com.zt.plat.module.qms.resource.record.controller.admin.recordfileattachment;

import com.zt.plat.module.qms.resource.record.controller.admin.recordfileattachment.vo.RecordFileAttachmentPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordfileattachment.vo.RecordFileAttachmentRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordfileattachment.vo.RecordFileAttachmentSaveReqVO;
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 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.resource.record.dal.dataobject.recordfileattachment.RecordFileAttachmentDO;
import com.zt.plat.module.qms.resource.record.service.recordfileattachment.RecordFileAttachmentService;

@Tag(name = "管理后台 - 文件记录附件")
@RestController
@RequestMapping("/qms/record-file-attachment")
@Validated
@FileUploadController(source = "qms.recordfileattachment")
public class RecordFileAttachmentController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private RecordFileAttachmentService recordFileAttachmentService;

    @PostMapping("/create")
    @Operation(summary = "创建文件记录附件")
    @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:create')")
    public CommonResult<RecordFileAttachmentRespVO> createRecordFileAttachment(@Valid @RequestBody RecordFileAttachmentSaveReqVO createReqVO) {
        return success(recordFileAttachmentService.createRecordFileAttachment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件记录附件")
    @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:update')")
    public CommonResult<Boolean> updateRecordFileAttachment(@Valid @RequestBody RecordFileAttachmentSaveReqVO updateReqVO) {
        recordFileAttachmentService.updateRecordFileAttachment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件记录附件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:delete')")
    public CommonResult<Boolean> deleteRecordFileAttachment(@RequestParam("id") Long id) {
        recordFileAttachmentService.deleteRecordFileAttachment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文件记录附件")
                @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:delete')")
    public CommonResult<Boolean> deleteRecordFileAttachmentList(@RequestBody BatchDeleteReqVO req) {
        recordFileAttachmentService.deleteRecordFileAttachmentListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件记录附件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:query')")
    public CommonResult<RecordFileAttachmentRespVO> getRecordFileAttachment(@RequestParam("id") Long id) {
        RecordFileAttachmentDO recordFileAttachment = recordFileAttachmentService.getRecordFileAttachment(id);
        return success(BeanUtils.toBean(recordFileAttachment, RecordFileAttachmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件记录附件分页")
    @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:query')")
    public CommonResult<PageResult<RecordFileAttachmentRespVO>> getRecordFileAttachmentPage(@Valid RecordFileAttachmentPageReqVO pageReqVO) {
        PageResult<RecordFileAttachmentDO> pageResult = recordFileAttachmentService.getRecordFileAttachmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordFileAttachmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件记录附件 Excel")
    @PreAuthorize("@ss.hasPermission('qms:record-file-attachment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordFileAttachmentExcel(@Valid RecordFileAttachmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordFileAttachmentDO> list = recordFileAttachmentService.getRecordFileAttachmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件记录附件.xls", "数据", RecordFileAttachmentRespVO.class,
                        BeanUtils.toBean(list, RecordFileAttachmentRespVO.class));
    }

}