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

import com.engine.core.impl.Service;
import com.engine.htsc.jgxt.service.JgxtsdbmtodetailServer;
import com.mysql.cj.x.protobuf.MysqlxExpr;
import jxl.read.biff.Record;
import oracle.sql.JAVA_STRUCT;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName JgxtsdbmtodetailServerImp.java
 * @createTime 2021年09月22日 18:15:00
 */
public class JgxtsdbmtodetailServerImp extends Service implements JgxtsdbmtodetailServer {

    @Override
    public void createtodetail(Map<String, Object> map) {
        String requestid= Util.null2String(map.get("requestid"));
        String tablename=Util.null2String(map.get("tablename"));
        String requestname=Util.null2String(map.get("requestname"));

        String sql="select id,sdbm,ldsp,hqbm from "+tablename+ " where 1=1 ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("requestid",requestid);
        Map<String,Object> tablemap=JAVATools.GetTableToMap(sql,wheremap);
        if(tablemap==null||tablemap.size()<=0){
            JAVATools.SetCmdwritelog("监管写入明细表2失败",sql);
            return ;
        }
        String sdbmvalue=Util.null2String(tablemap.get("sdbm"));
        String id=Util.null2String(tablemap.get("id"));
        String ldspvalue=Util.null2String(tablemap.get("ldsp"));
        String hqbmvalue=Util.null2String(tablemap.get("hqbm"));
        if("".equals(id)){
            JAVATools.SetCmdwritelog("监管写入明细表失败",sql);
            return ;
        }
        //if(!"".equals(sdbmvalue)){
            //首先先删除明细表
        updateletetable(id,tablename,sdbmvalue,id);
            //插入明细表

      //  }
       // if(!"".equals(ldspvalue)) {
            //部门领导操作
        updateleteleader(id, tablename, ldspvalue, id);

      //  }
      //  if(!"".equals(hqbmvalue)){
        updatecountersign(id, tablename,hqbmvalue,id);

       // }

    }

    private void updateletetable(String mainid,String tablename,String sdbmvalue,String id){
        if("".equals(tablename) || "".equals(mainid)) {
            return;
        }
        List newdeptid=new ArrayList();
      List<Map<String,Object>> list= getdetailtablevalue(mainid,tablename +"_dt10");
        if(list!=null ||list.size()>0){
        list.forEach((Map<String,Object> item)->{
           String sdbm= Util.null2String(item.get("sdbm"));
           String detailid= Util.null2String(item.get("id"));
           if(( "".equals(sdbmvalue) || (","+sdbmvalue+",").indexOf(","+sdbm+",")<0) && !"".equals(detailid)){
               //删除操作
               deldetailtablevalue(tablename +"_dt10",detailid);
           }else{
               newdeptid.add(sdbm);
           }
        });
        }

        Arrays.stream(sdbmvalue.split(",")).forEach((model)->{
            if((newdeptid.size()<=0 ||newdeptid.indexOf(model)<0 ) && !"".equals(model) ){
                Map<String,String> insertmap=new HashMap<>();
                String bmfzr=JAVATools.Getdeptdirectors(model);
                String djry=getdjry(model);
                insertmap.put("mainid",id);
                insertmap.put("sdbm",model);
                insertmap.put("bmfzr",bmfzr);
                insertmap.put("djry",djry);
                JAVATools.Excetesql(insertmap,tablename+"_dt10");
            }

        });

    }



    private void updateleteleader(String mainid,String tablename,String ldspvalue,String id){
        if("".equals(tablename) || "".equals(mainid)) {
            return;
        }
        List newuserids=new ArrayList();
        List<Map<String,Object>> list= getdetailtablevalue(mainid,tablename +"_dt6");
        if(list!=null && list.size()>0){
            list.forEach((Map<String,Object> item)->{
                String ry= Util.null2String(item.get("ry"));
                String detailid= Util.null2String(item.get("id"));
                if(( "".equals(ldspvalue) || (","+ldspvalue+",").indexOf(","+ry+",")<0) && !"".equals(detailid)){
                    //删除操作
                    deldetailtablevalue(tablename +"_dt6",detailid);
                }else{
                    newuserids.add(ry);
                }
            });
        }

        Arrays.stream(ldspvalue.split(",")).forEach((model) -> {
            if((newuserids.size()<=0 ||newuserids.indexOf(model)<0 ) && !"".equals(model) ) {
                Map<String, String> insertmap = new HashMap<>();
                insertmap.put("mainid", id);
                insertmap.put("ry", model);
                insertmap.put("bm", JAVATools.gethrmidtodeptid(model));
                JAVATools.Excetesql(insertmap, tablename + "_dt6");
            }
        });
//        RecordSet rs=new RecordSet();
//        String sql="delete from "+tablename +"_dt6 where mainid=?";
//        JAVATools.SetCmdwritelog("监管删除领导",sql+"params"+mainid);
//        rs.executeQuery(sql,mainid);
    }

