package com.engine.htsc.ReimbursementFlow.Dao;

import com.engine.htsc.ReimbursementFlow.model.UfZbtybxsp;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

public class UfZbbxandclsplDaoImpl {


    public List<UfZbtybxsp> getUSerToUfzbtybxsp(Map<String,String> userMap,String numbertable) {
        RecordSet rs=new RecordSet();
        List list=new ArrayList();
        for (String key:userMap.keySet()){
            String sql="select * from uf_zbbxandclspl where  sslc=? and cxrjs = ? and cxsplry=? ";
            System.out.println("select * from uf_zbbxandclspl where  sslc='" + numbertable + "' and cxrjs = '" + userMap.get(key) + "' and cxsplry='" + key + "' ");
            rs.executeQuery(sql,numbertable,userMap.get(key),key);

            while(rs.next()){
                UfZbtybxsp ufZbtybxs=new UfZbtybxsp();
                String[] colunmn=rs.getColumnName();
                Map<String,Object> map=new HashMap<>();
                for (String colvalue:colunmn){
                    //  rs.getString();
                    map.put(colvalue,rs.getString(colvalue));
                }
                list.add(JAVATools.getMapToClass(map,UfZbtybxsp.class));
            }
        }

        return list;
    }


    public Map getAllTables(String reqid,String tablename,String detailid) {
        String sqlmain="select * from "+tablename +" where 1=1 ";
        Map<String,String> map=new HashMap<>();
        map.put("requestid",reqid);
        Map<String,Object> mapmain= JAVATools.GetTableToMap(sqlmain,map);
        if(!JAVATools.ismapisnotnull(mapmain)){
            //数据获取为空
            return null;
        }
        String id= Util.null2String(mapmain.get("id").toString());
        if("".equals(id)){
            return null;
        }
        List detailistmap=new ArrayList();
        if(detailid.split(",").length>=1){
            for (String detailValue:detailid.split(","))
            {
                String sqldetail="select * from "+tablename+"_dt"+detailValue+" where mainid="+id;
                detailistmap.add(JAVATools.GetTableToListMap(sqldetail,null));
            }

        }
        mapmain.put("listdata",detailistmap);

        return mapmain;

    }


    public Map<String, Object> GetTablesRelation(String number) {
        RecordSet rs=new RecordSet();
        String sql="select tjbds,zbbds from uf_zbbxandclsplgx where bglzd='"+number+"' and sfjssj=0 ";
        rs.execute(sql);
        Map<String,Object> map=new HashMap<>();
        while(rs.next()){
            String colunmn[]=rs.getColumnName();
            map.put(rs.getString("tjbds"),rs.getString("zbbds"));
        }
        return map;


    }



    public List<Map<String, Object>> GetTJRelation(String number) {

        RecordSet rs=new RecordSet();
        String sql="select zdz,sfszb,qzlx from uf_zbbxandclsplcxtj where gllc='"+number+"'";
        rs.execute(sql);
        List<Map<String,Object>> listmap=new ArrayList<>();
        while(rs.next()){
            String colunmn[]=rs.getColumnName();
            Map<String,Object> map=new HashMap<>();
            for (String colvalue:colunmn){
                //  rs.getString();
                map.put(colvalue.toLowerCase(),rs.getString(colvalue));
            }
            listmap.add(map);
        }
        return listmap;

    }


    public List<String> GetTJTofieldid(String number) {

        RecordSet rs=new RecordSet();
        List<String> list=new ArrayList<>();
        String sql="select zbbds from uf_zbbxandclsplgx where bglzd='"+number+"' and sfjssj=0 " ;
        rs.execute(sql);
        List<Map<String,Object>> listmap=new ArrayList<>();
        while(rs.next()){
            if(!"".equals(Util.null2String(rs.getString("zbbds")))){
                list.add(Util.null2String(rs.getString("zbbds")));
            }


        }
        return list;

    }


    public String GetFykjtoHrmid(String deptid,String number) {
        RecordSet rs=new RecordSet();
        String hrmid="";
        if("".equals(deptid)||"".equals(number)){
            return hrmid;
        }
        String sql="select hjry,glbm from uf_zbkjpz  where lx like '%"+number+"%' and glbm in ("+deptid+")";
        rs.execute(sql);
        while(rs.next()){
            String hrmvalue=rs.getString("hjry");
            if(!"".equals(hrmvalue)){
                //因为需求需要最终人员累加去重，所以不做去重判断
                hrmid+=hrmid==""?hrmvalue:","+hrmvalue;
            }
        }
        return hrmid;
    }

    public List<Map<String, Object>> GetTablesRelation_old(String number) {
        RecordSet rs=new RecordSet();
        String sql="select * from uf_zbbxandclsplgx where bglzd='"+number+"' and sfjssj=0 ";
        rs.execute(sql);
        List<Map<String,Object>> listmap=new ArrayList<>();
        while(rs.next()){
            String colunmn[]=rs.getColumnName();
            Map<String,Object> map=new HashMap<>();
            for (String colvalue:colunmn){
                //  rs.getString();
                map.put(colvalue,rs.getString(colvalue));
            }
            listmap.add(map);
        }
        return listmap;
    }


    public Map<String,String> getSelectValue(String number){
        RecordSet rs=new RecordSet();
        String sql="select  tjbds,zbbds from uf_zbbxandclsplgx where bglzd='"+number+"' and sfjssj=1";
        rs.execute(sql);
        Map<String,String> map=new HashMap<>();
        while(rs.next()){
            map.put(rs.getString("tjbds"),rs.getString("zbbds"));
        }
        return  map;
    }



    //

    public Map GetSelectModel(List maplistdetail,String unnuunmber) {
        Map<String,Object> mapmain=new HashMap<>();
        // List<Map<String,Object>> maplist;
        List mapdetaillist=new ArrayList();
        List<String> listfieldids= GetTJTofieldid(unnuunmber);
        for (Object maplistdetailObj :maplistdetail){
            List<Map<String,Object>> maplist=(List<Map<String, Object>>) maplistdetailObj;
            Map<String,Object> mapdetailids=new HashMap<>();
            for(Map<String,Object> webmap : maplist){

                for (String fileid:listfieldids){

                    if(!"".equals(fileid)){
                        if(null!=webmap.get(fileid) && !"".equals(webmap.get(fileid))){
                            //判断是主表还是子表
                            if ("0".equals(Util.null2String(webmap.get("isdetail").toString()))) {
                                //将数据插入到主表中
                                mapmain.put(fileid,webmap.get(fileid));
                            }else{
                                mapdetailids.put(fileid,webmap.get(fileid));
                                //插入子表
                            }
                        }
                    }

                }

            }
            if(mapdetailids.size()>0) {
                mapdetaillist.add(mapdetailids);
            }
        }





        List list=new ArrayList();
        list.add(mapdetaillist);
        mapmain.put("listdata",mapdetaillist);
        return mapmain;

    }


}
