package com.engine.htsc.lhhtsq.Serivce.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.lhhtsq.Dao.LhhtsqbaseDao;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.ModeTools;
import weaver.interfaces.htsc.comInfo.PropBean;
import com.engine.htsc.lhhtsq.Serivce.*;
import weaver.interfaces.htsc.cus.HTUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName LhhtEndToOldData.java
 * @createTime 2021年12月15日 15:35:00
 */
public class LhhtEndToOldDataServiceImpl extends Service implements LhhtEndToOldDataService {

    @Override
    public void innerttodata(Map<String, Object> map) {
     String requestid= Util.null2String(map.get("requestid"));
     String tablename= Util.null2String(map.get("tablename"));
     String title=Util.null2String(map.get("title"));
     Map<String,Object>tablemap=  LhhtsqbaseDao.gettablemap(tablename,requestid);
     if(tablemap==null|| tablemap.size()<0){
         return;
     }
    // String sfkfs=Util.null2String(tablemap.get("sfkfs"));
//     if(!"1".equals(sfkfs)){
//         return ;
//     }
     String id=Util.null2String(tablemap.get("id"));

        //获取明细表13的数据
     List<Map<String,Object>> detaillist= getdetail(tablename+"_dt13",id);
//      if(detaillist==null ||detaillist.size()<=0){
//          return ;
//      }
        JSONObject paramsjson=new JSONObject();
        String htmc=Util.null2String(tablemap.get("htmc"));
        String htbh=Util.null2String(tablemap.get("htbh"));
        String htje=Util.null2String(tablemap.get("htje"));
        String lcbh=Util.null2String(tablemap.get("lcbh"));
        String ngr=Util.null2String(tablemap.get("ngr"));
        String szbm=Util.null2String(tablemap.get("szbm"));
        String szjg=Util.null2String(tablemap.get("szjg"));
        String ngrq=Util.null2String(tablemap.get("ngrq"));
        String sfklx=Util.null2String(tablemap.get("sfklx"));
        if( Util.getDoubleValue(htje)<=0){
            return ;
        }

        paramsjson.put("draftDate",ngrq);
        paramsjson.put("requestid",requestid);
        paramsjson.put("workFlowNum",lcbh);
     //   paramsjson.put("workFlowNumber","");
       // paramsjson.put("workFlowStatus",1);
        paramsjson.put("title",title);
        int contractSize=detaillist.size();//分期
        if(contractSize==0){
            contractSize=1;
        }
        String totalPay="0"; //金额
        String totalNotPay=htje;
        paramsjson.put("drafter",JAVATools.gethrmidtoworkcode(ngr));//拟稿人名称
        paramsjson.put("drafterName",JAVATools.getHrmName(ngr));//拟稿人名称
        Map<String,Object> szbmvaluemap=gethrmdepart(szbm);
        if(szbmvaluemap!=null &&szbmvaluemap.size()>0){
            paramsjson.put("drafterDepartment",Util.null2String(szbmvaluemap.get("departmentname")));
            paramsjson.put("drafterDepartmentCode",Util.null2String(szbmvaluemap.get("departmentcode")));
        }

        Map<String,Object> szjgvaluemap=gethrmdepart(szjg);
        if(szjgvaluemap!=null &&szjgvaluemap.size()>0){
            paramsjson.put("ldapOrganization",Util.null2String(szjgvaluemap.get("departmentname")));
            paramsjson.put("ldapOrganizationCode",Util.null2String(szjgvaluemap.get("departmentcode")));
        }


        paramsjson.put("authorizeUserBadges",getdeparment(requestid));

        paramsjson.put("contractName",htmc);
        paramsjson.put("contractNum",lcbh);
        paramsjson.put("contractCost",htje);
        paramsjson.put("contractSize",Util.null2String(contractSize));
        paramsjson.put("totalPay",totalPay);
        paramsjson.put("totalNotPay",totalNotPay);
        getdetailmapvalue(paramsjson);
        for (int i=0;i<detaillist.size();i++) {
            Map<String,Object> detailma=detaillist.get(i);
            paramsjson= getdetailmap(paramsjson,detailma);
        }


        String url=new PropBean().getPropName("lhht.sendoldurl");
        Map<String,String> headermap= new HTUtil().getAppHeader();
        innertmode(tablemap,detaillist);
        //String repstr="";
        String repstr= new HTUtil().httpPostRaw(url,paramsjson.toJSONString(),headermap,null);
        if("".equals(repstr)){
            JAVATools.SetCmdwritelog("联合合同写入老系统失败："+paramsjson.toString());
        }
        try{
          JSONObject jsonObject=  JSONObject.parseObject(repstr);
           String status= Util.null2String(jsonObject.get("status"));
           if("1".equals(status)){

               JAVATools.SetCmdwritelog("联合合同写入老系统成功："+paramsjson.toString());
           }else{
               JAVATools.SetCmdwritelog("联合合同写入老系统失败："+paramsjson.toString()+" repstr:"+repstr);
           }
        }catch (Exception exo){
            JAVATools.SetCmdwritelog("联合合同写入老系统异常："+paramsjson.toString()+" exo:"+exo.toString());

        }
       //拼接期数
        return;
    }
    private JSONObject getdetailmap(JSONObject paramsjson,Map<String,Object> map){
      //  select qs,je,bl from formtable_main_431_dt13

        String qs=Util.null2String(map.get("qs"));
        String je=Util.null2String(map.get("je"));
        String bl=Util.null2String(map.get("bl"));
        switch (qs){
            case "一期":
                paramsjson.put("moneyOne",je);
                paramsjson.put("paymentOne","");
                break;
            case "二期":
                paramsjson.put("moneyTwo",je);
                paramsjson.put("paymentTwo","");
                break;
            case "三期":
                paramsjson.put("moneyThree",je);
                paramsjson.put("paymentThree","");
                break;
            case "四期":
                paramsjson.put("moneyFour",je);
                paramsjson.put("paymentFour","");
                break;
        }
        return paramsjson;
    }


