package com.engine.htxc.workflow.create.business.contract.init;

import com.engine.core.impl.Service;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.integration.util.StringUtils;
import weaver.docs.docs.DocManager;
import weaver.general.Util;

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

/**
 * <p>提供一个和合同相关的类型</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName contractInit.java
 * @createTime 2022年07月15日 10:23:00
 */
public class ContractWorkflowData extends Service {

    private   DbTools dbTools=new DbTools();
    public Map<String,Object> map=new HashMap<>();
    public String requestid="";
    public String mainid="";
    public String requestname="";
    public String tablename="uf_htqc";
    public String filedvalue="*";

    public  ContractWorkflowData(Map<String,Object> map){
        if(map!=null && map.size()>0){
            this.map=map;
            this.requestid= Util.null2String(map.get("requestid"));
            if(!StringUtils.isBlank(this.requestid)){
                Map<String,Object> requestnamemap=new HashMap<>(1);
                requestnamemap.put("requestid",requestid);
                this.requestname=Util.null2String(dbTools.getRequesName(requestnamemap));
                this.tablename=dbTools.getTableName(requestid);
            }
        }
    }

    public Map<String,Object> getmaintable(List<String> fieldlist){
        Map<String,Object> retmap=new HashMap<>(1);
        if(StringUtils.isBlank(requestid)){
            return retmap;
        }

        if(fieldlist!=null && fieldlist.size()>0){
            filedvalue=fieldlist.stream().collect(Collectors.joining(","));
        }
        retmap=dbTools.getRequestidToMap(requestid,fieldlist);
        if(retmap==null|| retmap.size()<=0){
            return retmap;
        }
         String id=Util.null2String(retmap.get("id"));
        if(StringUtils.isBlank(id)){
            return retmap;
        }
        this.mainid=id;
        return retmap;
    }

    public List<Map<String,String>> getdetailtable(int number,List<String> fieldlist){
        List<Map<String,String>> maplist=new ArrayList<>();
        if(number<0){
            return maplist;
        }
        if(StringUtils.isBlank(mainid)){
              getmaintable(null);
            if(StringUtils.isBlank(mainid)) {
            return  maplist;
            }
        }
        if(fieldlist!=null && fieldlist.size()>0){
            filedvalue=fieldlist.stream().collect(Collectors.joining(","));
        }
        List<Map<String,Object>> retlist=dbTools.getRequestidToMapList(requestid,fieldlist,number);
        if(retlist==null|| retlist.size()<=0){
            return maplist;
        }
        retlist.forEach((Map<String,Object> mapvalue)->{
            Map<String,String> newmap=new HashMap<>(1);
            mapvalue.keySet().forEach((key)->{
                if(!StringUtils.isBlank(key) && !StringUtils.isBlank(Util.null2String(mapvalue.get(key)))){
                    newmap.put(key,Util.null2String(mapvalue.get(key)));
                }
            });
            if(newmap!=null && newmap.size()>0){
                maplist.add(newmap);
            }
        });
        return maplist;
    }


    public List<String> newFileIds(String htyjsmj){
        List<String> list=new ArrayList<>();
        if(!StringUtils.isBlank(htyjsmj)){
            Arrays.stream(htyjsmj.split(",")).forEach((item) -> {
                DocManager docManager = new DocManager();
                docManager.setId(Util.getIntValue(item, -1));
                try {
                    docManager.copyDocForNoRightAndNoChangeStatus();
                    int newdocid = docManager.getId();
                    if(newdocid>0){
                        list.add(Util.null2String(newdocid));
                    }
                }catch (Exception e){
                }
            });
        }
        return list;
    }
}
