package com.vci.ubcs.omd.service.impl; import com.alibaba.cloud.commons.lang.StringUtils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.vci.ubcs.omd.constant.OmdCacheConstant; import com.vci.ubcs.omd.dto.AttributeDTO; import com.vci.ubcs.omd.dto.BtmTypeLinkAttributesDTO; import com.vci.ubcs.omd.entity.Attribute; import com.vci.ubcs.omd.entity.BtmType; import com.vci.ubcs.omd.mapper.AttributeMapper; import com.vci.ubcs.omd.service.IAttributeService; import com.vci.ubcs.omd.vo.AttributeVO; import com.vci.ubcs.omd.vo.BtmTypeVO; import com.vci.ubcs.omd.wrapper.AttributeWrapper; import com.vci.ubcs.omd.wrapper.BtmTypeWrapper; import com.vci.ubcs.starter.exception.VciBaseException; import com.vci.ubcs.starter.web.constant.OmdRegExpConstant; import com.vci.ubcs.starter.web.enumpck.VciFieldTypeEnum; import com.vci.ubcs.starter.web.util.VciBaseUtil; import org.springblade.core.cache.utils.CacheUtil; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.Func; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.validation.constraints.NotEmpty; import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; /** * Description: 元数据(属性)的服务 * * @author LiHang * @date 2023/4/3 */ @Service public class AttributeServiceImpl extends ServiceImpl implements IAttributeService { private final String REGEXP = "^[A-Za-z]+$"; @Override public boolean deleteLogic(@NotEmpty List ids) { return false; } @Override public boolean changeStatus(@NotEmpty List ids, Integer status) { return false; } /** * 获取元数据详情 * * @param id 主键 * @return 元数据显示对象 */ @Override public AttributeVO getAttributeDetail(Long id) { Func.requireNotNull(id,"主键不能为空"); return AttributeWrapper.build().entityVO(baseMapper.selectById(id)); } /** * 新增或修改 * * @param dto 页面传输对象 * @return 执行结果 */ @Override public boolean submit(AttributeDTO dto) { VciBaseUtil.alertNotNull(dto.getId(),"属性编号",dto.getName(),"属性名称",dto.getTypeKey(),"属性类型",dto.getMaxLength(),"属性长度"); String msg = checkAttributeId(dto.getId(),dto.getOid()); if (StringUtils.isNotBlank(msg)){ throw new VciBaseException(msg); } Attribute attribute = AttributeWrapper.build().copyBeforeSave(dto); CacheUtil.clear(OmdCacheConstant.ATTR_CACHE); return saveOrUpdate(attribute); } /** * 元数据的ID校验 * @param id 编号值 * @param oid 数据主键 * @return 校验结果 */ private String checkAttributeId(String id, String oid) { if (!Pattern.compile(REGEXP).matcher(id).matches()){ return "属性编号"+id+"只能是英文"; } Long count = baseMapper.checkIdExist(id.toLowerCase(Locale.ROOT),oid); if (count > 0L) { return "属性编号"+id+"(不区分大小写)已存在!"; } return null; } /** * 删除 * * @param oids 主键集合 * @return 执行结果 */ @Override public boolean removeAttrs(String oids) { List oidList = Func.toStrList(",", oids); List list = list(Wrappers.query().lambda().in(Attribute::getOid, oidList)); if (!CollectionUtils.isEmpty(list)){ baseMapper.delete(Wrappers.query().lambda().in(Attribute::getOid,list.stream().map(Attribute::getOid).collect(Collectors.toList()))); } return true; } /** * 查看应用范围 * * @param oid 主键 * @return 查询已应用的业务类型名称 */ @Override public List applyRange(String oid) { List btmTypes = baseMapper.selectApplyRange(oid); if (CollectionUtils.isEmpty(btmTypes)){ return null; } return BtmTypeWrapper.build().listEntityVO(btmTypes); } /** * 检查属性是否存在 * * @param keyCollections 编号集合 * @return true表示都存在,false表示不存在,不存在的时候会抛出异常 * @throws VciBaseException 不存在的时候会抛出异常 */ @Override public boolean checkAttributeExists(Collection keyCollections) throws VciBaseException { List existAttributeVOList = listAttributeByKeyCollection(keyCollections); if (CollectionUtils.isEmpty(existAttributeVOList)) { throw new VciBaseException("使用的属性都在系统中不存在,请先查证"); } else { Set existAttributeOidSet = (existAttributeVOList.stream().collect(Collectors.toMap(s -> s.getId().trim(), t -> t))).keySet(); keyCollections.stream().forEach(s -> { if (!existAttributeOidSet.contains(s)) { throw new VciBaseException("使用的属性{0}在系统中不存在,请先查证", new Object[]{s}); } }); } return true; } /** * 根据编号集合批量获取属性对象 * * @param attributeIdCollection 编号集合 * @return 属性对象列表,如果有不存在的不会返回,全部不存在的则返回空列表 * @throws VciBaseException 参数为空或者查询出错时会抛出错误 */ @Override public List listAttributeByKeyCollection(Collection attributeIdCollection) throws VciBaseException { if(!CollectionUtils.isEmpty(attributeIdCollection)){ List attributeDOList = listAttributeByKeyCollectionDO(attributeIdCollection); if(!CollectionUtils.isEmpty(attributeDOList)) { return AttributeWrapper.build().listEntityVO(attributeDOList); } } return null; } /** * 根据编号集合获取属性数据对象,其中默认的属性应当剔除 * @param attributeIdCollection 属性的编号集合 * @return 属性数据对象列表,如果有不存在的不会返回,全部不存在的则返回空列表 * @throws VciBaseException mybatis查询出错的时候会抛出异常 */ private List listAttributeByKeyCollectionDO(Collection attributeIdCollection) throws VciBaseException { if(!CollectionUtils.isEmpty(attributeIdCollection)){ List attributeDOList = new ArrayList<>(); Collection distAttributeIdCollection = attributeIdCollection.stream().distinct().collect(Collectors.toList()); Collection> idCollections = VciBaseUtil.switchCollectionForOracleIn(distAttributeIdCollection); if(!CollectionUtils.isEmpty(idCollections)) { idCollections.stream().forEach(s -> { List attributeDOS = baseMapper.selectList(Wrappers.query().lambda().in(Attribute::getId, s)); if(!CollectionUtils.isEmpty(attributeDOS)){ attributeDOList.addAll(attributeDOS); } }); } return attributeDOList; } return null; } /** * 判断属性的内容是否符合要求 * * @param id 属性的编号 * @param attrDataType 属性的类型 * @param attributeLength 属性的长度 * @param defaultValue 默认值 * @return true表示符合要求,不符合要求会抛出异常 * @throws VciBaseException 不符合要求会抛出异常 */ @Override public boolean checkAttributePass(String id, String attrDataType, Integer attributeLength, String defaultValue) throws VciBaseException { String attributeDataTypeText = VciFieldTypeEnum.getTextByValue(attrDataType); if(StringUtils.isBlank(attributeDataTypeText)){ throw new VciBaseException("编号为{0}的属性的类型不符合要求",new Object[]{id}); } //必须要输入长度 if(VciBaseUtil.inArray(new String[] {VciFieldTypeEnum.VTString.name()},attrDataType) && (attributeLength == null ||attributeLength < 1)){ throw new VciBaseException("编号为{0}的属性的为{1}类型时,必须要输入长度的值",new Object[]{id,attributeDataTypeText}); } //判断默认值 if(StringUtils.isNotBlank(defaultValue)){ if(VciFieldTypeEnum.VTDouble.name().equalsIgnoreCase(attrDataType) && !defaultValue.matches(OmdRegExpConstant.DOUBLE)){ throw new VciBaseException("编号为{0}的属性的默认值不是双精度类型",new Object[]{id}); } if(VciFieldTypeEnum.VTInteger.name().equalsIgnoreCase(defaultValue) && !defaultValue.matches(OmdRegExpConstant.INT)){ throw new VciBaseException("编号为{0}的属性的默认值不是整数型",new Object[]{id}); } if(VciFieldTypeEnum.VTLong.name().equalsIgnoreCase(defaultValue) && !defaultValue.matches(OmdRegExpConstant.LONG)){ throw new VciBaseException("编号为{0}的属性的默认值不是长整形",new Object[]{id}); } } return true; } /** * 校验属性是否符合要求 * * @param attributesDTO 属性数据传输对象 * @return true 符合 false 不符合 * @throws VciBaseException 不符合时抛出异常 */ @Override public boolean checkAttribute(BtmTypeLinkAttributesDTO attributesDTO) throws VciBaseException { VciBaseUtil.alertNotNull(attributesDTO.getId(), "属性的编号", attributesDTO.getName(), "属性的名称", attributesDTO.getAttrDataType(), "属性的数据类型"); boolean pass = checkAttributePass(attributesDTO.getId(), attributesDTO.getAttrDataType(), attributesDTO.getAttributeLength(), attributesDTO.getDefaultValue()); if (!pass){ throw new VciBaseException("属性不符合要求"); } return pass; } /** * 单个删除 * * @param oid 主键 * @return 执行结果 */ @Override public R remove(String oid) { List vos = applyRange(oid); if (CollectionUtils.isEmpty(vos)){ return R.status(removeAttrs(oid)); }else { return R.fail("该属性已被使用,不允许删除"); } } }