java中产品分类返回给前台页面 后台数据组装

public ResultBean getSpfl(Integer yyb) {
        ResultBean res = new ResultBean();
        try {
            JSONArray data = new JSONArray();
            String sql = "select * from tspfl_wx  where yyb = " + yyb;
            sql += " and zt=" + GlobalStatusType.SPFlZT_QY;
            // 一级分类
            List<Map<String, Object>> list1 = sqlMapper.selectList(sql + " and type = 1 and pid = 0 order by orderno ");
            for (Map<String, Object> map1 : list1) {
                JSONObject obj1 = new JSONObject();
                obj1.put("id", map1.get("id"));
                obj1.put("enspxxid", map1.get("enspxxid"));
                obj1.put("banner", CommonUtils.isEmpty(map1.get("tb")) ? ""
                        : String.valueOf(map1.get("tb")).replace("zw:uri", Local.getUri()));
                obj1.put("cate", map1.get("flmc"));
                // 二级分类
                List<Map<String, Object>> list2 = sqlMapper
                        .selectList(sql + " and type = 2 and pid = " + map1.get("id") + " order by orderno");
                if (list2 != null && list2.size() > 0) {
                    obj1.put("ishaveChild", 1);
                } else {
                    obj1.put("ishaveChild", 0);
                }
                JSONArray detail = new JSONArray();
                for (Map<String, Object> map2 : list2) {
                    JSONObject obj2 = new JSONObject();
                    obj2.put("detail_id", map2.get("id"));
                    obj2.put("enspxxid", map2.get("enspxxid"));
                    obj2.put("detail_cate", map2.get("flmc"));
                    // 三级分类
                    List<Map<String, Object>> list3 = sqlMapper
                            .selectList(sql + " and type = 3 and pid = " + map2.get("id") + " order by orderno");
                    JSONArray detail_d = new JSONArray();
                    for (Map<String, Object> map3 : list3) {
                        JSONObject obj3 = new JSONObject();
                        obj3.put("thumb_id", map3.get("id"));
                        obj3.put("enspxxid", map3.get("enspxxid"));
                        obj3.put("thumb", CommonUtils.isEmpty(map3.get("tb")) ? ""
                                : String.valueOf(map3.get("tb")).replace("zw:uri", Local.getUri()));
                        obj3.put("name", map3.get("flmc"));
                        detail_d.add(obj3);
                    }
                    obj2.put("detail_d", detail_d);
                    detail.add(obj2);
                }
                obj1.put("detail", detail);
                if (list2 != null && list2.size() > 0) {
                    data.add(obj1);
                }
            }
            res.setSuccess(true);
            res.setData(data);
        } catch (Exception e) {
            res.setSuccess(false);
            res.setMessage("异常:" + e.getMessage());
        }
        return res;
    }

 

posted @ 2020-06-09 14:51  红尘沙漏  阅读(338)  评论(0编辑  收藏  举报