package com.engine.htsc.jgxt.service.imp;

import cn.hutool.core.date.DateUtil;
import com.engine.core.impl.Service;
import com.engine.htsc.jgxt.service.JgxtDataDetailToOtherService;
import com.engine.htsc.jgxt.service.JgxtDataToOtherService;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.HtmlUtil;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName JgxtDataToOtherServiceImp.java
 * @createTime 2021年11月10日 15:59:00
 */
public class JgxtDataDetailToOtherServiceImp extends Service implements JgxtDataDetailToOtherService {

    @Override
    public void innertdetaildata(Map<String, Object> map) {
       PropBean propbean=new PropBean();
        String requestid= Util.null2String(map.get("requestid"));
        String tablename= Util.null2String(map.get("tablename"));
        if("".equals(requestid) || "".equals(tablename)){
            return ;
        }

        String sql="select * from "+tablename+" where 1=1 ";
        Map<String,String> whereparams=new HashMap<>();
        whereparams.put("requestid",requestid);
        Map<String,Object> mapdata=JAVATools.GetTableToMap(sql,whereparams);
        String id=Util.null2String(mapdata.get("id"));
        String lcbh=Util.null2String(mapdata.get("lcbh"));
        if("".equals(id) && "".equals(lcbh)){
            return ;
        }
        Map<String,Object> detailmap=new HashMap<>();
        detailmap.put("id",id);
        detailmap.put("tablename",tablename);
        detailmap.put("workflowname",lcbh);

        t_customerinfoinnert(detailmap);
        t_reply_qualityinnert(detailmap);
       //


    }

    private void t_reply_qualityinnert(Map<String,Object> map){
        //t_customerinof 客户信息
        String id=Util.null2String(map.get("id"));
        String tablename=Util.null2String(map.get("tablename"));
        String workflowname=Util.null2String(map.get("workflowname"));
        String sql="select * from "+tablename+"_dt8 where 1=1";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("mainid",id);
        List<Map<String,Object>> list=JAVATools.GetTableToListMap(sql,wheremap);
       if(list!=null && list.size()>0){

           list.forEach((item)->{
               Map<String,Object> modevalue=item;
               String sfcq= Util.null2String(modevalue.get("sfcq"));  //是否超期
               String thcsyc=Util.null2String(modevalue.get("thcsyc")); //退回次数（严重）
               String thyyyc=Util.null2String(modevalue.get("thyyyc")); //退回原因（严重）
               String thcsyb=Util.null2String(modevalue.get("thcsyb")); //退回次数（一般）
               String thyyyb= Util.null2String(modevalue.get("thyyyb")); //退回原因（一般）
               String sdf= Util.null2String(modevalue.get("sdf")); //送达方
               String seriousreason=selectitem(thyyyc,tablename,"thyyyc");
               String pattern = "\\（\\w\\）";
               String commonlyreason=selectitem(thyyyb,tablename,"thyyyb");

               if(!"".equals(seriousreason)){
                   seriousreason=seriousreason.replace(",",";").replaceAll(pattern,"");
                   // seriousreason= Pattern.compile(pattern).matcher(seriousreason).matches();
               }
               if(!"".equals(commonlyreason)) {
                   commonlyreason = commonlyreason.replace(",", ";").replaceAll(pattern, "");
               }
               Map<String,String> detailmap=new HashMap<>();
               detailmap.put("workflowname",workflowname);
               detailmap.put("shiptoparty",getdeptname(sdf));
               detailmap.put("overdue",selectitem(sfcq,tablename,"sfcq"));
               detailmap.put("serioustimes",thcsyc);
               detailmap.put("seriousreason",seriousreason);
               detailmap.put("commonlytimes",thcsyb);
               detailmap.put("commonlyreason",commonlyreason);
               innert(detailmap,"t_reply_quality");
           });

       }



    }


