123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package com.caimei365.order.components;
- import com.caimei365.order.mapper.OrderCommonMapper;
- import com.caimei365.order.model.enums.OrderStatus;
- import com.caimei365.order.model.enums.ReceivablesType;
- import com.caimei365.order.model.vo.*;
- import com.caimei365.order.utils.ImageUtil;
- import com.caimei365.order.utils.MathUtil;
- import com.google.common.util.concurrent.AtomicDouble;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- import java.util.Objects;
- /**
- * Description
- *
- * @author : Charles
- * @date : 2021/7/26
- */
- @Slf4j
- @Service
- public class OrderCommonService {
- @Value("${caimei.wwwDomain}")
- private String domain;
- @Resource
- private OrderCommonMapper orderCommonMapper;
- /**
- * 设置订单状态
- */
- public void setOrderStatus(OrderVo order) {
- // 111, 待付待收待发
- if (OrderStatus.UNRECEIVED_AND_UNSHIPPED.getCode() == order.getStatus() && 1 == order.getPayStatus()) {
- order.setStatus(111);
- }
- // 判断交易全退情况下,是否发过货,77,交易全退可以查看物流
- int logisticsCount = orderCommonMapper.countLogisticsBatch(order.getOrderId());
- if (OrderStatus.FULL_RETURNED.getCode() == order.getStatus() && logisticsCount > 0) {
- order.setStatus(77);
- }
- // 判断二手订单情况下,若部分付款和已付款,排除退货/款的情况,且未确认打款供应商,10,添加确认打款供应商按钮
- if (1 == order.getSecondHandOrderFlag() && 0 == order.getRefundType() && 0 == order.getAffirmPaymentFlag()) {
- if (order.getStatus().toString().startsWith("2") || order.getStatus().toString().startsWith("3")) {
- // 之前是 “00”, 现在int 10, 前端显示按钮用
- order.setAffirmPaymentFlag(10);
- }
- }
- }
- /**
- * 设置子订单数据
- */
- public void getShopOrderData(OrderVo order) {
- // 子订单
- if (StringUtils.isEmpty(order.getShopOrderIds())){
- return;
- }
- String[] shopOrderIdArr = order.getShopOrderIds().split(",");
- List<String> shopOrderIds = Arrays.asList(shopOrderIdArr);
- List<ShopOrderVo> shopOrderList = orderCommonMapper.getShopOrderList(shopOrderIds);
- shopOrderList.forEach(shopOrder -> {
- // 店铺促销活动
- PromotionsVo shopPromotion = null;
- if (null != shopOrder.getOrderPromotionsId() && shopOrder.getOrderPromotionsId() > 0) {
- shopPromotion = orderCommonMapper.getOrderPromotionsById(shopOrder.getOrderPromotionsId());
- shopOrder.setShopPromotion(shopPromotion);
- }
- List<OrderProductVo> orderProductList = orderCommonMapper.getShopOrderProduct(shopOrder.getShopOrderId());
- orderProductList.removeIf(Objects::isNull);
- orderProductList.forEach(orderProduct -> {
- // 不含税可开票商品,单价在原基础上加上税费(折后单价discountPrice下单时已加上税费)
- boolean taxFlag = (Integer.valueOf(0).equals(orderProduct.getIncludedTax()) && (Integer.valueOf(1).equals(orderProduct.getInvoiceType()) || Integer.valueOf(2).equals(orderProduct.getInvoiceType())));
- if (taxFlag) {
- Double valueTax = MathUtil.div(MathUtil.mul(orderProduct.getPrice(), orderProduct.getTaxRate()), 100).doubleValue();
- orderProduct.setPrice(MathUtil.add(orderProduct.getPrice(), valueTax).doubleValue());
- }
- orderProduct.setImage(ImageUtil.getImageUrl("product", orderProduct.getImage(), domain));
- // 查询订单下商品的促销活动
- if (null != orderProduct.getOrderPromotionsId() && orderProduct.getOrderPromotionsId() > 0) {
- PromotionsVo promotions = orderCommonMapper.getOrderPromotionsById(orderProduct.getOrderPromotionsId());
- if (null != promotions) {
- if (Integer.valueOf(1).equals(promotions.getType()) && Integer.valueOf(1).equals(promotions.getMode())) {
- promotions.setTouchPrice(MathUtil.add(promotions.getTouchPrice(), MathUtil.div(MathUtil.mul(promotions.getTouchPrice(), orderProduct.getTaxRate()), 100)).doubleValue());
- }
- orderProduct.setProductPromotion(promotions);
- }
- }
- });
- shopOrder.setOrderProductList(orderProductList);
- shopOrder.setShopLogo(ImageUtil.getImageUrl("shopLogo", shopOrder.getShopLogo(), domain));
- });
- // 过滤运费商品
- shopOrderList.removeIf(shopOrder -> shopOrder.getShopId() == 998);
- order.setShopOrderList(shopOrderList);
- }
- /**
- * 设置付款金额
- * @param order OrderVo
- */
- public List<DiscernReceiptVo> getDiscernReceiptAndSetOrder(OrderVo order) {
- // 支付记录
- List<DiscernReceiptVo> discernReceiptList = orderCommonMapper.getDiscernReceipt(order.getOrderId(), order.getShopOrderIds());
- Double receiptAmount = 0d;
- // 订单款
- if (!discernReceiptList.isEmpty()) {
- AtomicDouble finalReceiptAmount = new AtomicDouble(0d);
- discernReceiptList.forEach(discernReceipt -> {
- finalReceiptAmount.set(MathUtil.add(finalReceiptAmount.get(), discernReceipt.getAssociateAmount()).doubleValue());
- if (null != discernReceipt.getPayType()) {
- discernReceipt.setPayTypeStr(ReceivablesType.getReceivablesType(discernReceipt.getPayType()));
- }
- });
- receiptAmount = finalReceiptAmount.get();
- } else {
- //返佣款
- if (!order.getShopOrderIds().contains(",")) {
- Double tempAmount = orderCommonMapper.getRebateAmountByShopOrder(Integer.parseInt(order.getShopOrderIds()));
- if (null == tempAmount) {
- order.setOnlinePayFlag(0);
- } else {
- order.setOnlinePayFlag(1);
- }
- }
- }
- // 判断是否可以走线上支付
- int offlineCount = orderCommonMapper.countOfflinePayment(order.getOrderId());
- if (offlineCount > 0) {
- order.setOnlinePayFlag(1);
- } else {
- order.setOnlinePayFlag(0);
- }
- //付供应商总金额 + 默认手续费 > 订单总金额
- Double payTotalFee = order.getPayTotalFee();
- Double handlingFee = MathUtil.mul(payTotalFee, 0.0038, 2).doubleValue();
- if (MathUtil.compare(handlingFee, 8) < 0) {
- handlingFee = 8d;
- }
- List<Double> shouldPayShopAmount = orderCommonMapper.getShouldPayShopAmountList(order.getOrderId());
- if (!shouldPayShopAmount.isEmpty()) {
- AtomicDouble finalHandlingFee = new AtomicDouble(handlingFee);
- shouldPayShopAmount.forEach(amount -> {
- finalHandlingFee.set(MathUtil.add(finalHandlingFee.get(), amount).doubleValue());
- });
- handlingFee = finalHandlingFee.get();
- }
- if (MathUtil.compare(payTotalFee, handlingFee) < 0) {
- order.setPayButton(true);
- } else {
- order.setPayButton(false);
- }
- //待付总金额
- order.setPendingPayments(MathUtil.sub(order.getPayableAmount(), receiptAmount).doubleValue());
- //支付总金额
- order.setReceiptAmount(receiptAmount);
- return discernReceiptList;
- }
- /**
- * 设置搜索关键词历史记录
- * @param userId 用户Id
- * @param searchWord 搜索关键词
- */
- public void setHistoryRecord(Integer userId, String searchWord) {
- // 初始化一个搜索关键词历史记录
- SearchHistoryVo historyRecord = new SearchHistoryVo();
- historyRecord.setUserId(userId);
- historyRecord.setSearchDate(new Date());
- historyRecord.setSearchWord(searchWord);
- historyRecord.setDelFlag(0);
- // 查询搜索关键词历史记录是否存在
- Integer recordId = orderCommonMapper.getSearchHistoryIdByWord(searchWord);
- // 保存搜索关键词历史记录
- if (null != recordId && recordId >0) {
- historyRecord.setId(recordId);
- orderCommonMapper.updateSearchHistory(historyRecord);
- } else {
- orderCommonMapper.insertSearchHistory(historyRecord);
- }
- // 查询关键字历史记录条数
- int count = orderCommonMapper.countSearchHistory(userId);
- // 只保留10条的搜索关键词历史记录
- if (count > 10) {
- orderCommonMapper.deleteSearchHistoryLimit(userId);
- }
- }
- }
|