package com.engine.htsc.seal.xctoeseal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.seal.xctoeseal.dao.XcCreateWfDao;
import com.engine.htsc.seal.xctoeseal.service.XcCreateWfService;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.XcTools;
import weaver.interfaces.htsc.GlobatTools.workflow.WorkflowCreateHandler;
import weaver.interfaces.htsc.comInfo.PropBean;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName SealXccreateWorkflowService
 * @Author K1810015
 * @Date 2022/3/3 16:53
 * Version 1.0
 */
public class XcCreateWfServiceImpl extends Service implements XcCreateWfService {

    /**
     * @Author K1810015
     * @Date 2022/3/3 16:53
     * @Remark新创环境下出发用印登记单
     */
    @Override
    public JSONObject createworkflow(JSONObject json){
        String logname="新创环境出发用印登记单：";
        JAVATools.SetCmdwritelog(logname+"查看请求数据",json.toJSONString());
        Map<String, String> mapstr = new HashMap<>(3);
        WorkflowCreateHandler workflowCreateHandler = new WorkflowCreateHandler();
        String yz=Util.null2String(json.get("yz"));
        String ngr=Util.null2String(json.get("ngr"));
        String requestid=Util.null2String(json.get("requestid"));
        String requestname=Util.null2String(json.get("requestname"));
        String szbm=Util.null2String(json.get("szbm"));
        String szjg=Util.null2String(json.get("szjg"));
        String lylx=Util.null2String(json.get("lylx"));
        String lxfs=Util.null2String(json.get("lxfs"));
        String ngsj=Util.null2String(json.get("ngsj"));
        String jjcd=Util.null2String(json.get("jjcd"));
        String fs=Util.null2String(json.get("fs"));
        String yywj=Util.null2String(json.get("yywj"));
        String zwpdf=Util.null2String(json.get("zwpdf"));
        String yzid=XcCreateWfDao.getyzbh(yz);
        if(StringUtils.isBlank(yzid)){
            return null;
        }
        mapstr.put("yz", yzid);
        mapstr.put("sfsdfq","1");
        mapstr.put("lclxbh","yydjb");
        if(!StringUtils.isBlank(ngr)){
            mapstr.put("ngr",ngr);
        }
        if(!StringUtils.isBlank(requestid)){
            mapstr.put("xclc",Util.null2String(requestid));
        }
        if(!StringUtils.isBlank(szbm)){
            mapstr.put("szbm",Util.null2String(szbm));
        }
        if(!StringUtils.isBlank(szjg)){
            mapstr.put("szjg",Util.null2String(szjg));
        }
        if(!StringUtils.isBlank(lxfs)){
            mapstr.put("lxfs",Util.null2String(lxfs));
        }
        if(!StringUtils.isBlank(jjcd)){
            mapstr.put("hjcd",Util.null2String(jjcd));
        }
        if(!StringUtils.isBlank(ngsj)){
            mapstr.put("ngsj",Util.null2String(ngsj));
        }
        if(!StringUtils.isBlank(lylx)){
            mapstr.put("lylx",Util.null2String(lylx));
        }

        String filedistinct ="";

        User usr=new User(Util.getIntValue(ngr));
        List<Map<String, String>> detaiMaplist = new ArrayList<>();
        List<String> strlist=new ArrayList<>();

        Arrays.stream(yywj.split(",")).forEach(value->{
            Map<String, String> detailmap = new HashMap<>(6);

            int newdocid=XcTools.getdocid(XcCreateWfDao.getFileInfo(value),usr);
            if(newdocid<=0){
                return;
            }
            strlist.add(Util.null2String(newdocid));
            detailmap.put("wjmc", Util.null2String(newdocid));
            if(value.equals(zwpdf)){
                detailmap.put("sfzw","1");
            }else{
                detailmap.put("sfzw","0");
            }
            detailmap.put("wjly", "0");
            detailmap.put("fwdyfs", fs);
            detailmap.put("wlyyfs", fs);
            detailmap.put("dsmdy", "1");
            detaiMaplist.add(detailmap);
        });
        mapstr.put("yywj", strlist.stream().distinct().collect(Collectors.joining(",")));
        String tgtfw_workflownum = new PropBean().getPropName("yydjd_workflownum");
        String workflowid = new PropBean().getPropName("yydjd.workflowid");
        String requestName = requestname+"--"+workflowCreateHandler.bulidWorkflowTitle(workflowid, Util.null2String(usr.getUID()));
        requestName= requestName.replace("用印登记单","用印单");
        JAVATools.SetCmdwritelog(logname+"开始出发触发用印登记单");
        String yydjdmainrequestid=  workflowCreateHandler.WorkflowCreateByRequestMap(Util.null2String(usr.getUID()), workflowid, requestName, "0", mapstr,"1",detaiMaplist);
        JSONObject json2=new JSONObject();
        json2.put("id",yydjdmainrequestid);
        json2.put("name",requestName);
        JAVATools.SetCmdwritelog(logname+"用印登记单返回参数："+yydjdmainrequestid);
        return json2;
    }


}



