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