优化内容

This commit is contained in:
2026-05-02 17:45:58 +08:00
parent bcd30ae5de
commit da2053c520
38 changed files with 364 additions and 133 deletions

View File

@@ -244,6 +244,31 @@ public class AdminController {
}
}
/**
* 删除普通用户
*/
@Operation(summary = "删除普通用户")
@DeleteMapping("/users/{id}")
public ResponseEntity<Map<String, Object>> deleteUser(@PathVariable Long id) {
try {
adminService.deleteUser(id);
Map<String, Object> response = new HashMap<>();
response.put("success", true);
response.put("message", "用户删除成功");
return ResponseEntity.ok(response);
} catch (Exception e) {
log.error("删除用户失败", e);
Map<String, Object> response = new HashMap<>();
response.put("success", false);
response.put("message", "删除用户失败: " + e.getMessage());
return ResponseEntity.badRequest().body(response);
}
}
/**
* 获取订单列表
*/

View File

@@ -10,6 +10,7 @@ import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@@ -25,8 +26,12 @@ public interface GroupBuyingGroupRepository extends JpaRepository<GroupBuyingGro
List<GroupBuyingGroup> findByLeaderUserId(Long userId);
List<GroupBuyingGroup> findByLeaderUserIdAndStatus(Long userId, Integer status);
Page<GroupBuyingGroup> findByGroupBuyingId(Long groupBuyingId, Pageable pageable);
List<GroupBuyingGroup> findByGroupBuyingId(Long groupBuyingId);
@Query("SELECT g FROM GroupBuyingGroup g WHERE g.id IN " +
"(SELECT m.groupId FROM GroupBuyingMember m WHERE m.userId = :userId AND m.status != 3)")
Page<GroupBuyingGroup> findByMemberUserId(@Param("userId") Long userId, Pageable pageable);
@@ -44,4 +49,8 @@ public interface GroupBuyingGroupRepository extends JpaRepository<GroupBuyingGro
@Modifying
@Query("UPDATE GroupBuyingGroup g SET g.status = :status, g.completedAt = :completedAt WHERE g.id = :id")
int updateStatusAndCompletedAt(@Param("id") Long id, @Param("status") Integer status, @Param("completedAt") LocalDateTime completedAt);
@Modifying
@Query("DELETE FROM GroupBuyingGroup g WHERE g.id IN :ids")
int deleteByIdIn(@Param("ids") Collection<Long> ids);
}

View File

@@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@@ -33,4 +34,10 @@ public interface GroupBuyingMemberRepository extends JpaRepository<GroupBuyingMe
@Query("SELECT COUNT(m) FROM GroupBuyingMember m WHERE m.userId = :userId AND m.status != 3 " +
"AND m.groupId IN (SELECT g.id FROM GroupBuyingGroup g WHERE g.groupBuyingId = :activityId)")
long countActiveByUserIdAndActivityId(@Param("userId") Long userId, @Param("activityId") Long activityId);
@Modifying
@Query("DELETE FROM GroupBuyingMember m WHERE m.groupId IN :groupIds")
int deleteByGroupIdIn(@Param("groupIds") Collection<Long> groupIds);
void deleteByUserId(Long userId);
}

View File

@@ -27,4 +27,6 @@ public interface NotificationRepository extends JpaRepository<Notification, Long
int markAsRead(@Param("id") Long id, @Param("userId") Long userId);
void deleteByUserId(Long userId);
void deleteByLinkIn(List<String> links);
}

View File

@@ -2,11 +2,13 @@ package com.org.flashsalesystem.repository;
import com.org.flashsalesystem.entity.OrderItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
@Repository
@@ -19,4 +21,8 @@ public interface OrderItemRepository extends JpaRepository<OrderItem, Long> {
@Query("SELECT COALESCE(SUM(i.subtotal), 0) FROM OrderItem i JOIN Order o ON i.orderId = o.id WHERE i.productId = :productId AND o.status IN (2,3,4)")
BigDecimal sumSubtotalByProductId(@Param("productId") Long productId);
@Modifying
@Query("DELETE FROM OrderItem i WHERE i.orderId IN :orderIds")
int deleteByOrderIdIn(@Param("orderIds") Collection<Long> orderIds);
}

View File

