<%@ page import="java.util.Map" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

    String sqdw = request.getParameter("sqdw");
    List<Map<String,String>> datalist = new ArrayList<Map<String,String>>();
    List<Map<String,String>> parentList =new ArrayList<Map<String,String>>();
    RecordSet rs = new RecordSet();
    if(StringUtils.isNotBlank(sqdw)){


        String sql =" select t.id,t.sqdw as parentid,p.gdmc as name,t.cgbl,k.selectname as gdlx\n" +
                    " from uf_gdxx t\n" +
                    " inner join uf_gd p on p.id = t.cggdx\n" +
                    " left join (\n" +
                    "   select selectvalue,selectname \n" +
                    "   from workflow_selectitem \n" +
                    "   where fieldid in(\n" +
                    "       select id from workflow_billfield \n" +
                    "       where billid in( select id from workflow_bill where tablename='uf_gd' ) and fieldname='gdlx' \n" +
                    "       and detailtable  is null\n" +
                    "   )  \n" +
                    " ) k on k.selectvalue = p.gdlx\n" +
                    " where t.sqdw = '"+sqdw+"'" ;

        rs.executeQuery(sql);
        while (rs.next()){
            String id = rs.getString("id");
            String name = rs.getString("name");
            String cgbl = rs.getString("cgbl");
            String gdlx = rs.getString("gdlx");

            Map<String,String> map = new HashMap<String,String>();
            map.put("id",id);
            map.put("parentid","T"+sqdw);
            map.put("name",name);
            map.put("cgbl",cgbl);
            map.put("gdlx",gdlx);
            datalist.add(map);
            parentList.add(map);

        }

        if(datalist.size()>0){
            queryShareHolder(datalist,parentList);
        }

        sql =" select id,'' as parentid,gdmc as name,'' as cgbl,'' as gdlx from \n" +
                " uf_gd\n" +
                " where id = '" + sqdw + "'";
        rs.executeQuery(sql);
        while (rs.next()){
            String id = rs.getString("id");
            String name = rs.getString("name");
            String cgbl = rs.getString("cgbl");
            String gdlx = rs.getString("gdlx");

            Map<String,String> map = new HashMap<String,String>();
            map.put("id","T"+id);
            map.put("parentid","");
            map.put("name",name);
            map.put("cgbl",cgbl);
            map.put("gdlx",gdlx);
            datalist.add(map);

        }
    }




//    for(int i=0;i<dataList.size();i++){
//        String dataId = dataList.get(i);
//
//        List<Map<String,String>> holderList = queryShareHolderByParent(dataId);
//        if(holderList.size()>0){
//            list.addAll(holderList);
//        }
//    }

    JSONObject dataJson = new JSONObject();
    dataJson.put("data",datalist);

%>

<%=dataJson.toJSONString() %>


<%!
    public List<Map<String,String>> queryShareHolderByParent(String parentId){
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        RecordSet rs = new RecordSet();
        if(StringUtils.isNotBlank(parentId)){

            String sql =" select t.id,t.sqdw as parentid,p.gdmc as name,t.cgbl,k.selectname as gdlx\n" +
                        " from uf_gdxx t\n" +
                        " inner join uf_gd p on p.id = t.cggdx\n" +
                        " left join (\n" +
                        "   select selectvalue,selectname \n" +
                        "   from workflow_selectitem \n" +
                        "   where fieldid in(\n" +
                        "       select id from workflow_billfield \n" +
                        "       where billid in( select id from workflow_bill where tablename='uf_gd' ) and fieldname='gdlx' \n" +
                        "       and detailtable  is null\n" +
                        "   )  \n" +
                        " ) k on k.selectvalue = p.gdlx\n" +
                        " where t.sqdw in ( select cggdx from uf_gdxx where id  = '"+parentId+"' ) " ;

            rs.executeQuery(sql);
            while (rs.next()){
                String id = rs.getString("id");
                String name = rs.getString("name");
                String cgbl = rs.getString("cgbl");
                String gdlx = rs.getString("gdlx");

                Map<String,String> map = new HashMap<String,String>();
                map.put("id",id);
                map.put("parentid",parentId);
                map.put("name",name);
                map.put("cgbl",cgbl);
                map.put("gdlx",gdlx);
                list.add(map);
            }
        }
        return list;
    }


    public List<Map<String,String>> queryShareHolder(List<Map<String,String>> datalist,List<Map<String,String>> parentList){

        for(int i=0;i<parentList.size();i++){
            Map<String,String> parentMap = parentList.get(i);
            String parentId = parentMap.get("id");
            if(StringUtils.isNotBlank(parentId)){
                List<Map<String,String>> holderList = queryShareHolderByParent(parentId);
                if(holderList.size()>0){
                    datalist.addAll(holderList);
                    queryShareHolder(datalist,holderList);
                }
            }
        }
        return datalist;
    }

%>

