<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.domain.TreeNode" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

    String dwxx = request.getParameter("sqdw");
    String gd = request.getParameter("gd");

    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

    RecordSet rs = new RecordSet();

    List<String> nodeList = new ArrayList<String>();

    if(StringUtils.isNotBlank(dwxx)){
        String sqdw = "";
        String sql = " select id from uf_gd where dwxx='"+dwxx+"' ";
        rs.executeQuery(sql);
        if(rs.next()){
            sqdw = Util.null2String(rs.getString("id"));
        }
        if(StringUtils.isNotBlank(sqdw)){
            TreeNode root = new TreeNode("T"+sqdw);
            getChildrenByRoot(root,sqdw,nodeList);
            Map<String,Object> dataMap = recurTree(root);
            dataMap.put("id","T"+sqdw);
            list.add(dataMap);
        }

        if(nodeList.size()>0){
            for(int i=0;i<nodeList.size();i++){
                String gdxxId = nodeList.get(i);
                System.out.println("gdxxId:"+gdxxId);
                if(StringUtils.isNotBlank(gdxxId)){
                    sql = " select cggdx from uf_gdxx where id = "+gdxxId;
                    rs.executeQuery(sql);
                    if(rs.next()){
                        String cggdx = Util.null2String(rs.getString("cggdx"));
                        if(StringUtils.isNotBlank(cggdx)){
                            Map<String,Object> nodeMap =  getRootDataByNode(cggdx);
                            nodeMap.put("id",gdxxId);
                            list.add(nodeMap);
                        }
                    }
                }
            }
        }
    }else if(StringUtils.isNotBlank(gd)){
        String sql =" select id from uf_gd where id = '" + gd + "'";
        String gdid = "";
        rs.executeQuery(sql);
        if (rs.next()) {
            gdid = rs.getString("id");
        }

        if(StringUtils.isNotBlank(gdid)){
            TreeNode root = new TreeNode("T"+gdid);
            getChildrenByRoot(root,gd,nodeList);
            Map<String,Object> dataMap = recurTree(root);
            dataMap.put("id","T"+gd);
            list.add(dataMap);
        }

        if(nodeList.size()>0){
            for(int i=0;i<nodeList.size();i++){
                String gdxxId = nodeList.get(i);
                System.out.println("gdxxId:"+gdxxId);
                if(StringUtils.isNotBlank(gdxxId)){
                    sql = " select cggdx from uf_gdxx where id = "+gdxxId;
                    rs.executeQuery(sql);
                    if(rs.next()){
                        String cggdx = Util.null2String(rs.getString("cggdx"));
                        if(StringUtils.isNotBlank(cggdx)){
                            Map<String,Object> nodeMap =  getRootDataByNode(cggdx);
                            nodeMap.put("id",gdxxId);
                            list.add(nodeMap);
                        }
                    }
                }
            }
        }
    }

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

%>

<%=dataJson.toJSONString() %>


<%!