    private void updatecountersign(String mainid,String tablename,String hqbmvalue,String id){
        if("".equals(tablename) || "".equals(mainid)) {
            return;
        }
        List newdeptid=new ArrayList();
        List<Map<String,Object>> list= getdetailtablevalue(mainid,tablename +"_dt2");
        if(list!=null && list.size()>0){
            list.forEach((Map<String,Object> item)->{
                String hqbm= Util.null2String(item.get("hqbm"));
                String detailid= Util.null2String(item.get("id"));
                if(( "".equals(hqbmvalue) || (","+hqbmvalue+",").indexOf(","+hqbm+",")<0) && !"".equals(detailid)){
                    //删除操作
                    deldetailtablevalue(tablename +"_dt2",detailid);
                }else{
                    newdeptid.add(hqbm);
                }
            });
        }
        Arrays.stream(hqbmvalue.split(",")).forEach((model) -> {
            if((newdeptid.size()<=0 ||newdeptid.indexOf(model)<0 ) && !"".equals(model) ) {
                Map<String, String> insertmap = new HashMap<>();
                insertmap.put("mainid", id);
                insertmap.put("hqbm", model);
                insertmap.put("bmfzr", JAVATools.Getdeptdirectors(model));
                insertmap.put("fkgry", getfkgry(model));
                String fkgwry = new PropBean().getPropName("jgxt.fs.hggwgwid");
                insertmap.put("hgfkgw", fkgwry);
                JAVATools.Excetesql(insertmap, tablename + "_dt2");
            }
        });
//
//        RecordSet rs=new RecordSet();
//        String sql="delete from "+tablename +"_dt2 where mainid=?";
//        JAVATools.SetCmdwritelog("监管删除会签",sql+"params"+mainid);
//        rs.executeQuery(sql,mainid);
    }

    private  String getdjry(String deptid){
        if("".equals(deptid)){
            return "";
        }

        String sql="select bm,djr from uf_djrpz";
        List<Map<String, Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list.size()<=0){
            return "";
        }
        for (Map<String, Object>  map : list){
            String bm=Util.null2String(map.get("bm"));
            if(bm.equals(deptid)){
                return Util.null2String(map.get("djr"));
            }
        }
        return "";
    }


    private  String getfkgry(String deptid){
        if("".equals(deptid)){
            return "";
        }
        String fkgwry= new PropBean().getPropName("jgxt.fs.hggwgwid");
        String sql="select jg,ry,xzgw from uf_lcgwry where xzlc='jgxtsq'  and xzgw='"+fkgwry+"'";
        List<Map<String, Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list.size()<=0){
            return "";
        }
        for (Map<String, Object>  map : list){
            String jg=Util.null2String(map.get("jg"));
            if(jg.equals(deptid)){
                return Util.null2String(map.get("ry"));
            }
        }
        return "";
    }

    //
    private List getdetailtablevalue(String id,String tablename){
        if("".equals(tablename) || "".equals(id)){
            return null;
        }
        String sql="select * from "+tablename +" where 1=1 ";
        Map<String,String> map=new HashMap<>();
        map.put("mainid",id);
        return JAVATools.GetTableToListMap(sql,map);
    }

    private  void deldetailtablevalue(String tablename,String id){
        RecordSet rs=new RecordSet();
        String sql="delete from "+tablename +" where id=?";
        JAVATools.SetCmdwritelog("监管删除明细表操作"+tablename,sql+"params"+id);
        rs.executeQuery(sql,id);

    }

}
