package weaver.interfaces.htsc.lhcd;

import cn.hutool.core.date.DateUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.lhcd.Service.LhcdbaseService;
import com.engine.htsc.lhcd.model.Uf_xrlccdrz;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.TableModelUtil;
import weaver.interfaces.htsc.GlobatTools.Uploadcory;
import weaver.interfaces.htsc.GlobatTools.workflow.WorkflowCreateHandler;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;

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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName delaytrggerJob.java
 * @createTime 2021年12月28日 16:19:00
 */
public class DelaytrggerJob extends BaseCronJob {

    @Override
    public void execute() {
        JAVATools.SetCmdwritelog("delaytrggerJob-----start");
        delaytrggervalue();
        JAVATools.SetCmdwritelog("delaytrggerJob-----end");
    }

    private  void delaytrggervalue(){
        String sql="    select  (select requestname from workflow_requestbase where requestid=t.lcmc) requestname ,t.*  from uf_cdzhcflc t where t.sfys=1  and   sysdate>=to_date(cfrq,'yyyy-MM-dd')   ";
        List<Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return ;
        }
        //开始写入
        list.stream().forEach((value)->{
            Map<String,Object> map=value;
            innertmodel(map);
        });

    }

    private  void innertmodel(Map<String,Object> map){
        PropBean propBean=new PropBean();
        String requestid= Util.null2String(map.get("lcmc"));
        String id= Util.null2String(map.get("id"));
        String requestname= Util.null2String(map.get("requestname"));
        if("".equals(requestid)){
            return ;
        }
       String tablename= JAVATools.getMaintable(requestid);
        String sql="select * from "+tablename+ " where 1=1 ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("requestid",requestid);
        Map<String,Object> maintablemap=JAVATools.GetTableToMap(sql,wheremap);
        if(maintablemap==null || maintablemap.size()<=0){
            return ;
        }
        String cdfj=Util.null2String(maintablemap.get("cdfj"));
        String ngr=Util.null2String(maintablemap.get("ngr"));
        Map<String,String> paramsmap=new HashMap<>();
        List<String> listdoc=new ArrayList<>();
        Arrays.stream(cdfj.split(",")).forEach(value->
                {
                    if(!"".equals(value)){
                        Map<String,Integer> mapvalue= Uploadcory.coryfile(value);
                        if(mapvalue!=null && mapvalue.size()>0){
                            String newdocid=Util.null2String(mapvalue.get("docid"));
                            listdoc.add(newdocid);
                        }
                    }
                }
        );
        for( String key:maintablemap.keySet()){
            if("cdfj".equals(key)){
                paramsmap.put("cdfj",listdoc.stream().collect(Collectors.joining(",")));
            }else if("ngrq".equals(key)){
                paramsmap.put("ngrq", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm"));
            }else if(",id,requestid,".indexOf(","+key+",")<0){
                if(!"".equals(Util.null2String(maintablemap.get(key)))){
                    paramsmap.put(key,Util.null2String(maintablemap.get(key)));

                }
            }

        }
      //        paramsmap.put("cdlc",Util.null2String(maintablemap.get("cdlc")));
//        paramsmap.put("ngr",ngr);
//        paramsmap.put("szbm",Util.null2String(maintablemap.get("szbm")));
//        paramsmap.put("szjg",Util.null2String(maintablemap.get("szjg")));
//        paramsmap.put("jjcd",Util.null2String(maintablemap.get("jjcd")));
//        paramsmap.put("lxfs",Util.null2String(maintablemap.get("lxfs")));
//        paramsmap.put("dajsr",Util.null2String(maintablemap.get("dajsr")));
//        paramsmap.put("tzyddx",Util.null2String(maintablemap.get("tzyddx")));
//        paramsmap.put("bz",Util.null2String(maintablemap.get("bz")));
//        paramsmap.put("ngrgh",Util.null2String(maintablemap.get("ngrgh")));
//        paramsmap.put("cdxz",Util.null2String(maintablemap.get("cdxz")));

        String workflowid=Util.null2String(propBean.getPropName("lhcd.workflowid"));
        WorkflowCreateHandler workflowCreateHandler=new WorkflowCreateHandler();
        String newrequestid=workflowCreateHandler.WorkflowCreateByRequestMap(ngr, workflowid, requestname, "0",paramsmap);
        if(Util.getIntValue(newrequestid)>0){
            updatemodel(id);
        }
        String message="成功："+newrequestid;
        if("".equals(newrequestid) && Util.getIntValue(newrequestid)<=0){
            message="失败："+paramsmap.toString();
        }
        Uf_xrlccdrz uf_xrlccdrz=new Uf_xrlccdrz();
        uf_xrlccdrz.setMsg(message);
        uf_xrlccdrz.setLcid(requestid);
        uf_xrlccdrz.setLylc("lhcd");
        uf_xrlccdrz.setModedatacreater(Util.null2String(maintablemap.get("ngr")));
        uf_xrlccdrz.setModedatacreatedate(DateUtil.format(new Date(), "yyyy-MM-dd"));
        try {
            boolean tableflag=  TableModelUtil.createtable(uf_xrlccdrz);
        }catch (Exception exo){

        }




    }
    private void updatemodel(String id){
        String tablename="uf_cdzhcflc";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("id",id);
        Map<String,String> paramsmap=new HashMap<>();
        paramsmap.put("sfys","0");
        JAVATools.Updatesql(paramsmap,tablename,wheremap);
    }


}
