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

import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.common.service.BusinessFileService;
import com.zt.plat.module.qms.resource.clientManage.controller.vo.VersionManagementRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapply.vo.RecordApplyRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapply.vo.RecordApplySaveReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordpermission.vo.RecordPermissionPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordpermission.vo.RecordPermissionRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordRecordPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordRecordRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordRecordSaveReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordpermission.RecordPermissionDO;
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.recordrecord.RecordRecordDO;
import com.zt.plat.module.qms.resource.record.service.recordrecord.RecordRecordService;

@Tag(name = "管理后台 - 文件、模板、记录")
@RestController
@RequestMapping("/qms/resource/record-record")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.recordrecord", codeKey = "data.fileUploadBusinessCode")
public class RecordRecordController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private RecordRecordService recordRecordService;
    @Resource
    private BusinessFileService businessFileService;


    @PostMapping("/create")
    @Operation(summary = "创建文件、模板、记录->分类明细")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:create')")
    public CommonResult<RecordRecordRespVO> createRecordRecord(@Valid @RequestBody RecordRecordSaveReqVO createReqVO) {
        return success(recordRecordService.createRecordRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件、模板、记录")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:update')")
    public CommonResult<RecordRecordRespVO> updateRecordRecord(@Valid @RequestBody RecordRecordSaveReqVO updateReqVO) {
        RecordRecordRespVO recordRecordRespVO = recordRecordService.updateRecordRecord(updateReqVO);
        return success(recordRecordRespVO);
    }

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

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


    @GetMapping("/get")
    @Operation(summary = "获得文件、模板、记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:query')")
    public CommonResult<RecordRecordRespVO> getRecordRecord(@RequestParam("id") Long id) {
        RecordRecordDO recordRecord = recordRecordService.getRecordRecord(id);
        RecordRecordRespVO vo = BeanUtils.toBean(recordRecord, RecordRecordRespVO.class);
        vo.setBusinessFileRet(businessFileService.getBusinessFileList(id));
        return success(vo);
    }

    @GetMapping("/justRecordPermission")
    @Operation(summary = "判断当前文件的权限")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:query')")
    public CommonResult<String> justRecordPermission(@RequestParam("id") Long id) {
        return recordRecordService.justRecordPermission(id);
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件、模板、记录分页")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:query')")
    public CommonResult<PageResult<RecordRecordRespVO>> getRecordRecordPage(@Valid RecordRecordPageReqVO pageReqVO) {
        PageResult<RecordRecordDO> pageResult;
        Integer isQueryHistory = pageReqVO.getIsQueryHistory();
        if (isQueryHistory != null && isQueryHistory == 1) { // 查询历史分页
            pageResult=recordRecordService.getRecordHistoryPage(pageReqVO);
        }else {
            pageResult = recordRecordService.getRecordRecordPage(pageReqVO);
        }
        return success(BeanUtils.toBean(pageResult, RecordRecordRespVO.class));
    }

    @GetMapping("/getMatchedPermissionList")
    @Operation(summary = "获得记录权限列表")
    //@PreAuthorize("@ss.hasPermission('qms:record-permission:query')")
    public CommonResult<List<RecordPermissionRespVO>> getMatchedPermissionList(@Valid RecordPermissionPageReqVO pageReqVO) {
        Long sourceId = pageReqVO.getSourceId();
        if (sourceId == null) return success(null);
        List<RecordPermissionDO> matchedPermissionList = recordRecordService.getMatchedPermissionList(sourceId);
        return success(BeanUtils.toBean(matchedPermissionList, RecordPermissionRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出文件、模板、记录 Excel")
//    //@PreAuthorize("@ss.hasPermission('qms:record-record:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportRecordRecordExcel(@Valid RecordRecordPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<RecordRecordDO> list = recordRecordService.getRecordRecordPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "文件、模板、记录.xls", "数据", RecordRecordRespVO.class,
//                        BeanUtils.toBean(list, RecordRecordRespVO.class));
//    }

}