许鹏程
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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package com.product.org.admin.service;
 
import com.product.admin.service.RolesService;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldMetaEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.permission.PermissionLoad;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.org.admin.config.CmnConst;
import com.product.org.admin.config.SystemCode;
import com.product.org.admin.service.idel.ISystemDataPermissionService;
import com.product.util.BaseUtil;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Map;
 
/**
 * Copyright © 2020 LX-BASE
 *
 * @Title: LX-BASE-SERVER
 * @Project: base-server-org-admin
 * @Date: 2020年6月11日
 * @Author: Mr.Xu
 * @Description:数据策略Service
 */
@Component
public class SystemDataPermissionService extends AbstractBaseService implements ISystemDataPermissionService {
 
    @Autowired
    public BaseDao dao;
    @Autowired
    public RolesService rolesService;
    /**
     * 数据权限初始
     */
    @Autowired
    public PermissionLoad permissionLoad = null;
 
    public PermissionLoad getPermissionLoad() {
        return permissionLoad;
    }
 
    public void setPermissionLoad(PermissionLoad permissionLoad) {
        this.permissionLoad = permissionLoad;
    }
 
    /**
     * 数据策略列表
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public DataTableEntity listDataPermission(FieldSetEntity fse) throws BaseException {
        if (SpringMVCContextHolder.getCurrentUser() != null && SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid() != null) {
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT ");
            sql.append(" c.uuid org_level_uuid, ");
            sql.append(" c.org_level_name,c.org_level_all, ");
            sql.append(" GROUP_CONCAT( property_name ORDER BY property_name ) property_name ");
            sql.append(" FROM ");
            sql.append("product_sys_data_strategy_master a ");
            sql.append(" JOIN product_sys_data_strategy_properties b ON a.property_uuid = b.uuid ");
            sql.append(" join product_sys_org_levels c on a.org_level_uuid=c.uuid ");
            sql.append(" WHERE ");
            sql.append(" c.uuid = ? ");
            sql.append(" AND a.is_used = 1 ");
            sql.append(" GROUP BY ");
            sql.append(" c.uuid, ");
            sql.append(" c.org_level_name, ");
            sql.append(" c.org_level_all ");
            return dao.listTable(sql.toString(), new Object[]{fse.getString(CmnConst.ORG_LEVEL_UUID)}, fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE));
        }
        return null;
    }
 
    /**
     * 数据策略详情
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findDataPermission(FieldSetEntity fse) throws BaseException {
        DataTableEntity dt = dao.listTable(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid=?", new Object[]{fse.getString(CmnConst.ORG_LEVEL_UUID)}, null, null, Integer.MAX_VALUE, 1, true);
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            FieldSetEntity fs = new FieldSetEntity();
            fs.setTableName(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER);
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fss = dt.getFieldSetEntity(i);
                if(i==0){
                    fs.setValue(CmnConst.EFFECTIVE_START_DATE,fss.getObject(CmnConst.EFFECTIVE_START_DATE));
                    fs.setValue(CmnConst.EFFECTIVE_END_DATE,fss.getObject(CmnConst.EFFECTIVE_END_DATE));
                    fs.setValue(CmnConst.ORG_LEVEL_UUID,fss.getObject(CmnConst.ORG_LEVEL_UUID));
                }
                fs.setValue("master_uuid_" + fss.getString("property_uuid"), fss.getUUID());
                if (fss.getSubTableNames() != null && fss.getSubTableNames().length > 0) {
                    fs.setValue(fss.getSubTableNames()[0] + "_is_used", fss.getInteger(CmnConst.IS_USED));
                    fs.setValue(fss.getSubTableNames()[0] + "_properties", fss.getString(CmnConst.PROPERTY_UUID));
                }
                Map<String, DataTableEntity> subData = fss.getSubData();
                if (subData != null && subData.size() > 0) {
                    for (String ket_table : subData.keySet()) {
                        fs.addSubDataTable(subData.get(ket_table));
                    }
                }
            }
            return fs;
        }
        return null;
    }
 
    /**
     * 数据策略详情列表
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findDataPermissionList(FieldSetEntity fse) throws BaseException {
        DataTableEntity dt = dao.listTable(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid=?", new Object[]{fse.getString(CmnConst.ORG_LEVEL_UUID)}, null, null, Integer.MAX_VALUE, 1, true);
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            if (BaseUtil.strIsNull(fse.getString("date"))) {
                return dt.getFieldSetEntity(0);
            }
            for (int i = 0, length = dt.getRows(); i < length; i++) {
                FieldSetEntity fs = dt.getFieldSetEntity(i);
                if (fs.getSubTableNames() != null && fs.getSubTableNames().length > 0 && fse.getString("date").equals(fs.getSubTableNames()[0])) {
                    fs.setValue(fs.getSubTableNames()[0] + "_is_used", fs.getString(CmnConst.IS_USED));
                    fs.setValue(fs.getSubTableNames()[0] + "_properties", fs.getString(CmnConst.PROPERTY_UUID));
                    return fs;
                }
            }
        }
 
        return null;
    }
 
    /**
     * 新增数据策略
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean addDataPermission(FieldSetEntity fse) throws BaseException {
        DataTableEntity listTable = dao.listTable(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid=?", new Object[]{fse.getString(CmnConst.ORG_LEVEL_UUID)});
        if (!BaseUtil.dataTableIsEmpty(listTable)) { //判断是否存在该公司策略
            throw new BaseException(SystemCode.SYSTEM_DATA_PERMISSION_ALREADY_EXIST_ADD_FAIL.getValue(), SystemCode.SYSTEM_DATA_PERMISSION_ALREADY_EXIST_ADD_FAIL.getText(), this.getClass(), "addDataPermission");
        }
        Map<String, DataTableEntity> subData = fse.getSubData();
        DataTableEntity masterDt = new DataTableEntity();
        if (fse.getSubData().size() != 0) {
            FieldMetaEntity f = new FieldMetaEntity();
            masterDt.setMeta(f);
            f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER});
        }
        if (subData != null && subData.size() > 0) {
            for (String sub_table_name : subData.keySet()) {
                if (subData.get(sub_table_name) != null) {
                    FieldSetEntity fs = new FieldSetEntity();
                    fs.setMeta(fse.getMeta());
                    fs.setValue(CmnConst.IS_USED, fse.getString(sub_table_name + "_is_used"));
                    fs.setValue(CmnConst.PROPERTY_UUID, fse.getString(sub_table_name + "_properties"));
                    fs.setValue("org_level_uuid", fse.getString("org_level_uuid"));
                    fs.setValue(CmnConst.EFFECTIVE_START_DATE, fse.getString(CmnConst.EFFECTIVE_START_DATE));
                    fs.setValue(CmnConst.EFFECTIVE_END_DATE, fse.getString(CmnConst.EFFECTIVE_END_DATE));
                    fs = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fs);
                    DataTableEntity subDataTable = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), subData.get(sub_table_name));
                    fs.addSubDataTable(subDataTable);
                    masterDt.addFieldSetEntity(fs);
                }
            }
        }
        boolean re = dao.add(masterDt);
        //刷新单位的数据权限
        if (re) {
            DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fse.getString(CmnConst.ORG_LEVEL_UUID));
        }
        return re;
    }
 
    /**
     * 新增数据策略
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean saveDataPermission(FieldSetEntity fse) throws BaseException {
        if (StringUtils.isEmpty(fse.getUUID())) {
            DataTableEntity listTable = dao.listTable(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid=?", new Object[]{fse.getString(CmnConst.ORG_LEVEL_UUID)});
            if (!BaseUtil.dataTableIsEmpty(listTable)) { //判断是否存在该公司策略
                throw new BaseException(SystemCode.SYSTEM_DATA_PERMISSION_ALREADY_EXIST_ADD_FAIL.getValue(), SystemCode.SYSTEM_DATA_PERMISSION_ALREADY_EXIST_ADD_FAIL.getText(), this.getClass(), "addDataPermission");
            }
        }
        Map<String, DataTableEntity> subData = fse.getSubData();
        DataTableEntity masterDt = new DataTableEntity();
        if (fse.getSubData().size() != 0) {
            FieldMetaEntity f = new FieldMetaEntity();
            masterDt.setMeta(f);
            f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER});
        }
        if (subData != null && subData.size() > 0) {
            for (String sub_table_name : subData.keySet()) {
                if (subData.get(sub_table_name) != null) {
                    FieldSetEntity fs = new FieldSetEntity();
                    fs.setMeta(fse.getMeta());
                    fs.setValue(CmnConst.IS_USED, fse.getString(sub_table_name + "_is_used"));
                    fs.setValue(CmnConst.PROPERTY_UUID, fse.getString(sub_table_name + "_properties"));
                    fs.setValue("org_level_uuid", fse.getString("org_level_uuid"));
                    fs.setValue(CmnConst.EFFECTIVE_START_DATE, fse.getString(CmnConst.EFFECTIVE_START_DATE));
                    fs.setValue(CmnConst.EFFECTIVE_END_DATE, fse.getString(CmnConst.EFFECTIVE_END_DATE));
                    fs = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fs);
                    DataTableEntity subDataTable = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), subData.get(sub_table_name));
                    fs.addSubDataTable(subDataTable);
                    masterDt.addFieldSetEntity(fs);
                }
            }
        }
        boolean re = dao.add(masterDt);
        //刷新单位的数据权限
        if (re) {
            DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fse.getString(CmnConst.ORG_LEVEL_UUID));
        }
        return re;
    }
 
    /**
     * 数据策略修改提交
     * cheng 2021年4月16日16:28:39
     * 批量修改数据策略
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    @Override
    public boolean updateDataPermission(FieldSetEntity fse) throws BaseException {
        if (fse != null ) {
            String effective_start_date = fse.getString(CmnConst.EFFECTIVE_START_DATE);
            String effective_end_date = fse.getString(CmnConst.EFFECTIVE_END_DATE);
            //罗鑫2021年3月1日 开始时间不能大于结束时间  cheng 2021年4月16日16:52:14 copy
            if (BaseUtil.strIsNull(effective_start_date)) {
                throw new BaseException(com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getValue(), com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getText(), this.getClass(), "public boolean updateDataPermissionBasics(FieldSetEntity fse)throws BaseException");
            }
            if (!BaseUtil.strIsNull(effective_end_date)) {
                if (BaseUtil.temporalComparison(effective_start_date, effective_end_date)) {
                    throw new BaseException(com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_FAIL.getValue(), com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_FAIL.getText(), this.getClass(), "public boolean updateDataPermissionBasics(FieldSetEntity fse)throws BaseException");
                }
            }
            DataTableEntity master_add = new DataTableEntity();
            DataTableEntity master_update = new DataTableEntity();
            Map<String, DataTableEntity> subData = fse.getSubData();
            if (subData != null && subData.size() > 0) {
                for (String sub_table_name : subData.keySet()) {
                    if (subData.get(sub_table_name) != null) {
                        String uuid = fse.getString("master_uuid_" + fse.getString(sub_table_name + "_properties"));
                        FieldSetEntity fs = new FieldSetEntity();
                        fs.setMeta(fse.getMeta());
                        fs.setValue(CmnConst.IS_USED, fse.getString(sub_table_name + "_is_used"));
                        fs.setValue(CmnConst.PROPERTY_UUID, fse.getString(sub_table_name + "_properties"));
                        fs.setValue(CmnConst.UUID, uuid);
                        fs.setValue("org_level_uuid", fse.getString("org_level_uuid"));
                        fs.setValue(CmnConst.EFFECTIVE_START_DATE, fse.getString(CmnConst.EFFECTIVE_START_DATE));
                        fs.setValue(CmnConst.EFFECTIVE_END_DATE, fse.getString(CmnConst.EFFECTIVE_END_DATE));
                        DataTableEntity subDataTable = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), subData.get(sub_table_name));
                        fs.addSubDataTable(subDataTable);
                        if(StringUtils.isEmpty(uuid)){
                            fs = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fs);
                            master_add.addFieldSetEntity(fs);
                        }else{
                            fs = BaseUtil.updatedRegeneratorAndUpdateTime(SpringMVCContextHolder.getCurrentUser(), fs);
                            master_update.addFieldSetEntity(fs);
                        }
                    }
                }
            }
            if(master_add.getRows()>0){
                dao.add(master_add);
            }
            if(master_update.getRows()>0){
                dao.add(master_update);
            }
            return true;
        }
        return false;
    }
 
    /**
     * 修改数据策略
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean updateDataPermissionBasics(FieldSetEntity fse) throws BaseException {
        BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse);
        String effective_start_date = fse.getString(CmnConst.EFFECTIVE_START_DATE);
        String effective_end_date = fse.getString(CmnConst.EFFECTIVE_END_DATE);
        //罗鑫2021年3月1日 开始时间不能大于结束时间
        if (BaseUtil.strIsNull(effective_start_date)) {
            throw new BaseException(com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getValue(), com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getText(), this.getClass(), "public boolean updateDataPermissionBasics(FieldSetEntity fse)throws BaseException");
        }
        if (!BaseUtil.strIsNull(effective_end_date)) {
            if (BaseUtil.temporalComparison(effective_start_date, effective_end_date)) {
                throw new BaseException(com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_FAIL.getValue(), com.product.admin.config.SystemCode.SYSTEM_VALID_TIME_FAIL.getText(), this.getClass(), "public boolean updateDataPermissionBasics(FieldSetEntity fse)throws BaseException");
            }
        }
        DataTableEntity dataTableEntity = dao.listTable(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid = ?", new String[]{fse.getString("org_level_uuid")});
        if (effective_end_date != null && effective_end_date.length() > 0) {
            for (int i = 0, length = dataTableEntity.getRows(); i < length; i++) {
                dataTableEntity.getFieldSetEntity(i).setValue(CmnConst.EFFECTIVE_START_DATE, fse.getString(CmnConst.EFFECTIVE_START_DATE));
                dataTableEntity.getFieldSetEntity(i).setValue(CmnConst.EFFECTIVE_END_DATE, effective_end_date);
            }
        } else {
            for (int i = 0, length = dataTableEntity.getRows(); i < length; i++) {
                dataTableEntity.getFieldSetEntity(i).setValue(CmnConst.EFFECTIVE_START_DATE, fse.getString(CmnConst.EFFECTIVE_START_DATE));
            }
        }
 
        boolean re = dao.update(dataTableEntity);
        //刷新单位的数据权限
        if (re) {
            DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fse.getString(CmnConst.ORG_LEVEL_UUID));
        }
        return re;
    }
 
    /**
     * 删除数据策略
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean deleteDataPermission(FieldSetEntity fse) throws BaseException {
 
        DataTableEntity dt = dao.listTable(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid=?", new Object[]{fse.getString(CmnConst.ORG_LEVEL_UUID)}, null, null, Integer.MAX_VALUE, 1, true);
        Object[] split = dt.getUuids();
        if (BaseUtil.dataTableIsEmpty(dt) || dt.getUuids() == null || split.length <= 0) {
            throw new BaseException(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText(), this.getClass(), "deleteDataPermission");
        }
        String placeholder = "";
        for (int i = 0; i < split.length; i++) {
            placeholder += ",?";
        }
        boolean re = dao.delete(fse.getTableName(), "uuid in (" + placeholder.substring(1) + ")", split);
        //刷新单位的数据权限
        if (re) {
            DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fse.getString(CmnConst.ORG_LEVEL_UUID));
        }
        return re;
    }
 
    /**
     * @param fse
     * @return
     * @throws BaseException
     */
    public DataTableEntity listModulesAndFunctions(FieldSetEntity fse) throws BaseException {
        return rolesService.listFunctionRoleForClients(null, fse.getString("client_role_uuid"), false);
    }
 