@@ -11,6 +11,7 @@ import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
/**
@@ -167,4 +168,12 @@ public interface OrderRepository extends JpaRepository<Order, Long> {
BigDecimal sumTotalPriceByProductId(@Param("productId") Long productId);
List<Order> findByGroupNoOrderByCreatedAtAsc(String groupNo);
List<Order> findByGroupBuyingGroupIdIn(Collection<Long> groupIds);
@Modifying
@Query("DELETE FROM Order o WHERE o.id IN :orderIds")
int deleteByIdIn(@Param("orderIds") Collection<Long> orderIds);
void deleteByUserId(Long userId);
}

View File

@@ -4,8 +4,12 @@ import com.org.flashsalesystem.entity.OrderReturn;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@@ -25,4 +29,10 @@ public interface OrderReturnRepository extends JpaRepository<OrderReturn, Long>
Page<OrderReturn> findAllByOrderByCreatedAtDesc(Pageable pageable);
long countByStatus(Integer status);
@Modifying
@Query("DELETE FROM OrderReturn r WHERE r.orderId IN :orderIds")
int deleteByOrderIdIn(@Param("orderIds") Collection<Long> orderIds);
void deleteByUserId(Long userId);
}

View File

@@ -2,10 +2,12 @@ package com.org.flashsalesystem.repository;
import com.org.flashsalesystem.entity.ProductReview;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@@ -30,4 +32,10 @@ public interface ProductReviewRepository extends JpaRepository<ProductReview, Lo
boolean existsByOrderIdAndProductId(Long orderId, Long productId);
Optional<ProductReview> findByOrderIdAndProductId(Long orderId, Long productId);
@Modifying
@Query("DELETE FROM ProductReview r WHERE r.orderId IN :orderIds")
int deleteByOrderIdIn(@Param("orderIds") Collection<Long> orderIds);
void deleteByUserId(Long userId);
}

View File

@@ -12,4 +12,6 @@ public interface UserAddressRepository extends JpaRepository<UserAddress, Long>
List<UserAddress> findByUserIdOrderByIsDefaultDescUpdatedAtDesc(Long userId);
Optional<UserAddress> findByUserIdAndIsDefaultTrue(Long userId);
Optional<UserAddress> findByIdAndUserId(Long id, Long userId);
void deleteByUserId(Long userId);
}

View File

@@ -14,4 +14,6 @@ public interface UserFavoriteRepository extends JpaRepository<UserFavorite, Long
boolean existsByUserIdAndProductId(Long userId, Long productId);
long countByUserId(Long userId);
void deleteByUserIdAndProductId(Long userId, Long productId);
void deleteByUserId(Long userId);
}

View File

@@ -1,18 +1,8 @@
package com.org.flashsalesystem.service;
import com.org.flashsalesystem.dto.UserDTO;
import com.org.flashsalesystem.entity.Order;
import com.org.flashsalesystem.entity.Product;
import com.org.flashsalesystem.entity.ProductReview;
import com.org.flashsalesystem.entity.User;
import com.org.flashsalesystem.entity.UserFavorite;
import com.org.flashsalesystem.repository.FlashSaleRepository;
import com.org.flashsalesystem.repository.OrderItemRepository;
import com.org.flashsalesystem.repository.OrderRepository;
import com.org.flashsalesystem.repository.ProductRepository;
import com.org.flashsalesystem.repository.ProductReviewRepository;
import com.org.flashsalesystem.repository.UserFavoriteRepository;
import com.org.flashsalesystem.repository.UserRepository;
import com.org.flashsalesystem.entity.*;
import com.org.flashsalesystem.repository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,6 +11,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.io.File;
@@ -60,6 +51,21 @@ public class AdminService {
@Autowired
private FlashSaleRepository flashSaleRepository;
@Autowired
private OrderReturnRepository orderReturnRepository;
@Autowired
private UserAddressRepository userAddressRepository;
@Autowired
private NotificationRepository notificationRepository;
@Autowired
private GroupBuyingGroupRepository groupBuyingGroupRepository;
@Autowired
private GroupBuyingMemberRepository groupBuyingMemberRepository;
@Autowired
private RedisService redisService;
@@ -404,6 +410,71 @@ public class AdminService {
}
}
@Transactional
public void deleteUser(Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("用户不存在"));
String role = user.getRole() == null ? "" : user.getRole();
if ("ADMIN".equalsIgnoreCase(role) || "admin".equalsIgnoreCase(user.getUsername())) {
throw new RuntimeException("管理员账号不能删除");
}
List<GroupBuyingGroup> formingLedGroups = groupBuyingGroupRepository.findByLeaderUserIdAndStatus(id, 1);
if (!formingLedGroups.isEmpty()) {
throw new RuntimeException("该用户是进行中团组的团长,不能删除");
}
List<Long> ledGroupIds = groupBuyingGroupRepository.findByLeaderUserId(id).stream()
.map(GroupBuyingGroup::getId)
.collect(Collectors.toList());
deleteGroupData(ledGroupIds);
List<Long> orderIds = orderRepository.findByUserId(id).stream()
.map(Order::getId)
.collect(Collectors.toList());
deleteOrderData(orderIds);
groupBuyingMemberRepository.deleteByUserId(id);
notificationRepository.deleteByUserId(id);
userFavoriteRepository.deleteByUserId(id);
userAddressRepository.deleteByUserId(id);
productReviewRepository.deleteByUserId(id);
orderReturnRepository.deleteByUserId(id);
orderRepository.deleteByUserId(id);
redisService.sRem("online_users", id.toString());
userRepository.deleteById(id);
}
private void deleteGroupData(List<Long> groupIds) {
if (groupIds == null || groupIds.isEmpty()) {
return;
}
List<Long> groupOrderIds = orderRepository.findByGroupBuyingGroupIdIn(groupIds).stream()
.map(Order::getId)
.collect(Collectors.toList());
deleteOrderData(groupOrderIds);
groupBuyingMemberRepository.deleteByGroupIdIn(groupIds);
groupBuyingGroupRepository.deleteByIdIn(groupIds);
}
private void deleteOrderData(List<Long> orderIds) {
if (orderIds == null || orderIds.isEmpty()) {
return;
}
List<String> orderLinks = orderIds.stream()
.map(orderId -> "/order/" + orderId)
.collect(Collectors.toList());
notificationRepository.deleteByLinkIn(orderLinks);
productReviewRepository.deleteByOrderIdIn(orderIds);
orderReturnRepository.deleteByOrderIdIn(orderIds);
orderItemRepository.deleteByOrderIdIn(orderIds);
orderRepository.deleteByIdIn(orderIds);
}
/**
* 获取订单列表
*/

