package com.engine.organization.service.impl; import com.api.browser.bean.SearchConditionItem; import com.engine.common.service.HrmCommonService; import com.engine.common.service.impl.HrmCommonServiceImpl; import com.engine.core.impl.Service; import com.engine.organization.entity.TopTab; import com.engine.organization.entity.codesetting.po.CodeRulePO; import com.engine.organization.entity.extend.ExtendInfoOperateType; import com.engine.organization.entity.extend.bo.ExtendInfoBO; import com.engine.organization.entity.extend.param.ExtendInfoParams; import com.engine.organization.entity.extend.po.ExtendGroupPO; import com.engine.organization.entity.extend.po.ExtendInfoPO; import com.engine.organization.entity.extend.po.ExtendTitlePO; import com.engine.organization.entity.personnelcard.po.CardButtonPO; import com.engine.organization.enums.ModuleTypeEnum; import com.engine.organization.mapper.codesetting.CodeRuleMapper; import com.engine.organization.mapper.extend.*; import com.engine.organization.mapper.personnelcard.CardButtonMapper; import com.engine.organization.service.ExtService; import com.engine.organization.util.OrganizationAssert; import com.engine.organization.util.db.DBType; import com.engine.organization.util.db.MapperProxyFactory; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import weaver.general.Util; import weaver.hrm.User; import java.util.*; import java.util.stream.Collectors; /** * @description: * @author:dxfeng * @createTime: 2022/05/24 * @version: 1.0 */ public class ExtServiceImpl extends Service implements ExtService { private static final Integer BROWSER_TYPE = 3; private ExtendInfoMapper getExtendInfoMapper() { return MapperProxyFactory.getProxy(ExtendInfoMapper.class); } private ExtendGroupMapper getExtendGroupMapper() { return MapperProxyFactory.getProxy(ExtendGroupMapper.class); } private ExtendTitleMapper getExtendTitleMapper() { return MapperProxyFactory.getProxy(ExtendTitleMapper.class); } private ExtDTMapper getExtDTMapper() { return MapperProxyFactory.getProxy(ExtDTMapper.class); } private ExtMapper getExtMapper() { return MapperProxyFactory.getProxy(ExtMapper.class); } @Override public List getExtForm(User user, String extendType, String tableName, int viewAttr, long id, String groupId, String... readOnlyFields) { List conditionItems = new ArrayList<>(); // 2编辑 1查看 OrganizationAssert.notNull(groupId, "请选择对应的拓展页"); List infoPOList = getExtendInfoMapper().listFields(extendType, groupId, tableName, 1 == viewAttr ? ExtendInfoOperateType.LIST.getValue() : ExtendInfoOperateType.EDIT.getValue(), "1"); String fields = infoPOList.stream().map(ExtendInfoPO::getFieldName).collect(Collectors.joining(",")); if (StringUtils.isEmpty(fields)) { return conditionItems; } List readOnlyFieldList = new ArrayList<>(Arrays.asList(readOnlyFields)); if ("4".equals(extendType) && noEditRight(user)) { if (Util.null2String(user.getUID()).equals(String.valueOf(id))) { List readOnlyList = infoPOList.stream().filter(item -> !"1".equals(Util.null2String(item.getIsModify()))).map(ExtendInfoPO::getFieldName).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(readOnlyList)) { readOnlyFieldList.addAll(readOnlyList); } } else { viewAttr = 1; } } ExtendInfoParams infoParams = ExtendInfoParams.builder().tableName(tableName).fields(fields).params(null).id(id).build(); Map compExtMap = getExtMapper().listExt(infoParams); // 组装拓展页内容 for (ExtendInfoPO extendInfoPO : infoPOList) { SearchConditionItem item = ExtendInfoBO.getSearchConditionItem(user, viewAttr, extendInfoPO, null == compExtMap ? null : compExtMap.get(DBType.isOracle() ? extendInfoPO.getFieldName().toUpperCase() : extendInfoPO.getFieldName())); item.setFieldcol(16); if (2 == viewAttr) { if (CollectionUtils.isNotEmpty(readOnlyFieldList) && readOnlyFieldList.contains(extendInfoPO.getFieldName())) { item.setViewAttr(1); } else if (1 == extendInfoPO.getIsrequired()) { item.setViewAttr(3); item.setRules("required"); } } // 分部,上级部门添加提示信息 if (1 != viewAttr && ModuleTypeEnum.departmentfielddefined.getValue().toString().equals(groupId) && "parent_dept".equalsIgnoreCase(extendInfoPO.getFieldName())) { item.setHelpfulTip("请先选择所属分部再选择" + extendInfoPO.getFieldNameDesc()); } conditionItems.add(item); } return conditionItems; } @Override public List getExtSaveForm(User user, String extendType, String tableName, int viewAttr, String groupId, String autoNoField, String serialType, Map params, String... readOnlyFields) { List conditionItems = new ArrayList<>(); if (null == params) { params = new HashMap<>(); } // 2编辑 1查看 OrganizationAssert.notNull(groupId, "请选择对应的拓展页"); List infoPOList = getExtendInfoMapper().listFields(extendType, groupId, tableName, ExtendInfoOperateType.ADD.getValue(), "1"); // 组装拓展页内容 List readOnlyFieldList = Arrays.asList(readOnlyFields); for (ExtendInfoPO extendInfoPO : infoPOList) { SearchConditionItem item = ExtendInfoBO.getSearchConditionItem(user, viewAttr, extendInfoPO, params.get(extendInfoPO.getFieldName())); item.setFieldcol(16); if (CollectionUtils.isNotEmpty(readOnlyFieldList) && readOnlyFieldList.contains(extendInfoPO.getFieldName())) { item.setViewAttr(1); } else if (1 == extendInfoPO.getIsrequired()) { item.setViewAttr(3); item.setRules("required"); } if (StringUtils.isNotBlank(autoNoField) && extendInfoPO.getFieldName().equalsIgnoreCase(autoNoField)) { // 判断是否开启自动编号 CodeRulePO codeRuleByType = MapperProxyFactory.getProxy(CodeRuleMapper.class).getCodeRuleByType(serialType); if (null != codeRuleByType && "1".equals(codeRuleByType.getSerialEnable())) { item.setViewAttr(2); item.setHelpfulTip("编号为空,则按照指定规则自动生成编号"); item.setRules(""); } } // 分部,上级部门添加提示信息 if (ModuleTypeEnum.departmentfielddefined.getValue().toString().equals(groupId) && "parent_dept".equalsIgnoreCase(extendInfoPO.getFieldName())) { item.setHelpfulTip("请先选择所属分部再选择" + extendInfoPO.getFieldNameDesc()); } conditionItems.add(item); } return conditionItems; } @Override public List> getExtendTables(User user, String extendType, Long groupId, String tableName, long id, int viewAttr, boolean showLabel) { List> tables = new ArrayList<>(); List infoPOList = new ArrayList<>(); // 查询明细表 List extendGroupList = getExtendGroupMapper().listGroupByPid(groupId, "1"); for (ExtendGroupPO extendGroup : extendGroupList) { List extendInfoPOS = getExtendInfoMapper().listFields(extendType, extendGroup.getId() + "", tableName, "", "1"); if (CollectionUtils.isNotEmpty(extendInfoPOS)) { infoPOList.addAll(extendInfoPOS); } } Map> allFields = infoPOList.stream().collect(Collectors.groupingBy(ExtendInfoPO::getExtendGroupId)); boolean checkRight = true; if (2 == viewAttr && "4".equals(extendType) && noEditRight(user)) { checkRight = false; if (Util.null2String(user.getUID()).equals(String.valueOf(id))) { infoPOList.removeIf(item -> !"1".equals(Util.null2String(item.getIsModify()))); } else { infoPOList = new ArrayList<>(); } } // 查询所有分布模块,拓展明细表信息 Map> groupMap = infoPOList.stream().collect(Collectors.groupingBy(ExtendInfoPO::getExtendGroupId)); // 遍历Map,组装数据 boolean finalCheckRight = checkRight; for (Map.Entry> entry : groupMap.entrySet()) { Map tableMap = new HashMap<>(); tableMap.put("hide", false); tableMap.put("tabname", getExtendGroupMapper().getGroupNameById(Util.null2String(entry.getKey()))); Map tabinfoMap = new HashMap<>(); tabinfoMap.put("columns", ExtendInfoBO.convertInfoListToTable(user, entry.getValue(), viewAttr, showLabel)); tabinfoMap.put("rownum", "rownum" + entry.getKey()); // 浏览按钮添加filespan字段 String fields = allFields.get(entry.getKey()).stream().map(item -> { if (BROWSER_TYPE.equals(item.getControlType())) { return item.getFieldName() + "," + item.getFieldName() + "span"; } return item.getFieldName(); }).collect(Collectors.joining(",")); // 去除null 元素 List> maps = getExtDTMapper().listCompExtDT(tableName, id, fields); maps.removeIf(Objects::isNull); // 兼容Oracle,map的key转换为小写 List> collect = maps.stream().map(item -> { Map resultMap = new HashMap<>(); Set keys = item.keySet(); for (String key : keys) { Object o = item.get(key); if (o instanceof Integer) { resultMap.put(key.toLowerCase(), Integer.toString((int) o)); } else { resultMap.put(key.toLowerCase(), o); } } resultMap.put("viewAttr", finalCheckRight ? viewAttr : 1); return resultMap; }).collect(Collectors.toList()); tabinfoMap.put("datas", collect); tableMap.put("tabinfo", tabinfoMap); tables.add(tableMap); } return tables; } /** * 拓展页面分组 * * @return */ public List getTabInfo(String extendType, String tableName) { List topTabs = new ArrayList<>(); List infoPOList = getExtendInfoMapper().listFields(extendType, "", tableName, ExtendInfoOperateType.EDIT.getValue(), "1"); List infoPOList1 = getExtendInfoMapper().listFields(extendType, "", tableName.toUpperCase().replace("EXT", ""), ExtendInfoOperateType.EDIT.getValue(), "1"); infoPOList.addAll(infoPOList1); List ids = infoPOList.stream().map(ExtendInfoPO::getExtendGroupId).collect(Collectors.toList()); List extendTitles = getExtendTitleMapper().getTitlesByIds(ids); Set groupIds = extendTitles.stream().map(ExtendTitlePO::getGroupId).collect(Collectors.toSet()); List extendGroupList = getExtendGroupMapper().listByType(Integer.parseInt(extendType), "1"); // 拓展信息 if (CollectionUtils.isNotEmpty(extendGroupList) && CollectionUtils.isNotEmpty(groupIds)) { for (ExtendGroupPO extendGroup : extendGroupList) { if (groupIds.contains(extendGroup.getId())) { topTabs.add(TopTab.builder().color("#000000").groupId(extendGroup.getId().toString()).showcount(false).title(extendGroup.getGroupName()).viewCondition(extendGroup.getId().toString()).build()); } } } return topTabs; } @Override public Long updateExtForm(User user, String extendType, String tableName, Map params, String groupId, Long id) { List extInfoPOList = getExtendInfoMapper().listFields(extendType, groupId, tableName, "", ""); Map map = new HashMap<>(); // 遍历Map Map collect = extInfoPOList.stream().collect(Collectors.toMap(ExtendInfoPO::getFieldName, item -> item)); collect.put("uuid", new ExtendInfoPO()); for (Map.Entry entry : params.entrySet()) { String key = entry.getKey(); if (collect.containsKey(key)) { Object value = entry.getValue(); if (null != value && StringUtils.isBlank(value.toString())) { value = null; } if (null == value) { // 兼容sqlServer数据库text字段类型 if ("text".equals(collect.get(key).getFieldType())) { value = ""; } } map.put(key, value); } } ExtendInfoParams infoParams = ExtendInfoParams.builder().tableName(tableName).params(map).id(id).build(); if (null != id) { // 判断更新还是插入 int count = getExtMapper().countExtById(infoParams); if (count > 0) { map.put("update_time", new Date()); getExtMapper().updateTable(infoParams); } else { map.put("creator", user.getUID()); map.put("delete_type", 0); map.put("create_time", new Date()); map.put("update_time", new Date()); map.put("id", id); getExtMapper().insertExtTable(infoParams); } } else { if (!tableName.equals("JCL_ORG_HRMRESOURCE")) { map.put("forbidden_tag", 0); } map.put("creator", user.getUID()); map.put("delete_type", 0); map.put("create_time", new Date()); map.put("update_time", new Date()); getExtMapper().insertTable(infoParams); } return infoParams.getId(); } @Override public void updateExtDT(User user, String extendType, String tableName, Map params, Long id) { List dtInfoPOList = getExtendInfoMapper().listFields(extendType, "", tableName, "", ""); List groupIds = dtInfoPOList.stream().map(ExtendInfoPO::getExtendGroupId).distinct().collect(Collectors.toList()); Map> poMaps = dtInfoPOList.stream().collect(Collectors.groupingBy(ExtendInfoPO::getExtendGroupId)); for (Long groupId : groupIds) { int rowNum = Util.getIntValue((String) params.get("rownum" + groupId)); // 删除原有明细表数据 getExtDTMapper().deleteByMainID(tableName, id, groupId); List filterS = poMaps.get(groupId); for (int i = 0; i < rowNum; i++) { Map map = new HashMap<>(); for (ExtendInfoPO extendInfoPO : filterS) { if (BROWSER_TYPE.equals(extendInfoPO.getControlType())) { map.put(extendInfoPO.getFieldName() + "span", parseDetailValue(params.get(extendInfoPO.getFieldName() + "span_" + i))); } map.put(extendInfoPO.getFieldName(), parseDetailValue(params.get(extendInfoPO.getFieldName() + "_" + i))); } map.put("mainid", id); map.put("group_id", groupId); map.put("creator", user.getUID()); map.put("delete_type", 0); map.put("create_time", new Date()); map.put("update_time", new Date()); // 去除value为null的元素 map.entrySet().removeIf(entry -> entry.getValue() == null); getExtDTMapper().insertCompExtDT(tableName, map); } } } /** * 转化明细表字段返回到前端的值 * * @param obj * @return */ private Object parseDetailValue(Object obj) { if (null == obj || StringUtils.isBlank(Util.null2String(obj))) { return null; } return obj; } /** * 判断是否为管理员或者 * * @param user * @return */ private boolean noEditRight(User user) { boolean hasEditRight = user.isAdmin(); CardButtonPO editButton = MapperProxyFactory.getProxy(CardButtonMapper.class).getEditButton(); // 非系统管理员判断是否拥有角色 if (!hasEditRight && null != editButton) { // 判断是否有这个角色 HrmCommonService hrmCommonService = new HrmCommonServiceImpl(); List roleIds = new ArrayList<>(Arrays.asList(hrmCommonService.getRoleIds(user.getUID()).split(","))); List accessRoleIds = new ArrayList<>(Arrays.asList(Util.null2String(editButton.getRoles()).split(","))); roleIds.retainAll(accessRoleIds); hasEditRight = CollectionUtils.isNotEmpty(roleIds); } return !hasEditRight; } }