<%@ 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 import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.domain.TreeNode" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%

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

    Map<String,Object> dataMap = new HashMap<String,Object>();
    RecordSet rs = new RecordSet();
    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"));
        }

        TreeNode root = new TreeNode("T"+sqdw);
        getChildrenDataByRoot(root,sqdw);

        dataMap = recurTree(root);
        dataMap.put("id","T"+sqdw);
        dataMap.put("parentid","");



    }else if(StringUtils.isNotBlank(gd)){
        TreeNode root = new TreeNode();

        String sql =" select id from uf_gd where id = '" + gd + "'";

        rs.executeQuery(sql);
        if (rs.next()){
            String id = rs.getString("id");
            root = new TreeNode("T"+id);
            getChildrenDataByRoot(root,gd);
        }
        dataMap = recurTree(root);
        dataMap.put("id","T"+gd);
        dataMap.put("parentid","");

    }

    JSONObject dataJson = new JSONObject();
    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
    list.add(dataMap);
    dataJson.put("data",list);

%>

<%=dataJson.toJSONString() %>


<%!


    public void getChildrenDataByRoot(TreeNode root,String sqdw){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
        bb.writeLog("getChildrenDataByRoot");

        List<String> treeList = new ArrayList<String>();
        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+"'" ;
        bb.writeLog("sql:"+sql);
        rs.executeQuery(sql);
        while (rs.next()){
            String id = rs.getString("id");
            treeList.add(id);
        }

        for(int i=0;i<treeList.size();i++){
            String id = treeList.get(i);
            if(StringUtils.isNotBlank(id)){
                TreeNode treeNode = new TreeNode();
                treeNode.setValue(id);
                List<TreeNode> childNodeList = queryShareHolderByParent2(id);
                if(childNodeList.size() > 0){
                    treeNode.setChildren(childNodeList);
                }
                treeNodeList.add(treeNode);
            }
        }
        root.setChildren(treeNodeList);
    }


    public List<TreeNode> queryShareHolderByParent2(String parentId){
        List<TreeNode> treeNodesList = new ArrayList<TreeNode>();
        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");
                if(StringUtils.isNotBlank(id)){
                    TreeNode treeNode = new TreeNode();
                    treeNode.setValue(id);
                    List<TreeNode> childList = queryShareHolderByParent2(id);
                    if(childList.size()>0){
                        treeNode.setChildren(childList);
                    }
                    treeNodesList.add(treeNode);
                }
            }
        }
        return treeNodesList;
    }

    public Map<String,Object> recurTree(TreeNode root){

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("recurTree");

        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<List<String>> result = new ArrayList<>();
        List<String> path = new ArrayList<>();

        path.add(root.getValue());
        findPath(result,root,path);

        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);
            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) && !gdid.startsWith("T")){
                    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 ;

                    bb.writeLog("sql:"+sql);

                    rs.executeQuery(sql);
                    if(rs.next()){
                        lx = Util.null2String(rs.getString("lx"));
                        cgbl = Util.null2String(rs.getString("cgbl"));
                        gdmc = Util.null2String(rs.getString("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 = "";
                for(int k=treeList.size()-1;k>=0;k--){

                    if(k==treeList.size()-1){

                        String gdid = treeList.get(k);
                        if(StringUtils.isNotBlank(gdid) && !gdid.startsWith("T")){
                            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 ;
                            bb.writeLog("sql:"+sql);
                            rs.executeQuery(sql);
                            if(rs.next()){
                                lx = Util.null2String(rs.getString("lx"));
                                cgbl = Util.null2String(rs.getString("cgbl"));
                                gdmcx = Util.null2String(rs.getString("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;
                                }else{
                                    double dou_cgbl = Double.parseDouble(cgbl);
                                    dou_cgbl =  new BigDecimal(dou_cgbl).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
                                    jncgbl = dou_cgbl;
                                }
                            }
                        }
                    }else if( k == 1){

                        String gdid = treeList.get(k);
                        if(StringUtils.isNotBlank(gdid) && !gdid.startsWith("T")) {
                            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 ;
                            bb.writeLog("sql:"+sql);
                            rs.executeQuery(sql);
                            if(rs.next()){
                                lx = Util.null2String(rs.getString("lx"));
                                cgbl = Util.null2String(rs.getString("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;
                                    jn_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 ;
                                    }
                                }
                            }
                        }
                    }else if(k !=0 ){
                        String gdid = treeList.get(k);
                        if(StringUtils.isNotBlank(gdid) && !gdid.startsWith("T")) {
                            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"));
                            }

                            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;
                                    jn_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 ;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            bb.writeLog("jwcgbl:"+jwcgbl);
            bb.writeLog("jncgbl:"+jncgbl);
            bb.writeLog("jn_msg:"+jn_msg);
            bb.writeLog("jw_msg:"+jw_msg);

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

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

//        System.out.println("return_jn_msg:"+return_jn_msg);
//        System.out.println("return_jw_msg:"+return_jw_msg);
//        System.out.println("jn_value:"+jn_value);
//        System.out.println("jw_value:"+jw_value);

        return dataMap;
    }

    public void findPath(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());
            findPath(result,child,cPath);
        }
    }

%>

