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

import com.alibaba.fastjson2.JSONObject;
import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapply.vo.RecordApplyDetailJoinPageVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapply.vo.RecordApplyPageReqVO;
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.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.recordapply.RecordApplyDO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordrecord.RecordRecordDO;
import com.zt.plat.module.qms.resource.record.service.recordapply.RecordApplyService;
import com.zt.plat.module.qms.resource.record.service.recordrecord.RecordRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文件记录通用申请")
@RestController
@RequestMapping("/qms/resource/record-apply")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.recordapply")
public class RecordApplyController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private RecordApplyService recordApplyService;


    @PostMapping("/create")
    @Operation(summary = "创建文件记录创建申请")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:create')")
    public CommonResult<RecordApplyRespVO> createRecordApply(@Valid @RequestBody RecordApplySaveReqVO createReqVO) {
        return success(recordApplyService.createRecordApply(createReqVO));
    }

    @PostMapping("/createApplyData")
    @Operation(summary = "创建文件申请数据")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:create')")
    public CommonResult<RecordApplyRespVO> createApplyData(@Valid @RequestBody RecordApplySaveReqVO respVO) {
        return recordApplyService.createApplyData(respVO);
    }

    @PostMapping("/submitApply")
    @Operation(summary = "创建文件记录通用申请")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:create')")
    public CommonResult<RecordApplyRespVO> submitApply(@Valid @RequestBody RecordApplySaveReqVO respVO) {
        return recordApplyService.submitRecordApply(respVO);
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件记录通用申请")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:update')")
    public CommonResult<Boolean> updateRecordApply(@Valid @RequestBody RecordApplySaveReqVO updateReqVO) {
        recordApplyService.updateRecordApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件记录通用申请")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:delete')")
    public CommonResult<Boolean> deleteRecordApply(@RequestParam("id") Long id) {
        recordApplyService.deleteRecordApply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文件记录通用申请")
                //@PreAuthorize("@ss.hasPermission('qms:record-apply:delete')")
    public CommonResult<Boolean> deleteRecordApplyList(@RequestBody BatchDeleteReqVO req) {
        recordApplyService.deleteRecordApplyListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件记录通用申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:query')")
    public CommonResult<RecordApplyRespVO> getRecordApply(@RequestParam("id") Long id) {
        RecordApplyDO recordApply = recordApplyService.getRecordApply(id);
        
        // 先转换为VO，避免JSONObject中包含ArrayList导致的转换错误
        RecordApplyRespVO respVO = BeanUtils.toBean(recordApply, RecordApplyRespVO.class);
        
//        // 单独处理formDataObject字段
//        String formData = recordApply.getFormData();
//        if (!ObjectUtils.isEmpty(formData)) {
//            JSONObject jsonObject = JSONObject.parseObject(formData, JSONObject.class);
//            respVO.setFormDataObject(jsonObject);
//        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件记录通用申请分页")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:query')")
    public CommonResult<PageResult<RecordApplyRespVO>> getRecordApplyPage(@Valid RecordApplyPageReqVO pageReqVO) {
        PageResult<RecordApplyDO> pageResult = recordApplyService.getRecordApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordApplyRespVO.class));
//        List<RecordApplyDO> list = pageResult.getList();
//
//        // 先转换为VO列表
//        List<RecordApplyRespVO> respVOList = BeanUtils.toBean(list, RecordApplyRespVO.class);
//        PageResult<RecordApplyRespVO> result = new PageResult<>();
//        result.setList(respVOList);
//        result.setTotal(pageResult.getTotal());
//        return success(result);
    }


    @GetMapping("/recordReviewPage")
    @Operation(summary = "文件评审分页")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:query')")
    public CommonResult<PageResult<RecordApplyPageReqVO>> recordReviewPage(@Valid RecordApplyPageReqVO pageReqVO) {
        PageResult<RecordApplyDO> pageResult = recordApplyService.getRecordApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordApplyPageReqVO.class));
    }

    @GetMapping("/recordDistributePage")
    @Operation(summary = "文件分发分页")
    //@PreAuthorize("@ss.hasPermission('qms:record-record:query')")
    public CommonResult<PageResult<RecordApplyPageReqVO>> recordDistributePage(@Valid RecordApplyPageReqVO pageReqVO) {
        PageResult<RecordApplyDO> pageResult = recordApplyService.recordDistributePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordApplyPageReqVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件记录通用申请 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordApplyExcel(@Valid RecordApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordApplyDO> list = recordApplyService.getRecordApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件记录通用申请.xls", "数据", RecordApplyRespVO.class,
                        BeanUtils.toBean(list, RecordApplyRespVO.class));
    }

}