//    public void getRootData(String sqdw){
//        RecordSet rs = new RecordSet();
//        String gdid = "";
//        String sql =" select id from uf_gd where id = '" + sqdw + "'";
//        rs.executeQuery(sql);
//        if (rs.next()) {
//            gdid = rs.getString("id");
//        }
//        if(StringUtils.isNotBlank(gdid)){
//            TreeNode root = new TreeNode("T"+gdid);
//            List<String> nodeList = new ArrayList<String>();
//            getChildrenByRoot(root,gdid,nodeList);
//            System.out.println("nodeList:"+nodeList.toString());
//            recurTree(root);
//            if(nodeList.size()>0){
//                for(int i=0;i<nodeList.size();i++){
//                    String gdxxId = nodeList.get(i);
//                    System.out.println("gdxxId:"+gdxxId);
//                    sql = " select t.cggdx from uf_gdxx t\n" +
//                            "inner join uf_gd k on k.id = t.cggdx\n" +
//                            "where t.id = "+gdxxId;
//                    rs.executeQuery(sql);
//                    if(rs.next()){
//                        String cggdx = Util.null2String(rs.getString("cggdx"));
//                        if(StringUtils.isNotBlank(cggdx)){
//                            Map<String,Object> nodeMap =  getRootDataByNode(cggdx);
//                        }
//                    }
//                }
//            }
//
//        }
//    }


    public Map<String,Object> getRootDataByNode(String sqdw){
        RecordSet rs = new RecordSet();
        Map<String,Object> nodeMap = new HashMap<String,Object>();
        String gdid = "";
        String sql =" select id from uf_gd where id = '" + sqdw + "'";
        rs.executeQuery(sql);
        if (rs.next()) {
            gdid = rs.getString("id");
        }
        if(StringUtils.isNotBlank(gdid)){
            TreeNode root = new TreeNode("T"+gdid);
            List<String> nodeList = new ArrayList<String>();
            getChildrenByRoot(root,gdid,nodeList);
            System.out.println("nodeList:"+nodeList.toString());
            nodeMap = recurTree(root);
        }

        return nodeMap;
    }



    public void getChildrenByRoot(TreeNode root,String sqdw,List<String> nodeList){
        RecordSet rs = new RecordSet();
        List<TreeNode> treeNodeList = new ArrayList<TreeNode>();

        List<String> treeList = new ArrayList<String>();
        String sql =" select t.id\n" +
                " from uf_gdxx t\n" +
                " inner join uf_gd p on p.id = t.cggdx\n" +
                " where t.sqdw ='"+sqdw+"'" ;
        System.out.println("getChildrenByRoot:sql:"+sql);
        rs.executeQuery(sql);
        while (rs.next()){
            String gdxxid = rs.getString("id");
            treeList.add(gdxxid);
        }

        for(int i=0;i<treeList.size();i++){
            String gdxxid = treeList.get(i);
            System.out.println("gdxxid:"+gdxxid);
            if(StringUtils.isNotBlank(gdxxid)){
                TreeNode treeNode = new TreeNode();
                treeNode.setValue(gdxxid);
                List<TreeNode> childNodeList = queryChildrenByGdxxId(gdxxid,nodeList);
                if(childNodeList.size() > 0){
                    treeNode.setChildren(childNodeList);
                    nodeList.add(gdxxid);
                }
                treeNodeList.add(treeNode);
            }
        }
        root.setChildren(treeNodeList);
    }


    public List<TreeNode> queryChildrenByGdxxId(String parentGdxxId,List<String> nodeList){
        List<TreeNode> treeNodesList = new ArrayList<TreeNode>();
        RecordSet rs = new RecordSet();
        if(StringUtils.isNotBlank(parentGdxxId)){

            String sql =" select t.id\n" +
                        " from uf_gdxx t\n" +
                        " inner join uf_gd p on p.id = t.cggdx\n" +
                        " where t.sqdw in ( select cggdx from uf_gdxx where id  = '"+parentGdxxId+"' ) " ;

            System.out.println("sql:"+sql);
            rs.executeQuery(sql);
            while (rs.next()){
                String gdxxId = rs.getString("id");
                if(StringUtils.isNotBlank(gdxxId)){
                    TreeNode treeNode = new TreeNode();
                    treeNode.setValue(gdxxId);
                    List<TreeNode> childList = queryChildrenByGdxxId(gdxxId,nodeList);
                    if(childList.size()>0){
                        treeNode.setChildren(childList);
                        nodeList.add(gdxxId);
                    }
                    treeNodesList.add(treeNode);
                }
            }
        }
        return treeNodesList;
    }

    public Map<String,Object> recurTree(TreeNode root){
        RecordSet rs = new RecordSet();
        Map<String,Object> dataMap = new HashMap<String,Object>();

        List<List<String>> result = new ArrayList<>();
        List<String> path = new ArrayList<>();
        path.add(root.getValue());
        findTreePath(result,root,path);

        System.out.println("result:"+result);

        String return_jn_msg = "";
        String return_jw_msg = "";
        double jn_value = 0.0;
        double jw_value = 0.0;

        for(int i=0;i<result.size();i++){

            List<String> treeList = result.get(i);
            System.out.println("treeList:"+treeList.size());
            double jwcgbl = 0.0;
            double jncgbl =  0.0;

            String jn_msg = "";
            String jw_msg = "";

            if(treeList.size() == 2){
                String gdid = treeList.get(1);

                if(StringUtils.isNotBlank(gdid)){
                    String lx ="";
                    String cgbl = "";
                    String gdmc = "";
                    String sql =" select t.cgbl,k.lx,k.gdmc \n" +
                                " from uf_gdxx t\n" +
                                " inner join uf_gd k on k.id = t.cggdx\n" +
                                " where t.id=  "+gdid ;
                    rs.executeQuery(sql);
                    if(rs.next()){
                        lx = Util.null2String(rs.getString("lx"));
                        cgbl = Util.null2String(rs.getString("cgbl"));
                        gdmc = Util.null2String(rs.getString("gdmc"));
                    }

                    System.out.println("cgbl:"+cgbl);
                    System.out.println("gdmc:"+gdmc);

                    if(StringUtils.isNotBlank(cgbl)){
                        if("1".equals(lx)){
                            double dou_cgbl = Double.parseDouble(cgbl);
                            dou_cgbl =  new BigDecimal(dou_cgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();

                            jwcgbl = dou_cgbl;

                            jw_msg = gdmc+"["+jwcgbl+"%]";

                            jw_value += jwcgbl;
                        }else{
                            double dou_cgbl = Double.parseDouble(cgbl);
                            dou_cgbl =  new BigDecimal(dou_cgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                            jncgbl = dou_cgbl;

                            jn_msg = gdmc+"["+jncgbl+"%]";

                            jn_value += jncgbl;
                        }
                    }
                }

            }else{

                String gdmcx = "";
                Collections.reverse(treeList);
                for(int k=0;k<treeList.size();k++){

                    String gdid = treeList.get(k);
                    if(StringUtils.isNotBlank(gdid) && !gdid.startsWith("T")){

                        if( k== 0){
                            String lx ="";
                            String cgbl = "";
                            String sql =" select t.cgbl,k.lx,k.gdmc \n" +
                                    " from uf_gdxx t\n" +
                                    " inner join uf_gd k on k.id = t.cggdx\n" +
                                    " where t.id=  "+gdid ;
                            rs.executeQuery(sql);
                            if(rs.next()){
                                lx = Util.null2String(rs.getString("lx"));
                                cgbl = Util.null2String(rs.getString("cgbl"));
                                gdmcx = Util.null2String(rs.getString("gdmc"));
                            }

                            System.out.println("cgbl:"+cgbl);
                            if(StringUtils.isNotBlank(cgbl)){
                                if("1".equals(lx)){
                                    double dou_cgbl = Double.parseDouble(cgbl);
                                    dou_cgbl =  new BigDecimal(dou_cgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                                    jwcgbl = dou_cgbl;
                                }else{
                                    double dou_cgbl = Double.parseDouble(cgbl);
                                    dou_cgbl =  new BigDecimal(dou_cgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                                    jncgbl = dou_cgbl;
                                }
                            }
                        }else{

                            String lx ="";
                            String cgbl = "";
                            String sql =" select t.cgbl,k.lx,k.gdmc \n" +
                                    " from uf_gdxx t\n" +
                                    " inner join uf_gd k on k.id = t.cggdx\n" +
                                    " where t.id=  "+gdid ;
                            rs.executeQuery(sql);
                            if(rs.next()){
                                lx = Util.null2String(rs.getString("lx"));
                                cgbl = Util.null2String(rs.getString("cgbl"));
                            }

                            System.out.println("cgbl:" + cgbl);
                            if (StringUtils.isNotBlank(cgbl)) {
                                if("1".equals(lx)){
                                    double dou_cgbl = Double.parseDouble(cgbl);
                                    dou_cgbl =  new BigDecimal(dou_cgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                                    jwcgbl = dou_cgbl;
                                    jw_msg += gdmcx+"["+jwcgbl+"%]";

                                    jw_value += jwcgbl ;

                                }else{
                                    double dou_cgbl = Double.parseDouble(cgbl);
                                    if(jncgbl > 0){
                                        jncgbl = (jncgbl * dou_cgbl)/100.00;
                                        jncgbl =  new BigDecimal(jncgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                                        jn_msg = gdmcx+"["+jncgbl+"%]";

                                        jn_value += jncgbl ;

                                    }

                                    if(jwcgbl > 0){
                                        jwcgbl = (jwcgbl * dou_cgbl)/100.00;
                                        jwcgbl =  new BigDecimal(jwcgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                                        jw_msg = gdmcx+"["+jwcgbl+"%]";

                                        jw_value += jwcgbl ;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if(StringUtils.isNotBlank(jn_msg)){
                return_jn_msg += StringUtils.isBlank(return_jn_msg) ? jn_msg : ","+jn_msg ;
            }

            if(StringUtils.isNotBlank(jw_msg)){
                return_jw_msg += StringUtils.isBlank(return_jw_msg) ? jw_msg : ","+jw_msg ;
            }

        }

        jn_value =  new BigDecimal(jn_value).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
        jw_value =  new BigDecimal(jw_value).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
        dataMap.put("jn_msg",return_jn_msg);
        dataMap.put("jw_msg",return_jw_msg);
        dataMap.put("jn_value",jn_value);
        dataMap.put("jw_value",jw_value);

        return dataMap;
    }

    private void findTreePath(List<List<String>> result,TreeNode node,List<String> path){
        if(node.getChildren() == null || node.getChildren().size() <=0){
            result.add(path);
            return;
        }

        for(int i=0;i<node.getChildren().size();i++){
            TreeNode child = node.getChildren().get(i);
            List<String> cPath = new ArrayList<>();
            cPath.addAll(path);
            cPath.add(child.getValue());
            findTreePath(result,child,cPath);
        }
    }

%>

