feat:[REQ-3488] 优化协同关系创建service方法
This commit is contained in:
parent
6e23456512
commit
d93864be71
@ -152,6 +152,9 @@ public class SaasCooperateShip implements Serializable {
|
||||
* @param parent
|
||||
*/
|
||||
public void calcPath(SaasCooperateShip parent) {
|
||||
if (this.getParentId() == null) {
|
||||
this.parentId = 0L;
|
||||
}
|
||||
if (Objects.equals(this.getParentId(), 0L)) {
|
||||
this.path = id + ",";
|
||||
}
|
||||
@ -159,6 +162,20 @@ public class SaasCooperateShip implements Serializable {
|
||||
this.path = parent.path + id + ",";
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算节点路径, 需要先将父级path设置好
|
||||
*/
|
||||
public void calcPath() {
|
||||
if (this.getParentId() == null) {
|
||||
this.parentId = 0L;
|
||||
}
|
||||
if (Objects.equals(this.getParentId(), 0L)) {
|
||||
this.path = id + ",";
|
||||
}
|
||||
this.path = this.path + id + ",";
|
||||
}
|
||||
|
||||
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Getter
|
||||
public enum ParterShipEnum {
|
||||
|
||||
@ -19,6 +19,15 @@ public interface CooperateShipQueryRepository {
|
||||
return oneOpt(req).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
default SaasCooperateShip getById(Long id) {
|
||||
return oneOpt(OneReq.builder().id(id).build()).orElse(null);
|
||||
}
|
||||
|
||||
default Optional<SaasCooperateShip> oneOpt(OneReq req) {
|
||||
req.check();
|
||||
ListReq listReq = BeanUtil.toBean(req, ListReq.class);
|
||||
|
||||
@ -28,7 +28,7 @@ public interface CooperateShipUpsertRepository {
|
||||
* @param node
|
||||
* @return
|
||||
*/
|
||||
SaasCooperateShip update(UpdateReq node);
|
||||
boolean update(UpdateReq node);
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@NoArgsConstructor
|
||||
|
||||
@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWra
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ -16,21 +17,34 @@ import org.springframework.stereotype.Service;
|
||||
public class CooperateShipUpsertRepositoryImpl implements CooperateShipUpsertRepository {
|
||||
|
||||
private final SaasCooperateShipDao cooperateShipDao;
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
|
||||
@Override
|
||||
public SaasCooperateShip create(SaasCooperateShip cooperateShip) {
|
||||
cooperateShipDao.save(cooperateShip);
|
||||
return cooperateShipDao.getById(cooperateShip.getId());
|
||||
|
||||
transactionTemplate.executeWithoutResult(r -> {
|
||||
// 保存协同关系
|
||||
cooperateShipDao.save(cooperateShip);
|
||||
// 计算path的值, 需要先保存,才能获取到id
|
||||
cooperateShip.calcPath();
|
||||
|
||||
// 更新path
|
||||
update(CooperateShipUpsertRepository.UpdateReq.builder()
|
||||
.id(cooperateShip.getId())
|
||||
.path(cooperateShip.getPath())
|
||||
.build());
|
||||
});
|
||||
|
||||
return cooperateShip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaasCooperateShip update(UpdateReq req) {
|
||||
public boolean update(UpdateReq req) {
|
||||
Axssert.checkNonNull(req.getId(), "更新协同部门,协同id不能为空");
|
||||
LambdaUpdateChainWrapper<SaasCooperateShip> wrapper = cooperateShipDao.lambdaUpdate().eq(SaasCooperateShip::getId, req.getId());
|
||||
if (CollUtil.isNotEmpty(req.getSetNullFields())) {
|
||||
req.getSetNullFields().forEach(e -> wrapper.set(e, null));
|
||||
}
|
||||
wrapper.update(req);
|
||||
return cooperateShipDao.getById(req.getId());
|
||||
return wrapper.update(req);
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,6 +42,15 @@ public interface NodeQueryRepository {
|
||||
return page(page).getData().stream().findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id获取
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
default OrganizationalNode getById(Long id) {
|
||||
return oneOpt(OneReq.builder().id(id).build()).orElse(null);
|
||||
}
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
|
||||
@ -38,6 +38,15 @@ public interface UnitQueryRepository {
|
||||
return page(page).getData().stream().findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id获取
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
default UnitResp getById(Long id) {
|
||||
return oneOpt(OneReq.builder().id(id).build()).orElse(null);
|
||||
}
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
|
||||
@ -33,6 +33,7 @@ import java.util.stream.Collectors;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
@ -43,7 +44,7 @@ public class CooperateShipFoundationServiceImpl implements CooperateShipFoundati
|
||||
private final CooperateShipQueryRepository cooperateShipQueryRepository;
|
||||
private final CooperateShipUpsertRepository cooperateShipUpsertRepository;
|
||||
private final EventProducer eventProducer;
|
||||
private final WorkspaceGateway workspaceGateway;
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
|
||||
/**
|
||||
* 顶级协同关系类型
|
||||
@ -142,16 +143,15 @@ public class CooperateShipFoundationServiceImpl implements CooperateShipFoundati
|
||||
@Override
|
||||
public SaasCooperateShip create(CooperateShipCreator creator) {
|
||||
|
||||
// 如果parentId不为空, 查询父级协同节点
|
||||
// 如果parentId不为空, 初始化父级协同节点
|
||||
SaasCooperateShip parentCooperateShip = null;
|
||||
if (Objects.nonNull(creator.getParentId()) && creator.getParentId() > 0) {
|
||||
parentCooperateShip = cooperateShipQueryRepository
|
||||
.oneOpt(CooperateShipQueryRepository.OneReq.builder().id(creator.getParentId()).build())
|
||||
.orElseThrow(() -> BizResultCode.ENTITY_NOT_FOUND.toException("父级协同部门不存在{}", creator.getOrganizationalNodeId()));
|
||||
parentCooperateShip = cooperateShipQueryRepository.getById(creator.getParentId());
|
||||
Axssert.check(parentCooperateShip != null, BizResultCode.ENTITY_NOT_FOUND, "父级协同部门不存在{}", creator.getOrganizationalNodeId());
|
||||
Axssert.check(Objects.equals(parentCooperateShip.getWorkspaceId(), creator.getWorkspaceId()), BizResultCode.INVALID_PARAM, "父级协同部门和要加入部门不是同一个工作台");
|
||||
}
|
||||
|
||||
// 创建协同关系
|
||||
// 构建新的协同关系
|
||||
SaasCooperateShip cooperateShip = SaasCooperateShip.builder()
|
||||
.parentId(creator.getParentId())
|
||||
.workspaceId(creator.getWorkspaceId())
|
||||
@ -165,17 +165,12 @@ public class CooperateShipFoundationServiceImpl implements CooperateShipFoundati
|
||||
.organizationalNodeId(creator.getOrganizationalNodeId())
|
||||
.partnerShip(creator.getPartnerShip())
|
||||
.createBy(creator.getOperatorId())
|
||||
.path(Optional.ofNullable(parentCooperateShip).map(SaasCooperateShip::getPath).orElse(""))
|
||||
.build();
|
||||
|
||||
// 保存协同关系
|
||||
SaasCooperateShip savedCooperateShip = cooperateShipUpsertRepository.create(cooperateShip);
|
||||
|
||||
|
||||
// 更新协同关系路径
|
||||
savedCooperateShip.calcPath(parentCooperateShip);
|
||||
SaasCooperateShip saved = cooperateShipUpsertRepository.update(CooperateShipUpsertRepository.UpdateReq.builder()
|
||||
.id(savedCooperateShip.getId())
|
||||
.path(savedCooperateShip.getPath())
|
||||
.build());
|
||||
|
||||
// 发送领域事件
|
||||
eventProducer.send(Event.builder()
|
||||
.eventCode(CooperateShipEventType.COOPERATE_SHIP_UPSERTED.getEventCode())
|
||||
@ -183,14 +178,14 @@ public class CooperateShipFoundationServiceImpl implements CooperateShipFoundati
|
||||
.operatorType(getClass().getSimpleName())
|
||||
.targetType("cooperate_ship_id")
|
||||
.targetId(cooperateShip.getId() + "")
|
||||
.shardingKey(saved.getOrganizationalNodeId() + "")
|
||||
.shardingKey(savedCooperateShip.getOrganizationalNodeId() + "")
|
||||
.data(CoopeateShipUpsertedPayload.builder()
|
||||
.newValue(BeanUtil.copyProperties(saved, OrgCooperateShipDTO.class))
|
||||
.newValue(BeanUtil.copyProperties(savedCooperateShip, OrgCooperateShipDTO.class))
|
||||
.oldValue(null)
|
||||
.build())
|
||||
.build());
|
||||
// 返回结果
|
||||
return cooperateShipQueryRepository.one(CooperateShipQueryRepository.OneReq.builder().id(saved.getId()).build());
|
||||
return cooperateShipQueryRepository.one(CooperateShipQueryRepository.OneReq.builder().id(savedCooperateShip.getId()).build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -41,23 +41,10 @@ import java.util.*;
|
||||
@RequiredArgsConstructor
|
||||
public class CooperateShipServiceImpl implements CooperateShipService {
|
||||
|
||||
private static final Set<Integer> ALLOWED_TOP_COOPERATE_TYPES = ImmutableSet.of(
|
||||
CooperateShipTypeEnum.PROJ_PRIMARY_CONTRACTING_UNIT.getCode(),
|
||||
CooperateShipTypeEnum.PROJ_CONSTRUCTION_UNIT.getCode(),
|
||||
CooperateShipTypeEnum.PROJ_SUPERVISION_UNIT.getCode(),
|
||||
CooperateShipTypeEnum.OMS.getCode(),
|
||||
CooperateShipTypeEnum.ENT_COMMON.getCode(),
|
||||
CooperateShipTypeEnum.SURVEY_UNIT.getCode(),
|
||||
CooperateShipTypeEnum.DESIGN_UNIT.getCode(),
|
||||
CooperateShipTypeEnum.OTHER.getCode()
|
||||
);
|
||||
|
||||
private final CooperateShipQueryRepository cooperateShipQueryRepository;
|
||||
private final CooperateShipUpsertRepository cooperateShipUpsertRepository;
|
||||
private final NodeQueryRepository nodeQueryRepository;
|
||||
private final UnitQueryRepository unitQueryRepository;
|
||||
private final WorkspaceGateway workspaceGateway;
|
||||
private final EventProducer eventProducer;
|
||||
private final CooperateShipFoundationService cooperateShipFoundationService;
|
||||
|
||||
@Transactional
|
||||
@ -65,8 +52,8 @@ public class CooperateShipServiceImpl implements CooperateShipService {
|
||||
public SaasCooperateShip create(CreateOrgCooperateShipReq req) {
|
||||
|
||||
// 获取组织节点
|
||||
OrganizationalNode node = nodeQueryRepository.oneOpt(NodeQueryRepository.OneReq.builder().id(req.getOrganizationalNodeId()).build())
|
||||
.orElseThrow(() -> BizResultCode.ENTITY_NOT_FOUND.toException("部门不存在{}", req.getOrganizationalNodeId()));
|
||||
OrganizationalNode node = nodeQueryRepository.getById(req.getOrganizationalNodeId());
|
||||
Axssert.check(node != null, BizResultCode.ENTITY_NOT_FOUND, "部门不存在", req.getOrganizationalNodeId());
|
||||
Axssert.check(node.isTopNode(), BizResultCode.INVALID_PARAM, "只有顶级节点才能添加协同关系");
|
||||
|
||||
// 获取工作台信息
|
||||
@ -74,9 +61,8 @@ public class CooperateShipServiceImpl implements CooperateShipService {
|
||||
Axssert.checkNonNull(workspace, "工作台不存在{}", req.getWorkspaceId());
|
||||
|
||||
// 获取组织单位
|
||||
UnitQueryRepository.UnitResp unit = unitQueryRepository.oneOpt(UnitQueryRepository.OneReq.builder()
|
||||
.id(node.getId())
|
||||
.build()).orElseThrow(() -> BizResultCode.ENTITY_NOT_FOUND.toException("单位不存在{}", node.getOrganizationalUnitId()));
|
||||
UnitQueryRepository.UnitResp unit = unitQueryRepository.getById(node.getOrganizationalUnitId());
|
||||
Axssert.check(unit != null, BizResultCode.ENTITY_NOT_FOUND, "单位不存在", node.getOrganizationalUnitId());
|
||||
|
||||
// 创建协同关系
|
||||
return cooperateShipFoundationService.create(CooperateShipCreator.builder()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user