package com.chinaweal.aiccs.aiccs.force.cancel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.auditing.entity.TSOpinion;
import com.chinaweal.aiccs.aiccs.auditing.service.TSOpinionService;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.entity.TSTwfProcessNode;
import com.chinaweal.aiccs.aiccs.business.rules.model.SequenceFlowModel;
import com.chinaweal.aiccs.aiccs.business.service.TSTaskListService;
import com.chinaweal.aiccs.aiccs.business.service.WorkFlowService;
import com.chinaweal.aiccs.aiccs.force.cancel.entity.CancelRevoke;
import com.chinaweal.aiccs.aiccs.force.cancel.entity.CancelRevokeApprove;
import com.chinaweal.aiccs.aiccs.force.cancel.entity.dto.CancelRevokeApproveDTO;
import com.chinaweal.aiccs.aiccs.force.cancel.entity.dto.CancelRevokeCreateDTO;
import com.chinaweal.aiccs.aiccs.force.cancel.entity.dto.CancelRevokeQueryDTO;
import com.chinaweal.aiccs.aiccs.force.cancel.entity.vo.*;
import com.chinaweal.aiccs.aiccs.force.cancel.mapper.CancelRevokeApproveMapper;
import com.chinaweal.aiccs.aiccs.force.cancel.mapper.CancelRevokeMapper;
import com.chinaweal.aiccs.aiccs.force.cancel.service.ICancelRevokeService;
import com.chinaweal.aiccs.aiccs.force.mapper.XrForceDeregisterMapper;
import com.chinaweal.aiccs.aiccs.revoke.entity.Revokelist;
import com.chinaweal.aiccs.aiccs.revoke.service.RevokelistService;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.BaseDataConstant;
import com.chinaweal.aiccs.common.util.FlowUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.service.AicorgService;
import com.chinaweal.aiccs.org.service.TUsersService;
import com.chinaweal.aicorg.model.AICUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 撤销吊销业务Service实现类
 */
@Slf4j
@Service
public class CancelRevokeServiceImpl extends BaseController implements ICancelRevokeService {

    @Autowired
    private CancelRevokeMapper cancelRevokeMapper;

    @Autowired
    private CancelRevokeApproveMapper cancelRevokeApproveMapper;

    @Autowired
    private XrForceDeregisterMapper xrForceDeregisterMapper;

    @Autowired
    private TSTaskListService taskListService;

    @Autowired
    private WorkFlowService workFlowService;

    @Autowired
    private TSOpinionService opinionService;

    @Autowired
    private TRBaseCodeService baseCodeService;

    @Autowired
    private AicorgService aicorgService;

    @Autowired
    private TUsersService usersService;
    @Autowired
    private RevokelistService revokelistService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CancelRevokeCreateVO createCancelRevoke(CancelRevokeCreateDTO createDTO, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }

        // 生成业务ID和业务编号
        String bizId = StringUtils.getUUID();
        String bizSeq = StringUtils.getUUID();

        // 获取用户所属机关区域码
        String decorgCode = aicorgService.getRegionCodeByUser(user);

        // 构建业务实体
        CancelRevoke cancelRevoke = new CancelRevoke();
        cancelRevoke.setBizId(bizId);
        cancelRevoke.setBizSeq(bizSeq);
        cancelRevoke.setPripid(createDTO.getPripid());
        cancelRevoke.setUniscid(createDTO.getUniscid());
        cancelRevoke.setRegno(createDTO.getRegno());
        cancelRevoke.setEntname(createDTO.getEntname());
        cancelRevoke.setRevokeReason(createDTO.getRevokeReason());
        cancelRevoke.setCancelRevokeReason(createDTO.getCancelRevokeReason());
        cancelRevoke.setApprovalDocNo(createDTO.getApprovalDocNo());
        cancelRevoke.setApprovalDept(createDTO.getApprovalDept());
        cancelRevoke.setApprovalDeptId(createDTO.getApprovalDeptId());
        cancelRevoke.setRemarks(createDTO.getRemarks());
        cancelRevoke.setRevokeListId(createDTO.getRevokeListId());
        cancelRevoke.setStatus("1"); // 待受理
        cancelRevoke.setCurrentNode("1"); // 受理节点
        cancelRevoke.setHandlerId(user.getPrimaryKey());
        cancelRevoke.setHandlerName(user.getName());
        cancelRevoke.setHandlerDate(new Date());
        cancelRevoke.setCreateTime(new Date());
        cancelRevoke.setAreaCode(decorgCode);

