package com.engine.htxc.workflow.create.autotrigger.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htxc.commonutil.workflow.WorkflowCreate;
import com.engine.htxc.workflow.create.autotrigger.contract.service.AutoApprovalService;
import com.engine.htxc.workflow.create.autotrigger.contract.vo.AutoApproval;
import com.engine.htxc.workflow.create.autotrigger.contract.vo.ContractInfo;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName AutoApprovalService.java
 * @createTime 2022-08-0510:07:00
 */
public class AutoApprovalServiceImpl extends Service implements AutoApprovalService {

    private String workflowid=new PropBean().getPropName("constract.approval.workflowid");
    private String creatorid="";
    private String requestname="";
    @Override
    public int create(AutoApproval autoApproval) {
        if(autoApproval==null){
            return -100;
        }
        creatorid=Util.null2String(autoApproval.creatorid);
        requestname=Util.null2String(autoApproval.requestname);
        if(StringUtils.isBlank(creatorid) || StringUtils.isBlank(requestname)){
            return -101;
        }
        Map<String,Object> map=new HashMap<>(15);
        map.put("ngr",creatorid);
        map.put("szbm",Util.null2String(autoApproval.getSzbm()));
        map.put("szjg",Util.null2String(autoApproval.getSzjg()));
        map.put("jjcd",Util.null2String(autoApproval.getJjcd()));
        map.put("lxfs",Util.null2String(autoApproval.getLxfs()));
        map.put("ngrq",Util.null2String(autoApproval.getNgrq()));
        map.put("nbsh",Util.null2String(autoApproval.getNbsh()));
        map.put("bmld",Util.null2String(autoApproval.getBmld()));
        map.put("sqjh",Util.null2String(autoApproval.getSqjh()));
        map.put("qttzyddx",Util.null2String(autoApproval.getQttzyddx()));
        map.put("shfs",Util.null2String(autoApproval.getShfs()));
        map.put("lkrqsfytsyq",Util.null2String(autoApproval.getLkrqsfytsyq()));
        map.put("xzhtlb",Util.null2String(autoApproval.getXzhtlb()));
        map.put("htlx",Util.null2String(autoApproval.getHtlx()));
        map.put("yz",Util.null2String(autoApproval.getYz()));
        Map<String,String> result= map.entrySet().stream().filter(mapfilter->
         !StringUtils.isBlank(Util.null2String(mapfilter.getValue()))
        ).collect(Collectors.toMap(p->p.getKey(), p->Util.null2String(p.getValue()) ));
        List<ContractInfo> contractlist= autoApproval.getListcontrat();
        List<Map<String,String>> listdetail=new ArrayList<>();
        if(contractlist!=null){
          contractlist.forEach(item->{
              Map<String,Object> map2=JSONObject.parseObject(JSON.toJSONString(item),Map.class);
              if(map2!=null && map2.size()>0) {
                  listdetail.add(map2.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> Util.null2String(p.getValue()))));
              }
           });
        }

        return createworkflow(result,listdetail);
    }

    private int createworkflow(Map<String,String> mainmap, List<Map<String,String>> listmap){
        WorkflowCreate workflowCreate= WorkflowCreate.createWorkflow(p->{
            p.setCreatorId(creatorid);
            p.setWorkflowId(workflowid);
            p.setRequestName(requestname);
            p.setIsNextFlow("");
            p.setRequestMainMap(mainmap);
        }).workflowCreateByRequestMap();
        String requestid= workflowCreate.requestid;
        if(StringUtils.isBlank(requestid)){
            return -1;
        }
        if(listmap!=null && listmap.size()>0){
            workflowCreate.workflowCreatelist("4",listmap);
        }

        return Util.getIntValue(requestid,-1);
    }
}