    private JSONObject getdetailmapvalue(JSONObject paramsjson){


                paramsjson.put("moneyOne","");
                paramsjson.put("paymentOne","");

                paramsjson.put("moneyTwo","");
                paramsjson.put("paymentTwo","");

                paramsjson.put("moneyThree","");
                paramsjson.put("paymentThree","");

                paramsjson.put("moneyFour","");
                paramsjson.put("paymentFour","");


        return paramsjson;
    }
    //g

    public List<Map<String,Object>> getdetail(String tablename, String id){
        String sql="select * from "+tablename+ " where 1=1";
        Map<String,String> map=new HashMap<>();
        map.put("mainid",id);
       return  JAVATools.GetTableToListMap(sql,map);
    }


    public void innertmode(Map<String,Object> maintable,List<Map<String,Object>> detaillist){
        PropBean pb=new PropBean();
        Map<String,String> map=new HashMap<>();
        Map<String,String> innertmap=new HashMap<>();
        String requestid=Util.null2String(maintable.get("requestid"));
        String htmc=Util.null2String(maintable.get("htmc"));
        String htbh=Util.null2String(maintable.get("htbh"));
        String htje=Util.null2String(maintable.get("htje"));

        innertmap.put("lcid",requestid);
        innertmap.put("htmc",htmc);
        innertmap.put("htbh",htbh);
        innertmap.put("htze",htje);
        innertmap.put("htqs",Util.null2String(detaillist.size()));
        innertmap.put("hjyf","");
        innertmap.put("hjwf","");

        JSONObject jsonObject = pb.getModeInfoByUUID(pb.getPropName("lhht.modecebuuuid"));
        String modeid=  Util.null2String(jsonObject.get("modeid").toString());

        innertmap.put("formmodeid",modeid);
        List<Map<String,String>>  listdetial=new ArrayList<>();
        detaillist.forEach((detailmap)->{
            String qs=Util.null2String(detailmap.get("qs"));
            String je=Util.null2String(detailmap.get("je"));
            Map<String,String> detmap=new HashMap<>();
            detmap.put("qs",qs);
            detmap.put("yfje",je);
            listdetial.add(detmap);
        });

        ModeTools.innertmode("uf_lhhtxxfkjlb",innertmap,listdetial,1);

     //   JAVATools.Excetesql(map,"uf_lhhtxxfkjlb");
       // select * from uf_lhhtxxfkjlb
    }

    public String getdeparment(String requestid){
        String sql=" select wm_concat_old(   (select  '#' || workcode || '#'  from hrmresource where id= processuser)) as workcode from workflow_currentoperator where  processuser is not null ";
        Map<String,String> mapparams=new HashMap<>();
        mapparams.put("requestid",requestid);
        Map<String,Object> map=JAVATools.GetTableToMap(sql,mapparams);
        if(map==null || map.size()<=0){
            return "";
        }
        String workcode=Util.null2String(map.get("workcode"));
        if("".equals(workcode)){
            return "";
        }
        return workcode.replace(",",";");
    }

    public Map<String,Object> gethrmdepart(String departid){
        if("".equals(departid)){
            return null;
        }
        String sql="select departmentname,departmentcode from hrmdepartment where 1=1 ";
        Map<String,String> paramsmap=new HashMap<>();
        paramsmap.put("id",departid);
        return JAVATools.GetTableToMap(sql,paramsmap);
    }
}
