package com.engine.htsc.seal.lhcd;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.lhcd.Service.LhcdbaseService;

import com.engine.htsc.lhcd.Service.impl.*;
import com.engine.htsc.wdzx.service.GetchangedetailService;
import com.engine.htsc.wdzx.service.imp.GetchangedetailServiceImp;
import com.sap.db.jdbc.Hash;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName LhcdactionApi.java
 * @createTime 2021年12月27日 18:02:00
 */
public class LhcdactionApi {


    private LhcdbaseService lhcdbaseService(){
        return ServiceUtil.getService(Lhcdbaseserviceimpl.class);
    }

    //判断是不是要送用印
    public boolean createlhcd(String requestid,String detailids){
        String tablename=JAVATools.getMaintable(requestid);
        String sql="select t.id,t.ngr,t.ngrq,t.szbm,t.szjg,t.requestid,t.lxfs,(select requestname from workflow_requestbase where requestid=t.requestid) as title, " +
                "(select currentnodetype from workflow_requestbase where requestid=t.requestid) as currentnodetype  from "+tablename+" t  where 1=1 ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("t.requestid",requestid);
        Map<String,Object> retmap=JAVATools.GetTableToMap(sql,wheremap);
        if(retmap==null || retmap.size()<=0){
            return false;
        }
        String currentnodetype= Util.null2String(retmap.get("currentnodetype"));
        if(!"3".equals(currentnodetype)){
            return false;
        }

        String id= Util.null2String(retmap.get("id"));
        List<String> list=getDocids(tablename,id,detailids);
        if(list==null || list.size()<=0){
            return false;
        }
        String ngr= Util.null2String(retmap.get("ngr"));
        String ngrq= Util.null2String(retmap.get("ngrq"));
        String szbm= Util.null2String(retmap.get("szbm"));
        String szjg= Util.null2String(retmap.get("szjg"));
        String title= Util.null2String(retmap.get("title"));
        Map<String,Object> mapparams=new HashMap<>();
        mapparams.put("ngr",ngr);
        mapparams.put("ngrq",ngrq);
        mapparams.put("szbm",szbm);
        mapparams.put("szjg",szjg);
        mapparams.put("title",title);
        mapparams.put("docid",list.stream().collect(Collectors.joining(",")));
        mapparams.put("requestid",requestid);
        return  lhcdbaseService().createworkflow(mapparams);
    }

    public List getDocids(String tablename,String id,String detailids){
        List<String> liststr=new ArrayList<>();
        if("".equals(id) || "".equals(detailids)){
            return liststr;
        }
        boolean flag=true;
        //Arrays.stream(detailids.split(",")).forEach((item)->{
          for (int i=0;i<detailids.split(",").length;i++){
              String item=Util.null2String(detailids.split(",")[i]);
              Map<String,String> docmap= getdoc(tablename+"_"+item,id);
              String status=Util.null2String(docmap.get("status"));
              if("-1".equals(status)){
                  //失败
                  flag=false;
              }
              String data=Util.null2String(docmap.get("data"));
              if(!"".equals(data)){
                  liststr.addAll(Arrays.stream(data.split(",")).collect(Collectors.toList()));
              }

          }
          if(flag){
              return null;
          }
          return liststr.stream().distinct().collect(Collectors.toList());


    }

    public Map<String,String> getdoc(String detailtablename,String id){
        Map<String,String> map=new HashMap<>();
        List<String> liststr=new ArrayList<>();
        if("".equals(detailtablename) || "".equals(id)){
            map.put("status","-1");
            return map;
        }
        String sql="select * from "+detailtablename+" where 1=1 ";
        Map<String,String> paramsMap=new HashMap<>();
        paramsMap.put("mainid",id);
        List<Map<String,Object>> listdatavalue=JAVATools.GetTableToListMap(sql,paramsMap);
        if(listdatavalue==null || listdatavalue.size()<=0){
            map.put("status","-1");
            return map;
        }
        boolean flag=true;
        listdatavalue.stream().forEach((model)->{
           Map<String,Object> detailmap=model;
           String yyhwj=Util.null2String(detailmap.get("yyhwj"));
           String yyzt=Util.null2String(detailmap.get("yyzt"));
           if("".equals(yyzt) || "2".equals(yyzt)){
            //  如果有的情况下则不触发
               map.put("status","-1");
           }
           if(!"".equals(yyhwj)){
               liststr.add(yyhwj);
           }
        });
        if(liststr!=null && liststr.size()>0){
            map.put("data",liststr.stream().distinct().collect(Collectors.joining(",")));
        }else{
            map.put("status","-1");
        }
        return map;

    }

}
