package nckd.jimin.jyyy.fi.plugin.form.common;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.logging.BizLog;
import kd.bos.mvc.list.ListDataProvider;
import kd.bos.servicehelper.workflow.WorkflowServiceHelper;
import kd.bos.workflow.api.BizProcessStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class CurApproveNodeListProvider extends ListDataProvider {
    private static final String NCKD_CURAPPROVENODE = "nckd_curapprovenode";
    @Override
    public DynamicObjectCollection getData(int start, int limit) {
        DynamicObjectCollection dataCol = super.getData(start, limit);

        Set<String> billIdSet = dataCol.stream().map(dyx -> dyx.getString("id")).collect(Collectors.toSet());
        Map<String, String> approveStepMap = getApproveStep(billIdSet);
        for(DynamicObject data : dataCol){
            String dataId = data.getString("id");
            data.set(NCKD_CURAPPROVENODE,approveStepMap.getOrDefault(dataId, ""));
        }
        return dataCol;
    }

    public Map<String , String> getApproveStep(Set<String> billIdSet){
        Map<String , String> approveStepMap = new HashMap<>();

        Map<String, List<BizProcessStatus>> allProcessMap = WorkflowServiceHelper.getBizProcessStatus(billIdSet.toArray(new String[0]));
        for(Map.Entry<String, List<BizProcessStatus>> dataRow : allProcessMap.entrySet()){
            List<BizProcessStatus> node = dataRow.getValue();
            int count = 0;
            String nodeName = "";
            StringBuilder participantName = new StringBuilder();
            StringBuilder msg = new StringBuilder();
            try {
                for (BizProcessStatus e : node) {
                    nodeName = e.getCurrentNodeName();
                    String auditor = e.getParticipantName();
                    e.getProcessStatus();
                    if (auditor != null && !"".equals(auditor.trim())) {
                        participantName.append(",");
                        participantName.append(auditor);
                        if (++count == 3) {
                            participantName.append("...");
                            break;
                        }
                        continue;
                    }
                }
                if (!org.apache.commons.lang3.StringUtils.isBlank(nodeName)) {
                    msg.append(nodeName);
                    if (!org.apache.commons.lang3.StringUtils.isBlank(participantName)) {
                        msg.append('/');
                        msg.append(participantName.toString().replaceFirst(",", ""));
                    }
                }
                approveStepMap.put(dataRow.getKey(),msg.toString());
            }
            catch (Exception e2) {
                BizLog.log("ErWorkFlowFlexListForOtherPlugin>>>获取审批人出现异常: " + e2.getMessage());
            }
        }
        return approveStepMap;
    }
}
