package com.engine.htxc.apprchalink.cinterface;

import com.engine.htxc.commonutil.user.UserTools;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;


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

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName ApprachaLinkBuilder.java
 * @createTime 2022年07月26日 15:00:00
 */
public class ApprachaLinkBuilder<T> {

    /**
     * <p>最终将多个记录合并</p>
     * @title collback
     * @author K1810015
     * @updateTime 2022/7/26 15:01
     * @ps 最开始考虑用Vo层来代替,有一些其他原因。。
     * @throws
     */
    public Map<String,Object> collback(List<T> list,List<String> fileid){
        Map<String,Object> retmap=new HashMap<>(2);
        if(list==null || list.size()<=0 || fileid==null) {
            return retmap;
        }
        list.forEach(t->{
           Map<String,Object> map=(Map<String, Object>) t;
           if(map!=null && map.size()>0){
               fileid.forEach(item->{
                String value= Util.null2String(map.get(item));
                if(!StringUtils.isBlank(value)){

                    if(StringUtils.isBlank(Util.null2String(retmap.get(item)))){
                        retmap.put(item,map.get(item));
                    }else {
                        retmap.put(item, retmap.get(item) + "," + map.get(item));
                    }
                }
               });
           }
        });
        fileid.forEach(item->{
            if(!retmap.containsKey(item)){
                retmap.put(item,"");
            }
        });

        Map<String,Object> filtermap= retmap.entrySet().stream().filter((item)->{
          return !StringUtils.isBlank(item.getKey());
        }).collect(Collectors.toMap(p->p.getKey(),p->
                Arrays.stream(p.getValue().toString().split(",")).distinct().collect(Collectors.joining(","))
        ));
        filtermap.entrySet().stream().forEach((item)->{
            retmap.put(item.getKey()+"_specialobj",getUserInfo(Util.null2String(item.getValue())));
        });
        return retmap;
    }


    public  List<Map<String,Object>> getUserInfo(String userids){
        return  UserTools.getUserInfo(userids,"id,lastname as name");

    }
}
