许鹏程
2023-05-25 213cc37cbf0b2515a4de56cc1e01813211bad183
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.product.admin.service;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.product.admin.config.CmnConst;
import com.product.admin.entity.RouterEntity;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.cache.util.RedisUtil;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
import com.product.util.SystemParamReplace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * Copyright  LX-BASE
 *
 * @Title: SystemButtonsService
 * @Project: LX-BASE-SERVER
 * @Date: 2020-06-09 16:25
 * @Author: ZhouJie
 * @Description: 按钮Service层
 */
@Service
public class SystemButtonsService extends AbstractBaseService {
 
    @Autowired
    public BaseDao baseDao;
 
    /***
     * 查询按钮列表
     * @param function_uuid 功能uuid
     * @return
     */
    public DataTableEntity getButtonList(String function_uuid) throws BaseException {
        String sql = "select button.uuid,button.button_name,button.button_title,button.button_description,button.url,button.script,button.script_method,button.is_main,button.sequence,button.created_by,button.created_utc_datetime," +
                "button.function_uuid," +
                "button.button_type,dicttype.dict_label as button_type_name," +
                "button.button_category_uuid,dictcategory.dict_label as button_category_name " +
                " FROM product_sys_buttons button " +
                " left join product_sys_dict dicttype on button.button_type = dicttype.uuid " +
                " left join product_sys_dict dictcategory on button.button_category_uuid = dictcategory.uuid " +
                " where button.function_uuid = ? ";
        DataTableEntity dataTableEntity = baseDao.listTable(sql, new Object[]{function_uuid});
        return dataTableEntity;
    }
 
 
    /***
     * 获取排序序号
     * @param function_uuid 功能uuid
     * @throws BaseException
     */
    public int getSequence(String function_uuid) throws BaseException {
        String sql = "select max(sequence) as sequence FROM product_sys_buttons where function_uuid = ? ";
        DataTableEntity dataTableEntity = baseDao.listTable(sql, new Object[]{function_uuid});
        if (dataTableEntity != null && dataTableEntity.getMeta() != null) {
            FieldSetEntity fieldSetEntity = dataTableEntity.getFieldSetEntity(0);
            //周杰 2020年12月17日 上午11:16 
            if (!StringUtils.isEmpty(fieldSetEntity.getString("sequence"))) {
                return fieldSetEntity.getInteger("sequence") + 1;
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }
 
    public FieldSetEntity getDict(String uuid) throws BaseException {
        FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntity("product_sys_dict", uuid, false);
        return fieldSetEntity;
    }
 
    /**
     * @Date: 2020-06-11 17:08
     * @Author: ZhouJie
     * @Description: 按钮详情查看
     */
    public FieldSetEntity findButton(FieldSetEntity fs) throws BaseException {
        return baseDao.getFieldSetEntity("product_sys_buttons", fs.getString("uuid"), false);
    }
 
    /**
     * @throws BaseException
     * @Date: 2020-06-11 17:46
     * @Author: ZhouJie
     * @Description: 按钮版本号
     */
    public FieldSetEntity findBottonVersion(FieldSetEntity fs) throws BaseException {
        String version = null;
        String version1 = fs.getString("version");
        if (version1 != null) {
            String version2 = version1.substring(0, version1.indexOf(".", version1.indexOf(".") + 1) + 1) + String.valueOf(Integer.valueOf(version1.substring(version1.indexOf(".", version1.indexOf(".") + 1) + 1, version1.length())) + 1);
            version = version1 + "," + version2;
        }
        fs.setValue("version", version);
        return fs;
    }
 
    /**
     * 获取路由信息
     *
     * @return
     */
    public RouterEntity getRouterEntity() {
        return (RouterEntity) RedisUtil.get("system-cache:router");
    }
 
    /**
     * @throws BaseException
     * @Date: 2020-06-12 11:05
     * @Author: ZhouJie
     * @Description: 新增按钮
     */
    public String addButton(FieldSetEntity fs) throws BaseException {
        FieldSetEntity newFunction = baseDao.getFieldSetEntityByFilter("product_sys_functions", " uuid = ?", new Object[]{fs.getString("function_uuid")}, false);
        FieldSetEntity newModule = baseDao.getFieldSetEntityByFilter("product_sys_modules", " uuid = ?", new Object[]{newFunction.getString("module_uuid")}, false);
        String bfs = fs.getString("version");
        String ffs = newFunction.getString("version");
        String mfs = newModule.getString("version");
        //功能版本号小于按钮的版本号,则回写到功能版本号
        if (Integer.valueOf(ffs.substring(ffs.indexOf(".", ffs.indexOf(".") + 1) + 1, ffs.length())) <
                Integer.valueOf(bfs.substring(bfs.indexOf(".", bfs.indexOf(".") + 1) + 1, bfs.length()))) {
            newFunction.setValue("version", bfs);
            baseDao.update(newFunction);//回写上级功能的版本号
        }
        //模块的版本号小于按钮长级后的版本,则把版本号回写到模块
        if (Integer.valueOf(mfs.substring(mfs.indexOf(".") + 1, mfs.indexOf(".", mfs.indexOf(".") + 1))) <
                Integer.valueOf(bfs.substring(bfs.indexOf(".") + 1, bfs.indexOf(".", bfs.indexOf(".") + 1)))) {
            newModule.setValue("version", bfs);
            baseDao.update(newModule);//回写上级模块的版本号
        }
        return baseDao.add(fs);
    }
 
    /**
     * @throws BaseException
     * @Date: 2020-06-13 10:19
     * @Author: ZhouJie
     * @Description: 按钮版本号升级并回写版本号
     */
    public FieldSetEntity upVersion(FieldSetEntity fs) throws BaseException {
        FieldSetEntity now = baseDao.getFieldSetEntityByFilter("product_sys_function_buttons ", " uuid = ?", new Object[]{fs.getString("uuid")}, false);
        //当前按钮版本号
        String bfs = now.getString("version");
        //升一级后的按钮版本号
        String version = bfs.substring(0, bfs.indexOf(".", bfs.indexOf(".") + 1) + 1) + String.valueOf(Integer.valueOf(bfs.substring(bfs.indexOf(".", bfs.indexOf(".") + 1) + 1, bfs.length())) + 1);
        fs.setValue("version", version);
        baseDao.update(fs);
        //回写版本号
        FieldSetEntity newFunction = baseDao.getFieldSetEntityByFilter("product_sys_functions ", " uuid = ?", new Object[]{now.getString("function_uuid")}, false);
        FieldSetEntity newModule = baseDao.getFieldSetEntityByFilter("product_sys_modules ", " uuid = ?", new Object[]{newFunction.getString("module_uuid")}, false);
        String ffs = newFunction.getString("version");
        String mfs = newModule.getString("version");
        if (Integer.valueOf(ffs.substring(ffs.indexOf(".", ffs.indexOf(".") + 1) + 1, ffs.length())) <
                Integer.valueOf(version.substring(version.indexOf(".", version.indexOf(".") + 1) + 1, version.length()))) {
            newFunction.setValue("version", version);
            baseDao.update(newFunction);//回写上级功能的版本号
        }
        //模块的版本号小于按钮长级后的版本,则把版本号回写到模块
        if (Integer.valueOf(mfs.substring(mfs.indexOf(".") + 1, mfs.indexOf(".", mfs.indexOf(".") + 1))) <
                Integer.valueOf(version.substring(version.indexOf(".") + 1, version.indexOf(".", version.indexOf(".") + 1)))) {
            newModule.setValue("version", version);
            baseDao.update(newModule);//回写上级模块的版本号
        }
        return fs;
    }
 
    /**
     * 查询页面的按钮
     *
     * @param fse
     */
    public DataTableEntity findButtonByPage(FieldSetEntity fse) throws BaseException {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        if (currentUser == null || StringUtils.isEmpty(currentUser.getRoles())) {
            return null;
        }
        String function_uuid = fse.getString(CmnConst.FUNCTION_UUID);
        //当前人拥有的角色
        String[] role_uuid = currentUser.getRoles().split(",");
        List<DataTableEntity> permission = new ArrayList<>();
        for (int i = 0; i < role_uuid.length; i++) {
            //根据角色取出功能权限
            permission.add(DataPoolCacheImpl.getInstance().getCacheData("功能权限", new String[]{role_uuid[i], function_uuid}));
        }
        Set<String> buttons = new HashSet<>();
        //循环功能权限将按钮权限放入Set集合
        for (int i = 0; i < permission.size(); i++) {
            if (BaseUtil.dataTableIsEmpty(permission.get(i))) {
                continue;
            }
            for (int j = 0; j < permission.get(i).getRows(); j++) {
                String button_uuid = permission.get(i).getString(j, "button_uuid");
                if (!StringUtils.isEmpty(button_uuid)) {
                    buttons.addAll(Arrays.asList(button_uuid.split(",")));
                }
            }
        }
        DataTableEntity dt = DataPoolCacheImpl.getInstance().getCacheData("页面-按钮", new String[]{fse.getString(CmnConst.MVC_PAGE_UUID), function_uuid});
        DataTableEntity button;
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            button = dt.clones();
        } else {
            return null;
        }
        RouterEntity routerEntity = getRouterEntity();
        for (int i = 0; i < button.getRows(); i++) {
            String button_uuid = button.getString(i, "button_uuid");
            String params = button.getString(i, "params");
            String upload_parameter = button.getString(i, "upload_parameter");
            if (!buttons.contains(button_uuid)) {
                if (!StringUtils.isEmpty(params)) {
                    if (JSON.isValidObject(params)) {
                        JSONObject jsonObject = JSON.parseObject(params);
                        for (Map.Entry<String, Object> v : jsonObject.entrySet()) {
                            String value = (String) v.getValue();
                            List<String> matchStr = BaseUtil.getMatchStr("(\\{#)([\\w]+)(#\\})", value);
                            if (matchStr.size() > 0) {
                                if (StringUtils.isEmpty(upload_parameter)) {
                                    upload_parameter = "";
                                } else if (upload_parameter.length() > 0) {
                                    upload_parameter += ',';
                                }
                                upload_parameter += Joiner.on(",").join(matchStr);
 
                            }
                        }
                        button.setFieldValue(i, "upload_parameter", upload_parameter);
                    }
                }
                button.removeFieldSetEntity(i);
                i--;
                continue;
            }
            RouterEntity.RouterMeta routerByButton = routerEntity.getRouterByButton(button_uuid);
            if (routerByButton != null) {
                button.setFieldValue(i, "page_type", routerByButton.getPageType());
            }
        }
        return button;
    }
 
    /**
     * 查询页面的按钮
     *
     * @param fse
     */
    public FieldSetEntity findFromButtonByPage(FieldSetEntity fse) throws BaseException {
        // xupengheng 2021年3月10日14:15:18
        String function_uuid = fse.getString(CmnConst.FUNCTION_UUID);
        String mvc_page_uuid = fse.getString(CmnConst.MVC_PAGE_UUID);
        String last_button = fse.getString("last_button");
        Object[] param = null;
        String filter = "";
        if (StringUtils.isEmpty(last_button)) {
            param = new Object[]{function_uuid, mvc_page_uuid};
        } else {
            param = new Object[]{function_uuid, mvc_page_uuid, last_button};
            filter = " and (route_name =?) ";
        }
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" a.*,JSON_object('face_uuid',c.face_uuid,'flow_title',flow_title,'uuid',c.uuid,'page_type',c.page_type,'tree_data',c.tree_data,'tree_data_type',tree_data_type ,'flow_uuid' ,c.flow_uuid,'page_element_disabled',c.page_element_disabled,'page_name',c.page_name,'from_param_key',c.from_param_key) as current_page, ");
        sql.append(" ( SELECT table_name FROM product_sys_datamodel_table where uuid =(select table_uuid FROM product_sys_functions b WHERE a.function_uuid = b.uuid and data_type=1) ) table_name ");
        sql.append(" FROM ");
        sql.append(" product_sys_function_buttons a ");
        sql.append(" JOIN product_sys_link b ON b.line_from = a.uuid ");
        sql.append(" JOIN product_sys_mvc_page c ON c.uuid = b.line_to ");
        sql.append(" WHERE ");
        sql.append(" b.function_uuid = ? ");
        sql.append(" AND b.line_to = ? ");
        sql.append(filter);
        sql.append(" AND b.from_type IN ( 0, 1 ) ");
        FieldSetEntity fs = baseDao.getFieldSetEntityBySQL(sql.toString(), param, false);
        if (fs != null && !StringUtils.isEmpty(fs.getString("params"))) {
            String params = fs.getString("params");
            boolean valid = JSON.isValid(params);
            if (valid) {
                JSONObject json = JSON.parseObject(params);
                for (Map.Entry<String, Object> one : json.entrySet()) {
                    String val;
                    Object value = one.getValue();
                    if (value != null) {
                        val = value.toString();
                    } else {
                        continue;
                    }
                    one.setValue(SystemParamReplace.replaceParams(val, null));
                }
                fs.setValue("params", json.toJSONString());
            }
        }
        return fs;
    }
 
 
}