package com.bonus.material.work.service.impl;


import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.ma.domain.DirectApplyInfo;

import com.bonus.material.work.domain.DirectAudit;
import com.bonus.material.work.domain.SysWorkflowNode;
import com.bonus.material.work.domain.SysWorkflowRecordHistory;
import com.bonus.material.work.mapper.*;
import com.bonus.material.work.service.IDirectAuditService;
import com.bonus.material.work.service.SysWorkflowNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hay
 * @description 业务办理--直转审核
 * @date 2025/2/17 13:16
 */
@Service
@Slf4j
public class DirectAuditImpl implements IDirectAuditService {

    @Resource
    private DirectAuditMapper mapper;

    @Resource
    private SysWorkflowNodeMapper sysWorkflowNodeMapper;

    @Resource
    private SysWorkflowRecordHistoryMapper sysWorkflowRecordHistoryMapper;

    @Resource
    private SysWorkflowConfigMapper sysWorkflowConfigMapper;

    @Resource
    private SysWorkflowNodeService sysWorkflowNodeService;

    @Override
    public List<DirectApplyInfo> getList(DirectApplyInfo directApplyInfo) {
        List<DirectApplyInfo> directApplyInfoList = mapper.getList(directApplyInfo);
        directApplyInfoList.removeIf(item -> item == null || item.getNodeId() == null);

//        for (DirectApplyInfo  directApplyInfoNew : directApplyInfoList) {
//            log.info("当前节点为{}", directApplyInfoNew.getNodeId());
//            SysWorkflowRecordHistory sysWorkflowRecordHistory = sysWorkflowRecordHistoryMapper.getWorkflowRecordHistoryByRecordId(directApplyInfoNew.getRecordId());
//            directApplyInfoNew.setNodeId(sysWorkflowRecordHistory.getNodeId());
//            //设置当前节点
//            if (1 == directApplyInfoNew.getNodeSignType() && sysWorkflowRecordHistory.getNodeSort() == 1) {
//                directApplyInfoNew.setNodeId(sysWorkflowRecordHistory.getNextNodeId());
//            }
//            String configValue = sysWorkflowConfigMapper.selectConfigValueByNodeId(directApplyInfoNew.getNodeId());
//            SysWorkflowNode sysWorkflowNode = sysWorkflowNodeMapper.selectSysWorkflowNodeById(directApplyInfoNew.getNodeId());
//            directApplyInfoNew.setConfigValue(configValue);
//            //判断当前节点是否审核
//            sysWorkflowRecordHistory.setRecordId(directApplyInfoNew.getRecordId());
//            sysWorkflowRecordHistory.setNodeId(directApplyInfoNew.getNodeId());
//            List<SysWorkflowRecordHistory> sysWorkflowRecordHistoryList  = sysWorkflowRecordHistoryMapper.seleteSysWorkflowRecordHistory(sysWorkflowRecordHistory);
//            //判断是或签还是会签0:或签  1:会签
//            if (sysWorkflowNode.getNodeSignType() == 0) {
//                //当前节点已经审核需要修改节点值
//                if (sysWorkflowRecordHistoryList.get(0) != null) {
//                    // 查询下个节点需要审核的人员
//                    // 不能采用简单的直接查询数据库字段，而是要调用方法，根据配置来获取对应的配置
//                    // String configValueNew = sysWorkflowConfigMapper.selectConfigValueByNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
//
//                    // 先根据下个节点的ID去查询下个节点信息
//                    SysWorkflowNode nextWorkflowInfo = sysWorkflowNodeMapper.selectSysWorkflowNodeById(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
//                    // 再根据下个节点信息查询到配置值
//                    sysWorkflowNodeService.copeNodeConfigPersonValuesByNodeTwo(nextWorkflowInfo,directApplyInfoNew);
//                    directApplyInfoNew.setNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
//                    if (nextWorkflowInfo != null && nextWorkflowInfo.getConfigValues() != null) {
//                        directApplyInfoNew.setConfigValue(nextWorkflowInfo.getConfigValues());
//                    } else {
//                        directApplyInfoNew.setConfigValue("");
//                    }
//                }
//            } else {
//                String[] ConfigValue = configValue.split(",");
//                List<String> filteredList = Arrays.stream(ConfigValue).collect(Collectors.toList());
//                for (SysWorkflowRecordHistory sysWorkflowRecordHistoryNew : sysWorkflowRecordHistoryList) {
//                    if (sysWorkflowRecordHistoryNew != null) {
//                        // 过滤数组元素
//                        filteredList = filteredList.stream()
//                                .filter(value -> value != null && !value.equals(sysWorkflowRecordHistoryNew.getCreateBy()))
//                                .collect(Collectors.toList());
//                        directApplyInfoNew.setConfigValue(filteredList.toString());
//                    }
//                }
//
//                // 判断是否这个节点的所有人都需要审核
//                if (filteredList.isEmpty()) {
//                    // 不能采用简单的直接查询数据库字段，而是要调用方法，根据配置来获取对应的配置
//                    // String configValueNew = sysWorkflowConfigMapper.selectConfigValueByNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
//                    // 先根据下个节点的ID去查询下个节点信息
//                    SysWorkflowNode nextWorkflowInfo = sysWorkflowNodeMapper.selectSysWorkflowNodeById(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
//                    // 再根据下个节点信息查询到配置值
//                    sysWorkflowNodeService.copeNodeConfigPersonValuesByNode(nextWorkflowInfo);
//
//                    directApplyInfoNew.setNodeId(sysWorkflowRecordHistoryList.get(0).getNextNodeId());
//                    directApplyInfoNew.setConfigValue(nextWorkflowInfo.getConfigValues());
//                } else {
//                    if (!sysWorkflowRecordHistoryList.isEmpty()) {
//                        directApplyInfoNew.setNodeId(directApplyInfoNew.getNodeId());
//                        directApplyInfoNew.setConfigValue(filteredList.toString());
//                    }
//                }
//
//            }
//        }
        return directApplyInfoList;
    }