    /**
     * 修改数据策略详情
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean updateDataPermissionDetails(FieldSetEntity fse) throws BaseException {
        int is_used = fse.getInteger(fse.getTableName() + "_is_used");
        String property_uuid = fse.getString(fse.getTableName() + "_properties");
        //针对哪家公司进行配置:关联组织架构UUID
        String org_level_uuid = fse.getString("org_level_uuid");
        FieldSetEntity master = new FieldSetEntity();
        master.setTableName(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER);
        master.setValue(CmnConst.IS_USED, is_used);
        master.setValue(CmnConst.PROPERTY_UUID, property_uuid);
        master.setValue("org_level_uuid", org_level_uuid);
        Map<String, DataTableEntity> details = fse.getSubData();
        DataTableEntity dt = details.get(fse.getSubTableNames()[0]);
        boolean status = false;
        String strategy_uuid;
        if (dt != null && dt.getRows() > 0) {
            if (dt.getFieldSetEntity(0).getString(CmnConst.STRATEGY_UUID) == null) {
                master = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), master);
                strategy_uuid = dao.add(master);
                status = true;
            } else {
                strategy_uuid = dt.getFieldSetEntity(0).getString(CmnConst.STRATEGY_UUID);
                master.setValue("uuid", strategy_uuid);
                master = BaseUtil.updatedRegeneratorAndUpdateTime(SpringMVCContextHolder.getCurrentUser(), master);
                status = dao.update(master);
            }
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fs = dt.getFieldSetEntity(i);
                fs.setTableName(fse.getTableName());
                if (StringUtils.isEmpty(fs.getString("uuid")) && !"del".equals(fs.getString("type"))) {
                    fs.setValue(CmnConst.STRATEGY_UUID, strategy_uuid);
                    fs = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fs);
                    dao.add(fs);
                } else if (!StringUtils.isEmpty(fs.getString("uuid")) && !"del".equals(fs.getString("type"))) {
                    fs.remove("created_utc_datetime");
                    fs.remove("updated_utc_datetime");
                    fs = BaseUtil.updatedRegeneratorAndUpdateTime(SpringMVCContextHolder.getCurrentUser(), fs);
                    dao.update(fs);
                } else {
                    dao.delete(fs.getTableName(), "uuid=?", new String[]{fs.getString("uuid")});
                }
            }
            //更新数据权限缓存
            //刷新单位的数据权限
            DataPoolCacheImpl.getInstance().reFreshCacheDataByPermission(fse.getString(CmnConst.ORG_LEVEL_UUID));
            return status;
        }
        return false;
    }
}