|
|
@@ -79,8 +79,10 @@ import com.fs.hisStore.constants.StoreConstants;
|
|
|
import com.fs.hisStore.domain.*;
|
|
|
import com.fs.hisStore.enums.*;
|
|
|
import com.fs.hisStore.service.*;
|
|
|
+import com.fs.store.vo.FsStoreCartScrmQueryVO;
|
|
|
import com.fs.system.domain.SysConfig;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
+import com.fs.utils.TwelveDigitSnowflake;
|
|
|
import com.fs.ybPay.domain.RefundResult;
|
|
|
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
|
|
|
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
|
|
@@ -985,6 +987,12 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public void deStockIncSales(List<FsStoreCartScrmQueryVO> cartInfo) {
|
|
|
+ for (FsStoreCartScrmQueryVO storeCartVO : cartInfo) {
|
|
|
+ productService.decProductStock(storeCartVO.getProductId(),
|
|
|
+ storeCartVO.getProductAttrValueId(), storeCartVO.getCartNum());
|
|
|
+ }
|
|
|
+ }
|
|
|
public void deStockIncSale(List<FsStoreCartQueryVO> cartInfo) {
|
|
|
for (FsStoreCartQueryVO storeCartVO : cartInfo) {
|
|
|
productService.decProductStock(storeCartVO.getProductId(),
|
|
|
@@ -2086,7 +2094,7 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
|
|
|
List<FsStorePaymentScrm> payments = paymentService.selectFsStorePaymentByOrderId(order.getId());
|
|
|
if (payments != null) {
|
|
|
String json;
|
|
|
- if (CloudHostUtils.hasCloudHostName("康年堂")) {
|
|
|
+ if (CloudHostUtils.hasCloudHostName("康年堂") || CloudHostUtils.hasCloudHostName("医健宝")) {
|
|
|
json = configService.selectConfigByKey("his.pay");
|
|
|
} else {
|
|
|
json = configService.selectConfigByKey("store.pay");
|
|
|
@@ -2129,7 +2137,7 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
|
|
|
request.setHuifuId(fsPayConfig.getHuifuId());
|
|
|
request.setOrdAmt(payment.getPayMoney().toString());
|
|
|
request.setOrgReqDate(new SimpleDateFormat("yyyyMMdd").format(payment.getCreateTime()));
|
|
|
- request.setReqSeqId("refund-" + payment.getPayCode());
|
|
|
+ request.setReqSeqId("refund-" + new TwelveDigitSnowflake(1).nextId());
|
|
|
Map<String, Object> extendInfoMap = new HashMap<>();
|
|
|
extendInfoMap.put("org_party_order_id", payment.getBankSerialNo());
|
|
|
request.setExtendInfo(extendInfoMap);
|
|
|
@@ -3687,50 +3695,818 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static final DateTimeFormatter CST_FORMATTER = DateTimeFormatter
|
|
|
- .ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US)
|
|
|
- .withZone(ZoneId.of("Asia/Shanghai"));
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ @Synchronized
|
|
|
+ public String payConfirmMultiStore(Integer type, Long orderId, String payCode, String tradeNo, String bankTransactionId, String bankSerialNo) {
|
|
|
+ //支付订单
|
|
|
+// FsStoreOrder order = null;
|
|
|
+ try {
|
|
|
+ List<FsStoreOrderScrm> orders = new ArrayList<>();
|
|
|
+ if (type.equals(1)) {
|
|
|
+ List<FsStorePaymentScrm> storePayments = paymentService.selectFsStorePaymentByPaymentCode(payCode);
|
|
|
+ if (storePayments != null && !storePayments.isEmpty()) {
|
|
|
+ for (FsStorePaymentScrm storePayment : storePayments) {
|
|
|
+ if (storePayment == null || !storePayment.getStatus().equals(0)) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ storePayment.setStatus(1);
|
|
|
+ storePayment.setPayTime(new Date());
|
|
|
+ storePayment.setTradeNo(tradeNo);
|
|
|
+ storePayment.setBankSerialNo(bankSerialNo);
|
|
|
+ storePayment.setBankTransactionId(bankTransactionId);
|
|
|
+ paymentService.updateFsStorePayment(storePayment);
|
|
|
+ if (storePayment.getBusinessOrderId() != null && storePayment.getBusinessOrderId().length() > 30) {
|
|
|
+ orders.addAll(fsStoreOrderMapper.getStoreOrderByCombinationId(storePayment.getBusinessOrderId()));
|
|
|
+ } else {
|
|
|
+ orders.add(fsStoreOrderMapper.selectFsStoreOrderById(storePayment.getOrderId()));
|
|
|
|
|
|
- private static final DateTimeFormatter STANDARD_FORMATTER = DateTimeFormatter
|
|
|
- .ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public static LocalDateTime parseCstToLocalDateTime(String cstDateStr) {
|
|
|
- if (cstDateStr == null || cstDateStr.trim().isEmpty()) {
|
|
|
- return null;
|
|
|
+
|
|
|
+ for (FsStoreOrderScrm order : orders) {
|
|
|
+ if (order != null && !order.getStatus().equals(OrderInfoEnum.STATUS_0.getValue())) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (order != null && !order.getPaid().equals(0)) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (type.equals(2)) {
|
|
|
+ //货到付款
|
|
|
+ FsStoreOrderScrm order = fsStoreOrderMapper.selectFsStoreOrderById(orderId);
|
|
|
+ if (!order.getStatus().equals(OrderInfoEnum.STATUS_0.getValue())) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ orders.add(order);
|
|
|
+ }
|
|
|
+ //写入公司佣金
|
|
|
+ if (orders.size() > 0) {
|
|
|
+ for (FsStoreOrderScrm order : orders) {
|
|
|
+ if (order.getCompanyId() != null && order.getCompanyId() > 0) {
|
|
|
+ companyService.addCompanyTuiMoney(order);
|
|
|
+ }
|
|
|
+ //增加用户购买次数
|
|
|
+ userService.incPayCount(order.getUserId());
|
|
|
+ //增加状态
|
|
|
+ orderStatusService.create(order.getId(), OrderLogEnum.PAY_ORDER_SUCCESS.getValue(),
|
|
|
+ OrderLogEnum.PAY_ORDER_SUCCESS.getDesc());
|
|
|
+ // FsUser user=userService.selectFsUserById(order.getUserId());
|
|
|
+ // //增加流水
|
|
|
+ // String payTypeMsg = PayTypeEnum.WEIXIN.getDesc();
|
|
|
+ // billService.addBill(user.getUserId(),
|
|
|
+ // BillDetailEnum.CATEGORY_1.getValue(),
|
|
|
+ // 0,
|
|
|
+ // BillDetailEnum.TYPE_1.getDesc(),
|
|
|
+ // order.getPayPrice().doubleValue(), user.getNowMoney().doubleValue(),
|
|
|
+ // payTypeMsg + order.getPayPrice() + "元购买商品",order.getId().toString());
|
|
|
+
|
|
|
+ FsStoreOrderScrm storeOrder = new FsStoreOrderScrm();
|
|
|
+ storeOrder.setId(order.getId());
|
|
|
+ storeOrder.setPaid(OrderInfoEnum.PAY_STATUS_1.getValue());
|
|
|
+ storeOrder.setStatus(OrderInfoEnum.STATUS_1.getValue());
|
|
|
+ storeOrder.setPayTime(new Date());
|
|
|
+ fsStoreOrderMapper.updateFsStoreOrder(storeOrder);
|
|
|
+ //非处方直接提交OMS
|
|
|
+ if (order.getIsPrescribe().equals(0)) {
|
|
|
+ createOmsOrder(order.getId());
|
|
|
+ } else if (order.getIsPrescribe().equals(1)) {
|
|
|
+ //是否已开方
|
|
|
+ FsPrescribeScrm prescribe = prescribeService.selectFsPrescribeByOrderId(order.getId());
|
|
|
+ if (prescribe != null && prescribe.getStatus() == 1) {
|
|
|
+ createOmsOrder(order.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //处理佣金 套餐不分佣金
|
|
|
+ if (order.getIsPackage() != 1 && order.getTuiUserId() != null && order.getTuiUserId() > 0) {
|
|
|
+ FsStoreOrderItemScrm orderItemMap = new FsStoreOrderItemScrm();
|
|
|
+ orderItemMap.setOrderId(order.getId());
|
|
|
+ List<FsStoreOrderItemScrm> items = storeOrderItemService.selectFsStoreOrderItemList(orderItemMap);
|
|
|
+ userService.addTuiMoney(order, items);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.getStackTrace();
|
|
|
+ logger.info("payConfirm:" + e.getMessage());
|
|
|
+ logger.info("payConfirm:" + e.getLocalizedMessage());
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return "";
|
|
|
}
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
|
|
|
- try {
|
|
|
- // 解析为带时区的日期时间
|
|
|
- ZonedDateTime zonedDateTime = ZonedDateTime.parse(cstDateStr, CST_FORMATTER);
|
|
|
- // 转换为本地日期时间(忽略时区,仅保留年月日时分秒)
|
|
|
- return zonedDateTime.toLocalDateTime();
|
|
|
- } catch (DateTimeParseException e) {
|
|
|
- System.err.println("日期解析失败: " + e.getMessage());
|
|
|
- return null;
|
|
|
+ @Override
|
|
|
+ public String payRemainConfirmMultiStore(String payCode, String tradeNo, String bankTransactionId, String bankSerialNo) {
|
|
|
+ //支付订单
|
|
|
+// FsStoreOrder order = null;
|
|
|
+ String orderIds = "";
|
|
|
+ List<FsStorePaymentScrm> storePayments = paymentService.selectFsStorePaymentByPaymentCode(payCode);
|
|
|
+ if (storePayments != null && !storePayments.isEmpty()) {
|
|
|
+ for (int i = 0; i < storePayments.size(); i++) {
|
|
|
+ if (storePayments.get(i) == null || !storePayments.get(i).getStatus().equals(0)) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ storePayments.get(i).setStatus(1);
|
|
|
+ storePayments.get(i).setPayTime(new Date());
|
|
|
+ storePayments.get(i).setTradeNo(tradeNo);
|
|
|
+ storePayments.get(i).setBankSerialNo(bankSerialNo);
|
|
|
+ storePayments.get(i).setBankTransactionId(bankTransactionId);
|
|
|
+ storePayments.get(i).setIsPayRemain(1);
|
|
|
+ paymentService.updateFsStorePayment(storePayments.get(i));
|
|
|
+ if (i == 0) {
|
|
|
+ orderIds = orderIds + (storePayments.get(i).getOrderId());
|
|
|
+ } else {
|
|
|
+ orderIds = orderIds + "," + storePayments.get(i).getOrderId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// order = fsStoreOrderMapper.selectFsStoreOrderById(storePayment.getOrderId());
|
|
|
+ List<FsStoreOrderScrm> orders = fsStoreOrderMapper.selectFsStoreOrderByOrderIds(orderIds);
|
|
|
+ for (FsStoreOrderScrm order : orders) {
|
|
|
+ //增加状态
|
|
|
+ orderStatusService.create(order.getId(), OrderLogEnum.PAY_REMAIN_ORDER_SUCCESS.getValue(),
|
|
|
+ OrderLogEnum.PAY_REMAIN_ORDER_SUCCESS.getDesc());
|
|
|
+
|
|
|
+ FsStoreOrderScrm storeOrder = new FsStoreOrderScrm();
|
|
|
+ storeOrder.setId(order.getId());
|
|
|
+ storeOrder.setIsPayRemain(1);
|
|
|
+ fsStoreOrderMapper.updateFsStoreOrder(storeOrder);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ return "success";
|
|
|
}
|
|
|
|
|
|
- public static String parseCstToDateOnlyString(String cstDateStr) {
|
|
|
- LocalDateTime dateTime = parseCstToLocalDateTime(cstDateStr);
|
|
|
- return dateTime != null ? dateTime.format(DateTimeFormatter
|
|
|
- .ofPattern("yyyy-MM-dd")) : null;
|
|
|
+ @Override
|
|
|
+ public List<FsStoreOrderScrm> getStoreOrderByCombinationId(String combinationId) {
|
|
|
+ return fsStoreOrderMapper.getStoreOrderByCombinationId(combinationId);
|
|
|
}
|
|
|
|
|
|
- public Map<String, String> buildExpressDeliveryMap() {
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
- map.put("SF", "顺丰");
|
|
|
- map.put("EMS", "邮政");
|
|
|
- map.put("ZTO", "中通");
|
|
|
- map.put("JD", "京东");
|
|
|
- map.put("DBL", "德邦");
|
|
|
- return map;
|
|
|
+ @Override
|
|
|
+ public R confirmOrderMultiStore(long uid, List<FsStoreConfirmOrderGroupStoreParam> cartParamList) {
|
|
|
+ FsUserAddressScrm address = null;
|
|
|
+ List<String> uuids = new ArrayList<>();
|
|
|
+ List<FsStoreCartQueryGroupStoreVO> list = new ArrayList<>();
|
|
|
+ for (FsStoreConfirmOrderGroupStoreParam cartGroupStoreParam : cartParamList) {
|
|
|
+ FsStoreConfirmOrderParam cartParam = cartGroupStoreParam.getData();
|
|
|
+ //获取地址信息和购物车信息
|
|
|
+ address = userAddressMapper.selectFsUserAddressByDefaultAddress(uid);
|
|
|
+// List<FsStoreCartQueryVO> carts=cartMapper.selectFsStoreCartListByIds(cartParam.getCartIds());
|
|
|
+ //分组展示购物车信息
|
|
|
+ List<FsStoreCartQueryGroupStoreVO> carts = new ArrayList<>();
|
|
|
+ List<FsStoreCartScrmQueryVO> redisCarts = new ArrayList<>();
|
|
|
+ //查询店铺id
|
|
|
+ FsStoreScrm fsStore = fsStoreMapper.selectFsStoreByStoreId(cartGroupStoreParam.getStoreId());
|
|
|
+// List<FsStore> store = cartMapper.selectFsStoreListByIds(cartParam.getCartIds());
|
|
|
+// for (FsStore fsStore : store) {
|
|
|
+ FsStoreCartQueryGroupStoreVO vo = new FsStoreCartQueryGroupStoreVO();
|
|
|
+ ////todo 目前存在商品属性没有storeid的脏数据 暂且会查询为null的
|
|
|
+ List<FsStoreCartScrmQueryVO> cartQueryVo = null;
|
|
|
+ if (fsStore == null) {
|
|
|
+ vo.setStoreName("null");
|
|
|
+ cartQueryVo = cartMapper.selectFsStoreCartMultiStoreListByIds(cartParam.getCartIds(), -1L);
|
|
|
+ vo.setList(cartQueryVo);
|
|
|
+ } else {
|
|
|
+ vo.setStoreName(fsStore.getStoreName());
|
|
|
+ cartQueryVo = cartMapper.selectFsStoreCartMultiStoreListByIds(cartParam.getCartIds(), cartGroupStoreParam.getStoreId());
|
|
|
+
|
|
|
+ }
|
|
|
+ vo.setList(cartQueryVo);
|
|
|
+ carts.add(vo);
|
|
|
+ redisCarts.addAll(cartQueryVo);
|
|
|
+// }
|
|
|
+
|
|
|
+ list.addAll(carts);
|
|
|
+ String uuid = IdUtil.randomUUID();
|
|
|
+ uuids.add(uuid);
|
|
|
+ redisCache.setCacheObject("orderKey:" + uuid, cartParam.getCartIds(), 300, TimeUnit.SECONDS);
|
|
|
+// redisCache.setCacheObject("orderCarts:" + uuid, carts, 300, TimeUnit.SECONDS);
|
|
|
+ redisCache.setCacheObject("orderCarts:" + uuid, redisCarts, 300, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.ok().put("orderKeys", uuids).put("address", address).put("carts", list);
|
|
|
}
|
|
|
|
|
|
- // 抽取的资质检查方法
|
|
|
- private boolean isStoreQualificationExpired(FsStoreScrm store, LocalDate today) {
|
|
|
- return store.getBusinessLicenseExpireEnd().isBefore(today) ||
|
|
|
- store.getDrugLicenseExpiryEnd().isBefore(today) ||
|
|
|
- store.getMedicalDevice2ExpiryEnd().isBefore(today) ||
|
|
|
- store.getMedicalLicenseExpiryEnd().isBefore(today);
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public R createOrderMultiStore(long userId, FsStoreOrderCreateParam param) {
|
|
|
+
|
|
|
+ FsStoreOrderComputedParam computedParam = new FsStoreOrderComputedParam();
|
|
|
+ BeanUtils.copyProperties(param, computedParam);
|
|
|
+ //计算金额
|
|
|
+ FsStoreOrderComputeDTO dto = this.computedOrderMultiStore(userId, computedParam);
|
|
|
+ String cartIds = redisCache.getCacheObject("orderKey:" + param.getOrderKey());
|
|
|
+ Integer totalNum = 0;
|
|
|
+ BigDecimal integral = BigDecimal.ZERO;
|
|
|
+ if (cartIds != null) {
|
|
|
+ //获取购物车列表
|
|
|
+ List<FsStoreCartScrmQueryVO> carts = redisCache.getCacheObject("orderCarts:" + param.getOrderKey());
|
|
|
+ //获取地址
|
|
|
+ FsUserAddressScrm address = userAddressMapper.selectFsUserAddressById(param.getAddressId());
|
|
|
+ //检测库存
|
|
|
+ //如果是美乳产品,发武汉仓库
|
|
|
+ Integer meiruCount = 0;
|
|
|
+
|
|
|
+ for (FsStoreCartScrmQueryVO cart : carts) {
|
|
|
+ cartService.checkProductStocks(cart.getProductId(), cart.getProductAttrValueId(), cart.getCartNum());
|
|
|
+ totalNum += cart.getCartNum();
|
|
|
+ integral = NumberUtil.add(integral,
|
|
|
+ NumberUtil.mul(cart.getCartNum(), cart.getIntegral()));
|
|
|
+ if (cart.getCateId() != null && cart.getCateId().equals(176L)) {
|
|
|
+ meiruCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //生成分布式唯一值
|
|
|
+ String orderSn = IdUtil.getSnowflake(0, 0).nextIdStr();
|
|
|
+ //是否使用积分
|
|
|
+ Boolean isIntegral = false;
|
|
|
+ //组合数据
|
|
|
+ FsStoreOrderScrm storeOrder = new FsStoreOrderScrm();
|
|
|
+ //修改默认仓库
|
|
|
+ if (meiruCount.equals(carts.size())) {
|
|
|
+ storeOrder.setStoreHouseCode("WHSP001");
|
|
|
+ } else {
|
|
|
+ storeOrder.setStoreHouseCode("CQDS001");
|
|
|
+ }
|
|
|
+ storeOrder.setCompanyId(param.getCompanyId());
|
|
|
+ storeOrder.setCompanyUserId(param.getCompanyUserId());
|
|
|
+ if (param.getCompanyUserId() != null) {
|
|
|
+ CompanyUser companyUser = companyUserService.selectCompanyUserById(param.getCompanyUserId());
|
|
|
+ if (companyUser != null) {
|
|
|
+ storeOrder.setDeptId(companyUser.getDeptId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ storeOrder.setUserId(userId);
|
|
|
+ storeOrder.setOrderCode(orderSn);
|
|
|
+ storeOrder.setRealName(address.getRealName());
|
|
|
+ storeOrder.setUserPhone(address.getPhone());
|
|
|
+ storeOrder.setUserAddress(address.getProvince() + " " + address.getCity() +
|
|
|
+ " " + address.getDistrict() + " " + address.getDetail().trim());
|
|
|
+ storeOrder.setCartId(cartIds);
|
|
|
+// storeOrder.setTotalNum(Long.parseLong(String.valueOf(carts.size())));
|
|
|
+ storeOrder.setTotalNum(Long.valueOf(totalNum));
|
|
|
+ storeOrder.setTotalPrice(dto.getTotalPrice());
|
|
|
+ storeOrder.setTotalPostage(dto.getPayPostage());
|
|
|
+
|
|
|
+ if (param.getStoreId() != null) {
|
|
|
+ storeOrder.setStoreId(param.getStoreId());
|
|
|
+ }
|
|
|
+
|
|
|
+ //优惠券处理
|
|
|
+ if (param.getCouponUserId() != null) {
|
|
|
+ FsStoreCouponUserScrm couponUser = couponUserService.selectFsStoreCouponUserById(param.getCouponUserId());
|
|
|
+ if (couponUser != null && couponUser.getStatus() == 0) {
|
|
|
+ storeOrder.setCouponId(couponUser.getId());
|
|
|
+ storeOrder.setCouponPrice(couponUser.getCouponPrice());
|
|
|
+ //更新优惠券状态
|
|
|
+ couponUser.setStatus(1);
|
|
|
+ couponUser.setUseTime(new Date());
|
|
|
+ couponUserService.updateFsStoreCouponUser(couponUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //处理推荐人
|
|
|
+ FsUserScrm user = userService.selectFsUserById(storeOrder.getUserId());
|
|
|
+ if (user.getSpreadUserId() != null && user.getSpreadUserId() > 0) {
|
|
|
+ storeOrder.setTuiUserId(user.getSpreadUserId());
|
|
|
+ } else {
|
|
|
+ if (param.getTuiUserId() != null) {
|
|
|
+ FsUserScrm tuiUser = userService.selectFsUserById(param.getTuiUserId());
|
|
|
+ if (tuiUser != null && tuiUser.getIsPromoter() == 1) {
|
|
|
+ storeOrder.setTuiUserId(param.getTuiUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ storeOrder.setPayPostage(dto.getPayPostage());
|
|
|
+ storeOrder.setDeductionPrice(dto.getDeductionPrice());
|
|
|
+ storeOrder.setPaid(0);
|
|
|
+ storeOrder.setPayType(param.getPayType());
|
|
|
+ if (isIntegral) {
|
|
|
+ storeOrder.setPayIntegral(integral);
|
|
|
+ }
|
|
|
+ storeOrder.setUseIntegral(BigDecimal.valueOf(dto.getUsedIntegral()));
|
|
|
+ storeOrder.setBackIntegral(BigDecimal.ZERO);
|
|
|
+ storeOrder.setGainIntegral(BigDecimal.ZERO);
|
|
|
+ storeOrder.setMark(param.getMark());
|
|
|
+ //todo 获取成本价
|
|
|
+ BigDecimal costPrice = this.getOrderSumPriceMultiStore(carts, "costPrice");
|
|
|
+ storeOrder.setCost(costPrice);
|
|
|
+ storeOrder.setIsChannel(1);
|
|
|
+ storeOrder.setShippingType(1);
|
|
|
+ storeOrder.setCreateTime(new Date());
|
|
|
+ if (param.getPayType().equals("1")) {
|
|
|
+ //全款支付
|
|
|
+ storeOrder.setStatus(0);
|
|
|
+ } else if (param.getPayType().equals("2")) {
|
|
|
+ //货到付款
|
|
|
+ storeOrder.setStatus(1);
|
|
|
+ }
|
|
|
+ //后台制单处理
|
|
|
+ if (param.getPayPrice() != null && param.getPayPrice().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ storeOrder.setPayPrice(param.getPayPrice());
|
|
|
+ } else {
|
|
|
+ storeOrder.setPayPrice(dto.getPayPrice());
|
|
|
+ }
|
|
|
+ storeOrder.setOrderCreateType(param.getOrderCreateType());
|
|
|
+ long prescribe = carts.stream().filter(item -> item.getProductType() != null && item.getProductType() == 2).count();
|
|
|
+ if (prescribe > 0) {
|
|
|
+ storeOrder.setIsPrescribe(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ storeOrder.setCombinationOrderId(param.getCombinationOrderId());
|
|
|
+
|
|
|
+ int flag = fsStoreOrderMapper.insertFsStoreOrder(storeOrder);
|
|
|
+ if (flag == 0) {
|
|
|
+ return R.error("订单创建失败");
|
|
|
+ }
|
|
|
+ //收款单更新
|
|
|
+// if(param.getPaymentId()!=null&¶m.getPaymentId()>0){
|
|
|
+// FsStorePayment payment=new FsStorePayment();
|
|
|
+// payment.setOrderId(storeOrder.getId());
|
|
|
+// payment.setPaymentId(param.getPaymentId());
|
|
|
+// paymentService.updateFsStorePayment(payment);
|
|
|
+// }
|
|
|
+ //使用了积分扣积分
|
|
|
+ if (dto.getUsedIntegral() > 0) {
|
|
|
+ this.decIntegral(userId, dto.getUsedIntegral(), dto.getDeductionPrice().doubleValue(), storeOrder.getId().toString());
|
|
|
+ }
|
|
|
+ //减库存加销量
|
|
|
+ this.deStockIncSales(carts);
|
|
|
+ //保存OrderItem
|
|
|
+ List<FsStoreOrderItemScrm> listOrderItem = new ArrayList<>();
|
|
|
+ //保存购物车商品信息
|
|
|
+ for (FsStoreCartScrmQueryVO vo : carts) {
|
|
|
+ FsStoreCartDTO fsStoreCartDTO = new FsStoreCartDTO();
|
|
|
+ fsStoreCartDTO.setProductId(vo.getProductId());
|
|
|
+ fsStoreCartDTO.setPrice(vo.getPrice());
|
|
|
+ fsStoreCartDTO.setSku(vo.getProductAttrName());
|
|
|
+ fsStoreCartDTO.setProductName(vo.getProductName());
|
|
|
+ fsStoreCartDTO.setNum(vo.getCartNum());
|
|
|
+ fsStoreCartDTO.setErpCode(vo.getErpCode());
|
|
|
+ fsStoreCartDTO.setErpSkuName(vo.getErpSkuName());
|
|
|
+ fsStoreCartDTO.setBarCode(vo.getBarCode());
|
|
|
+ fsStoreCartDTO.setGroupBarCode(vo.getGroupBarCode());
|
|
|
+ fsStoreCartDTO.setBrokerage(vo.getBrokerage());
|
|
|
+ fsStoreCartDTO.setBrokerageTwo(vo.getBrokerageTwo());
|
|
|
+ fsStoreCartDTO.setBrokerageThree(vo.getBrokerageThree());
|
|
|
+ if (StringUtils.isEmpty(vo.getProductAttrImage())) {
|
|
|
+ fsStoreCartDTO.setImage(vo.getProductImage());
|
|
|
+ } else {
|
|
|
+ fsStoreCartDTO.setImage(vo.getProductAttrImage());
|
|
|
+ }
|
|
|
+ FsStoreOrderItemScrm item = new FsStoreOrderItemScrm();
|
|
|
+ item.setOrderId(storeOrder.getId());
|
|
|
+ item.setOrderCode(orderSn);
|
|
|
+ item.setCartId(vo.getId());
|
|
|
+ item.setProductId(vo.getProductId());
|
|
|
+ item.setJsonInfo(JSONUtil.toJsonStr(fsStoreCartDTO));
|
|
|
+ item.setNum(vo.getCartNum());
|
|
|
+ item.setIsAfterSales(0);
|
|
|
+ //处方药
|
|
|
+ if (vo.getProductType().equals(2)) {
|
|
|
+ item.setIsPrescribe(1);
|
|
|
+ }
|
|
|
+ fsStoreOrderItemMapper.insertFsStoreOrderItem(item);
|
|
|
+ listOrderItem.add(item);
|
|
|
+ }
|
|
|
+ if (listOrderItem.size() > 0) {
|
|
|
+ String itemJson = JSONUtil.toJsonStr(listOrderItem);
|
|
|
+ storeOrder.setItemJson(itemJson);
|
|
|
+ fsStoreOrderMapper.updateFsStoreOrder(storeOrder);
|
|
|
+ }
|
|
|
+
|
|
|
+ //购物车状态修改
|
|
|
+ cartMapper.updateIsPay(cartIds);
|
|
|
+
|
|
|
+ //删除缓存
|
|
|
+ redisCache.deleteObject("orderKey:" + param.getOrderKey());
|
|
|
+ redisCache.deleteObject("orderCarts:" + param.getOrderKey());
|
|
|
+
|
|
|
+ //添加记录
|
|
|
+ orderStatusService.create(storeOrder.getId(), OrderLogEnum.CREATE_ORDER.getValue(),
|
|
|
+ OrderLogEnum.CREATE_ORDER.getDesc());
|
|
|
+
|
|
|
+ //加入redis,24小时自动取消
|
|
|
+ String redisKey = String.valueOf(StrUtil.format("{}{}",
|
|
|
+ StoreConstants.REDIS_ORDER_OUTTIME_UNPAY, storeOrder.getId()));
|
|
|
+ String json = configService.selectConfigByKey("store.config");
|
|
|
+ com.fs.store.config.StoreConfig config = JSONUtil.toBean(json, com.fs.store.config.StoreConfig.class);
|
|
|
+ if (config.getUnPayTime() != null && config.getUnPayTime() > 0) {
|
|
|
+ redisCache.setCacheObject(redisKey, storeOrder.getId(), config.getUnPayTime(), TimeUnit.MINUTES);
|
|
|
+ } else {
|
|
|
+ redisCache.setCacheObject(redisKey, storeOrder.getId(), 30, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+ //添加支付到期时间
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(storeOrder.getCreateTime());
|
|
|
+ calendar.add(Calendar.MINUTE, config.getUnPayTime());
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String payLimitTime = format.format(calendar.getTime());
|
|
|
+ //删除推荐订单KEY
|
|
|
+ if (StringUtils.isNotEmpty(param.getCreateOrderKey())) {
|
|
|
+ redisCache.deleteObject("createOrderKey:" + param.getCreateOrderKey());
|
|
|
+ redisCache.deleteObject("orderCarts:" + param.getCreateOrderKey());
|
|
|
+ redisCache.deleteObject("createOrderMoney:" + param.getCreateOrderKey());
|
|
|
+ redisCache.deleteObject("createOrderMoneyDetails:" + param.getCreateOrderKey());
|
|
|
+ }
|
|
|
+ return R.ok().put("order", storeOrder).put("payLimitTime", payLimitTime);
|
|
|
+ } else {
|
|
|
+ return R.error("订单已过期");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public List<FsStoreOrderComputeDTO> computedOrders(long uid, FsStoreOrderComputedGroupStoreParam params) {
|
|
|
+ //返回值
|
|
|
+ List<FsStoreOrderComputeDTO> dtos = new ArrayList<>();
|
|
|
+ List<String> orderKeys = params.getOrderKeys();
|
|
|
+ //总数
|
|
|
+ BigDecimal payPriceTotal = BigDecimal.ZERO;
|
|
|
+ BigDecimal totalPriceTotal = BigDecimal.ZERO;
|
|
|
+ Double usedIntegralTotal = 0.;
|
|
|
+ BigDecimal payIntegralTotal = BigDecimal.ZERO;
|
|
|
+ BigDecimal payPostageTotal = BigDecimal.ZERO;
|
|
|
+ BigDecimal deductionPriceTotal = BigDecimal.ZERO;
|
|
|
+
|
|
|
+
|
|
|
+ List<String> createOrderKeys = null;
|
|
|
+ if (params.getCreateOrderKey() == null) {
|
|
|
+ createOrderKeys = orderKeys;
|
|
|
+ } else {
|
|
|
+ createOrderKeys = Arrays.asList(params.getCreateOrderKey().split(","));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i < orderKeys.size(); i++) {
|
|
|
+// for (String orderKey : orderKeys) {
|
|
|
+ FsStoreOrderComputedParam computedParam = new FsStoreOrderComputedParam();
|
|
|
+ BeanUtils.copyProperties(computedParam, params);
|
|
|
+ computedParam.setOrderKey(orderKeys.get(i));
|
|
|
+ if (!createOrderKeys.isEmpty() && createOrderKeys.get(i) != null) {
|
|
|
+ computedParam.setCreateOrderKey(createOrderKeys.get(i));
|
|
|
+ }
|
|
|
+
|
|
|
+ FsStoreOrderComputeDTO dto = this.computedOrderMultiStore(uid, computedParam);
|
|
|
+ payPriceTotal = NumberUtil.add(payPriceTotal, dto.getPayPrice());
|
|
|
+ totalPriceTotal = NumberUtil.add(totalPriceTotal, dto.getTotalPrice());
|
|
|
+ usedIntegralTotal += dto.getUsedIntegral();
|
|
|
+ payIntegralTotal = NumberUtil.add(payIntegralTotal, dto.getPayIntegral());
|
|
|
+ payPostageTotal = NumberUtil.add(payPostageTotal, dto.getPayPostage());
|
|
|
+ deductionPriceTotal = NumberUtil.add(deductionPriceTotal, dto.getDeductionPrice());
|
|
|
+ dtos.add(dto);
|
|
|
+ }
|
|
|
+//
|
|
|
+ dtos.add(FsStoreOrderComputeDTO.builder()
|
|
|
+ .totalPrice(totalPriceTotal)
|
|
|
+ .payPrice(payPriceTotal)
|
|
|
+ .payPostage(payPostageTotal)
|
|
|
+ .deductionPrice(deductionPriceTotal)
|
|
|
+ .usedIntegral(usedIntegralTotal)
|
|
|
+ .payIntegral(payIntegralTotal)
|
|
|
+ .build());
|
|
|
+ return dtos;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final DateTimeFormatter CST_FORMATTER = DateTimeFormatter
|
|
|
+ .ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US)
|
|
|
+ .withZone(ZoneId.of("Asia/Shanghai"));
|
|
|
+
|
|
|
+ private static final DateTimeFormatter STANDARD_FORMATTER = DateTimeFormatter
|
|
|
+ .ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ public static LocalDateTime parseCstToLocalDateTime(String cstDateStr) {
|
|
|
+ if (cstDateStr == null || cstDateStr.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 解析为带时区的日期时间
|
|
|
+ ZonedDateTime zonedDateTime = ZonedDateTime.parse(cstDateStr, CST_FORMATTER);
|
|
|
+ // 转换为本地日期时间(忽略时区,仅保留年月日时分秒)
|
|
|
+ return zonedDateTime.toLocalDateTime();
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
+ System.err.println("日期解析失败: " + e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String parseCstToDateOnlyString(String cstDateStr) {
|
|
|
+ LocalDateTime dateTime = parseCstToLocalDateTime(cstDateStr);
|
|
|
+ return dateTime != null ? dateTime.format(DateTimeFormatter
|
|
|
+ .ofPattern("yyyy-MM-dd")) : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, String> buildExpressDeliveryMap() {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("SF", "顺丰");
|
|
|
+ map.put("EMS", "邮政");
|
|
|
+ map.put("ZTO", "中通");
|
|
|
+ map.put("JD", "京东");
|
|
|
+ map.put("DBL", "德邦");
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 抽取的资质检查方法
|
|
|
+ private boolean isStoreQualificationExpired(FsStoreScrm store, LocalDate today) {
|
|
|
+ return store.getBusinessLicenseExpireEnd().isBefore(today) ||
|
|
|
+ store.getDrugLicenseExpiryEnd().isBefore(today) ||
|
|
|
+ store.getMedicalDevice2ExpiryEnd().isBefore(today) ||
|
|
|
+ store.getMedicalLicenseExpiryEnd().isBefore(today);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public FsStoreOrderComputeDTO computedOrderMultiStore(long uid, FsStoreOrderComputedParam param) {
|
|
|
+ String cartIds = redisCache.getCacheObject("orderKey:" + param.getOrderKey());
|
|
|
+ if (ObjectUtil.isNull(cartIds)) {
|
|
|
+ throw new CustomException("订单已过期", 501);
|
|
|
+ }
|
|
|
+ List<FsStoreCartScrmQueryVO> carts = redisCache.getCacheObject("orderCarts:" + param.getOrderKey());
|
|
|
+ BigDecimal payPrice = getOrderSumPriceMultiStore(carts, "truePrice");
|
|
|
+ if (StringUtils.isNotEmpty(param.getCreateOrderKey())) {
|
|
|
+// BigDecimal money = redisCache.getCacheObject("createOrderMoney:" + param.getCreateOrderKey());
|
|
|
+// if (money != null) {
|
|
|
+// payPrice = money;
|
|
|
+// }
|
|
|
+
|
|
|
+ FsOrderMoneyDetailsVo detailsVo = redisCache.getCacheObject("createOrderMoneyDetails:" + param.getCreateOrderKey());
|
|
|
+ if (detailsVo != null) {
|
|
|
+ BigDecimal money = detailsVo.getTotalPrice();
|
|
|
+ payPrice = detailsVo.getPayPrice();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ FsUserAddressScrm userAddress = null;
|
|
|
+ if (param.getAddressId() != null) {
|
|
|
+ userAddress = userAddressMapper.selectFsUserAddressById(param.getAddressId());
|
|
|
+ }
|
|
|
+ FsStoreOrderPriceDTO priceGroup = this.getOrderPriceGroupMultiStore(carts, userAddress);
|
|
|
+ BigDecimal payPostage = priceGroup.getStorePostage();
|
|
|
+ payPrice = NumberUtil.add(payPrice, payPostage);
|
|
|
+ FsUserScrm user = userService.selectFsUserById(uid);
|
|
|
+ // 积分抵扣
|
|
|
+ BigDecimal deductionPrice = BigDecimal.ZERO; //积分抵扣金额
|
|
|
+ double usedIntegral = 0; //使用了多少积分
|
|
|
+ if (param.getUseIntegral() != null && param.getUseIntegral() == 1 && user.getIntegral().intValue() > 0) {
|
|
|
+ //如果积分总和小于用户积分 抵扣比例 计算抵扣价格
|
|
|
+ //默认配置
|
|
|
+ Double integralMax = Double.valueOf(100);
|
|
|
+ BigDecimal integralFull = new BigDecimal(100);
|
|
|
+ Double integralRatio = Double.valueOf(1);
|
|
|
+ String json = configService.selectConfigByKey("store.integral");
|
|
|
+ if (StringUtils.isEmpty(json)) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ StoreIntegralConfig integralConfig = JSONUtil.toBean(json, StoreIntegralConfig.class);
|
|
|
+ integralMax = integralConfig.getIntegralMax();
|
|
|
+ integralFull = integralConfig.getIntegralFull();
|
|
|
+ integralRatio = integralConfig.getIntegralRatio();
|
|
|
+ }
|
|
|
+ if (priceGroup.getTotalPrice().compareTo(integralFull) >= 0) {
|
|
|
+ Double userIntegral = user.getIntegral().doubleValue();
|
|
|
+ if (integralMax.intValue() > 0 && Double.compare(userIntegral, integralMax) >= 0) {
|
|
|
+ userIntegral = integralMax;
|
|
|
+ }
|
|
|
+ deductionPrice = BigDecimal.valueOf(NumberUtil.mul(userIntegral, integralRatio));
|
|
|
+ if (deductionPrice.compareTo(payPrice) < 0) {
|
|
|
+ payPrice = NumberUtil.sub(payPrice, deductionPrice);
|
|
|
+ usedIntegral = userIntegral;
|
|
|
+ } else {
|
|
|
+ deductionPrice = payPrice;
|
|
|
+ payPrice = BigDecimal.ZERO;
|
|
|
+ usedIntegral = NumberUtil.round(NumberUtil.div(deductionPrice,
|
|
|
+ BigDecimal.valueOf(integralRatio)), 2).doubleValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (payPrice.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ payPrice = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ //优惠券
|
|
|
+ if (param.getCouponUserId() != null) {
|
|
|
+ FsStoreCouponUserScrm couponUser = couponUserService.selectFsStoreCouponUserById(param.getCouponUserId());
|
|
|
+ if (couponUser != null && couponUser.getStatus() == 0) {
|
|
|
+ if (couponUser.getUseMinPrice().compareTo(priceGroup.getTotalPrice()) == -1) {
|
|
|
+ payPrice = payPrice.subtract(couponUser.getCouponPrice());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return FsStoreOrderComputeDTO.builder()
|
|
|
+ .totalPrice(priceGroup.getTotalPrice())
|
|
|
+ .payPrice(payPrice)
|
|
|
+ .payPostage(payPostage)
|
|
|
+ .deductionPrice(deductionPrice)
|
|
|
+ .usedIntegral(usedIntegral)
|
|
|
+ .payIntegral(priceGroup.getPayIntegral())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某字段价格
|
|
|
+ *
|
|
|
+ * @param cartInfo 购物车
|
|
|
+ * @param key key值
|
|
|
+ * @return Double
|
|
|
+ */
|
|
|
+ private BigDecimal getOrderSumPriceMultiStore(List<FsStoreCartScrmQueryVO> cartInfo, String key) {
|
|
|
+ BigDecimal sumPrice = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ if ("truePrice".equals(key)) {
|
|
|
+ for (FsStoreCartScrmQueryVO storeCart : cartInfo) {
|
|
|
+ sumPrice = NumberUtil.add(sumPrice, NumberUtil.mul(storeCart.getCartNum(), storeCart.getPrice()));
|
|
|
+ }
|
|
|
+ } else if ("costPrice".equals(key)) {
|
|
|
+ for (FsStoreCartScrmQueryVO storeCart : cartInfo) {
|
|
|
+ sumPrice = NumberUtil.add(sumPrice,
|
|
|
+ NumberUtil.mul(storeCart.getCartNum(), storeCart.getCost()));
|
|
|
+ }
|
|
|
+ } else if ("payIntegral".equals(key)) {
|
|
|
+ for (FsStoreCartScrmQueryVO storeCart : cartInfo) {
|
|
|
+ if (storeCart.getIntegral() != null) {
|
|
|
+ sumPrice = NumberUtil.add(sumPrice,
|
|
|
+ NumberUtil.mul(storeCart.getCartNum(), storeCart.getIntegral()));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sumPrice;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取订单价格
|
|
|
+ */
|
|
|
+ private FsStoreOrderPriceDTO getOrderPriceGroupMultiStore(List<FsStoreCartScrmQueryVO> cartInfo, FsUserAddressScrm userAddress) {
|
|
|
+ BigDecimal storePostage = BigDecimal.ZERO;
|
|
|
+ //免运费设置
|
|
|
+ BigDecimal storeFreePostage = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ BigDecimal totalPrice = this.getOrderSumPriceMultiStore(cartInfo, "truePrice");//获取订单总金额
|
|
|
+ BigDecimal costPrice = this.getOrderSumPriceMultiStore(cartInfo, "costPrice");//获取订单成本价
|
|
|
+ BigDecimal payIntegral = this.getOrderSumPriceMultiStore(cartInfo, "payIntegral");//获取订单需要的积分
|
|
|
+
|
|
|
+ //如果设置满包邮0 表示全局包邮,如果设置大于0表示满这价格包邮,否则走运费模板算法
|
|
|
+ if (storeFreePostage.compareTo(BigDecimal.ZERO) != 0 && totalPrice.compareTo(storeFreePostage) <= 0) {
|
|
|
+ storePostage = this.handlePostageMultiStore(cartInfo, userAddress);
|
|
|
+ }
|
|
|
+ FsStoreOrderPriceDTO priceGroupDTO = new FsStoreOrderPriceDTO();
|
|
|
+ priceGroupDTO.setStorePostage(storePostage);
|
|
|
+ priceGroupDTO.setStoreFreePostage(storeFreePostage);
|
|
|
+ priceGroupDTO.setTotalPrice(totalPrice);
|
|
|
+ priceGroupDTO.setCostPrice(costPrice);
|
|
|
+ priceGroupDTO.setPayIntegral(payIntegral);
|
|
|
+ return priceGroupDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据运费模板算法返回邮费
|
|
|
+ *
|
|
|
+ * @param cartInfo 购物车
|
|
|
+ * @param userAddress 地址
|
|
|
+ * @return double
|
|
|
+ */
|
|
|
+ private BigDecimal handlePostageMultiStore(List<FsStoreCartScrmQueryVO> cartInfo, FsUserAddressScrm userAddress) {
|
|
|
+// private BigDecimal handlePostage(List<FsStoreCartQueryGroupStoreVO> cartInfo, FsUserAddress userAddress) {
|
|
|
+ BigDecimal storePostage = BigDecimal.ZERO;
|
|
|
+ if (userAddress != null) {
|
|
|
+ if (userAddress.getCityId() == null) {
|
|
|
+ return storePostage;
|
|
|
+ }
|
|
|
+ String cityId = userAddress.getCityId();
|
|
|
+ List<Long> citys = new ArrayList<>();
|
|
|
+ citys.add(Long.valueOf(cityId));
|
|
|
+ citys.add(0l);//城市包括默认
|
|
|
+ //获取商品所有的TEMPID
|
|
|
+ List<Long> tempIds = cartInfo
|
|
|
+ .stream()
|
|
|
+ .map(FsStoreCartScrmQueryVO::getTempId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ //获取商品用到的运费模板
|
|
|
+ List<FsShippingTemplatesScrm> shippingTemplatesList = shippingTemplatesService.selectFsShippingTemplatesByIds(StringUtils.join(tempIds, ","));
|
|
|
+
|
|
|
+ //获取运费模板区域列表按照城市排序
|
|
|
+ List<FsShippingTemplatesRegionScrm> shippingTemplatesRegionList = shippingTemplatesRegionService.selectFsShippingTemplatesRegionListByTempIdsAndCityIds(StringUtils.join(tempIds, ","), StringUtils.join(citys, ","));
|
|
|
+
|
|
|
+ //提取运费模板类型
|
|
|
+ Map<Long, Integer> shippingTemplatesMap = shippingTemplatesList
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.toMap(FsShippingTemplatesScrm::getId,
|
|
|
+ FsShippingTemplatesScrm::getType));
|
|
|
+ //提取运费模板有相同值覆盖
|
|
|
+ Map<Long, FsShippingTemplatesRegionScrm> shippingTemplatesRegionMap =
|
|
|
+ shippingTemplatesRegionList.stream()
|
|
|
+ .collect(Collectors.toMap(FsShippingTemplatesRegionScrm::getTempId,
|
|
|
+ YxShippingTemplatesRegion -> YxShippingTemplatesRegion,
|
|
|
+ (key1, key2) -> key2));
|
|
|
+
|
|
|
+
|
|
|
+ Map<Long, TemplateDTO> templateDTOMap = new HashMap<>();
|
|
|
+ for (FsStoreCartScrmQueryVO storeCartVO : cartInfo) {
|
|
|
+ Long tempId = storeCartVO.getTempId();
|
|
|
+ //根据模板类型获取相应的数量
|
|
|
+ double num = 0d;
|
|
|
+ if (ShippingTempEnum.TYPE_1.getValue().equals(shippingTemplatesMap.get(tempId))) {
|
|
|
+ num = storeCartVO.getCartNum().doubleValue();
|
|
|
+ } else if (ShippingTempEnum.TYPE_2.getValue().equals(shippingTemplatesMap.get(tempId))) {
|
|
|
+ num = NumberUtil.mul(storeCartVO.getCartNum(),
|
|
|
+ storeCartVO.getWeight()).doubleValue();
|
|
|
+ } else if (ShippingTempEnum.TYPE_3.getValue().equals(shippingTemplatesMap.get(tempId))) {
|
|
|
+ num = NumberUtil.mul(storeCartVO.getCartNum(),
|
|
|
+ storeCartVO.getVolume()).doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ FsShippingTemplatesRegionScrm shippingTemplatesRegion = shippingTemplatesRegionMap.get(tempId);
|
|
|
+ BigDecimal price = NumberUtil.round(NumberUtil.mul(storeCartVO.getCartNum(),
|
|
|
+ storeCartVO.getPrice()), 2);
|
|
|
+ if (!templateDTOMap.containsKey(tempId)) {
|
|
|
+ TemplateDTO templateDTO = TemplateDTO.builder()
|
|
|
+ .number(num)
|
|
|
+ .price(price)
|
|
|
+ .first(shippingTemplatesRegion.getFirst().doubleValue())
|
|
|
+ .firstPrice(shippingTemplatesRegion.getFirstPrice())
|
|
|
+ .continues(shippingTemplatesRegion.getContinues().doubleValue())
|
|
|
+ .continuePrice(shippingTemplatesRegion.getContinuePrice())
|
|
|
+ .tempId(tempId)
|
|
|
+ .cityId(cityId)
|
|
|
+ .build();
|
|
|
+ templateDTOMap.put(tempId, templateDTO);
|
|
|
+ } else {
|
|
|
+ TemplateDTO templateDTO = templateDTOMap.get(tempId);
|
|
|
+ templateDTO.setNumber(templateDTO.getNumber() + num);
|
|
|
+ templateDTO.setPrice(NumberUtil.add(templateDTO.getPrice().doubleValue(), price));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //处理包邮情况
|
|
|
+ free:
|
|
|
+ for (Map.Entry<Long, TemplateDTO> entry : templateDTOMap.entrySet()) {
|
|
|
+ Long mapKey = entry.getKey();
|
|
|
+ TemplateDTO mapValue = entry.getValue();
|
|
|
+ int count = shippingTemplatesFreeService.selectCountByTemplateDTO(mapValue);
|
|
|
+ //满足包邮条件剔除
|
|
|
+ if (count > 0) {
|
|
|
+ templateDTOMap.remove(mapKey);
|
|
|
+ break free;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //处理区域邮费
|
|
|
+ boolean isFirst = true; //用来是否多个产品的标识 false表示数量大于1
|
|
|
+ for (TemplateDTO templateDTO : templateDTOMap.values()) {
|
|
|
+ if (isFirst) {//首件
|
|
|
+ //只满足首件
|
|
|
+ if (Double.compare(templateDTO.getNumber(), templateDTO.getFirst()) <= 0) {
|
|
|
+ storePostage = NumberUtil.round(NumberUtil.add(storePostage,
|
|
|
+ templateDTO.getFirstPrice()), 2);
|
|
|
+ } else {
|
|
|
+ BigDecimal firstPrice = NumberUtil.add(storePostage, templateDTO.getFirstPrice());
|
|
|
+
|
|
|
+ if (templateDTO.getContinues() <= 0) {
|
|
|
+ storePostage = firstPrice;
|
|
|
+ } else {
|
|
|
+ //续件平均值且向上取整数
|
|
|
+ double average = Math.ceil(NumberUtil.div(NumberUtil.sub(templateDTO.getNumber(),
|
|
|
+ templateDTO.getFirst()),
|
|
|
+ templateDTO.getContinues().doubleValue()));
|
|
|
+ //最终邮费
|
|
|
+ storePostage = NumberUtil.add(firstPrice, NumberUtil.mul(average,
|
|
|
+ templateDTO.getContinuePrice()));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ isFirst = false;
|
|
|
+ } else {
|
|
|
+ //多件直接在以前的基数继续续建
|
|
|
+ if (templateDTO.getContinues() > 0) {
|
|
|
+ //续件平均值且向上取整数
|
|
|
+ double average = Math.ceil(
|
|
|
+ NumberUtil.div(
|
|
|
+ templateDTO.getNumber(),
|
|
|
+ templateDTO.getContinues()
|
|
|
+ )
|
|
|
+ );
|
|
|
+ //最终邮费
|
|
|
+ storePostage = NumberUtil.add(storePostage.doubleValue(), NumberUtil.mul(average,
|
|
|
+ templateDTO.getContinuePrice()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return storePostage;
|
|
|
}
|
|
|
}
|