|
@@ -3,13 +3,14 @@ package com.persagy.dc.define.service.impl;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
+import cn.hutool.core.text.StrFormatter;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.persagy.dc.amqp.handler.RabbitMessageSender;
|
|
|
import com.persagy.dc.basic.constant.DigitalMessageConstant;
|
|
|
-import com.persagy.dc.basic.model.DigitalManageMessage;
|
|
|
+import com.persagy.dc.basic.utils.DigitalMessageHelper;
|
|
|
import com.persagy.dc.common.constant.ValidEnum;
|
|
|
+import com.persagy.dc.common.exception.BusinessException;
|
|
|
import com.persagy.dc.common.model.entity.BaseEntity;
|
|
|
import com.persagy.dc.define.dao.ObjectTypeMapper;
|
|
|
import com.persagy.dc.define.entity.ObjectType;
|
|
@@ -19,8 +20,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 对象类型 实现类
|
|
@@ -34,7 +34,7 @@ public class ObjectTypeServiceImpl implements IObjectTypeService {
|
|
|
@Resource
|
|
|
private ObjectTypeMapper dao;
|
|
|
@Autowired
|
|
|
- private RabbitMessageSender messageSender;
|
|
|
+ private DigitalMessageHelper messageSender;
|
|
|
|
|
|
@Override
|
|
|
public List<ObjectType> queryByCondition(Wrapper<ObjectType> queryWrapper) {
|
|
@@ -51,18 +51,14 @@ public class ObjectTypeServiceImpl implements IObjectTypeService {
|
|
|
if(CollUtil.isEmpty(voList)) {
|
|
|
return null;
|
|
|
}
|
|
|
+ validateOnSave(voList, false);
|
|
|
voList.forEach(vo -> {
|
|
|
// 新增前消息
|
|
|
- DigitalManageMessage message = new DigitalManageMessage();
|
|
|
- message.setOperatorType(DigitalMessageConstant.OPERATE_BEFORE_INSERT);
|
|
|
- message.setOperatorObj(vo.getClass().getSimpleName());
|
|
|
- message.setNewObj(vo);
|
|
|
- sendMessage(message, true);
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_BEFORE_INSERT, null, vo, true);
|
|
|
// 新增
|
|
|
dao.insert(vo);
|
|
|
// 新增后消息
|
|
|
- message.setOperatorType(DigitalMessageConstant.OPERATE_AFTER_INSERT);
|
|
|
- sendMessage(message, false);
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_INSERT, null, vo, false);
|
|
|
});
|
|
|
return voList;
|
|
|
}
|
|
@@ -72,26 +68,62 @@ public class ObjectTypeServiceImpl implements IObjectTypeService {
|
|
|
if(CollUtil.isEmpty(voList)) {
|
|
|
return null;
|
|
|
}
|
|
|
+ validateOnSave(voList, true);
|
|
|
List<String> idList = CollUtil.getFieldValues(voList, BaseEntity.PROP_ID, String.class);
|
|
|
List<ObjectType> dbList = dao.selectBatchIds(idList);
|
|
|
Map<String, ObjectType> dbMap = CollectionUtil.fieldValueMap(dbList, BaseEntity.PROP_ID);
|
|
|
voList.forEach(vo -> {
|
|
|
+ ObjectType dbVO = MapUtil.get(dbMap, vo.getId(), ObjectType.class);
|
|
|
+ // 容错处理
|
|
|
+ if(dbVO == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
// 修改前消息
|
|
|
- DigitalManageMessage message = new DigitalManageMessage();
|
|
|
- message.setOperatorType(DigitalMessageConstant.OPERATE_BEFORE_UPDATE);
|
|
|
- message.setOperatorObj(vo.getClass().getSimpleName());
|
|
|
- message.setOldObj(MapUtil.get(dbMap, vo.getId(), ObjectType.class));
|
|
|
- message.setNewObj(vo);
|
|
|
- sendMessage(message, true);
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_BEFORE_UPDATE, dbVO, vo, true);
|
|
|
// 修改
|
|
|
dao.updateById(vo);
|
|
|
// 修改后消息
|
|
|
- message.setOperatorType(DigitalMessageConstant.OPERATE_AFTER_UPDATE);
|
|
|
- sendMessage(message, false);
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_UPDATE, dbVO, vo, false);
|
|
|
});
|
|
|
return voList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 保存校验
|
|
|
+ * 编码全局唯一
|
|
|
+ * @param voList
|
|
|
+ * @param isUpdate
|
|
|
+ */
|
|
|
+ private void validateOnSave(List<ObjectType> voList, boolean isUpdate) {
|
|
|
+ Set<String> codeSet = new HashSet<>();
|
|
|
+ List<String> idList = new ArrayList<>();
|
|
|
+ for(ObjectType vo:voList) {
|
|
|
+ // 编码重复
|
|
|
+ if(!codeSet.add(vo.getCode())) {
|
|
|
+ throw new BusinessException(StrFormatter.format("存在重复编码{},不允许保存!", vo.getCode()));
|
|
|
+ }
|
|
|
+ // 修改时,收集待排除的主键
|
|
|
+ if(isUpdate) {
|
|
|
+ idList.add(vo.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询条件
|
|
|
+ LambdaQueryWrapper<ObjectType> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.select(ObjectType::getCode);
|
|
|
+ queryWrapper.in(ObjectType::getValid, ValidEnum.TRUE.getType());
|
|
|
+ queryWrapper.in(ObjectType::getCode, codeSet);
|
|
|
+ if(isUpdate) {
|
|
|
+ queryWrapper.notIn(ObjectType::getId, idList);
|
|
|
+ }
|
|
|
+ // 查询重复的编码
|
|
|
+ List<ObjectType> dbList = dao.selectList(queryWrapper);
|
|
|
+ if(CollUtil.isEmpty(dbList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> codes = CollUtil.getFieldValues(voList, "code", String.class);
|
|
|
+ throw new BusinessException(StrFormatter.format("存在重复编码{},不允许保存!", codes));
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void delete(List<String> idList) {
|
|
|
if(CollUtil.isEmpty(idList)) {
|
|
@@ -100,40 +132,21 @@ public class ObjectTypeServiceImpl implements IObjectTypeService {
|
|
|
List<ObjectType> dbList = dao.selectBatchIds(idList);
|
|
|
Map<String, ObjectType> dbMap = CollectionUtil.fieldValueMap(dbList, BaseEntity.PROP_ID);
|
|
|
idList.forEach(id -> {
|
|
|
+ ObjectType dbVO = MapUtil.get(dbMap, id, ObjectType.class);
|
|
|
+ // 容错处理
|
|
|
+ if(dbVO == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
// 删除前消息
|
|
|
- DigitalManageMessage message = new DigitalManageMessage();
|
|
|
- message.setOperatorType(DigitalMessageConstant.OPERATE_BEFORE_DELETE);
|
|
|
- message.setOperatorObj(ObjectType.class.getSimpleName());
|
|
|
- message.setNewObj(MapUtil.get(dbMap, id, ObjectType.class));
|
|
|
- sendMessage(message, true);
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_BEFORE_DELETE, null, dbVO, true);
|
|
|
+ // 删除
|
|
|
ObjectType vo = new ObjectType();
|
|
|
vo.setId(id);
|
|
|
vo.setValid(ValidEnum.FALSE.getType());
|
|
|
- // 删除
|
|
|
dao.updateById(vo);
|
|
|
// 删除后消息
|
|
|
- message.setOperatorType(DigitalMessageConstant.OPERATE_AFTER_DELETE);
|
|
|
- message.getNewObj().setValid(ValidEnum.FALSE.getType());
|
|
|
- sendMessage(message, true);
|
|
|
+ dbVO.setValid(ValidEnum.FALSE.getType());
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_DELETE, null, dbVO, false);
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送消息
|
|
|
- * @param message
|
|
|
- * @param syncFlag
|
|
|
- */
|
|
|
- private void sendMessage(DigitalManageMessage message, boolean syncFlag) {
|
|
|
- // 同步消息
|
|
|
- if(syncFlag) {
|
|
|
- messageSender.sendAndReceive(DigitalMessageConstant.MESSAGE_EXCHANGE,
|
|
|
- DigitalMessageConstant.MESSAGE_ROUTING,
|
|
|
- JSONUtil.toJsonStr(message));
|
|
|
- } else {
|
|
|
- // 异步消息
|
|
|
- messageSender.send(DigitalMessageConstant.MESSAGE_EXCHANGE,
|
|
|
- DigitalMessageConstant.MESSAGE_ROUTING,
|
|
|
- JSONUtil.toJsonStr(message));
|
|
|
- }
|
|
|
- }
|
|
|
}
|