123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- package com.caimei365.order.components;
- import com.caimei365.order.mapper.BaseMapper;
- 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.*;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- /**
- * Description
- *
- * @author : Charles
- * @date : 2021/7/26
- */
- @Slf4j
- @Service
- public class OrderCommonService {
- @Value("${caimei.wwwDomain}")
- private String domain;
- @Resource
- private OrderCommonMapper orderCommonMapper;
- @Resource
- private BaseMapper baseMapper;
- /**
- * 设置订单状态
- */
- 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;
- }
- Integer organizeId = orderCommonMapper.getOrganizeId(order.getOrderId());
- String[] shopOrderIdArr = order.getShopOrderIds().split(",");
- List<String> shopOrderIds = Arrays.asList(shopOrderIdArr);
- List<ShopOrderVo> shopOrderList = orderCommonMapper.getShopOrderList(shopOrderIds);
- shopOrderList.forEach(shopOrder -> {
- // 冷链商品类型
- List<Integer> arrList = new ArrayList<>();
- // 店铺促销活动
- 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);
- // 冷链商品判定
- List<Integer> collect = orderProductList.stream().map(OrderProductVo::getProductId).collect(Collectors.toList());
- collect.removeIf(c -> c == 999);
- if (collect.stream().allMatch(c -> c == 7536)) {
- arrList.add(1);
- } else if (!collect.contains(7536)) {
- arrList.add(2);
- } else {
- arrList.add(3);
- }
- orderProductList.forEach(orderProduct -> {
- // 不含税可开票商品,单价/折后单价在原基础上加上税费
- boolean taxFlag = false;
- if (0 == organizeId) {
- 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.setDiscountPrice(MathUtil.add(orderProduct.getDiscountPrice(), orderProduct.getAddedValueTax()).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 (1 == promotions.getType() && 1 == promotions.getMode()) {
- // 单品优惠价取sku的优惠价
- Double touchPrice = baseMapper.getTouchPriceBySku(orderProduct.getSkuId(),promotions.getId());
- promotions.setTouchPrice(touchPrice);
- }
- if (taxFlag && 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);
- }
- }
- // 超级会员优惠商品设置优惠标签
- if (null != orderProduct.getSvipPriceFlag() && 1 == orderProduct.getSvipPriceFlag()) {
- if (1 == orderProduct.getSvipPriceType()) {
- orderProduct.setSvipPriceTag(MathUtil.div(orderProduct.getSvipDiscount(), 10, 1) + "折");
- } else if (2 == orderProduct.getSvipPriceType()) {
- orderProduct.setSvipPriceTag("¥" + orderProduct.getDiscountPrice());
- }
- }
- });
- // 冷链运输费 -- 联合丽格
- if (null != shopOrder.getIsColdChina() && 1 == shopOrder.getIsColdChina()) {
- shopOrder.setColdChina(700.00d);
- } else {
- shopOrder.setColdChina(0.00d);
- }
- // 拼接运费信息--联合丽格
- if (null != shopOrder.getColdChina()) {
- String open = "";
- String close = "";
- if (shopOrder.getIsColdChina()==1) {
- open = "(";
- close = ")";
- } else if (!arrList.stream().allMatch(c -> c == 1)) {
- open = "(";
- close = ")";
- } else {
- open = "";
- close = "";
- }
- if (-1 == shopOrder.getShopPostFlag()) {
- // 到付
- shopOrder.setPostageInfo("¥" + shopOrder.getShopPostFee().doubleValue() + open +(shopOrder.getIsColdChina()==1?"冷链费: ¥" + shopOrder.getColdChina().doubleValue():"") + (arrList.stream().allMatch(c -> c == 1)?"":" 其他: 到付")+close);
- } else if (0 == shopOrder.getShopPostFlag()) {
- // 包邮
- shopOrder.setPostageInfo("¥" + shopOrder.getShopPostFee().doubleValue() + open +(shopOrder.getIsColdChina()==1?"冷链费: ¥" + shopOrder.getColdChina().doubleValue():"") + (arrList.stream().allMatch(c -> c == 1)?"":" 其他: 包邮")+close);
- } else {
- // 有运费
- shopOrder.setPostageInfo("¥" + shopOrder.getShopPostFee().doubleValue() + open +(shopOrder.getIsColdChina()==1?"冷链费: ¥" + shopOrder.getColdChina().doubleValue():"") + (!arrList.stream().allMatch(c -> c == 1)?" 其他: ¥" + MathUtil.sub(order.getPostage(), order.getColdChina()).doubleValue():"") + close);
- }
- } else {
- shopOrder.setPostageInfo(shopOrder.getShopPostFlag() == -1?"到付":shopOrder.getShopPostFlag() == 0?"包邮":"¥" + shopOrder.getShopPostFee().doubleValue());
- }
- shopOrder.setOrderProductList(orderProductList);
- shopOrder.setShopLogo(ImageUtil.getImageUrl("shopLogo", shopOrder.getShopLogo(), domain));
- if (0 == order.getOnlinePayFlag()) {
- shopOrder.setObligation(MathUtil.sub(shopOrder.getRealPay(), shopOrder.getReceiptAmount()).doubleValue());
- }
- });
- // 过滤运费商品
- 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(1);
- }
- }
- }
- // 判断是否可以走线上支付
- int offlineCount = orderCommonMapper.countOfflinePayment(order.getOrderId());
- if (offlineCount > 0) {
- order.setOnlinePayFlag(1);
- }
- // //付供应商总金额 + 默认手续费 > 订单总金额
- // 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;
- }
- /**
- * 设置子订单状态
- * shopStatus 0:待确认 1:已确认 2:交易完成 3:订单完成 4:已关闭 5:交易全退
- *
- * payStatus (付款供应商)付款状态:1待付款、2部分付款、3已付款
- * sendOutStatus 发货状态:1待发货、2部分发货、3已发货
- * receiptStatus 收款状态:1待收款、2部分收款、3已收款
- */
- public void setShopOrderStatus(ShopOrderVo shopOrderVo) {
- // 111, 待付待收待发
- if (1 == shopOrderVo.getStatus() ) {
- if ( 1 == shopOrderVo.getSendOutStatus() && 1 == shopOrderVo.getReceiptStatus()){
- shopOrderVo.setStatus(11);
- } else if (1 == shopOrderVo.getSendOutStatus() && 2 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(12);
- } else if (1 == shopOrderVo.getSendOutStatus() && 3 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(13);
- } else if (2 == shopOrderVo.getSendOutStatus() && 1 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(21);
- } else if (2 == shopOrderVo.getSendOutStatus() && 2 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(22);
- } else if (2 == shopOrderVo.getSendOutStatus() && 3 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(23);
- } else if (3 == shopOrderVo.getSendOutStatus() && 1 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(31);
- } else if (3 == shopOrderVo.getSendOutStatus() && 2 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(32);
- } else if (3 == shopOrderVo.getSendOutStatus() && 3 == shopOrderVo.getReceiptStatus()) {
- shopOrderVo.setStatus(33);
- }
- }
- // 判断交易全退情况下,是否发过货,6,交易全退可以查看物流
- int logisticsCount = orderCommonMapper.countLogisticsBatchShop(shopOrderVo.getShopOrderId());
- if (5 == shopOrderVo.getStatus() && logisticsCount > 0) {
- shopOrderVo.setStatus(6);
- }
- }
- /**
- * 子订单信息
- * @param shopOrder
- */
- public void setShopOrderInfo(ShopOrderVo shopOrder) {
- if (null != shopOrder) {
- Integer organizeId = orderCommonMapper.getShopOrganizeId(shopOrder.getShopOrderId());
- // 冷链商品类型
- List<Integer> arrList = new ArrayList<>();
- // 店铺促销活动
- 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);
- // 冷链商品判定
- List<Integer> collect = orderProductList.stream().map(OrderProductVo::getProductId).collect(Collectors.toList());
- collect.removeIf(c -> c == 999);
- if (collect.stream().allMatch(c -> c == 7536)) {
- arrList.add(1);
- } else if (!collect.contains(7536)) {
- arrList.add(2);
- } else {
- arrList.add(3);
- }
- orderProductList.forEach(orderProduct -> {
- // 不含税可开票商品,单价/折后单价在原基础上加上税费
- boolean taxFlag = false;
- if (0 == organizeId) {
- 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.setDiscountPrice(MathUtil.add(orderProduct.getDiscountPrice(), orderProduct.getAddedValueTax()).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 (1 == promotions.getType() && 1 == promotions.getMode()) {
- // 单品优惠价取sku的优惠价
- Double touchPrice = baseMapper.getTouchPriceBySku(orderProduct.getSkuId(),promotions.getId());
- promotions.setTouchPrice(touchPrice);
- }
- if (taxFlag && 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);
- }
- }
- // 超级会员优惠商品设置优惠标签
- if (null != orderProduct.getSvipPriceFlag() && 1 == orderProduct.getSvipPriceFlag()) {
- if (1 == orderProduct.getSvipPriceType()) {
- orderProduct.setSvipPriceTag(MathUtil.div(orderProduct.getSvipDiscount(), 10, 1) + "折");
- } else if (2 == orderProduct.getSvipPriceType()) {
- orderProduct.setSvipPriceTag("¥" + orderProduct.getDiscountPrice());
- }
- }
- });
- // 冷链运输费 -- 联合丽格
- if (null != shopOrder.getIsColdChina() && 1 == shopOrder.getIsColdChina()) {
- shopOrder.setColdChina(700.00d);
- } else {
- shopOrder.setColdChina(0.00d);
- }
- // 拼接运费信息--联合丽格
- if (null != shopOrder.getColdChina()) {
- String open = "";
- String close = "";
- if (shopOrder.getIsColdChina()==1) {
- open = "(";
- close = ")";
- } else if (!arrList.stream().allMatch(c -> c == 1)) {
- open = "(";
- close = ")";
- } else {
- open = "";
- close = "";
- }
- if (-1 == shopOrder.getShopPostFlag()) {
- // 到付
- shopOrder.setPostageInfo("¥" + MathUtil.add(shopOrder.getShopPostFee(), shopOrder.getColdChina()).doubleValue() + open +(shopOrder.getIsColdChina()==1?"冷链费: ¥" + shopOrder.getColdChina().doubleValue():"") + (arrList.stream().allMatch(c -> c == 1)?"":" 其他: 到付")+close);
- } else if (0 == shopOrder.getShopPostFlag()) {
- // 包邮
- shopOrder.setPostageInfo("¥" + MathUtil.add(shopOrder.getShopPostFee(), shopOrder.getColdChina()).doubleValue() + open +(shopOrder.getIsColdChina()==1?"冷链费: ¥" + shopOrder.getColdChina().doubleValue():"") + (arrList.stream().allMatch(c -> c == 1)?"":" 其他: 包邮")+close);
- } else {
- // 有运费
- shopOrder.setPostageInfo("¥" + MathUtil.add(shopOrder.getShopPostFee(), shopOrder.getColdChina()).doubleValue() + open +(shopOrder.getIsColdChina()==1?"冷链费: ¥" + shopOrder.getColdChina().doubleValue():"") + (!arrList.stream().allMatch(c -> c == 1)?" 其他: ¥" + shopOrder.getShopPostFee():"") + close);
- }
- } else {
- shopOrder.setPostageInfo(shopOrder.getShopPostFlag() == -1?"到付":shopOrder.getShopPostFlag() == 0?"包邮":"¥" + shopOrder.getShopPostFee().doubleValue());
- }
- shopOrder.setOrderProductList(orderProductList);
- shopOrder.setShopLogo(ImageUtil.getImageUrl("shopLogo", shopOrder.getShopLogo(), domain));
- //if (0 == order.getOnlinePayFlag()) {
- shopOrder.setObligation(MathUtil.sub(shopOrder.getRealPay(), shopOrder.getReceiptAmount()).doubleValue());
- //}
- }
- }
- /**
- * 设置子订单付款金额
- * @param shopOrderVo
- * @return
- */
- public List<DiscernReceiptVo> getDiscernReceiptAndSetShoporder(ShopOrderVo shopOrderVo) {
- // 支付记录
- List<DiscernReceiptVo> discernReceiptList = orderCommonMapper.getDiscernReceipt(shopOrderVo.getOrderId(), shopOrderVo.getShopOrderId().toString());
- 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 {
- //返佣款
- Double tempAmount = orderCommonMapper.getRebateAmountByShopOrder(shopOrderVo.getShopOrderId());
- if (null != tempAmount) {
- shopOrderVo.setOnlinePay(1);
- }
- }
- // 判断是否可以走线上支付
- int offlineCount = orderCommonMapper.countOfflinePaymentshopOrder(shopOrderVo.getShopOrderId());
- if (offlineCount > 0) {
- shopOrderVo.setOnlinePay(1);
- }
- //待付总金额
- shopOrderVo.setObligation(MathUtil.sub(shopOrderVo.getRealPay(), receiptAmount).doubleValue());
- //支付总金额
- shopOrderVo.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);
- }
- }
- }
|