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

import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.resource.record.controller.admin.recordRelation.vo.RecordRelationPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordRelation.vo.RecordRelationRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordRelation.vo.RecordRelationSaveReqVO;
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 java.util.stream.Collectors;

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.recordRelation.RecordRelationDO;
import com.zt.plat.module.qms.resource.record.service.recordRelation.RecordRelationService;

@Tag(name = "管理后台 - 文件记录关系")
@RestController
@RequestMapping("/qms/record-relation")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.recordRelation")
public class RecordRelationController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private RecordRelationService recordRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建文件记录关系")
    //@PreAuthorize("@ss.hasPermission('qms:record-relation:create')")
    public CommonResult<RecordRelationRespVO> createRecordRelation(@Valid @RequestBody RecordRelationSaveReqVO createReqVO) {
        return success(recordRelationService.createRecordRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件记录关系")
    //@PreAuthorize("@ss.hasPermission('qms:record-relation:update')")
    public CommonResult<Boolean> updateRecordRelation(@Valid @RequestBody RecordRelationSaveReqVO updateReqVO) {
        recordRelationService.updateRecordRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件记录关系")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:record-relation:delete')")
    public CommonResult<Boolean> deleteRecordRelation(@RequestParam("id") String id) {
        recordRelationService.deleteRecordRelation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文件记录关系")
                //@PreAuthorize("@ss.hasPermission('qms:record-relation:delete')")
    public CommonResult<Boolean> deleteRecordRelationList(@RequestBody BatchDeleteReqVO req) {
        recordRelationService.deleteRecordRelationListByIds(
                req.getIds().stream()
                        .map(String::valueOf)
                        .collect(Collectors.toList())
        );

        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件记录关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:record-relation:query')")
    public CommonResult<RecordRelationRespVO> getRecordRelation(@RequestParam("id") String id) {
        RecordRelationDO recordRelation = recordRelationService.getRecordRelation(id);
        return success(BeanUtils.toBean(recordRelation, RecordRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件记录关系分页")
    //@PreAuthorize("@ss.hasPermission('qms:record-relation:query')")
    public CommonResult<PageResult<RecordRelationRespVO>> getRecordRelationPage(@Valid RecordRelationPageReqVO pageReqVO) {
        PageResult<RecordRelationDO> pageResult = recordRelationService.getRecordRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件记录关系 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:record-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordRelationExcel(@Valid RecordRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordRelationDO> list = recordRelationService.getRecordRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件记录关系.xls", "数据", RecordRelationRespVO.class,
                        BeanUtils.toBean(list, RecordRelationRespVO.class));
    }

}