View File

@@ -79,6 +79,14 @@ public class FlashSaleService {
Product product = productOpt.get();
if (createDTO.getStartTime() != null && createDTO.getStartTime().isBefore(LocalDateTime.now())) {
throw new RuntimeException("开始时间不能早于当前时间");
}
if (createDTO.getStartTime() != null && createDTO.getEndTime() != null
&& !createDTO.getEndTime().isAfter(createDTO.getStartTime())) {
throw new RuntimeException("结束时间必须晚于开始时间");
}
// 创建秒杀活动
FlashSale flashSale = new FlashSale();
BeanUtils.copyProperties(createDTO, flashSale);

View File

@@ -56,6 +56,15 @@ public class GroupBuyingService {
@Autowired
private OrderItemRepository orderItemRepository;
@Autowired
private ProductReviewRepository productReviewRepository;
@Autowired
private OrderReturnRepository orderReturnRepository;
@Autowired
private NotificationRepository notificationRepository;
@Autowired
private RedisService redisService;
@@ -132,15 +141,49 @@ public class GroupBuyingService {
GroupBuying gb = groupBuyingRepository.findById(id)
.orElseThrow(() -> new RuntimeException("拼团活动不存在"));
if (gb.getStatus() == 2) {
if (isEffectivelyActive(gb)) {
throw new RuntimeException("进行中的活动不能删除");
}
groupBuyingRepository.deleteById(id);
List<GroupBuyingGroup> groups = groupBuyingGroupRepository.findByGroupBuyingId(id);
List<Long> groupIds = groups.stream()
.map(GroupBuyingGroup::getId)
.collect(Collectors.toList());
if (!groupIds.isEmpty()) {
List<Long> orderIds = orderRepository.findByGroupBuyingGroupIdIn(groupIds).stream()
.map(Order::getId)
.collect(Collectors.toList());
deleteOrderData(orderIds);
groupBuyingMemberRepository.deleteByGroupIdIn(groupIds);
groupBuyingGroupRepository.deleteByIdIn(groupIds);
}
groupBuyingRepository.delete(gb);
redisService.delete(GB_STOCK_PREFIX + id);
return true;
}
private boolean isEffectivelyActive(GroupBuying gb) {
LocalDateTime now = LocalDateTime.now();
return Integer.valueOf(2).equals(gb.getStatus()) && !now.isBefore(gb.getStartTime()) && now.isBefore(gb.getEndTime());
}
private void deleteOrderData(List<Long> orderIds) {
if (orderIds == null || orderIds.isEmpty()) {
return;
}
List<String> orderLinks = orderIds.stream()
.map(orderId -> "/order/" + orderId)
.collect(Collectors.toList());
notificationRepository.deleteByLinkIn(orderLinks);
productReviewRepository.deleteByOrderIdIn(orderIds);
orderReturnRepository.deleteByOrderIdIn(orderIds);
orderItemRepository.deleteByOrderIdIn(orderIds);
orderRepository.deleteByIdIn(orderIds);
}
// ========== 查询操作 ==========
public Map<String, Object> getGroupBuyingList(int page, int size, Integer status) {
@@ -584,8 +627,9 @@ public class GroupBuyingService {
dto.setTotalStock(gb.getTotalStock());
dto.setRemainingStock(gb.getRemainingStock());
dto.setMaxPerUser(gb.getMaxPerUser());
dto.setStatus(gb.getStatus());
dto.setStatusDescription(getStatusDescription(gb.getStatus()));
int effectiveStatus = getEffectiveStatus(gb);
dto.setStatus(effectiveStatus);
dto.setStatusDescription(getStatusDescription(effectiveStatus));
dto.setStartTime(gb.getStartTime());
dto.setEndTime(gb.getEndTime());
dto.setCreatedAt(gb.getCreatedAt());
@@ -668,6 +712,17 @@ public class GroupBuyingService {
}
}
private int getEffectiveStatus(GroupBuying gb) {
LocalDateTime now = LocalDateTime.now();
if (Integer.valueOf(2).equals(gb.getStatus()) && !now.isBefore(gb.getEndTime())) {
return 3;
}
if (Integer.valueOf(1).equals(gb.getStatus()) && !now.isBefore(gb.getStartTime()) && now.isBefore(gb.getEndTime())) {
return 2;
}
return gb.getStatus() == null ? 0 : gb.getStatus();
}
private String getGroupStatusDescription(Integer status) {
if (status == null) return "未知";
switch (status) {