package com.engine.htsc.lhcd.Service.impl;

import cn.hutool.core.date.DateUtil;
import com.engine.core.impl.Service;
import com.engine.htsc.lhcd.Service.LhcdbaseService;
import com.engine.htsc.lhcd.model.Uf_xrlccdrz;
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 java.util.*;
import java.util.stream.Collectors;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName Lhcdbaseserviceimpl.java
 * @createTime 2021年12月27日 09:35:00
 */
public class Lhcdbaseserviceimpl extends Service implements LhcdbaseService {

    private  String logname="联合合同办结";

    /*
     * @title createworkflow
     * @remark 触发联存档流程（后续会有其他流程调用，统一定义map参数的接口方便后续调用）
     * @author xf
     * @Params：
     * @param: map
     * @updateTime 2021/1/5
     **/
    @Override
    public boolean createworkflow(Map<String, Object> map) {
        JAVATools.SetCmdwritelog(logname,"联合合同开始调用");
        PropBean propBean=new PropBean();
        String title= Util.null2String(map.get("title"));
        String docid= Util.null2String(map.get("docid"));
        String requestid= Util.null2String(map.get("requestid"));
        String ngr= Util.null2String(map.get("ngr"));
        String szbm= Util.null2String(map.get("szbm"));
        String szjg= Util.null2String(map.get("szjg"));
        String lxfs= Util.null2String(map.get("lxfs"));
        String htje= Util.null2String(map.get("htje"));
        String bmldsh= Util.null2String(map.get("bmldsh"));
        String cdfl= Util.null2String(map.get("cdfl"));
        String jjcd=Util.null2String(propBean.getPropName("lhcd.jjcd"));
        String dajsr=Util.null2String(propBean.getPropName("lhcd.dajsr"));
        JAVATools.SetCmdwritelog(logname,"查看必填参数-title:"+title+"docid:"+docid+"requestid:"+requestid);
        if("".equals(title)  || "".equals(requestid)){
            JAVATools.SetCmdwritelog(logname,"error");
            return false;
        }
        title+="的存档";
        List<String> listdoc=new ArrayList<>();
        Arrays.stream(docid.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);
            }
            }
        }
        );


        String lylc= JAVATools.getmainunnumber(requestid);
        Map<String,String> paramsmap=new HashMap<>();
        paramsmap.put("cdfj",listdoc.stream().collect(Collectors.joining(",")));
        paramsmap.put("cdlc",requestid);
        paramsmap.put("bmldsh",bmldsh);
        paramsmap.put("ngr",ngr);
        paramsmap.put("szbm",szbm);
        paramsmap.put("szjg",szjg);
        paramsmap.put("jjcd",jjcd);
        paramsmap.put("lxfs",lxfs);
        if(!"".equals(htje)){
            paramsmap.put("htje",htje);
        }
        paramsmap.put("ngrq",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm"));
        paramsmap.put("dajsr",dajsr);
        paramsmap.put("cdfl",cdfl); //存档分类。如果是0为合同
        JAVATools.SetCmdwritelog(logname,"查看参数:"+paramsmap.toString());
        String workflowid=Util.null2String(propBean.getPropName("lhcd.workflowid"));
        WorkflowCreateHandler workflowCreateHandler=new WorkflowCreateHandler();
        String newrequestid=workflowCreateHandler.WorkflowCreateByRequestMap(ngr, workflowid, title, "0",paramsmap);
        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(lylc);
        uf_xrlccdrz.setModedatacreater(ngr);
        uf_xrlccdrz.setModedatacreatedate(DateUtil.format(new Date(), "yyyy-MM-dd"));
        try {
            boolean tableflag=  TableModelUtil.createtable(uf_xrlccdrz);
        }catch (Exception exo){

        }
        JAVATools.SetCmdwritelog(logname,"联合合同开始调用结束");
        return false;
    }
}
