package com.engine.htxc.apprchalink.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htxc.apprchalink.cinterface.ApprachaLink;
import com.engine.htxc.apprchalink.cinterface.ApprachaLinkBuilder;
import com.engine.htxc.apprchalink.contract.cmd.ContractLinkCmd;
import com.engine.htxc.apprchalink.contract.service.ContractLinkService;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Remark 合同模块审批链
 * @author K1810015
 * @version 1.0.0
 * @ClassName uf_contapprcha.java
 * @createTime 2022年07月26日 14:25:00
 */
public class ContractLinkServiceImpl extends Service implements ContractLinkService {

    @Override
    public JSONObject getnextuser(JSONObject jsonObject) {
     try {

       String data=Util.null2String(jsonObject.get("data"));
       String fieldids=Util.null2String(jsonObject.get("fieldids"));
       if(StringUtils.isBlank(data) || StringUtils.isBlank(fieldids)){
           return null;
       }
       List<Map> lisltmap= JSON.parseArray(data,Map.class);
       Map<String,Object> map=new HashMap<>();
       map.put("data",lisltmap);
       List<String> fieldlist= Arrays.stream(fieldids.split(",")).collect(Collectors.toList());
       Map datanull= fieldlist.stream().collect(Collectors.toMap(
                p->p.toString(),p->""
        ));

          Map<String, Object> retmap = commandExecutor.execute(new ContractLinkCmd(map));
          if (retmap == null || retmap.size() < 0) {
              return new JSONObject(datanull);
          }
          List<Map<String, Object>> listmap = (List<Map<String, Object>>) retmap.get("data");
          if (listmap == null || listmap.size() <= 0) {
              return new JSONObject(datanull);
          }
         Map<String,Object> datamap=new ApprachaLinkBuilder<Map<String,Object>>().collback(listmap,fieldlist);
          if(datamap==null || datamap.size()<=0){
              return new JSONObject(datanull);

          }
          return new JSONObject(datamap);
      }catch (Exception e){
          return null;
      }
    }
}
