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.Character; 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; /** *

* 服务实现类 *

* * @author kimi * @since 2020-03-23 */ @Service public class MasterAuthorServiceImpl extends ServiceImpl 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 merageRoleAuthor(Set roleIds) { List masterAuthors = selectList(new EntityWrapper().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds)); HashMap resultMap = new HashMap<>(); for (MasterAuthor masterAuthor : masterAuthors) { List masterAuthorDetails = authorDetailService.selectList(new EntityWrapper().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 getMenu(Character character) { MasterAuthorType type = character.getType(); List masterAuthors = null; switch (type){ case role: masterAuthors = getRoleAuthors(character.getId(), null); break; case groupInfo: masterAuthors = getOneGroupAuthors(character.getId(), null); break; case user: masterAuthors = getUserAuthor(character.getId(), null); } if (masterAuthors == null || masterAuthors.isEmpty()) { return null; } List menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList()); LinkedHashSet strings = new LinkedHashSet<>(menuIds); LinkedHashSet byParentId = menuService.getByParentId(strings); if (byParentId == null) { return null; } List sysMenus = menuService.selectBatchIds(byParentId); return sysMenus; } private List getUserAuthor(String characterId, MaintainField maintainField) { Wrapper userWrapper = new EntityWrapper().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, characterId); if (maintainField != null) { if (maintainField.getId() != null) { userWrapper.eq("maintain_field_id", maintainField.getId()); } if (maintainField.getTableName() != null) { userWrapper.eq("table_name",maintainField.getTableName()); } } List masterAuthors = selectList(userWrapper); if (masterAuthors.isEmpty()) { //user 获取role Set roleIdSet = DbUtils.getRoleByUser(characterId); if (roleIdSet.isEmpty()) { return new ArrayList<>(); }else { Wrapper roleWrapper = new EntityWrapper().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIdSet); if (maintainField != null) { roleWrapper.eq("maintain_field_id", maintainField.getId()).eq("table_name",maintainField.getTableName()); } masterAuthors = selectList(roleWrapper); } } List groupAuthors = getUserGroupAuthor(characterId, null); masterAuthors.addAll(groupAuthors); return masterAuthors; } private List getRoleAuthors(String characterId, MaintainField maintainField) { Wrapper roleWrapper = new EntityWrapper().eq(Constant.TYPE, MasterAuthorType.role).eq(MasterAuthorController.character_id, characterId); if (maintainField != null) { if (maintainField.getId() != null) { roleWrapper.eq("maintain_field_id", maintainField.getId()); } if (maintainField.getTableName() != null) { roleWrapper.eq("table_name",maintainField.getTableName()); } } return selectList(roleWrapper); } private List getUserGroupAuthor(String userId, MaintainField maintainField) { Set groupIdList = DbUtils.getGroupByUser(userId); List result = new ArrayList<>(); if (groupIdList == null) { return result; } for (String groupId : groupIdList) { List oneGroupAuthors = getOneGroupAuthors(groupId, maintainField); if (oneGroupAuthors == null || oneGroupAuthors.isEmpty()) { continue; } result.addAll(oneGroupAuthors); } return result; } @Override public List getOneGroupAuthors(String groupId, MaintainField maintainField) { Wrapper userWrapper = new EntityWrapper().eq("user_group", true).eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, groupId); Wrapper roleWrapper = new EntityWrapper().eq("user_group", true).eq(Constant.TYPE, MasterAuthorType.role).eq(MasterAuthorController.character_id, groupId); if (maintainField != null) { if (maintainField.getId() != null) { userWrapper.eq("maintain_field_id", maintainField.getId()); roleWrapper.eq("maintain_field_id", maintainField.getId()); } if (maintainField.getTableName() != null) { userWrapper.eq("table_name",maintainField.getTableName()); roleWrapper.eq("table_name",maintainField.getTableName()); } } List groupAuthors = selectList(userWrapper); if (groupAuthors.isEmpty()) { Set roleByGroup = DbUtils.getRoleByGroup(groupId); if(roleByGroup.isEmpty()) { groupAuthors = new ArrayList<>(); }else { groupAuthors = selectList(roleWrapper); } } return groupAuthors; } @Override public List getField(Character character, String maintainId) { Maintain maintain = maintainService.selectById(maintainId); if (maintain == null){ return new ArrayList<>(); } boolean isAll = checkUnFilterAuthor(character, maintain.getTableName()); if (isAll) { List total = fieldService.getFieldByMaintain(maintainId); return total; } MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId); if (maintainField == null){ return new ArrayList<>(); } String maintainFieldId = maintainField.getId(); MasterAuthorType type = character.getType(); List masterAuthors = null; switch (type){ case role: masterAuthors = getRoleAuthors(character.getId(), maintainField); break; case groupInfo: masterAuthors = getOneGroupAuthors(character.getId(), maintainField); break; case user: masterAuthors = getUserAuthor(character.getId(), maintainField); } if (masterAuthors == null || masterAuthors.isEmpty()) { return null; } List authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList()); List masterAuthorDetails = authorDetailService.selectList(new EntityWrapper().in(Constant.PARENT_ID, authorIds)); Set codes = masterAuthorDetails.stream().map(masterAuthorDetail -> masterAuthorDetail.getField()).collect(Collectors.toSet()); if (codes.isEmpty()) { return null; } Wrapper wrapper = new EntityWrapper().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 fieldList = fieldService.selectList(wrapper); return fieldList; } private boolean checkUnFilterAuthor(Character character, String tableName) { MasterAuthorType type = character.getType(); List masterAuthors = null; MaintainField maintainField = new MaintainField().setTableName(tableName); switch (type){ case role: masterAuthors = getRoleAuthors(character.getId(), maintainField); break; case groupInfo: masterAuthors = getOneGroupAuthors(character.getId(), maintainField); break; case user: masterAuthors = getUserAuthor(character.getId(), maintainField); } if (masterAuthors.isEmpty()) { return false; } long isAll = masterAuthors.stream().filter(masterAuthor -> masterAuthor.getMaintainFieldId().equalsIgnoreCase(Constant.All)).count(); if (isAll > 0) { return true; }else { return false; } } @Override public boolean checkMaintainAuthor(Character character, String maintainId) { Maintain maintain = maintainService.selectById(maintainId); if (maintain == null) { return false; } int checked = 0; boolean isAll = checkUnFilterAuthor(character, maintain.getTableName()); if (isAll){ return true; } MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId); if (maintainField == null) { return false; } MasterAuthorType type = character.getType(); List masterAuthors = null; switch (type){ case role: masterAuthors = getRoleAuthors(character.getId(), maintainField); break; case groupInfo: masterAuthors = getOneGroupAuthors(character.getId(), maintainField); break; case user: masterAuthors = getUserAuthor(character.getId(), maintainField); } if (masterAuthors.isEmpty()) { return false; }else { return true; } } @Override public String getFilter(Character character, String maintainId) { Maintain maintain = maintainService.selectById(maintainId); if (maintain == null){ return Constant.WHERE_DEFAULTUN; } boolean isAll = checkUnFilterAuthor(character, maintain.getTableName()); if (isAll) { return Constant.WHERE_DEFAULT; } MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId); MasterAuthorType type = character.getType(); List masterAuthors = null; switch (type){ case role: masterAuthors = getRoleAuthors(character.getId(), maintainField); break; case groupInfo: masterAuthors = getOneGroupAuthors(character.getId(), maintainField); break; case user: masterAuthors = getUserAuthor(character.getId(), maintainField); } if (masterAuthors.isEmpty()) { return Constant.WHERE_DEFAULTUN; } List authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList()); List masterAuthorDetails = authorDetailService.selectList(new EntityWrapper().in(Constant.PARENT_ID, authorIds)); if (masterAuthorDetails.isEmpty()) { return Constant.WHERE_DEFAULTUN; } HashMap> segmentMap = new HashMap<>(); HashMap 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 split = DbUtils.split(val); segmentMap.put(field,split); } ContentBuilder builder = new ContentBuilder(Constant.AND); Set keySet = segmentMap.keySet(); for (String code : keySet) { List 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(Character character, String tableName) { List maintainList = maintainService.selectList(new EntityWrapper().eq("table_name", tableName).orderBy("order_no desc")); for (Maintain maintain : maintainList) { ActivitiStatus status = flowsService.getStatusByBusinessId(maintain.getId()); if (!ActivitiStatus.open.equals(status)) { continue; } boolean b = checkMaintainAuthor(character, maintain.getId()); if (b) { return maintain; } } return null; } private MasterAuthor merage(MasterAuthor preMerageMasterAuthor, MasterAuthor masterAuthor) { // table name masterField 一样 只有字段不同了 List preFields = preMerageMasterAuthor.getFields(); Map fieldMap = masterAuthor.getFields().stream().collect(Collectors.toMap(MasterAuthorDetail::getField, masterAuthorDetail -> masterAuthorDetail)); HashSet 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; } }