1821349743@qq.com
2023-05-04 255b8a89d9af9722f78a48a2f685c56d40f98c5e
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.product.admin.service;
 
import com.alibaba.fastjson.JSONObject;
import com.product.admin.config.CmnCode;
import com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.admin.service.idel.IFunctionService;
import com.product.common.lang.StringUtils;
import com.product.core.config.CoreConst;
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.core.transfer.Transactional;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * Copyright  LX-BASE
 *
 * @Title: FunctionsService
 * @Project: LX-BASE-SERVER
 * @Date: 2020-06-03 11:48
 * @Author: ZhouJie
 * @Description: 功能管理
 */
@Service
public class FunctionsService extends AbstractBaseService implements IFunctionService {
    @Autowired
    public BaseDao baseDao;
 
    @Autowired
    private CodeService codeService;
 
    /**
     * 产品模块树
     *
     * @return
     * @auth cheng
     */
    public DataTableEntity getProductModuleTree() {
        StringBuilder sql = new StringBuilder();
        sql.append("\n SELECT  ");
        sql.append("\n uuid,0 type,product_name `name`,null function_description,product_shortcode function_shortcode, ");
        sql.append("\n tricode,null tricode_parent  ");
        sql.append("\n FROM  ");
        sql.append("\n product_sys_products ");
        sql.append("\n union all  ");
        sql.append("\n SELECT uuid,data_type type,function_name,function_description,function_shortcode,tricode, ");
        sql.append("\n tricode_parent  ");
        sql.append("\n FROM ");
        sql.append("\n product_sys_functions  ");
        sql.append("\n where data_type=2  ");
        sql.append("\n order by length(tricode),tricode ");
        DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{});
 