    private void t_customerinfoinnert(Map<String,Object> map){
        //t_customerinof 客户信息
        String id=Util.null2String(map.get("id"));
        String tablename=Util.null2String(map.get("tablename"));
        String workflowname=Util.null2String(map.get("workflowname"));
        String sql="select * from "+tablename+"_dt7 where 1=1";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("mainid",id);
        List<Map<String,Object>> list=JAVATools.GetTableToListMap(sql,wheremap);
        if(list!=null && list.size()>0) {
            list.forEach((item) -> {
                Map<String, Object> modevalue = item;
                String tzhjskhsj = Util.null2String(modevalue.get("tzhjskhsj"));  //通知或警示客户时间
                String tzkhgzryxm = Util.null2String(modevalue.get("tzkhgzryxm")); //通知客户工作人员姓名
                String khxm = Util.null2String(modevalue.get("khxm")); //客户姓名
                String dkhcqdcsjbzsm = Util.null2String(modevalue.get("dkhcqdcsjbzsm")); //对客户采取的措施及备注说明
                String khfk = Util.null2String(modevalue.get("khfk")); //客户反馈
                String dqczr = Util.null2String(modevalue.get("dqczr")); //客户反馈
                Map<String, String> detailmap = new HashMap<>();
                detailmap.put("username", JAVATools.getHrmName(dqczr));
                detailmap.put("workflowname", workflowname);
                detailmap.put("cusname", khxm);
                detailmap.put("noticcus", tzhjskhsj);
                detailmap.put("noticcusname", JAVATools.getHrmName(tzkhgzryxm));
                detailmap.put("measure", dkhcqdcsjbzsm);
                detailmap.put("cusfeedback", khfk);
                innert(detailmap, "t_customerinof");
            });
        }
    }

    private  int innert(Map<String,String> map,String tablename){
        if(map==null || map.size()<=0){
            return 0;
        }
        List<String> addlistkey =new ArrayList<>();
        List<String> valuelistkey =new ArrayList<>();
        for (String key:map.keySet()) {
            String value=Util.null2String(map.get(key));
            if(!"".equals(value)){
                addlistkey.add(key);
                valuelistkey.add("'"+value+"'");
            }
        }
       String keybody= addlistkey.stream().collect(Collectors.joining(","));
       String valuebody= valuelistkey.stream().collect(Collectors.joining(","));
        int id = getmaxid();
       String sql= "insert into "+tablename+"(id,"+keybody+") values("+id+","+valuebody+")";
        JAVATools.SetCmdwritelog("监管协同下游数据:");
        JAVATools.SetCmdwritelog(sql);
        RecordSetDataSource rsd=new RecordSetDataSource("Jgxt");
       if(!rsd.execute(sql)){
           JAVATools.SetCmdwritelog("监管协同下游数据写入失败");
           return 0;
       }
       return id;
    }


    private String selectitem(String value,String tablename,String fieldname  ){
        String sql=" select selectvalue as id,selectname from workflow_selectitem t where t.fieldid in ( " +
                "  select id from workflow_billfield t where t.billid in （ select id from workflow_bill where tablename='"+tablename+"' ） and rownum=1 and t.fieldname='"+fieldname+"' " +
                " )";
        List<Map<String,Object> > list= JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return "";
        }
        List<String> liststr=new ArrayList<>();
        List<String> valuearray= Arrays.stream(value.split(",")).collect(Collectors.toList());
        list.forEach((Map<String,Object> model)->{
            if(valuearray.contains(Util.null2String(model.get("id"))) ){
                liststr.add(Util.null2String(model.get("selectname")));
            }
        });
        if(liststr==null && liststr.size()<=0){
            return "";
        }else{
            return  liststr.stream().collect(Collectors.joining(","));
        }
    }

    private  String getdeptname(String value){
        if("".equals(value)){
            return "";
        }
        String sql="select  wm_concat_old(t.departmentmark) as deptnames from hrmdepartment t where t.id in ("+value+") ";
        Map<String,Object > map=JAVATools.GetTableToMap(sql,null);
        if(map==null && map.size()<=0){
            return "";
        }
        return Util.null2String(map.get("deptnames"));
    }


    public int getmaxid(){
      String sql="select JGXT_SEQ.nextval as id from dual where rownum = 1";
      int id=0;
      RecordSetDataSource rsd=new RecordSetDataSource("Jgxt");
      rsd.executeSql(sql);
       if(rsd.getCounts()>0){
           rsd.next();
           id= rsd.getInt("id");
       }
       return id;
    }




}

