| | |
| | | import com.tourguide.common.api.PageResponse; |
| | | import com.tourguide.common.exception.BusinessException; |
| | | import com.tourguide.common.validation.ChineseIdCardValidator; |
| | | import com.tourguide.modules.order.dto.VisitorOrderStatRow; |
| | | import com.tourguide.modules.order.mapper.TourOrderMapper; |
| | | import com.tourguide.modules.review.dto.VisitorReviewStatRow; |
| | | import com.tourguide.modules.review.mapper.OrderReviewMapper; |
| | | import com.tourguide.modules.user.dto.MiniProfileUpdateRequest; |
| | | import com.tourguide.modules.user.dto.MiniRealNameUpdateRequest; |
| | | import com.tourguide.modules.user.dto.MiniUserDetailResponse; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** 仅处理小程序用户,管理后台只能通过本服务执行查询或状态管理。 */ |
| | | @Service |
| | | public class MiniUserService { |
| | | private final MiniUserMapper userMapper; |
| | | private final TourOrderMapper orderMapper; |
| | | private final OrderReviewMapper reviewMapper; |
| | | |
| | | public MiniUserService(MiniUserMapper userMapper) { this.userMapper = userMapper; } |
| | | public MiniUserService(MiniUserMapper userMapper, TourOrderMapper orderMapper, OrderReviewMapper reviewMapper) { |
| | | this.userMapper = userMapper; |
| | | this.orderMapper = orderMapper; |
| | | this.reviewMapper = reviewMapper; |
| | | } |
| | | |
| | | public MiniUser findEnabledById(Long id) { |
| | | MiniUser user = userMapper.selectById(id); |
| | |
| | | .lt(endDate != null, MiniUser::getCreatedAt, endDate == null ? null : endDate.plusDays(1).atStartOfDay()) |
| | | .orderByDesc(MiniUser::getId); |
| | | Page<MiniUser> result = userMapper.selectPage(new Page<MiniUser>(page, size), query); |
| | | return new PageResponse<MiniUserResponse>(result.getRecords().stream().map(MiniUserResponse::from).collect(Collectors.toList()), |
| | | List<MiniUser> users = result.getRecords(); |
| | | Map<Long, UserStats> statsByUserId = loadUserStats( |
| | | users.stream().map(MiniUser::getId).collect(Collectors.toList())); |
| | | return new PageResponse<MiniUserResponse>(users.stream() |
| | | .map(user -> MiniUserResponse.from(user, statsOf(statsByUserId, user.getId()).orderCount)) |
| | | .collect(Collectors.toList()), |
| | | result.getTotal(), result.getCurrent(), result.getSize()); |
| | | } |
| | | |
| | | public MiniUserDetailResponse detail(Long id) { |
| | | MiniUser user = userMapper.selectById(id); |
| | | if (user == null) { throw new BusinessException(404, "游客不存在"); } |
| | | return MiniUserDetailResponse.from(user); |
| | | UserStats stats = statsOf(loadUserStats(Collections.singletonList(id)), id); |
| | | return MiniUserDetailResponse.from(user, stats.orderCount, stats.totalConsumption, stats.reviewCount); |
| | | } |
| | | |
| | | private Map<Long, UserStats> loadUserStats(List<Long> visitorIds) { |
| | | if (visitorIds.isEmpty()) { return Collections.emptyMap(); } |
| | | Map<Long, UserStats> statsByUserId = new HashMap<Long, UserStats>(); |
| | | for (Long visitorId : visitorIds) { |
| | | statsByUserId.put(visitorId, new UserStats()); |
| | | } |
| | | List<VisitorOrderStatRow> orderRows = orderMapper.selectVisitorOrderStats(visitorIds); |
| | | if (orderRows != null) { |
| | | for (VisitorOrderStatRow row : orderRows) { |
| | | if (row.getVisitorId() == null) { continue; } |
| | | UserStats stats = statsByUserId.computeIfAbsent(row.getVisitorId(), ignored -> new UserStats()); |
| | | stats.orderCount = defaultLong(row.getOrderCount()); |
| | | stats.totalConsumption = row.getTotalConsumption() == null ? BigDecimal.ZERO : row.getTotalConsumption(); |
| | | } |
| | | } |
| | | List<VisitorReviewStatRow> reviewRows = reviewMapper.selectVisitorReviewStats(visitorIds); |
| | | if (reviewRows != null) { |
| | | for (VisitorReviewStatRow row : reviewRows) { |
| | | if (row.getVisitorId() == null) { continue; } |
| | | statsByUserId.computeIfAbsent(row.getVisitorId(), ignored -> new UserStats()) |
| | | .reviewCount = defaultLong(row.getReviewCount()); |
| | | } |
| | | } |
| | | return statsByUserId; |
| | | } |
| | | |
| | | private static UserStats statsOf(Map<Long, UserStats> statsByUserId, Long userId) { |
| | | UserStats stats = statsByUserId.get(userId); |
| | | return stats == null ? new UserStats() : stats; |
| | | } |
| | | |
| | | private static long defaultLong(Long value) { |
| | | return value == null ? 0L : value; |
| | | } |
| | | |
| | | @Transactional |
| | |
| | | user.setStatus(status); |
| | | userMapper.updateById(user); |
| | | } |
| | | |
| | | private static final class UserStats { |
| | | private long orderCount; |
| | | private BigDecimal totalConsumption = BigDecimal.ZERO; |
| | | private long reviewCount; |
| | | } |
| | | } |