|
|
@@ -8,11 +8,19 @@ import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
import com.ruoyi.system.domain.CheckoutCardRow;
|
|
|
import com.ruoyi.system.domain.CheckoutMemberRow;
|
|
|
+import com.ruoyi.system.domain.InfoUser;
|
|
|
import com.ruoyi.system.domain.PosOrder;
|
|
|
+import com.ruoyi.system.domain.PosStore;
|
|
|
import com.ruoyi.system.mapper.MultiStoreCheckoutQueryMapper;
|
|
|
+import com.ruoyi.system.service.IInfoUserService;
|
|
|
import com.ruoyi.system.service.IPosOrderService;
|
|
|
+import com.ruoyi.system.service.IPosStoreService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -31,20 +39,26 @@ public class MultiStoreCheckoutQueryService {
|
|
|
private final MultiStoreCheckoutQueryMapper mapper;
|
|
|
private final MultiStoreCheckoutRefundProjectionService refunds;
|
|
|
private final IPosOrderService ordinaryOrders;
|
|
|
+ private final IPosStoreService stores;
|
|
|
+ private final IInfoUserService users;
|
|
|
|
|
|
public MultiStoreCheckoutQueryService(MultiStoreCheckoutQueryMapper mapper,
|
|
|
MultiStoreCheckoutRefundProjectionService refunds,
|
|
|
- IPosOrderService ordinaryOrders) {
|
|
|
+ IPosOrderService ordinaryOrders,
|
|
|
+ IPosStoreService stores,
|
|
|
+ IInfoUserService users) {
|
|
|
this.mapper = mapper;
|
|
|
this.refunds = refunds;
|
|
|
this.ordinaryOrders = ordinaryOrders;
|
|
|
+ this.stores = stores;
|
|
|
+ this.users = users;
|
|
|
}
|
|
|
|
|
|
public MultiStorePageView userCards(Long userId, int page, int size, String tab) {
|
|
|
validate(userId, page, size, tab, USER_TABS);
|
|
|
long total = mapper.countUserCards(userId, tab);
|
|
|
List<CheckoutCardRow> rows = mapper.selectUserCards(userId, tab, (page - 1) * size, size);
|
|
|
- return new MultiStorePageView(assemble(rows), total, page, size);
|
|
|
+ return new MultiStorePageView(assemble(rows, null, null), total, page, size);
|
|
|
}
|
|
|
|
|
|
public Map<String, Long> userCardCount(Long userId) {
|
|
|
@@ -58,7 +72,7 @@ public class MultiStoreCheckoutQueryService {
|
|
|
if (batchId == null || userId == null) throw invalid();
|
|
|
CheckoutCardRow row = mapper.selectBatchForUser(batchId, userId);
|
|
|
if (row == null) throw invalid();
|
|
|
- return assemble(List.of(row)).get(0);
|
|
|
+ return assemble(List.of(row), null, null).get(0);
|
|
|
}
|
|
|
|
|
|
public MultiStorePageView riderTasks(Long riderId, int page, int size, String tab,
|
|
|
@@ -68,7 +82,7 @@ public class MultiStoreCheckoutQueryService {
|
|
|
long total = mapper.countRiderTasks(riderId, tab, longitude, latitude, limitMeters);
|
|
|
List<CheckoutCardRow> rows = mapper.selectRiderTasks(riderId, tab, longitude, latitude,
|
|
|
limitMeters, (page - 1) * size, size);
|
|
|
- return new MultiStorePageView(assemble(rows), total, page, size);
|
|
|
+ return new MultiStorePageView(assemble(rows, longitude, latitude), total, page, size);
|
|
|
}
|
|
|
|
|
|
public Map<String, Long> riderTaskCount(Long riderId, BigDecimal longitude, BigDecimal latitude,
|
|
|
@@ -85,10 +99,11 @@ public class MultiStoreCheckoutQueryService {
|
|
|
if (groupId == null || riderId == null) throw invalid();
|
|
|
CheckoutCardRow row = mapper.selectGroupForRider(groupId, riderId, limitMeters);
|
|
|
if (row == null) throw invalid();
|
|
|
- return assemble(List.of(row)).get(0);
|
|
|
+ return assemble(List.of(row), null, null).get(0);
|
|
|
}
|
|
|
|
|
|
- private List<MultiStoreCardView> assemble(List<CheckoutCardRow> rows) {
|
|
|
+ private List<MultiStoreCardView> assemble(List<CheckoutCardRow> rows, BigDecimal longitude,
|
|
|
+ BigDecimal latitude) {
|
|
|
if (rows == null || rows.isEmpty()) return List.of();
|
|
|
List<Long> batchIds = rows.stream().map(CheckoutCardRow::getBatchId).filter(Objects::nonNull).distinct().toList();
|
|
|
Map<Long, List<CheckoutMemberRow>> members = new HashMap<>();
|
|
|
@@ -105,19 +120,74 @@ public class MultiStoreCheckoutQueryService {
|
|
|
List<MultiStoreCardView> result = rows.stream().map(row -> assembleOne(row,
|
|
|
members.getOrDefault(row.getBatchId(), List.of()),
|
|
|
refundFacts.getOrDefault(row.getBatchId(), Map.of()))).toList();
|
|
|
- List<Long> ordinaryIds = rows.stream().filter(row -> "ORDER".equals(row.getCardType()))
|
|
|
- .map(CheckoutCardRow::getCardId).filter(Objects::nonNull).distinct().toList();
|
|
|
- if (!ordinaryIds.isEmpty()) {
|
|
|
- // 仅查询当前页普通单,复用原 PosOrder 快照字段,避免新组合接口丢失旧单展示内容。
|
|
|
+ List<Long> orderIds = new ArrayList<>();
|
|
|
+ for (CheckoutCardRow row : rows) {
|
|
|
+ if ("ORDER".equals(row.getCardType()) && row.getCardId() != null) orderIds.add(row.getCardId());
|
|
|
+ }
|
|
|
+ for (List<CheckoutMemberRow> batchMembers : members.values()) {
|
|
|
+ for (CheckoutMemberRow member : batchMembers) {
|
|
|
+ if (member.getOrderId() != null) orderIds.add(member.getOrderId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!orderIds.isEmpty()) {
|
|
|
+ // 与旧骑手列表逐项对齐:本页普通单与组子单都补全量订单、门店、用户与直线距离。
|
|
|
Map<Long, PosOrder> originals = new HashMap<>();
|
|
|
- for (PosOrder order : ordinaryOrders.listByIds(ordinaryIds)) originals.put(order.getId(), order);
|
|
|
+ for (PosOrder order : ordinaryOrders.listByIds(orderIds.stream().distinct().toList())) {
|
|
|
+ originals.put(order.getId(), order);
|
|
|
+ }
|
|
|
+ Set<Long> storeIds = new HashSet<>();
|
|
|
+ Set<Long> userIds = new HashSet<>();
|
|
|
+ for (PosOrder order : originals.values()) {
|
|
|
+ if (order.getMdId() != null) storeIds.add(order.getMdId());
|
|
|
+ if (order.getUserId() != null) userIds.add(order.getUserId());
|
|
|
+ }
|
|
|
+ Map<Integer, PosStore> storeMap = storeIds.isEmpty() ? Map.of()
|
|
|
+ : stores.listByIds(storeIds).stream().collect(Collectors.toMap(PosStore::getId, s -> s));
|
|
|
+ Map<Long, InfoUser> userMap = userIds.isEmpty() ? Map.of()
|
|
|
+ : users.listByIds(userIds).stream().collect(Collectors.toMap(InfoUser::getUserId, s -> s));
|
|
|
for (MultiStoreCardView view : result) {
|
|
|
- if ("ORDER".equals(view.getCardType())) view.setLegacyOrder(originals.get(view.getOrderId()));
|
|
|
+ if ("ORDER".equals(view.getCardType())) {
|
|
|
+ attachOrderContext(view.getOrderId(), originals, storeMap, userMap, longitude, latitude,
|
|
|
+ view::setLegacyOrder, view::setStore, view::setUser, view::setJuli);
|
|
|
+ } else {
|
|
|
+ for (MultiStoreMemberView member : view.getMembers()) {
|
|
|
+ attachOrderContext(member.getOrderId(), originals, storeMap, userMap, longitude, latitude,
|
|
|
+ member::setLegacyOrder, member::setStore, member::setUser, member::setJuli);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ private void attachOrderContext(Long orderId, Map<Long, PosOrder> originals,
|
|
|
+ Map<Integer, PosStore> storeMap, Map<Long, InfoUser> userMap,
|
|
|
+ BigDecimal longitude, BigDecimal latitude,
|
|
|
+ java.util.function.Consumer<PosOrder> orderSetter,
|
|
|
+ java.util.function.Consumer<PosStore> storeSetter,
|
|
|
+ java.util.function.Consumer<InfoUser> userSetter,
|
|
|
+ java.util.function.Consumer<Double> juliSetter) {
|
|
|
+ PosOrder order = originals.get(orderId);
|
|
|
+ orderSetter.accept(order);
|
|
|
+ storeSetter.accept(order == null || order.getMdId() == null ? null : storeMap.get(order.getMdId().intValue()));
|
|
|
+ userSetter.accept(order == null || order.getUserId() == null ? null : userMap.get(order.getUserId()));
|
|
|
+ juliSetter.accept(straightKm(order, longitude, latitude));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 旧骑手列表同款球面直线距离(公里、两位小数);订单或请求坐标缺失返回空。 */
|
|
|
+ private Double straightKm(PosOrder order, BigDecimal longitude, BigDecimal latitude) {
|
|
|
+ if (order == null || order.getLongitude() == null || order.getLatitude() == null
|
|
|
+ || longitude == null || latitude == null) return null;
|
|
|
+ double lat1 = Math.toRadians(order.getLatitude().doubleValue());
|
|
|
+ double lat2 = Math.toRadians(latitude.doubleValue());
|
|
|
+ double dlat = lat2 - lat1;
|
|
|
+ double dlng = Math.toRadians(longitude.doubleValue() - order.getLongitude().doubleValue());
|
|
|
+ double a = Math.sin(dlat / 2) * Math.sin(dlat / 2)
|
|
|
+ + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dlng / 2) * Math.sin(dlng / 2);
|
|
|
+ double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
|
+ return Math.round(6371 * c * 100.0) / 100.0;
|
|
|
+ }
|
|
|
+
|
|
|
private MultiStoreCardView assembleOne(CheckoutCardRow row, List<CheckoutMemberRow> members,
|
|
|
Map<Long, MultiStoreRefundMemberView> refundFacts) {
|
|
|
MultiStoreCardView view = new MultiStoreCardView();
|