        // 插入业务数据
        cancelRevokeMapper.insert(cancelRevoke);

        // 创建工作流待办
        return startWorkFlow(cancelRevoke, user);
    }

    /**
     * 启动工作流
     */
    private CancelRevokeCreateVO startWorkFlow(CancelRevoke cancelRevoke, AICUser user) {
        CancelRevokeCreateVO resultVO = new CancelRevokeCreateVO();

        // 创建任务列表对象
        TSTaskList taskList = new TSTaskList();
        taskList.setBizseqid(cancelRevoke.getBizSeq());
        taskList.setBustype(BaseDataConstant.BIZ_TYPE_CANCEL_REVOKE);
        taskList.setBusname(cancelRevoke.getEntname());
        taskList.setBusstatus(BaseDataConstant.BUSINESS_STATUS_TRANSACT);

        // 使用TSTaskListService的startWorkFlow方法启动工作流
        String tasklistid = taskListService.startWorkFlow(taskList, user, FlowUtil.SERIOUS_ILLEGAL_REM_WORKFLOW);

        // 更新业务表的流程ID
        String workflowid = taskList.getWorkflowid();
        CancelRevoke updateEntity = new CancelRevoke();
        updateEntity.setBizId(cancelRevoke.getBizId());
        updateEntity.setWorkflowid(workflowid);
        updateEntity.setTasklistid(tasklistid);
        cancelRevokeMapper.update(updateEntity);

        // 获取最新的任务信息
        List<TSTaskList> taskLists = taskListService.selectByBizSeqID(cancelRevoke.getBizSeq());
        if (taskLists != null && !taskLists.isEmpty()) {
            taskList = taskLists.get(0);
        }

        // 设置返回结果
        resultVO.setBizId(cancelRevoke.getBizId());
        resultVO.setBizSeq(cancelRevoke.getBizSeq());
        resultVO.setTasklistid(taskList.getTasklistid());
        resultVO.setWorkflowid(workflowid);

        // 获取下一步流程信息
        List<SequenceFlowModel> nextSequenceFlowList = workFlowService.findNextSequenceFlowList(workflowid);
        TSTwfProcessNode currentNode = workFlowService.findCurrentNode(workflowid);

        resultVO.setNextSequenceFlowList(nextSequenceFlowList);
        resultVO.setCurrentNode(currentNode != null ? currentNode.getCurrentnode() : null);

        return resultVO;
    }

    @Override
    public CancelRevokeVO getCancelRevokeById(String bizId, String tasklistid) {
        CancelRevoke cancelRevoke = cancelRevokeMapper.selectById(bizId);
        if (cancelRevoke == null) {
            return null;
        }

        CancelRevokeVO vo = convertToVO(cancelRevoke);

        // 获取工作流信息
        List<TSTaskList> taskLists = taskListService.selectByBizSeqID(cancelRevoke.getBizSeq());
        if (CollectionUtils.isNotEmpty(taskLists)) {
            TSTaskList taskList = taskLists.get(0);
            vo.setTasklistid(taskList.getTasklistid());
            vo.setBizseqid(taskList.getBizseqid());
            vo.setBustype(taskList.getBustype());
            vo.setBusname(taskList.getBusname());
            vo.setWorkflowid(taskList.getWorkflowid());
            vo.setBusstatus(taskList.getBusstatus());
            vo.setSignuserid(taskList.getSignuserid());
            vo.setSignloginname(taskList.getSignloginname());

            // 获取下一节点和意见信息
            if (StringUtils.isNotBlank(taskList.getWorkflowid())) {
                String opinionType = null;
                if (StringUtils.notEquals(taskList.getBusstatus(), "2")) {
                    List<SequenceFlowModel> nextSequenceFlowList = workFlowService.findNextSequenceFlowList(taskList.getWorkflowid());
                    vo.setNextSequenceFlowList(nextSequenceFlowList);

                    TSTwfProcessNode currentNode = workFlowService.findCurrentNode(taskList.getWorkflowid());
                    if (currentNode != null) {
                        opinionType = BaseDataConstant.getOpinionTypeByWorkFlowNode(currentNode.getCurrentnode());
                    }
                }
                if (opinionType == null) {
                    opinionType = "1";
                }
                vo.setOpinionType(opinionType);
                vo.setOpinionList(opinionService.selectByAuditingID(taskList.getWorkflowid()));
            }
        }

        // 查询审批记录
        List<CancelRevokeApprove> approveList = cancelRevokeApproveMapper.selectByBizId(bizId);
        vo.setApproveList(approveList);

        return vo;
    }

    @Override
    public CancelRevokeVO getCancelRevokeDetail(String bizSeq, String tasklistid) {
        CancelRevoke cancelRevoke = cancelRevokeMapper.selectByBizSeq(bizSeq);
        if (cancelRevoke == null) {
            return null;
        }
        return getCancelRevokeById(cancelRevoke.getBizId(), tasklistid);
    }

    @Override
    public CancelRevokePageVO queryCancelRevokeList(CancelRevokeQueryDTO queryDTO) {
        int pageNum = queryDTO.getPageNum() != null ? queryDTO.getPageNum() : 1;
        int pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10;

        Page<CancelRevoke> page = new Page<>(pageNum, pageSize);
        Map<String, Object> params = buildQueryParams(queryDTO);
        LambdaQueryWrapper<CancelRevoke> wrapper = buildWrapper(params);
        com.baomidou.mybatisplus.core.metadata.IPage<CancelRevoke> pageResult = cancelRevokeMapper.selectPage(page, wrapper);

        CancelRevokePageVO result = new CancelRevokePageVO();
        result.setList(pageResult.getRecords());
        result.setTotal(pageResult.getTotal());
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);

        return result;
    }

    @Override
    public CancelRevokePageVO queryApproveList(CancelRevokeQueryDTO queryDTO) {
        int pageNum = queryDTO.getPageNum() != null ? queryDTO.getPageNum() : 1;
        int pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10;

        Page<CancelRevoke> page = new Page<>(pageNum, pageSize);
        Map<String, Object> params = buildQueryParams(queryDTO);
        LambdaQueryWrapper<CancelRevoke> wrapper = buildWrapper(params);
        // 待审批状态为 2, 3, 4
        wrapper.in(CancelRevoke::getStatus, Arrays.asList("2", "3", "4"));
        com.baomidou.mybatisplus.core.metadata.IPage<CancelRevoke> pageResult = cancelRevokeMapper.selectPage(page, wrapper);

        CancelRevokePageVO result = new CancelRevokePageVO();
        result.setList(pageResult.getRecords());
        result.setTotal(pageResult.getTotal());
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CancelRevokeApproveResultVO approve(CancelRevokeApproveDTO approveDTO, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }

        CancelRevoke cancelRevoke = cancelRevokeMapper.selectById(approveDTO.getBizId());
        if (cancelRevoke == null) {
            throw new RuntimeException("业务不存在");
        }

        CancelRevokeApproveResultVO resultVO = new CancelRevokeApproveResultVO();
        resultVO.setBizId(cancelRevoke.getBizId());
        resultVO.setBizSeq(cancelRevoke.getBizSeq());

        // 获取任务列表
        List<TSTaskList> taskLists = taskListService.selectByBizSeqID(cancelRevoke.getBizSeq());
        if (taskLists == null || taskLists.isEmpty()) {
            throw new RuntimeException("任务不存在");
        }
        TSTaskList taskList = taskLists.get(0);

        // 创建审批记录
        CancelRevokeApprove approve = new CancelRevokeApprove();
        approve.setApproveId(StringUtils.getUUID());
        approve.setBizId(approveDTO.getBizId());
        approve.setNode(approveDTO.getCurrentNode());
        approve.setApproverId(user.getPrimaryKey());
        approve.setApproverName(user.getName());
        approve.setApproveStatus(approveDTO.getApproveStatus());
        approve.setApproveOpinion(approveDTO.getApproveOpinion());
        approve.setApproveTime(new Date());
        approve.setCreateTime(new Date());

        cancelRevokeApproveMapper.insert(approve);

        // 处理审批通过或拒绝
        if ("1".equals(approveDTO.getApproveStatus())) {
            // 通过
            resultVO.setFlag("success");
            resultVO.setNextNodeID(approveDTO.getNextNodeID());
            processApprovePass(cancelRevoke, approveDTO, taskList, user);
        } else {
            // 拒绝
            resultVO.setFlag("reject");
            resultVO.setStatus("6");
            processApproveReject(cancelRevoke, approveDTO, taskList, user);
        }

        return resultVO;
    }

    /**
     * 处理审批通过
     */
    private void processApprovePass(CancelRevoke cancelRevoke, CancelRevokeApproveDTO approveDTO,
                                    TSTaskList taskList, AICUser user) {
        String nextNodeID = approveDTO.getNextNodeID();

        // 更新业务流程信息
        cancelRevokeMapper.updateStatus(cancelRevoke.getBizId(), "2", nextNodeID);

        // 保存审批意见
        TSOpinion opinion = new TSOpinion();
        opinion.setBizseq(cancelRevoke.getBizSeq());
        opinion.setWorkflowid(taskList.getWorkflowid());
        opinion.setHandlerid(user.getPrimaryKey());
        opinion.setHandler(user.getName());
        opinion.setOpiniontype("1"); // 审批
        opinion.setOpinioncontent(approveDTO.getApproveOpinion());
        opinion.setIsagree("1"); // 同意
        opinion.setLauptime(LocalDateTime.now());
        opinionService.save(opinion);

        // 推进工作流到下一节点
        String[] nextPerformerids = null;
        String[] nextPerformerNames = null;
        if (StringUtils.isNotBlank(approveDTO.getNextPerformerids())) {
            nextPerformerids = approveDTO.getNextPerformerids().split(",");
            nextPerformerNames = approveDTO.getNextPerformerNames().split(",");
        }
        taskListService.updateWorkFlow(user, opinion, taskList, nextNodeID, nextPerformerids, nextPerformerNames);

        // 如果工作流已结束，更新业务状态为已通过
        if (BaseDataConstant.BUSINESS_STATUS_END.equals(taskList.getBusstatus())) {
            cancelRevokeMapper.updateStatus(cancelRevoke.getBizId(), "5", nextNodeID);
            revokelistService.lambdaUpdate()
                    .set(Revokelist::getLauptime, LocalDateTime.now())
                    .set(Revokelist::getState, "2")
                    .eq(Revokelist::getUniscid, cancelRevoke.getUniscid()).update();
        }
    }

    /**
     * 处理审批拒绝
     */
    private void processApproveReject(CancelRevoke cancelRevoke, CancelRevokeApproveDTO approveDTO,
                                      TSTaskList taskList, AICUser user) {
        // 更新业务流程状态
        cancelRevokeMapper.updateStatus(cancelRevoke.getBizId(), "6", approveDTO.getCurrentNode());

        // 保存审批意见
        TSOpinion opinion = new TSOpinion();
        opinion.setBizseq(cancelRevoke.getBizSeq());
        opinion.setWorkflowid(taskList.getWorkflowid());
        opinion.setHandlerid(user.getPrimaryKey());
        opinion.setHandler(user.getName());
        opinion.setOpiniontype("1"); // 审批
        opinion.setOpinioncontent(approveDTO.getApproveOpinion());
        opinion.setIsagree("2"); // 拒绝
        opinion.setLauptime(LocalDateTime.now());
        opinionService.save(opinion);

        // 更新工作流任务状态为已作废
        taskList.setBusstatus(BaseDataConstant.BUSINESS_STATUS_XYXF_END);
        taskListService.updateById(taskList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CancelRevokeApprovalNextVO approvalNext(CancelRevokeApproveDTO approveDTO, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }

        CancelRevokeApprovalNextVO resultVO = new CancelRevokeApprovalNextVO();

        // 获取业务信息
        CancelRevoke cancelRevoke = null;
        if (StringUtils.isNotBlank(approveDTO.getBizSeq())) {
            cancelRevoke = cancelRevokeMapper.selectByBizSeq(approveDTO.getBizSeq());
        } else if (StringUtils.isNotBlank(approveDTO.getBizId())) {
            cancelRevoke = cancelRevokeMapper.selectById(approveDTO.getBizId());
        }

        if (cancelRevoke == null) {
            throw new RuntimeException("业务不存在");
        }

        // 获取任务信息
        List<TSTaskList> taskLists = taskListService.selectByBizSeqID(cancelRevoke.getBizSeq());
        if (taskLists == null || taskLists.isEmpty()) {
            throw new RuntimeException("任务不存在");
        }
        TSTaskList taskList = taskLists.get(0);

        // 获取下一步流程信息
        List<SequenceFlowModel> nextSequenceFlowList = workFlowService.findNextSequenceFlowList(taskList.getWorkflowid());
        TSTwfProcessNode currentNode = workFlowService.findCurrentNode(taskList.getWorkflowid());

        resultVO.setBizSeq(cancelRevoke.getBizSeq());
        resultVO.setBizId(cancelRevoke.getBizId());
        resultVO.setTasklistid(taskList.getTasklistid());
        resultVO.setWorkflowid(taskList.getWorkflowid());
        resultVO.setNextSequenceFlowList(nextSequenceFlowList);
        resultVO.setCurrentNode(currentNode != null ? currentNode.getCurrentnode() : null);

        // 获取意见列表
        String opinionType = null;
        if (currentNode != null) {
            opinionType = BaseDataConstant.getOpinionTypeByWorkFlowNode(currentNode.getCurrentnode());
        }
        if (opinionType == null) {
            opinionType = "1";
        }
        resultVO.setOpinionType(opinionType);
        resultVO.setOpinionList(opinionService.selectByAuditingID(taskList.getWorkflowid()));

        return resultVO;
    }

    @Override
    public CancelRevokePageVO queryCancelRevokeRecords(CancelRevokeQueryDTO queryDTO) {
        int pageNum = queryDTO.getPageNum() != null ? queryDTO.getPageNum() : 1;
        int pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10;

        Page<CancelRevoke> page = new Page<>(pageNum, pageSize);
        Map<String, Object> params = buildQueryParams(queryDTO);
        LambdaQueryWrapper<CancelRevoke> wrapper = buildWrapper(params);
        // 已通过、已拒绝状态为 5, 6
        wrapper.in(CancelRevoke::getStatus, Arrays.asList("5", "6"));
        com.baomidou.mybatisplus.core.metadata.IPage<CancelRevoke> pageResult = cancelRevokeMapper.selectPage(page, wrapper);

        CancelRevokePageVO result = new CancelRevokePageVO();
        result.setList(pageResult.getRecords());
        result.setTotal(pageResult.getTotal());
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);

        return result;
    }

    @Override
    public CancelRevokePageVO queryRevokedEntList(CancelRevokeQueryDTO queryDTO) {
        int pageNum = queryDTO.getPageNum() != null ? queryDTO.getPageNum() : 1;
        int pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10;

        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        Map<String, Object> params = buildQueryParams(queryDTO);
        com.baomidou.mybatisplus.core.metadata.IPage<Map<String, Object>> pageResult =
                cancelRevokeMapper.selectRevokedEntPage(page, params);

        CancelRevokePageVO result = new CancelRevokePageVO();
        result.setList(pageResult.getRecords());
        result.setTotal(pageResult.getTotal());
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);

        return result;
    }

    @Override
    public CancelRevokePageVO queryTodoList(CancelRevokeQueryDTO queryDTO, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }

        int pageNum = queryDTO.getPageNum() != null ? queryDTO.getPageNum() : 1;
        int pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10;

        // 获取当前用户的待办任务
        LambdaQueryWrapper<TSTaskList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TSTaskList::getSignuserid, user.getPrimaryKey())
                .eq(TSTaskList::getBustype, BaseDataConstant.BIZ_TYPE_CANCEL_REVOKE)
                .eq(TSTaskList::getBusstatus, BaseDataConstant.BUSINESS_STATUS_TRANSACT);
        Page<TSTaskList> page = new Page<>(pageNum, pageSize);
        com.baomidou.mybatisplus.core.metadata.IPage<TSTaskList> pageResult = taskListService.page(page, wrapper);

        // 转换为VO
        List<CancelRevokeVO> voList = new ArrayList<>();
        for (TSTaskList task : pageResult.getRecords()) {
            CancelRevoke cancelRevoke = cancelRevokeMapper.selectByBizSeq(task.getBizseqid());
            if (cancelRevoke != null) {
                CancelRevokeVO vo = convertToVO(cancelRevoke);
                vo.setTasklistid(task.getTasklistid());
                vo.setBizseqid(task.getBizseqid());
                vo.setBustype(task.getBustype());
                vo.setBusname(task.getBusname());
                vo.setWorkflowid(task.getWorkflowid());
                vo.setBusstatus(task.getBusstatus());
                vo.setSignuserid(task.getSignuserid());
                vo.setSignloginname(task.getSignloginname());
                voList.add(vo);
            }
        }

        CancelRevokePageVO result = new CancelRevokePageVO();
        result.setList(voList);
        result.setTotal(pageResult.getTotal());
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);

        return result;
    }

    /**
     * 构建查询参数
     */
    private Map<String, Object> buildQueryParams(CancelRevokeQueryDTO queryDTO) {
        Map<String, Object> params = new HashMap<>();
        if (queryDTO.getEntname() != null && !queryDTO.getEntname().isEmpty()) {
            params.put("entname", queryDTO.getEntname());
        }
        if (queryDTO.getUniscid() != null && !queryDTO.getUniscid().isEmpty()) {
            params.put("uniscid", queryDTO.getUniscid());
        }
        if (queryDTO.getRegno() != null && !queryDTO.getRegno().isEmpty()) {
            params.put("regno", queryDTO.getRegno());
        }
        if (queryDTO.getBizSeq() != null && !queryDTO.getBizSeq().isEmpty()) {
            params.put("bizSeq", queryDTO.getBizSeq());
        }
        if (queryDTO.getStatus() != null && !queryDTO.getStatus().isEmpty()) {
            params.put("status", queryDTO.getStatus());
        }
        if (queryDTO.getCurrentNode() != null && !queryDTO.getCurrentNode().isEmpty()) {
            params.put("currentNode", queryDTO.getCurrentNode());
        }
        if (queryDTO.getHandlerId() != null && !queryDTO.getHandlerId().isEmpty()) {
            params.put("handlerId", queryDTO.getHandlerId());
        }
        return params;
    }

    /**
     * 构建LambdaQueryWrapper
     */
    private LambdaQueryWrapper<CancelRevoke> buildWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<CancelRevoke> wrapper = new LambdaQueryWrapper<>();
        if (params.containsKey("entname") && params.get("entname") != null) {
            wrapper.like(CancelRevoke::getEntname, params.get("entname"));
        }
        if (params.containsKey("uniscid") && params.get("uniscid") != null) {
            wrapper.eq(CancelRevoke::getUniscid, params.get("uniscid"));
        }
        if (params.containsKey("regno") && params.get("regno") != null) {
            wrapper.eq(CancelRevoke::getRegno, params.get("regno"));
        }
        if (params.containsKey("bizSeq") && params.get("bizSeq") != null) {
            wrapper.eq(CancelRevoke::getBizSeq, params.get("bizSeq"));
        }
        if (params.containsKey("status") && params.get("status") != null) {
            wrapper.eq(CancelRevoke::getStatus, params.get("status"));
        }
        if (params.containsKey("handlerId") && params.get("handlerId") != null) {
            wrapper.eq(CancelRevoke::getHandlerId, params.get("handlerId"));
        }
        wrapper.orderByDesc(CancelRevoke::getCreateTime);
        return wrapper;
    }

    /**
     * 转换为VO
     */
    private CancelRevokeVO convertToVO(CancelRevoke cancelRevoke) {
        CancelRevokeVO vo = new CancelRevokeVO();
        BeanUtils.copyProperties(cancelRevoke, vo);
        return vo;
    }
}