    @Override
    public List<DirectAudit> getAuditInfo(DirectAudit du) {
        List<DirectAudit> list = new ArrayList<>();
        try {

            List<DirectAudit> auditlist = mapper.getAuditInfo(du);

            if (auditlist != null && !auditlist.isEmpty()) {
                list.add(auditlist.get(0));
                for (int i= 0 ; i < auditlist.size(); i++) {
                    DirectAudit da = new DirectAudit();
                    da.setNodeId(auditlist.get(i).getNextNodeId());
                    da.setNodeName(auditlist.get(i).getNextNodeName());
                    da.setFlowId(auditlist.get(i).getFlowId());
                    if (auditlist.size() - 1 == i) {
                        da.setIsAccept("");
                        da.setRemark("");
                    } else {
                        da.setIsAccept(auditlist.get(i).getIsAccept());
                        da.setRemark(auditlist.get(i).getRemark());
                    }

                    da.setCreator(auditlist.get(i).getCreator());
                    da.setCreateTime(auditlist.get(i).getCreateTime());

                    list.add(da);
                }
            }
        } catch (DataAccessException e) {
            log.error("查询直转审核记录失败", e);
            return list;
        }

        return list;
    }

    @Override
    public AjaxResult auditDir(DirectAudit audit) {

        //1判断节点是否为最后一个节点,如果是最后一个节点,则修改状态为通过，如果不是循环节点进行审核
        //判断最后一个节点，查询审核流配置倒序排序，如果前端传回下个节点和查询节点相等，则为最后一个节点

        SysWorkflowNode node =  new SysWorkflowNode();

        node.setTypeId(15);

        String isAccept = audit.getIsAccept();
        String remark = audit.getRemark();
        List<SysWorkflowNode> nodes = sysWorkflowNodeMapper.selectSysWorkflowNodeList(node);
        int flowId = audit.getFlowId();
        if(nodes !=null && nodes.size()>0){

            for(SysWorkflowNode n : nodes){
                 int flowNodeId = n.getId();
                 String nodeLable = n.getNodeLabel();
                if (audit.getNodeId()==flowNodeId) {

                    DirectAudit fr = new DirectAudit();
                    fr.setId(flowId);
                    if(2==Integer.parseInt(isAccept)){
                        fr.setFlowStatus("3");
                    }else{
                        if("2".equals(nodeLable)){
                            fr.setFlowStatus("2");
                        }else{
                            fr.setFlowStatus("1");

                            //这里未判断下个节点的逻辑，后面需要修改
                            int nextNodeId = flowNodeId + 1;

                            //2 生成直转审核人
                            SysWorkflowRecordHistory his = new SysWorkflowRecordHistory();

                            his.setRecordId(flowId);
                            his.setNodeId(flowNodeId);
                            his.setNextNodeId(nextNodeId);
                            his.setIsAccept(Integer.parseInt(isAccept));
                            his.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
                            his.setCreateTime(new Date());
                            his.setRemark(remark);
                            sysWorkflowRecordHistoryMapper.addSysWorkflowRecordHistory(his) ;
                        }

                    }
                    mapper.updateWorkflowRecord(fr);
                }

            }
        }

        return AjaxResult.success();
    }
}
