OrderCommonService.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.caimei365.order.components;
  2. import com.caimei365.order.mapper.OrderCommonMapper;
  3. import com.caimei365.order.model.enums.OrderStatus;
  4. import com.caimei365.order.model.enums.ReceivablesType;
  5. import com.caimei365.order.model.vo.*;
  6. import com.caimei365.order.utils.ImageUtil;
  7. import com.caimei365.order.utils.MathUtil;
  8. import com.google.common.util.concurrent.AtomicDouble;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. import java.util.Arrays;
  15. import java.util.Date;
  16. import java.util.List;
  17. import java.util.Objects;
  18. /**
  19. * Description
  20. *
  21. * @author : Charles
  22. * @date : 2021/7/26
  23. */
  24. @Slf4j
  25. @Service
  26. public class OrderCommonService {
  27. @Value("${caimei.wwwDomain}")
  28. private String domain;
  29. @Resource
  30. private OrderCommonMapper orderCommonMapper;
  31. /**
  32. * 设置订单状态
  33. */
  34. public void setOrderStatus(OrderVo order) {
  35. // 111, 待付待收待发
  36. if (OrderStatus.UNRECEIVED_AND_UNSHIPPED.getCode() == order.getStatus() && 1 == order.getPayStatus()) {
  37. order.setStatus(111);
  38. }
  39. // 判断交易全退情况下,是否发过货,77,交易全退可以查看物流
  40. int logisticsCount = orderCommonMapper.countLogisticsBatch(order.getOrderId());
  41. if (OrderStatus.FULL_RETURNED.getCode() == order.getStatus() && logisticsCount > 0) {
  42. order.setStatus(77);
  43. }
  44. // 判断二手订单情况下,若部分付款和已付款,排除退货/款的情况,且未确认打款供应商,10,添加确认打款供应商按钮
  45. if (1 == order.getSecondHandOrderFlag() && 0 == order.getRefundType() && 0 == order.getAffirmPaymentFlag()) {
  46. if (order.getStatus().toString().startsWith("2") || order.getStatus().toString().startsWith("3")) {
  47. // 之前是 “00”, 现在int 10, 前端显示按钮用
  48. order.setAffirmPaymentFlag(10);
  49. }
  50. }
  51. }
  52. /**
  53. * 设置子订单数据
  54. */
  55. public void getShopOrderData(OrderVo order) {
  56. // 子订单
  57. if (StringUtils.isEmpty(order.getShopOrderIds())){
  58. return;
  59. }
  60. String[] shopOrderIdArr = order.getShopOrderIds().split(",");
  61. List<String> shopOrderIds = Arrays.asList(shopOrderIdArr);
  62. List<ShopOrderVo> shopOrderList = orderCommonMapper.getShopOrderList(shopOrderIds);
  63. shopOrderList.forEach(shopOrder -> {
  64. // 店铺促销活动
  65. PromotionsVo shopPromotion = null;
  66. if (null != shopOrder.getOrderPromotionsId() && shopOrder.getOrderPromotionsId() > 0) {
  67. shopPromotion = orderCommonMapper.getOrderPromotionsById(shopOrder.getOrderPromotionsId());
  68. shopOrder.setShopPromotion(shopPromotion);
  69. }
  70. List<OrderProductVo> orderProductList = orderCommonMapper.getShopOrderProduct(shopOrder.getShopOrderId());
  71. orderProductList.removeIf(Objects::isNull);
  72. orderProductList.forEach(orderProduct -> {
  73. // 不含税可开票商品,单价在原基础上加上税费(折后单价discountPrice下单时已加上税费)
  74. boolean taxFlag = (Integer.valueOf(0).equals(orderProduct.getIncludedTax()) && (Integer.valueOf(1).equals(orderProduct.getInvoiceType()) || Integer.valueOf(2).equals(orderProduct.getInvoiceType())));
  75. if (taxFlag) {
  76. Double valueTax = MathUtil.div(MathUtil.mul(orderProduct.getPrice(), orderProduct.getTaxRate()), 100).doubleValue();
  77. orderProduct.setPrice(MathUtil.add(orderProduct.getPrice(), valueTax).doubleValue());
  78. }
  79. orderProduct.setImage(ImageUtil.getImageUrl("product", orderProduct.getImage(), domain));
  80. // 查询订单下商品的促销活动
  81. if (null != orderProduct.getOrderPromotionsId() && orderProduct.getOrderPromotionsId() > 0) {
  82. PromotionsVo promotions = orderCommonMapper.getOrderPromotionsById(orderProduct.getOrderPromotionsId());
  83. if (null != promotions) {
  84. if (Integer.valueOf(1).equals(promotions.getType()) && Integer.valueOf(1).equals(promotions.getMode())) {
  85. promotions.setTouchPrice(MathUtil.add(promotions.getTouchPrice(), MathUtil.div(MathUtil.mul(promotions.getTouchPrice(), orderProduct.getTaxRate()), 100)).doubleValue());
  86. }
  87. orderProduct.setProductPromotion(promotions);
  88. }
  89. }
  90. });
  91. shopOrder.setOrderProductList(orderProductList);
  92. shopOrder.setShopLogo(ImageUtil.getImageUrl("shopLogo", shopOrder.getShopLogo(), domain));
  93. });
  94. // 过滤运费商品
  95. shopOrderList.removeIf(shopOrder -> shopOrder.getShopId() == 998);
  96. order.setShopOrderList(shopOrderList);
  97. }
  98. /**
  99. * 设置付款金额
  100. * @param order OrderVo
  101. */
  102. public List<DiscernReceiptVo> getDiscernReceiptAndSetOrder(OrderVo order) {
  103. // 支付记录
  104. List<DiscernReceiptVo> discernReceiptList = orderCommonMapper.getDiscernReceipt(order.getOrderId(), order.getShopOrderIds());
  105. Double receiptAmount = 0d;
  106. // 订单款
  107. if (!discernReceiptList.isEmpty()) {
  108. AtomicDouble finalReceiptAmount = new AtomicDouble(0d);
  109. discernReceiptList.forEach(discernReceipt -> {
  110. finalReceiptAmount.set(MathUtil.add(finalReceiptAmount.get(), discernReceipt.getAssociateAmount()).doubleValue());
  111. if (null != discernReceipt.getPayType()) {
  112. discernReceipt.setPayTypeStr(ReceivablesType.getReceivablesType(discernReceipt.getPayType()));
  113. }
  114. });
  115. receiptAmount = finalReceiptAmount.get();
  116. } else {
  117. //返佣款
  118. if (!order.getShopOrderIds().contains(",")) {
  119. Double tempAmount = orderCommonMapper.getRebateAmountByShopOrder(Integer.parseInt(order.getShopOrderIds()));
  120. if (null == tempAmount) {
  121. order.setOnlinePayFlag(0);
  122. } else {
  123. order.setOnlinePayFlag(1);
  124. }
  125. }
  126. }
  127. // 判断是否可以走线上支付
  128. int offlineCount = orderCommonMapper.countOfflinePayment(order.getOrderId());
  129. if (offlineCount > 0) {
  130. order.setOnlinePayFlag(1);
  131. } else {
  132. order.setOnlinePayFlag(0);
  133. }
  134. //付供应商总金额 + 默认手续费 > 订单总金额
  135. Double payTotalFee = order.getPayTotalFee();
  136. Double handlingFee = MathUtil.mul(payTotalFee, 0.0038, 2).doubleValue();
  137. if (MathUtil.compare(handlingFee, 8) < 0) {
  138. handlingFee = 8d;
  139. }
  140. List<Double> shouldPayShopAmount = orderCommonMapper.getShouldPayShopAmountList(order.getOrderId());
  141. if (!shouldPayShopAmount.isEmpty()) {
  142. AtomicDouble finalHandlingFee = new AtomicDouble(handlingFee);
  143. shouldPayShopAmount.forEach(amount -> {
  144. finalHandlingFee.set(MathUtil.add(finalHandlingFee.get(), amount).doubleValue());
  145. });
  146. handlingFee = finalHandlingFee.get();
  147. }
  148. if (MathUtil.compare(payTotalFee, handlingFee) < 0) {
  149. order.setPayButton(true);
  150. } else {
  151. order.setPayButton(false);
  152. }
  153. //待付总金额
  154. order.setPendingPayments(MathUtil.sub(order.getPayableAmount(), receiptAmount).doubleValue());
  155. //支付总金额
  156. order.setReceiptAmount(receiptAmount);
  157. return discernReceiptList;
  158. }
  159. /**
  160. * 设置搜索关键词历史记录
  161. * @param userId 用户Id
  162. * @param searchWord 搜索关键词
  163. */
  164. public void setHistoryRecord(Integer userId, String searchWord) {
  165. // 初始化一个搜索关键词历史记录
  166. SearchHistoryVo historyRecord = new SearchHistoryVo();
  167. historyRecord.setUserId(userId);
  168. historyRecord.setSearchDate(new Date());
  169. historyRecord.setSearchWord(searchWord);
  170. historyRecord.setDelFlag(0);
  171. // 查询搜索关键词历史记录是否存在
  172. Integer recordId = orderCommonMapper.getSearchHistoryIdByWord(searchWord);
  173. // 保存搜索关键词历史记录
  174. if (null != recordId && recordId >0) {
  175. historyRecord.setId(recordId);
  176. orderCommonMapper.updateSearchHistory(historyRecord);
  177. } else {
  178. orderCommonMapper.insertSearchHistory(historyRecord);
  179. }
  180. // 查询关键字历史记录条数
  181. int count = orderCommonMapper.countSearchHistory(userId);
  182. // 只保留10条的搜索关键词历史记录
  183. if (count > 10) {
  184. orderCommonMapper.deleteSearchHistoryLimit(userId);
  185. }
  186. }
  187. }