package weaver.interfaces.htsc.lhhtsq;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.lhcd.Service.LhcdbaseService;
import com.engine.htsc.lhcd.Service.impl.Lhcdbaseserviceimpl;
import com.engine.htsc.lhhtsq.Serivce.LhhtsqOldToDataService;
import com.engine.htsc.lhhtsq.Serivce.impl.LhhtsqOldToDataServiceImpl;
import com.weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName LhhtEndTocd.java
 * @createTime 2021年12月29日 16:38:00
 * @REMARK 将联合合同数据触发存档流程
 */
public class LhhtEndTocdAction  extends BaseBean implements Action{

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


    @Override
    public String execute(RequestInfo requestInfo) {
        JAVATools.SetCmdwritelog("联合合同用印办结触发存档流程："+requestInfo.getRequestid());
        String requestid= Util.null2String(requestInfo.getRequestid());
        String tablename=JAVATools.getMaintable(requestid);
        String sql="select t.id,t.htje,t.ngr,t.ngrq,(select DEPTDIRECTORS from hrmdepartmentdefined where deptid=t.szbm) as deptdirectors,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 Action.SUCCESS;
        }
        String currentnodetype= Util.null2String(retmap.get("currentnodetype"));


        String id= Util.null2String(retmap.get("id"));
        List<String> list=getDocids(tablename,id,"7");
        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"));
        String lxfs= Util.null2String(retmap.get("lxfs"));
        String bmldsh=Util.null2String(retmap.get("deptdirectors"));
        String htje=Util.null2String(retmap.get("htje"));
        if(bmldsh.split(",").length>=2){
            bmldsh="";
        }

        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("lxfs",lxfs);
        if(list!=null && list.size()>0){
            mapparams.put("docid",list.stream().collect(Collectors.joining(",")));
        }
        mapparams.put("requestid",requestid);
        mapparams.put("bmldsh",bmldsh);
        mapparams.put("htje",htje);
        mapparams.put("cdfl","0");
        getlcd().createworkflow(mapparams);
        return Action.SUCCESS;
    }

    public List getDocids(String tablename,String id,String detailids){
        List<String> liststr=new ArrayList<>();
        if("".equals(id) || "".equals(detailids)){
            return liststr;
        }

        //Arrays.stream(detailids.split(",")).forEach((item)->{
        for (int i=0;i<detailids.split(",").length;i++){
            String item=Util.null2String(detailids.split(",")[i]);
            String data= getdoc(tablename+"_dt"+item,id);
            if(!"".equals(data)){
                liststr.addAll(Arrays.stream(data.split(",")).collect(Collectors.toList()));
            }
        }
        if(liststr==null || liststr.size()<=0){
            return liststr;
        }

        return liststr.stream().distinct().collect(Collectors.toList());


    }

    public String getdoc(String detailtablename,String id){
        String value="";
        Map<String,String> map=new HashMap<>();
        List<String> liststr=new ArrayList<>();
        if("".equals(detailtablename) || "".equals(id)){

            return value;
        }
        String sql="select wm_concat_old（yyhwj） as yyhwj from "+detailtablename+" where 1=1 ";
        Map<String,String> paramsMap=new HashMap<>();
        paramsMap.put("mainid",id);
        paramsMap.put("yyzt","3");
      Map<String,Object> listdatavalue=JAVATools.GetTableToMap(sql,paramsMap);
        if(listdatavalue==null || listdatavalue.size()<=0){
            return value;
        }

        return Util.null2String(listdatavalue.get("yyhwj"));

    }


}
