删除动态接口逻辑更改

This commit is contained in:
tianfeng 2026-02-04 10:28:38 +08:00
parent bf9f08e34d
commit 758759f780
2 changed files with 100 additions and 4 deletions

View File

@ -1,16 +1,22 @@
package com.red.circle.other.app.command.dynamic.delete;
import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.common.business.core.enums.DynamicEventTypeEnum;
import com.red.circle.common.business.dto.cmd.IdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.mq.business.model.event.dynamic.DynamicEvent;
import com.red.circle.mq.rocket.business.producer.DynamicMqMessage;
import com.red.circle.other.app.enums.violation.ViolationTypeEnum;
import com.red.circle.other.infra.database.mongo.service.dynamic.DynamicPopularService;
import com.red.circle.other.infra.database.rds.entity.approval.ApprovalDynamicContent;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicContent;
import com.red.circle.other.infra.database.rds.service.approval.ApprovalDynamicContentService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicContentService;
import com.red.circle.other.infra.database.rds.service.sys.AdministratorAuthService;
import com.red.circle.other.infra.database.rds.service.sys.AdministratorService;
import com.red.circle.other.inner.asserts.DynamicErrorCode;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
@ -32,13 +38,32 @@ public class DynamicContentDelCmdExe {
private final DynamicContentService dynamicContentService;
private final DynamicPopularService dynamicPopularService;
private final ApprovalDynamicContentService approvalDynamicContentService;
private final AdministratorService administratorService;
private final AdministratorAuthService administratorAuthService;
public void execute(IdLongCmd cmd) {
DynamicContent dynamicContent = dynamicContentService.getById(cmd.getId());
ResponseAssert.notNull(DynamicErrorCode.CONTENT_IS_NULL, dynamicContent);
ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
Objects.equals(dynamicContent.getCreateUser(), cmd.getReqUserId()));
boolean isOwn = Objects.equals(dynamicContent.getCreateUser(), cmd.getReqUserId());
boolean isAdminAuth = administratorService.existsAdmin(cmd.getReqUserId()) &&
administratorAuthService.existsAuth(cmd.requiredReqUserId(), ViolationTypeEnum.DELETE_CONTENT.name());
ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION, isOwn || isAdminAuth);
// 管理员删除别人的 送入待审核
if (isAdminAuth && !isOwn) {
ApprovalDynamicContent approvalDynamicContent = new ApprovalDynamicContent();
approvalDynamicContent.setUserId(cmd.getReqUserId());
approvalDynamicContent.setDynamicId(dynamicContent.getId());
approvalDynamicContent.setApproveStatus(ApprovalStatusEnum.PENDING.name());
approvalDynamicContent.setSysOrigin(dynamicContent.getSysOrigin());
approvalDynamicContent.setCreateTime(TimestampUtils.now());
approvalDynamicContent.setCreateUser(cmd.getReqUserId());
approvalDynamicContentService.save(approvalDynamicContent);
return;
}
dynamicContent.setDel(Boolean.TRUE);
dynamicContent.setUpdateUser(cmd.getReqUserId());

View File

@ -1,25 +1,96 @@
package user;
import com.mongodb.bulk.BulkWriteResult;
import com.red.circle.OtherServiceApplication;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMemberTarget;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberTargetService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@SpringBootTest(classes = OtherServiceApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class UserProfileTest {
@Autowired
private UserProfileClient userProfileClient;
@Autowired
private TeamProfileService teamProfileService;
@Autowired
private UserProfileGateway userProfileGateway;
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void userProfileTest(){
ResultResponse<UserProfileDTO> byUserId = userProfileClient.getByUserId(1999166404179394561L);
public void test(){
List<TeamProfile> teamProfiles = teamProfileService.listStatusAvailable();
if (teamProfiles.isEmpty()) {
log.info("没有需要更新的团队");
return;
}
Set<Long> userIds = teamProfiles.stream()
.map(TeamProfile::getOwnUserId)
.collect(Collectors.toSet());
Map<Long, UserProfile> userProfileMap = userProfileGateway.mapByUserIds(userIds);
BulkOperations bulkOps = mongoTemplate.bulkOps(
BulkOperations.BulkMode.UNORDERED,
TeamMemberTarget.class
);
int updateCount = 0;
for (TeamProfile teamProfile : teamProfiles) {
UserProfile userProfile = userProfileMap.get(teamProfile.getOwnUserId());
if (userProfile == null) {
log.warn("团队 {} 的团队长 {} 用户资料不存在",
teamProfile.getId(), teamProfile.getOwnUserId());
continue;
}
if (teamProfile.getRegion() == null || userProfile.getCountryCode() == null) {
log.warn("团队 {} 的区域或国家代码为空", teamProfile.getId());
continue;
}
bulkOps.updateMulti(
Query.query(Criteria.where("teamId").is(teamProfile.getId())),
new Update()
.set("region", teamProfile.getRegion())
.set("countryCode", userProfile.getCountryCode())
);
updateCount++;
}
if (updateCount > 0) {
BulkWriteResult result = bulkOps.execute();
log.info("批量更新完成,影响 {} 条记录", result.getModifiedCount());
} else {
log.info("没有需要更新的有效数据");
}
}
}