刘光辉
9 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.base.service;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jnpf.base.entity.ModuleButtonEntity;
import jnpf.base.entity.ModuleEntity;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.model.button.ButtonListVO;
import jnpf.base.model.module.PropertyJsonModel;
import jnpf.flowable.TemplateApi;
import jnpf.onlinedev.VisualdevOnlineApi;
import jnpf.util.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
 
@Service
@RequiredArgsConstructor
public class MenuActionCandidateService {
 
    private final ModuleService moduleService;
    private final ModuleButtonService moduleButtonService;
    private final TemplateApi templateApi;
    private final VisualdevOnlineApi visualdevApi;
 
    public List<ButtonListVO> getCandidates(String menuId, String keyword) {
        ModuleEntity menu = moduleService.getInfo(menuId);
        if (menu == null || (!Objects.equals(menu.getType(), 3) && !Objects.equals(menu.getType(), 9))) {
            return new ArrayList<>();
        }
        PropertyJsonModel property = JsonUtil.getJsonToBean(menu.getPropertyJson(), PropertyJsonModel.class);
        if (property == null || property.getModuleId() == null || property.getModuleId().isEmpty()) {
            return new ArrayList<>();
        }
        String formId = Objects.equals(menu.getType(), 9) ? templateApi.getFormByFlowId(property.getModuleId()) : property.getModuleId();
        if (formId == null || formId.isEmpty()) {
            return new ArrayList<>();
        }
        VisualdevEntity form = visualdevApi.getReleaseInfo(formId);
        if (form == null) {
            return new ArrayList<>();
        }
        Set<String> associatedCodes = new HashSet<>();
        for (ModuleButtonEntity button : moduleButtonService.getListByModuleIds(menuId)) {
            associatedCodes.add(button.getEnCode());
        }
        String columnData = "App".equalsIgnoreCase(menu.getCategory()) ? form.getAppColumnData() : form.getColumnData();
        return extractCandidates(columnData, associatedCodes, keyword);
    }
 
    static List<ButtonListVO> extractCandidates(String columnData, Set<String> associatedCodes, String keyword) {
        Map<String, ButtonListVO> candidates = new LinkedHashMap<>();
        if (columnData == null || columnData.isEmpty()) {
            return new ArrayList<>();
        }
        JSONObject columns = JSON.parseObject(columnData);
        addButtons(candidates, columns.getJSONArray("btnsList"), true, associatedCodes, keyword);
        addButtons(candidates, columns.getJSONArray("columnBtnsList"), true, associatedCodes, keyword);
        addButtons(candidates, columns.getJSONArray("customBtnsList"), false, associatedCodes, keyword);
        return new ArrayList<>(candidates.values());
    }
 
    private static void addButtons(Map<String, ButtonListVO> candidates, JSONArray buttons, boolean builtIn,
                                   Set<String> associatedCodes, String keyword) {
        if (buttons == null) {
            return;
        }
        for (int i = 0; i < buttons.size(); i++) {
            JSONObject button = buttons.getJSONObject(i);
            if (button == null || Boolean.FALSE.equals(button.getBoolean("show"))) {
                continue;
            }
            String value = button.getString("value");
            String name = button.getString("label");
            if (value == null || value.isEmpty() || name == null || name.isEmpty()) {
                continue;
            }
            String code = builtIn ? "btn_" + value : value;
            if (associatedCodes.contains(code) || (keyword != null && !keyword.isEmpty()
                    && !code.contains(keyword) && !name.contains(keyword))) {
                continue;
            }
            ButtonListVO candidate = new ButtonListVO();
            candidate.setId(code);
            candidate.setEnCode(code);
            candidate.setFullName(name);
            candidates.putIfAbsent(code, candidate);
        }
    }
}