kimi
2020-03-31 74472c9d22dddcb41383794caf0011043b20f817
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
package com.highdatas.mdm.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.highdatas.mdm.controller.MasterAuthorController;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.mapper.MasterAuthorMapper;
import com.highdatas.mdm.pojo.ActivitiStatus;
import com.highdatas.mdm.pojo.MasterAuthorType;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.ContentBuilder;
import com.highdatas.mdm.util.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kimi
 * @since 2020-03-23
 */
@Service
public class MasterAuthorServiceImpl extends ServiceImpl<MasterAuthorMapper, MasterAuthor> implements IMasterAuthorService {
    public static final String characterId = "characterId";
    @Autowired
    IMasterAuthorDetailService authorDetailService;
    @Autowired
    ITUserRoleService userRoleService;
    @Autowired
    ISysMenuService menuService;
    @Autowired
    ISysFieldService fieldService;
    @Autowired
    IMaintainFieldService maintainFieldService;
    @Autowired
    IMaintainService maintainService;
    @Autowired
    IFlowsService flowsService;
 
    @Override
    public HashMap<String, MasterAuthor> merageRoleAuthor(List<String> roleIds) {
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds));
        HashMap<String, MasterAuthor> resultMap = new HashMap<>();
 
        for (MasterAuthor masterAuthor : masterAuthors) {
            List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().eq(Constant.PARENT_ID, masterAuthor.getId()));
            masterAuthor.setFields(masterAuthorDetails);
 
            String key = DbUtils.getFieldRedisKey(masterAuthor.getTableName(), masterAuthor.getMaintainFieldId());
            MasterAuthor preMerageMasterAuthor = resultMap.get(key);
            if (preMerageMasterAuthor == null) {
                resultMap.put(key, masterAuthor);
            }
            else {
                preMerageMasterAuthor = merage(preMerageMasterAuthor, masterAuthor);
                resultMap.put(key, preMerageMasterAuthor);
            }
        }
        return resultMap;
    }
 
    @Override
    public List<SysMenu> getMenu(String userId) {
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, userId));
        if (masterAuthors.size() == 0) {
            //user 获取role
            List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
            List<String> roleIds = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
            masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds));
        }
        List<String> menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList());
        LinkedHashSet<String> strings = new LinkedHashSet<>(menuIds);
        LinkedHashSet<String> byParentId = menuService.getByParentId(strings);
        List<SysMenu> sysMenus = menuService.selectBatchIds(byParentId);
        return sysMenus;
    }
 
    @Override
    public List<SysField> getField(String userId, String maintainId) {
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null){
            return new ArrayList<>();
        }
        boolean isAll = checkUnFilterAuthor(userId, maintain.getTableName());
        if (isAll) {
            List<SysField> total = fieldService.getFieldByMaintain(maintainId);
            return total;
        }
 
        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
        if (maintainField == null){
            return new ArrayList<>();
        }
        String maintainFieldId = maintainField.getId();
 
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, userId).eq("maintain_field_id", maintainFieldId));
        if (masterAuthors.size() == 0) {
            //user 获取role
            List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
            List<String> roleIds = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
            masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds).eq("maintain_field_id", maintainFieldId));
        }
        if (masterAuthors.isEmpty()) {
            return null;
        }
        List<String> authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList());
        List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().in(Constant.PARENT_ID, authorIds));
        Set<String> codes = masterAuthorDetails.stream().map(masterAuthorDetail -> masterAuthorDetail.getField()).collect(Collectors.toSet());
        if (codes.isEmpty()) {
            return null;
        }
        Wrapper<SysField> wrapper = new EntityWrapper<SysField>().in(Constant.FIELD, codes).eq("table_name", maintain.getTableName());
 
        if (!maintainFieldId.equalsIgnoreCase(Constant.Default)) {
            wrapper.eq("maintain_field_id", maintainFieldId);
        }else {
            wrapper.isNull("maintain_field_id");
        }
        wrapper.orderBy("order_no");
 
        List<SysField> fieldList = fieldService.selectList(wrapper);
        return fieldList;
    }
 
    private boolean checkUnFilterAuthor(String userId, String tableName) {
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, userId).eq("table_name", tableName));
        long isAll = masterAuthors.stream().filter(masterAuthor -> masterAuthor.getMaintainFieldId().equalsIgnoreCase(Constant.All)).count();
        if (isAll > 0) {
            return true;
        }
        if (!masterAuthors.isEmpty()) {
            return false;
        }
        Set<String> roleByUser = DbUtils.getRoleByUser(userId);
        isAll = selectCount(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleByUser).eq("table_name", tableName).eq("maintain_field_id", Constant.All_UPCASE));
        if (isAll > 0) {
            return true;
        }
 
        return false;
    }
 
    @Override
    public boolean checkMaintainAuthor(String userId, String maintainId) {
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null) {
            return false;
        }
        int checked = 0;
        boolean isAll = checkUnFilterAuthor(userId, maintain.getTableName());
        if (isAll){
            return true;
 
        }
        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
        if (maintainField == null) {
            return false;
        }
        //get user author
        checked = selectCount(new EntityWrapper<MasterAuthor>()
                .eq(Constant.TYPE, MasterAuthorType.user)
                .eq(MasterAuthorController.character_id, userId)
                .eq("table_name", maintain.getTableName())
                .eq("maintain_field_id", maintainField.getId()));
        if (checked > 0) {
            return true;
        }
        List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
        Set<String> collect = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toSet());
        checked = selectCount(new EntityWrapper<MasterAuthor>()
                .eq(Constant.TYPE, MasterAuthorType.role)
                .in(MasterAuthorController.character_id, collect)
                .eq("table_name", maintain.getTableName())
                .eq("maintain_field_id", maintainField.getId()));
        if (checked > 0) {
            return true;
        }
        return false;
    }
 
    @Override
    public String getFilter(String userId, String maintainId) {
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null){
            return Constant.WHERE_DEFAULTUN;
        }
        boolean isAll = checkUnFilterAuthor(userId, maintain.getTableName());
        if (isAll) {
            return Constant.WHERE_DEFAULT;
        }
        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
        String maintainFieldId = maintainField.getId();
 
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>()
                .eq("table_name", maintain.getTableName())
                .eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, userId).eq("maintain_field_id", maintainFieldId));
        if (masterAuthors.size() == 0) {
            //user 获取role
            List<TUserRole> roles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq("user_id", userId));
            List<String> roleIds = roles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
            masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq("table_name", maintain.getTableName()).eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds).eq("maintain_field_id", maintainFieldId));
        }
        if (masterAuthors.isEmpty()){
            return Constant.WHERE_DEFAULTUN;
        }
        List<String> authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList());
        List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().in(Constant.PARENT_ID, authorIds));
        if (masterAuthorDetails.isEmpty()) {
            return Constant.WHERE_DEFAULTUN;
        }
        HashMap<String, List<String>> segmentMap = new HashMap<>();
        HashMap<String, Boolean> segmentAllMap = new HashMap<>();
 
        for (MasterAuthorDetail detail : masterAuthorDetails) {
            String field = detail.getField();
            Boolean all = detail.getAll();
            if (!(segmentAllMap.containsKey(field) && segmentAllMap.get(field))) {
                segmentAllMap.put(field, all);
            }
 
            Boolean preAll = segmentAllMap.get(field);
            if (preAll) {
                if (segmentMap.containsKey(field)) {
                    segmentMap.remove(field);
                }
                continue;
            }
 
            String val = detail.getVal();
            List<String> split = DbUtils.split(val);
            segmentMap.put(field,split);
        }
        ContentBuilder builder = new ContentBuilder(Constant.AND);
        Set<String> keySet = segmentMap.keySet();
        for (String code : keySet) {
            List<String> vals = segmentMap.get(code);
            String val = vals.stream()
                    .filter(s -> !StringUtils.isEmpty(s))
                    .map(s -> DbUtils.quotedStr(s)).collect(Collectors.joining(Constant.COMMA));
            String format = MessageFormat.format(Constant.InSql, code, val);
            builder.append(format);
        }
        String filter = builder.toString();
        return filter;
    }
 
    @Override
    public Maintain getMaxVersionMaintain(String userId, String tableName) {
        List<Maintain> maintainList = maintainService.selectList(new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no desc"));
        for (Maintain maintain : maintainList) {
            ActivitiStatus status = flowsService.getStatusByBusinessId(maintain.getId());
            if (!status.equals(ActivitiStatus.open)) {
                continue;
            }
            boolean b = checkMaintainAuthor(userId, maintain.getId());
            if (b) {
                return maintain;
            }
        }
        return null;
    }
 
    private MasterAuthor merage(MasterAuthor preMerageMasterAuthor, MasterAuthor masterAuthor) {
        // table name  masterField 一样 只有字段不同了
        List<MasterAuthorDetail> preFields = preMerageMasterAuthor.getFields();
        Map<String, MasterAuthorDetail> fieldMap = masterAuthor.getFields().stream().collect(Collectors.toMap(MasterAuthorDetail::getField, masterAuthorDetail -> masterAuthorDetail));
 
        HashSet<String> valSet = new HashSet<>();
        for (MasterAuthorDetail preField : preFields) {
            valSet.clear();
            String fieldId = preField.getField();
            String val = preField.getVal();
            if (val.equalsIgnoreCase(Constant.All)) {
                continue;
            }
            MasterAuthorDetail detail = fieldMap.get(fieldId);
            if (detail == null) {
                continue;
            }
            String preAddVal = detail.getVal();
            if (preAddVal.equalsIgnoreCase(Constant.All)) {
                preField.setVal(preAddVal);
                continue;
            }
 
            valSet.addAll(DbUtils.split(val));
            valSet.addAll(DbUtils.split(preAddVal));
 
            String resultVal = valSet.stream().collect(Collectors.joining(Constant.SEMICOLON));
            preField.setVal(resultVal);
        }
        return preMerageMasterAuthor;
    }
}