wuxw
2025-03-08 4a1e7c9d58fab8a3786b7d97e829a70de325b3fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.java110.common.cmd.area;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.area.AreaDto;
import com.java110.intf.common.IAreaInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.List;
 
/**
 * 查询地区树形结构
 */
@Java110Cmd(serviceCode = "area.queryAreaTree")
public class QueryAreaTreeCmd extends Cmd {
 
    @Autowired
    private IAreaInnerServiceSMO areaInnerServiceSMOImpl;
 
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
 
    }
 
    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
 
        AreaDto areaDto = new AreaDto();
        List<AreaDto> areaDtos = areaInnerServiceSMOImpl.getArea(areaDto);
 
        JSONObject areaData = new JSONObject();
        areaData.put("id", "0");
        areaData.put("areaCode", "0");
 
        areaData.put("text", "地区");
        areaData.put("icon", "/img/org.png");
        areaData.put("children", new JSONArray());
        JSONArray areas = areaData.getJSONArray("children");
        if (ListUtil.isNull(areaDtos)) {
            context.setResponseEntity(ResultVo.createResponseEntity(areas));
            return;
        }
 
        JSONObject areaInfo = null;
        for (AreaDto tmpAreaDto : areaDtos) {
            if (AreaDto.AREA_LEVEL_ONE.equals(tmpAreaDto.getAreaLevel())) {
                areaInfo = new JSONObject();
                areaInfo.put("id", tmpAreaDto.getId());
                areaInfo.put("areaCode", tmpAreaDto.getAreaCode());
                areaInfo.put("areaLevel", tmpAreaDto.getAreaLevel());
                areaInfo.put("text", tmpAreaDto.getAreaName());
                areaInfo.put("icon", "/img/org.png");
                areaInfo.put("children", new JSONArray());
                areas.add(areaInfo);
            }
        }
 
        JSONObject area = null;
        for (int cIndex = 0; cIndex < areas.size(); cIndex++) {
            area = areas.getJSONObject(cIndex);
            // find floor data in unitDtos
            findTwoLevel(area, areaDtos);
        }
        context.setResponseEntity(ResultVo.createResponseEntity(areaData));
 
    }
 
    private void findTwoLevel(JSONObject area, List<AreaDto> areaDtos) {
 
        JSONArray childrens = area.getJSONArray("children");
        JSONObject childrenInfo = null;
        for (AreaDto tmpAreaDto : areaDtos) {
            if (!AreaDto.AREA_LEVEL_TWO.equals(tmpAreaDto.getAreaLevel())) {
                continue;
            }
            if (area.getString("areaCode").equals(tmpAreaDto.getParentAreaCode())) {
                childrenInfo = new JSONObject();
                childrenInfo.put("id", tmpAreaDto.getId());
                childrenInfo.put("areaCode", tmpAreaDto.getAreaCode());
                childrenInfo.put("areaLevel", tmpAreaDto.getAreaLevel());
                childrenInfo.put("text", tmpAreaDto.getAreaName());
                childrenInfo.put("children", new JSONArray());
                childrenInfo.put("icon", "/img/floor.png");
                childrens.add(childrenInfo);
            }
        }
 
 
        JSONObject floor = null;
        for (int cIndex = 0; cIndex < childrens.size(); cIndex++) {
            floor = childrens.getJSONObject(cIndex);
            // find floor data in unitDtos
            findThreeLevel(floor, areaDtos);
        }
    }
 
    private void findThreeLevel(JSONObject twoArea, List<AreaDto> areaDtos) {
        JSONArray childrens = twoArea.getJSONArray("children");
        JSONObject childrenInfo = null;
        for (AreaDto tmpAreaDto : areaDtos) {
            if (!AreaDto.AREA_LEVEL_THREE.equals(tmpAreaDto.getAreaLevel())) {
                continue;
            }
            if (twoArea.getString("areaCode").equals(tmpAreaDto.getParentAreaCode())) {
                childrenInfo = new JSONObject();
                childrenInfo.put("id", tmpAreaDto.getId());
                childrenInfo.put("areaCode", tmpAreaDto.getAreaCode());
                childrenInfo.put("areaLevel", tmpAreaDto.getAreaLevel());
                childrenInfo.put("text", tmpAreaDto.getAreaName());
                childrenInfo.put("children", new JSONArray());
                childrenInfo.put("icon", "/img/unit.png");
                childrens.add(childrenInfo);
            }
        }
    }
}