xiejun
2023-07-04 100bdd6595cc5c7a447ea6fedd6827f311b37f48
Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/AttributeServiceImpl.java
@@ -8,18 +8,20 @@
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.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -29,6 +31,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -40,6 +43,8 @@
 */
@Service
public class AttributeServiceImpl extends ServiceImpl<AttributeMapper, Attribute>  implements IAttributeService {
   private final String REGEXP = "^[A-Za-z]+$";
   @Override
   public boolean deleteLogic(@NotEmpty List<Long> ids) {
@@ -60,7 +65,7 @@
   @Override
   public AttributeVO getAttributeDetail(Long id) {
      Func.requireNotNull(id,"主键不能为空");
      return AttributeWrapper.build().entityVO(baseMapper.selectByPrimaryKey(id));
      return AttributeWrapper.build().entityVO(baseMapper.selectById(id));
   }
   /**
@@ -71,44 +76,55 @@
    */
   @Override
   public boolean submit(AttributeDTO dto) {
      LambdaQueryWrapper<Attribute> wrapper = Wrappers.<Attribute>query().lambda().eq(Attribute::getKey, dto.getKey());
      Long count = baseMapper.selectCount((Func.isEmpty(dto.getId())) ? wrapper : wrapper.notIn(Attribute::getId, dto.getId()));
      if (count > 0L) {
         throw new ServiceException("属性名已存在!");
      VciBaseUtil.alertNotNull(dto.getId(),"属性编号",dto.getName(),"属性名称",dto.getTypeKey(),"属性类型",dto.getMaxLength(),"属性长度");
      if (!Pattern.compile(REGEXP).matcher(dto.getId()).matches()){
         throw new VciBaseException("属性编号{0}只能是英文",new Object[]{dto.getId()});
      }
      dto.setIsDeleted(BladeConstant.DB_NOT_DELETED);
      Attribute omdAttribute = BeanUtil.copy(dto, Attribute.class);
      LambdaQueryWrapper<Attribute> wrapper = Wrappers.<Attribute>query().lambda().eq(Attribute::getId, dto.getId());
      Long count = baseMapper.selectCount((Func.isEmpty(dto.getOid())) ? wrapper : wrapper.notIn(Attribute::getOid, dto.getOid()));
      if (count > 0L) {
         throw new ServiceException("属性编号已存在!");
      }
      Attribute attribute = AttributeWrapper.build().copyBeforeSave(dto);
      CacheUtil.clear(OmdCacheConstant.ATTR_CACHE);
      return saveOrUpdate(omdAttribute);
      return saveOrUpdate(attribute);
   }
   /**
    * 删除
    *
    * @param ids 主键集合
    * @param oids 主键集合
    * @return 执行结果
    */
   @Override
   public boolean removeAttrs(String ids) {
      List<Long> idList = Func.toLongList(ids);
      return !deleteLogic(idList) && removeByIds(idList);
   public boolean removeAttrs(String oids) {
      List<String> oidList = Func.toStrList(",", oids);
      List<Attribute> list = list(Wrappers.<Attribute>query().lambda().in(Attribute::getOid, oidList));
      if (!CollectionUtils.isEmpty(list)){
         baseMapper.delete(Wrappers.<Attribute>query().lambda().in(Attribute::getOid,list.stream().map(Attribute::getOid).collect(Collectors.toList())));
      }
      return true;
   }
   /**
    * 查看应用范围
    *
    * @param id 主键
    * @param oid 主键
    * @return 查询已应用的业务类型名称
    */
   @Override
   public List<String> applyRange(Long id) {
      return new ArrayList<>();
   public List<BtmTypeVO> applyRange(String oid) {
      List<BtmType> btmTypes = baseMapper.selectApplyRange(oid);
      if (CollectionUtils.isEmpty(btmTypes)){
         return null;
      }
      return BtmTypeWrapper.build().listEntityVO(btmTypes);
   }
   /**
    * 检查属性是否存在
    *
    * @param keyCollections 英文名称集合
    * @param keyCollections 编号集合
    * @return true表示都存在,false表示不存在,不存在的时候会抛出异常
    * @throws VciBaseException 不存在的时候会抛出异常
    */
@@ -118,9 +134,9 @@
      if (CollectionUtils.isEmpty(existAttributeVOList)) {
         throw new VciBaseException("使用的属性都在系统中不存在,请先查证");
      } else {
         Set<String> existAttributeIdSet = (existAttributeVOList.stream().collect(Collectors.toMap(s -> s.getKey().toLowerCase().trim(), t -> t))).keySet();
         Set<String> existAttributeOidSet = (existAttributeVOList.stream().collect(Collectors.toMap(s -> s.getId().toLowerCase().trim(), t -> t))).keySet();
         keyCollections.stream().forEach(s -> {
            if (!existAttributeIdSet.contains(s)) {
            if (!existAttributeOidSet.contains(s)) {
               throw new VciBaseException("使用的属性{0}在系统中不存在,请先查证", new Object[]{s});
            }
         });
@@ -129,9 +145,9 @@
   }
   /**
    * 根据英文名称集合批量获取属性对象
    * 根据编号集合批量获取属性对象
    *
    * @param attributeIdCollection 英文名称集合
    * @param attributeIdCollection 编号集合
    * @return 属性对象列表,如果有不存在的不会返回,全部不存在的则返回空列表
    * @throws VciBaseException 参数为空或者查询出错时会抛出错误
    */
@@ -147,8 +163,8 @@
   }
   /**
    * 根据英文名称集合获取属性数据对象
    * @param attributeIdCollection 属性的英文名称集合
    * 根据编号集合获取属性数据对象,其中默认的属性应当剔除
    * @param attributeIdCollection 属性的编号集合
    * @return 属性数据对象列表,如果有不存在的不会返回,全部不存在的则返回空列表
    * @throws VciBaseException mybatis查询出错的时候会抛出异常
    */
@@ -159,7 +175,7 @@
         Collection<Collection<String>> idCollections = VciBaseUtil.switchCollectionForOracleIn(distAttributeIdCollection);
         if(!CollectionUtils.isEmpty(idCollections)) {
            idCollections.stream().forEach(s -> {
               List<Attribute> attributeDOS = baseMapper.selectList(Wrappers.<Attribute>query().lambda().in(Attribute::getKey, s));
               List<Attribute> attributeDOS = baseMapper.selectList(Wrappers.<Attribute>query().lambda().in(Attribute::getId, s));
               if(!CollectionUtils.isEmpty(attributeDOS)){
                  attributeDOList.addAll(attributeDOS);
               }
@@ -173,7 +189,7 @@
   /**
    * 判断属性的内容是否符合要求
    *
    * @param id              属性的主键
    * @param id              属性的编号
    * @param attrDataType    属性的类型
    * @param attributeLength 属性的长度
    * @param defaultValue    默认值
@@ -184,26 +200,26 @@
   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});
         throw new VciBaseException("编号为{0}的属性的类型不符合要求",new Object[]{id});
      }
      //必须要输入长度
      if( VciBaseUtil.inArray(new String[] {VciFieldTypeEnum.VTString.name()},attrDataType)
      if(VciBaseUtil.inArray(new String[] {VciFieldTypeEnum.VTString.name()},attrDataType)
         && (attributeLength == null ||attributeLength < 1)){
         throw new VciBaseException("英文名称为{0}的属性的为{1}类型时,必须要输入长度的值",new Object[]{id,attributeDataTypeText});
         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});
            throw new VciBaseException("编号为{0}的属性的默认值不是双精度类型",new Object[]{id});
         }
         if(VciFieldTypeEnum.VTInteger.name().equalsIgnoreCase(defaultValue)
            && !defaultValue.matches(OmdRegExpConstant.INT)){
            throw new VciBaseException("英文名称为{0}的属性的默认值不是整数型",new Object[]{id});
            throw new VciBaseException("编号为{0}的属性的默认值不是整数型",new Object[]{id});
         }
         if(VciFieldTypeEnum.VTLong.name().equalsIgnoreCase(defaultValue)
            && !defaultValue.matches(OmdRegExpConstant.LONG)){
            throw new VciBaseException("英文名称为{0}的属性的默认值不是长整形",new Object[]{id});
            throw new VciBaseException("编号为{0}的属性的默认值不是长整形",new Object[]{id});
         }
      }
      return true;
@@ -218,7 +234,7 @@
    */
   @Override
   public boolean checkAttribute(BtmTypeLinkAttributesDTO attributesDTO) throws VciBaseException {
      VciBaseUtil.alertNotNull(attributesDTO.getId(), "属性的英文名称", attributesDTO.getName(), "属性的中文名称",
      VciBaseUtil.alertNotNull(attributesDTO.getId(), "属性的编号", attributesDTO.getName(), "属性的名称",
         attributesDTO.getAttrDataType(), "属性的数据类型");
      boolean pass = checkAttributePass(attributesDTO.getId(), attributesDTO.getAttrDataType(), attributesDTO.getAttributeLength(), attributesDTO.getDefaultValue());
      if (!pass){
@@ -226,4 +242,20 @@
      }
      return pass;
   }
   /**
    * 单个删除
    *
    * @param oid 主键
    * @return 执行结果
    */
   @Override
   public R remove(String oid) {
      List<BtmTypeVO> vos = applyRange(oid);
      if (CollectionUtils.isEmpty(vos)){
          return R.status(removeAttrs(oid));
      }else {
         return R.fail("该属性已被使用,不允许删除");
      }
   }
}