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

import com.engine.core.impl.Service;
import com.engine.htsc.jgxt.service.JgxtsdbmtodetailServer;
import com.engine.htsc.wdzx.service.WdzxmaintodetailServer;
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 WdzxmaintodetailServerImp extends Service implements WdzxmaintodetailServer {

    @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,ldsp,xgbm 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 xgbmvalue=Util.null2String(tablemap.get("xgbm"));
        String id=Util.null2String(tablemap.get("id"));
        String ldshvalue=Util.null2String(tablemap.get("ldsp"));

        if("".equals(id)){
            JAVATools.SetCmdwritelog("文档中心写入明细表失败",sql);
            return ;
        }

        updateletetable(id,tablename,xgbmvalue,id);

        updateleteleader(id, tablename, ldshvalue, id);



    }

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

        Arrays.stream(xgbmvalue.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("xgbm",model);
                insertmap.put("bmfzr",bmfzr);
                insertmap.put("sfxz","1");

                JAVATools.Excetesql(insertmap,tablename+"_dt5");
            }

        });

    }



    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));
                insertmap.put("ssjg", JAVATools.gethrmidtojgid(model));
                insertmap.put("sfxz","1");
                JAVATools.Excetesql(insertmap, tablename + "_dt6");
            }
        });

    }

    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);

    }

}
