shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
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
package com.product.org.admin.service;
 
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
 
 
import com.product.admin.config.SystemCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.alibaba.druid.util.StringUtils;
import com.product.org.admin.config.CmnConst;
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.PermissionService;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.service.support.QueryFilterService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.org.admin.service.idel.IOperationReplaceService;
import com.product.util.BaseUtil;
 
/**
 * 
 * Copyright LX-BASE
 * 
 * @Title: OperationReplaceService
 * @Project: LX-BASE-SERVER
 * @Date: 2020年5月29日 上午10:59:05
 * @Author: 郑盟
 * @Description:业务代理设置,对应业务代理表
 */
@Component
public class OperationReplaceService extends AbstractBaseService implements IOperationReplaceService {
    @Autowired
    public BaseDao baseDao;
    @Autowired
    PermissionService permissionService;
    @Autowired
    public QueryFilterService queryFilterService;
 
    /**
     * 业务操作代替列表查询
     */
    public DataTableEntity listReplace(FieldSetEntity fs) throws BaseException {
        String dataFilter = permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID);
        // 郑盟 2020年12月22日 下午18:34
        String queryFilter = "";
        if (!BaseUtil.dataTableIsEmpty(fs.getSubDataTable("systemSeniorQueryString"))) {
            if (!StringUtils.isEmpty(dataFilter)) {
                queryFilter = " and" + queryFilterService.getQueryFilter(fs);
            } else {
                queryFilter = queryFilterService.getQueryFilter(fs);
            }
        }
        return baseDao.listTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, dataFilter + queryFilter, new String[] {},
                new String[] {}, null, fs.getInteger("pagesize"), fs.getInteger("cpage"));
    }
 
    /**
     * 业务操作代替详情查询
     */
    public FieldSetEntity findReplace(FieldSetEntity fs) throws BaseException {
        return baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, fs.getString("uuid"), true);
 
    }
 
    /**
     * 业务操作代替新增
     */
    @Transactional
    public String addReplaceList(FieldSetEntity fs) throws BaseException {
        fs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        String org_level_uuid = fs.getString(CmnConst.ORG_LEVEL_UUID);
        if (StringUtils.isEmpty(org_level_uuid)) {
            fs.setValue(CmnConst.ORG_LEVEL_UUID, SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid());
        }
        //罗鑫2021年3月1日 开始时间不能大于结束时间
        String effective_utc_start_date = fs.getString("effective_utc_start_date");
        if(BaseUtil.strIsNull(effective_utc_start_date)){
            throw new BaseException(SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getText(),this.getClass(),"public String addReplaceList(FieldSetEntity fs) throws BaseException");
        }
        String effective_utc_end_date = fs.getString("effective_utc_end_date");
        if(!BaseUtil.strIsNull(effective_utc_end_date)) {
            if (BaseUtil.temporalComparison(effective_utc_start_date, effective_utc_end_date)) {
                throw new BaseException(SystemCode.SYSTEM_VALID_TIME_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_FAIL.getText(), this.getClass(), "public String addReplaceList(FieldSetEntity fs) throws BaseException");
            }
        }
//            DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS);
//        DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS);
//        if (subDataTable != null) {
//            for (int i = 0; i < subDataTable.getRows(); i++) {
//                FieldSetEntity subfs = subDataTable.getFieldSetEntity(i);
//                subfs.setValue(CmnConst.ORG_LEVEL_UUID, fs.getString(CmnConst.ORG_LEVEL_UUID));
//                String addReplaceSubRelation = addReplaceSubRelation(subfs, fs.getString(CmnConst.EFFECTIVE_UTC_START_DATE),
//                        fs.getString(CmnConst.EFFECTIVE_UTC_END_DATE));
//                if (addReplaceSubRelation == null)
//                    return null;
//            }
//        }
 
        return baseDao.add(fs);
    }
 
    /**
     * 业务操作代替修改
     * 
     * @param fs
     * @return boolean
     */
    @Transactional
    public boolean updateReplaceList(FieldSetEntity fs) throws BaseException {
//        DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS);
//        for (int i = 0; i < subDataTable.getRows(); i++) {
//            FieldSetEntity subfs = subDataTable.getFieldSetEntity(i);
//            FieldSetEntity exampleSubFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS,
//                    subfs.getString("uuid"), false);
//            if (exampleSubFs == null) {
//                // 新增子表
//                subfs.setValue(CmnConst.ORG_LEVEL_UUID, fs.getString(CmnConst.ORG_LEVEL_UUID));
//                String addReplaceSubRelation = addReplaceSubRelation(subfs, fs.getString(CmnConst.EFFECTIVE_UTC_START_DATE),
//                        fs.getString(CmnConst.EFFECTIVE_UTC_END_DATE));
//                if (addReplaceSubRelation == null)
//                    return false;
//                continue;
//            }
//            String type = subfs.getString("~type~");
//            if (type != null && "del".equals(type)) {
//                // 删除子表
//                boolean delReplaceSubRelation = delReplaceSubRelation(exampleSubFs);
//                if (!delReplaceSubRelation)
//                    return false;
//                continue;
//            }
//            String role_uuid = exampleSubFs.getString(CmnConst.ROLE_UUID);
//            boolean forRoleUuidArr = forRoleUuidArr(role_uuid, i, fs, subfs);
//            // 前端发过啊里的修改后的数据替换人uuid
//            boolean forStaff = forStaff(exampleSubFs, subfs, role_uuid);
//            if (!forStaff||!forRoleUuidArr)
//                return false;
//            subfs.setValue(CmnConst.ROLE_UUID, role_uuid);
//        }
        //罗鑫2021年3月1日 开始时间不能大于结束时间
        String effective_utc_start_date = fs.getString("effective_utc_start_date");
        if(BaseUtil.strIsNull(effective_utc_start_date)){
            throw new BaseException(SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getText(),this.getClass(),"public boolean updateReplaceList(FieldSetEntity fs) throws BaseException");
        }
        String effective_utc_end_date = fs.getString("effective_utc_end_date");
        if(!BaseUtil.strIsNull(effective_utc_end_date)) {
            if (BaseUtil.temporalComparison(effective_utc_start_date, effective_utc_end_date)) {
                throw new BaseException(SystemCode.SYSTEM_VALID_TIME_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_FAIL.getText(), this.getClass(), "public boolean updateReplaceList(FieldSetEntity fs) throws BaseException");
            }
        }
        return updateReplaceTime(fs);
    }
 
    private boolean forStaff(FieldSetEntity exampleSubFs, FieldSetEntity subfs, String role_uuid) {
        // 修改替换人后staffs表role_uuids的处理
        String replace_staff_uuid = exampleSubFs.getString(CmnConst.ON_BEHALF_STAFF_UUID);
        String staff_uuid_arr = subfs.getString(CmnConst.ON_BEHALF_STAFF_UUID);
        String[] role_uuid_arr = role_uuid.split(",");
        if (!replace_staff_uuid.equals(staff_uuid_arr)) {
            String[] split = replace_staff_uuid.split(",");
            for (int j = 0; j < split.length; j++) {
                String staff_uuid = split[j];
                FieldSetEntity staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false);
                String[] delArrVal = delArrVal(staffsFs.getString(CmnConst.ROLE_UUIDS).split(","), role_uuid_arr);
                String sqls = "UPDATE product_sys_staffs SET  role_uuids = ? where uuid=?";
                boolean staffUpdate = baseDao.executeUpdate(sqls,
                        new String[] { String.join(",", delArrVal), staff_uuid });
                if (!staffUpdate)
                    return staffUpdate;
            }
            String[] split2 = staff_uuid_arr.split(",");
            for (int j = 0; j < split2.length; j++) {
                String staff_uuid = split2[j];
                ArrayList<String> list = new ArrayList<>();
                FieldSetEntity staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false);
                list.add(staffsFs.getString(CmnConst.ROLE_UUIDS));
                list.add(role_uuid);
                String sqls = "UPDATE product_sys_staffs SET  role_uuids = ? where uuid=?";
                boolean staffUpdate = baseDao.executeUpdate(sqls, new String[] { String.join(",", list), staff_uuid });
                if (!staffUpdate)
                    return staffUpdate;
            }
 
        }
        return true;
    }
 
    private boolean forRoleUuidArr(String role_uuid, int i, FieldSetEntity fs, FieldSetEntity subfs) {
        fs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
        String[] role_uuid_arr = role_uuid.split(",");
        String sql = "UPDATE product_sys_role SET effective_start_date = ?,effective_end_date=? where uuid =?";
        for (int j = 0; j < role_uuid_arr.length; j++) {
            boolean executeUpdate = baseDao.executeUpdate(sql, new String[] { fs.getString(CmnConst.EFFECTIVE_UTC_START_DATE),
                    fs.getString(CmnConst.EFFECTIVE_UTC_END_DATE), role_uuid_arr[i] });
            if (!executeUpdate)
                return executeUpdate;
            // 保存角色权限表表
            FieldSetEntity fun_permissionFs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_PERMISSION,
                    "role_uuid=?", new String[] { role_uuid_arr[i] }, false);
            fun_permissionFs.setValue(CmnConst.FUNCTION_UUID, subfs.getString(CmnConst.FUNCTION_UUID));
            fun_permissionFs.setValue(CmnConst.BUTTON_UUID, subfs.getString(CmnConst.BUTTON_UUID));
            fun_permissionFs.setValue(CmnConst.ORG_LEVEL_UUID, fs.getString(CmnConst.ORG_LEVEL_UUID));
            fun_permissionFs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
            fun_permissionFs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
            // 保存功能许可表
            boolean permissionUpdate = baseDao.update(fun_permissionFs);
            if (!permissionUpdate)
                return permissionUpdate;
        }
        return true;
    }
 
    private boolean updateReplaceTime(FieldSetEntity fs) {
        String start_time = fs.getString("effective_start_time_zone");
        // 郑盟 2020年12月17日 下午16:05
        if (StringUtils.isEmpty(start_time)) {
            fs.setValue("effective_start_time_zone", null);
        }
        String end_time = fs.getString("effective_end_time_zone");
        // 郑盟 2020年12月17日 下午16:05
        if (StringUtils.isEmpty(end_time)) {
            fs.setValue("effective_end_time_zone", null);
        }
        return baseDao.update(fs);
    }
 
    @Transactional
    public boolean deleteReplaceList(FieldSetEntity fs) throws BaseException {
//        String uuid = fs.getString("uuid");
//        FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, uuid, true);
//        DataTableEntity subDataTable = fss.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS);
        boolean delete = baseDao.delete(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, "uuid=?", new String[] { fs.getString("uuid") });
//        for (int i = 0; i < subDataTable.getRows(); i++) {
//            FieldSetEntity detailsFs = subDataTable.getFieldSetEntity(i);
//            boolean delReplaceSubRelation = delReplaceSubRelation(detailsFs);
//            if (!delReplaceSubRelation)
//                return false;
//        }
        return delete;
    }
 
    /**
     * 新增子表
     * 
     * @param detailsFs
     * @return
     * @throws BaseException
     */
    public String addReplaceSubRelation(FieldSetEntity detailsFs, String effective_start, String effective_end)
            throws BaseException {
        String staff_uuid_arr = detailsFs.getString(CmnConst.ON_BEHALF_STAFF_UUID);
        if (staff_uuid_arr == null) {
            return null;
        }
        String[] split = staff_uuid_arr.split(",");
        String add = null;
 
        StringBuilder str = new StringBuilder();// 定义变长字符串
        Random random = null;
        try {
            random = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return null;
        }
        // 随机生成源数字,并添知加到字符串
        for (int j = 0; j < 8; j++) {
            if (random != null) {
                str.append(random.nextInt(10));
            }
        }
        FieldSetEntity roleFs = new FieldSetEntity();
        FieldMetaEntity fm = new FieldMetaEntity();
        fm.setTableName(new Object[] { CmnConst.PRODUCT_SYS_ROLE });
        roleFs.setMeta(fm);
        roleFs.setValue("role_description", "业务操作代替创建role_" + str.toString());
        roleFs.setValue(CmnConst.IS_USED, "1");
        roleFs.setValue(CmnConst.ORG_LEVEL_UUID, detailsFs.getString(CmnConst.ORG_LEVEL_UUID));
        roleFs.setValue(CmnConst.EFFECTIVE_START_DATE, effective_start);
        roleFs.setValue(CmnConst.EFFECTIVE_END_DATE, effective_end);
 
        roleFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        roleFs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
 
        String default_language = SpringMVCContextHolder.getCurrentUser().getDefault_language();
        DataTableEntity languageDt = parseInternational("role_name", default_language, "role_" + str.toString());
        roleFs.addSubDataTable(languageDt);
        String roleAdd = baseDao.add(roleFs);
        // 保存角色权限表表
 
        FieldSetEntity fun_permissionFs = new FieldSetEntity();
        fm.setTableName(new Object[] { CmnConst.PRODUCT_SYS_FUNCTION_PERMISSION });
        fun_permissionFs.setMeta(fm);
        fun_permissionFs.setValue(CmnConst.ROLE_UUID, roleAdd);
        fun_permissionFs.setValue(CmnConst.FUNCTION_UUID, detailsFs.getString(CmnConst.FUNCTION_UUID));
        fun_permissionFs.setValue(CmnConst.BUTTON_UUID, detailsFs.getString(CmnConst.BUTTON_UUID));
        fun_permissionFs.setValue(CmnConst.ORG_LEVEL_UUID, detailsFs.getString(CmnConst.ORG_LEVEL_UUID));
        fun_permissionFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fun_permissionFs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        add = baseDao.add(fun_permissionFs);
        if (add == null) {
            return add;
        }
        // 保存功能许可表
        for (int i = 0; i < split.length; i++) {
            String staff_uuid = split[i];
            FieldSetEntity staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false);
            if (staffsFs == null || staffsFs.getString("uuid") == null) {
                return null;
            }
            String role_uuids = staffsFs.getString(CmnConst.ROLE_UUIDS);
            ArrayList<String> list = new ArrayList<>();
            list.add(role_uuids);
            list.add(roleAdd);
            String role_uuidarr = String.join(",", list);
            String sql = "UPDATE product_sys_staffs SET  role_uuids = ? where uuid=?";
            boolean executeUpdate = baseDao.executeUpdate(sql, new String[] { role_uuidarr, staff_uuid });
            // 将新增的权限放进
            if (!executeUpdate)
                return null;
        }
        detailsFs.setValue(CmnConst.ROLE_UUID, roleAdd);
        return add;
    }
 
    /**
     * 删除子表以及还原其他表单数据
     * 
     * @param detailsFs
     * @return
     * @throws BaseException
     */
    public boolean delReplaceSubRelation(FieldSetEntity detailsFs) throws BaseException {
        String role_uuid = detailsFs.getString(CmnConst.ROLE_UUID);
        String[] role_uuid_arr = role_uuid.split(",");
        String replace_staff_uuid_arr = detailsFs.getString(CmnConst.ON_BEHALF_STAFF_UUID);
        String[] split = replace_staff_uuid_arr.split(",");
        boolean executeUpdate = false;
        for (int i = 0; i < split.length; i++) {
            String replace_staff_uuid = split[i];
            FieldSetEntity replace_staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, replace_staff_uuid, false);
            if (null == replace_staffsFs) {
                continue;
            }
            String role_uuids = replace_staffsFs.getString(CmnConst.ROLE_UUIDS);
            String[] role_uuidArr = role_uuids.split(",");
            String[] delArrVal = delArrVal(role_uuidArr, role_uuid_arr);
            String join = String.join(",", delArrVal);
 
            String sql = "UPDATE product_sys_staffs SET  role_uuids = ? where uuid =?";
            executeUpdate = baseDao.executeUpdate(sql, new String[] { join, replace_staffsFs.getString("uuid") });
            if (!executeUpdate)
                return executeUpdate;
        }
 
        boolean delPermission = baseDao.delete(CmnConst.PRODUCT_SYS_FUNCTION_PERMISSION,
                BaseUtil.buildQuestionMarkFilter(CmnConst.ROLE_UUID, role_uuid_arr.length, true), role_uuid_arr);
        if (!delPermission)
            return false;
        return baseDao.delete(CmnConst.PRODUCT_SYS_ROLE,
                BaseUtil.buildQuestionMarkFilter("uuid", role_uuid_arr.length, true), role_uuid_arr);
    }
 
//删除数组中指定元素
    public String[] delArrVal(String[] arr, String[] val) {
        List<String> list = new LinkedList<>();
        for (String str : arr) { // 处理第一个数组,list里面的值为1,2,3,4
            if (!list.contains(str.trim())) {
                list.add(str.trim());
            }
        }
        for (String str : val) { // 如果第二个数组存在和第一个数组相同的值,就删除
            if (list.contains(str.trim())) {
                list.remove(str.trim());
            }
        }
        String[] result = {}; // 创建空数组
        return list.toArray(result); // List to Array
    }
 
    /**
     * 国际化封装
     * 
     * @param currentField  当前字段
     * @param language_code 语言代码
     * @param languageValue 语言内容
     * @return
     * @throws BaseException
     */
    public DataTableEntity parseInternational(String currentField, String language_code, String languageValue)
            throws BaseException {
        FieldSetEntity fseLanguage = new FieldSetEntity();
        fseLanguage.setTableName(currentField);
        fseLanguage.setValue(language_code, languageValue);
        DataTableEntity languageDt = new DataTableEntity();
        languageDt.addFieldSetEntity(fseLanguage);
        return languageDt;
    }
}