        return BaseUtil.dataTableToTreeTable(dt, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, null);
    }
 
    /**
     * 查询功能根据模块
     *
     * @param fse
     * @return
     */
    public DataTableEntity getFunctionByModule(FieldSetEntity fse) {
        if (StringUtils.isEmpty(fse.getString(CmnConst.TRICODE))) {
            return null;
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_FUNCTIONS, "data_type=1 and tricode_parent=?", new Object[]{fse.getString(CmnConst.TRICODE)});
        baseDao.loadPromptData(dt);
        return dt;
    }
 
 
    /**
     * 功能列表
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public DataTableEntity getFunctionList(FieldSetEntity fse) throws BaseException {
        DataTableEntity dt = baseDao.listTable(fse.getTableName(), null, null, null, fse.getString("orderby"), fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE));
        baseDao.loadPromptData(dt);
        return dt;
    }
 
    /**
     * @return FieldSetEntity
     * @Author: cheng
     * @Description: 查看功能详情
     */
    public FieldSetEntity findFunction(FieldSetEntity fs) throws BaseException {
        FieldSetEntity fieldSetEntity;
        if (!StringUtils.isEmpty(fs.getString(CmnConst.FUNCTION_UUID))) {
            fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_FUNCTIONS, fs.getString(CmnConst.FUNCTION_UUID), true);
        } else {
            fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_FUNCTIONS, fs.getString(CmnConst.UUID), true);
        }
        return fieldSetEntity;
    }
 
    /**
     * @Date: 2020-06-11 10:09
     * @Author: ZhouJie
     * @Description: 功能版本号
     */
    public FieldSetEntity findVersion(FieldSetEntity fs) throws BaseException {
        FieldSetEntity modufs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTIONS, CmnConst.TRICODE + " = ? AND data_type = 2 ", new Object[]{fs.getString(CmnConst.TRICODE_PARENT)}, false);
        if (modufs == null) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), "findVersion");
        }
        FieldSetEntity profs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PRODUCTS, CmnConst.UUID + "=?", new Object[]{modufs.getString(CmnConst.PRODUCT_UUID)}, false);
        if (profs == null) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), "findVersion");
        }
        FieldSetEntity verfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_VERSION, CmnConst.UUID + "=?", new Object[]{profs.getString(CmnConst.VERSION_UUID)}, false);
        modufs.setValue(CmnConst.VERSION_UUID, verfs.getString(CmnConst.VERSION_CODE));
        return modufs;
    }
 
 
    /**
     * @throws BaseException
     * @Date: 2020-06-12 11:19
     * @Author: ZhouJie
     * @Description: 新增功能
     */
    public String addFunction(FieldSetEntity fs) throws BaseException {
        //判断功能按钮入口唯一
        DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS);
        int sum_main = 0;
        if (subDataTable != null && subDataTable.getRows() > 0) {
            for (int i = 0; i < subDataTable.getRows(); i++) {
                FieldSetEntity button = subDataTable.getFieldSetEntity(i);
                if ("main".equals(button.getString("type"))) {
                    button.setValue("main", "button_category_uuid");
                }
                if (button.getString("button_category_uuid") != null && "main".equals(button.getString("button_category_uuid"))) {
                    sum_main = sum_main + 1;
                    button.setValue("button_type", 1);
                    button.setValue("is_main", 1);
                } else {
                    button.setValue("button_type", 2);
                    button.setValue("is_main", 0);
                }
            }
        }
        if (sum_main != 1) {
            throw new BaseException(CmnCode.MANY_MAIN.getValue(), CmnCode.MANY_MAIN.getText());
        }
        FieldSetEntity newModule = baseDao.listInternationDataTable(baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTIONS, CmnConst.TRICODE + " = ? AND data_type = 2", new Object[]{fs.getString(CmnConst.TRICODE_PARENT)}, true), null);
        if (newModule == null) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), "addFunction");
        }
        fs.setValue("data_type", 1);
        return baseDao.add(fs);
    }
 
    /**
     * 删除功能、按钮
     *
     * @param fs
     * @return
     * @throws BaseException
     */
    public boolean deletefunction(FieldSetEntity fs) throws BaseException {
        String uuid = fs.getString(CmnConst.UUID);
        FieldSetEntity ffs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTIONS, CmnConst.UUID + "=?", new Object[]{uuid}, false);
        FieldSetEntity ffbs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS, CmnConst.UUID + "=?", new Object[]{uuid}, false);
        FieldSetEntity fbs = null;
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS, CmnConst.FUNCTION_UUID + "=?", new Object[]{uuid});
        if (ffs != null) {//删除功能
            baseDao.delete(CmnConst.PRODUCT_SYS_FUNCTIONS, new String[]{uuid});
        }
        if (ffbs != null) {//单独删除按钮
            baseDao.delete(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS, new String[]{uuid});
        }
        if (dt != null && dt.getRows() > 0) {//删除功能下的按钮
            for (int i = 0; i < dt.getRows(); i++) {
                fbs = dt.getFieldSetEntity(i);
                String buuid = fbs.getString(CmnConst.UUID);
                baseDao.delete(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS, new String[]{buuid});
            }
        }
        DataTableEntity menus = baseDao.listTable("product_sys_menus", CmnConst.FUNCTION_UUID + "=?", new String[]{uuid});
        if (menus != null && menus.getRows() > 0) {
            for (int i = 0; i < menus.getRows(); i++) {
                FieldSetEntity fss = menus.getFieldSetEntity(i);
                String sql = "update product_sys_menus set is_show=0 where uuid=?";
                baseDao.executeUpdate(sql, new String[]{fss.getUUID()});
            }
        }
        return true;
    }
 
    /**
     * 修改功能按钮
     *
     * @param fs
     * @return
     * @throws BaseException
     */
    public boolean updateButton(FieldSetEntity fs) throws BaseException {
        //判断功能按钮入口唯一
        DataTableEntity subDataTable = baseDao.listInternationDataTable(fs.getSubDataTable(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS), null);
        int sum_main = 0;
        if (subDataTable != null && subDataTable.getRows() > 0) {
            for (int i = 0; i < subDataTable.getRows(); i++) {
                FieldSetEntity button = subDataTable.getFieldSetEntity(i);
                if (button.getInteger("is_main") == 1 && StringUtils.isEmpty(button.getString("~type~"))) {
                    sum_main = sum_main + 1;
                }
            }
        }
        if (sum_main != 1) {
            throw new BaseException(CmnCode.MANY_MAIN.getValue(), CmnCode.MANY_MAIN.getText());
        }
 
        // 删除多余的逻辑 2021年1月15日17:16:08 重设主表表名 function 表 ,删除国际化字段
        fs.remove("function_name");
        fs.getMeta().setTableName(new Object[]{CmnConst.PRODUCT_SYS_FUNCTIONS});
        BaseUtil.updatedRegeneratorAndUpdateTime(SpringMVCContextHolder.getCurrentUser(), fs);
        return update(fs);
    }
 
    /**
     * 修改功能
     *
     * @param fs
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean updatefunction(FieldSetEntity fs) throws BaseException {
        String tricode_parent = fs.getString(CmnConst.TRICODE_PARENT);
        String uuid = fs.getString(CmnConst.UUID);
        FieldSetEntity fse = baseDao.getFieldSetEntity(fs.getTableName(), uuid, false);
        // updateTime 2021年10月25日10:03:32 cheng
        if (fse != null && !tricode_parent.equals(fse.getString(CmnConst.TRICODE_PARENT))) {
            // 上级模块改变
            codeService.createCode(fs, fs.getTableName(), CmnConst.TRICODE, tricode_parent);
        }
        fs.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());// 获取登录帐号
        fs.setValue("updated_utc_datetime", new Date());// 创建时间
        return baseDao.update(fs);
    }
 
    @Override
    @Transactional
    public FieldSetEntity saveModule(FieldSetEntity fse) throws BaseException {
        String uuid = fse.getUUID();
        if (StringUtils.isEmpty(fse.getString(CmnConst.TRICODE_PARENT))) {
            //默认为顶级设置产品的code给模块
            fse.setValue(CmnConst.TRICODE_PARENT, "001");
        }
        DataTableEntity subDt = null;
        if (StringUtils.isEmpty(uuid)) {
            //创建编码
            codeService.createCode(fse, fse.getTableName(), CmnConst.TRICODE, fse.getString(CmnConst.TRICODE_PARENT));
 
        } else {
            FieldSetEntity fs = baseDao.getFieldSetEntity(fse.getTableName(), uuid, false);
            if (FieldSetEntity.isEmpty(fs)) {
                throw new BaseException(SystemCode.SYSTEM_MODULE_GET_FAIL);
            }
            String oldParentCode = fs.getString(CmnConst.TRICODE_PARENT);
            if (!StringUtils.equals(oldParentCode, fse.getString(CmnConst.TRICODE_PARENT))) {
                codeService.createCode(fse, fse.getTableName(), CmnConst.TRICODE, fse.getString(CmnConst.TRICODE_PARENT));
                //上级编码被修改更改下级编码
                DataTableEntity dt = baseDao.listTable(fse.getTableName(), "tricode like concat(?,'-%')", new Object[]{fs.getString(CmnConst.TRICODE)},
                        new Object[]{CmnConst.UUID, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, "data_type", CmnConst.FUNCTION_NAME, CmnConst.FUNCTION_DESCRIPTION, "function_shortcode"});
                for (int i = 0; i < dt.getRows(); i++) {
                    dt.setFieldValue(i, CmnConst.TRICODE, codeService.createCode(fse.getTableName(), CmnConst.TRICODE, fse.getString(CmnConst.TRICODE)));
                    dt.setFieldValue(i, CmnConst.TRICODE_PARENT, fse.getString(CmnConst.TRICODE));
                    dt.getFieldSetEntity(i).setCodeFieldUpdateFlat(CmnConst.TRICODE, true);
                    dt.setFieldValue(i, CmnConst.UPDATED_UTC_DATETIME, new Date());
                }
                baseDao.update(dt);
                subDt = dt;
            }
        }
        BaseUtil.createCreatorAndCreationTime(fse);
        baseDao.saveFieldSetEntity(fse);
        FieldSetEntity fs = baseDao.getFieldSetByFilter(fse.getTableName() + " a ", new String[]{"ifnull((select uuid from product_sys_functions b WHERE b.tricode=a.tricode_parent),(select uuid from product_sys_products b WHERE b.tricode=a.tricode_parent)) parent_uuid ,a.*"}, "a.uuid=?", new Object[]{fse.getUUID()}, false, null);
        if (!DataTableEntity.isEmpty(subDt)) {
            fs.setValue(CmnConst.CHILDREN, BaseUtil.dataTableToTreeData(subDt, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, (f) -> {
                JSONObject ff = f[0];
                ff.put(CmnConst.NAME, ff.getString(CmnConst.FUNCTION_NAME));
                ff.put("type", ff.getString("data_type"));
                ff.remove(CmnConst.FUNCTION_NAME);
                ff.remove(CmnConst.UPDATED_BY);
                ff.remove(CmnConst.UPDATED_UTC_DATETIME);
                ff.remove("data_type");
            }, false));
        }
        return fs;
    }
 
    @Override
    @Transactional
    public boolean deleteModule(FieldSetEntity fse) throws BaseException {
        String uuid = fse.getUUID();
        if (!StringUtils.isEmpty(uuid)) {
            String sql = "SELECT uuid,(select 1 from product_sys_functions b where b.tricode_parent=a.tricode limit 1) is_children from product_sys_functions a where uuid=?";
            FieldSetEntity fs = baseDao.getFieldSetBySQL(sql, new Object[]{uuid}, false);
            if (FieldSetEntity.isEmpty(fs)) {
                return false;
            }
            if ("1".equals(fs.getString("is_children"))) {
                //存在下级模块或功能
                throw new BaseException(SystemCode.DELETE_MODULE_FAIL_CHILDREN_EXEIST);
            }
            return baseDao.delete(CmnConst.PRODUCT_SYS_FUNCTIONS, new Object[]{uuid});
        }
        return false;
    }
 
    /**
     * @Date: 2020-08-24 18:08
     * @Author: ZhouJie
     * @Description: 加载功能的所有按钮
     */
    public Object[] onloadButton(FieldSetEntity fs) throws BaseException {
        String fuuid = fs.getString(CmnConst.FUNCTION_UUID);
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(fuuid)) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), "returnPwdSecurity");
        }
        SystemUser user = SpringMVCContextHolder.getCurrentUser();
        String role_uuids;
        if (user.getUserType() == 0) {
            role_uuids = user.getCurrentCustomer().getString("role_uuid");
        } else {
            role_uuids = user.getCurrentManager().getString("role_uuids");
        }
        //updatetiem 2021年1月14日19:53:48 增加按钮客户端类型过滤 优化查询方式 已测试暂无问题
        //修改代码前 运行时间组 start:1610625440822 end:1610625440902 修改代码后 运行时间组 start:1610625657648 end:1610625657660
        String[] role_uuid = role_uuids.split(",");
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT ");
        sb.append(" * ");
        sb.append(" FROM ");
        sb.append(" ( ");
        sb.append(" SELECT ");
        sb.append(" c.button_name as uuid, ");
        sb.append(" button_uuid, ");
        sb.append(" a.uuid function_permission_uuid ");
        sb.append(" FROM ");
        sb.append(" product_sys_function_permission a ");
        sb.append(" JOIN product_sys_datamodel_field b ON b.field_id <= ( length( a.button_uuid ) - length( REPLACE ( a.button_uuid, ',', '' ) ) + 2 ) ");
        sb.append(" JOIN product_sys_function_buttons c ON substring_index( substring_index( a.button_uuid, ',', b.field_id ), ',',- 1 ) = c.uuid ");
        sb.append(" where ");
        sb.append(" " + BaseUtil.buildQuestionMarkFilter("a.role_uuid", role_uuid.length, true) + " ");
        sb.append(" and a.function_uuid=? and concat(',',client_type_uuid,',') like concat('%,',?,',%') ");
        sb.append(" ) a ");
        sb.append(" GROUP BY ");
        sb.append(" function_permission_uuid, ");
        sb.append(" button_uuid, ");
        sb.append(" uuid ");
        List<Object> params = new ArrayList<>();
        params.addAll(Arrays.asList(role_uuid));
        params.add(fuuid);
        params.add(fs.getString(CoreConst.SYSTEM_CLIENT_TYPE_));
        DataTableEntity dataTableEntity = baseDao.listTable(sb.toString(), params.toArray());
        return dataTableEntity.getUuids();
    }
 
 
}