From 26ffb618e472c3681f17b8b1b1a19ea55383928c Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Fri, 12 Mar 2021 18:28:43 +0800
Subject: [PATCH] Merge branch 'score_shop' of http://120.27.238.55:7000/r/beauty-erp into score_shop

---
 zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 106 insertions(+), 4 deletions(-)

diff --git a/zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java b/zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java
index 8fd2018..a61c024 100644
--- a/zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java
+++ b/zq-erp/src/main/java/com/matrix/system/shopXcx/mqTask/OrderTask.java
@@ -3,11 +3,16 @@
 
 import com.matrix.biz.bean.BizUser;
 import com.matrix.biz.service.BizUserService;
+import com.matrix.core.constance.MatrixConstance;
 import com.matrix.core.tools.LogUtil;
 import com.matrix.core.tools.StringUtils;
 import com.matrix.system.common.constance.AppConstance;
 import com.matrix.system.common.dao.BusParameterSettingsDao;
 import com.matrix.system.constance.Dictionary;
+import com.matrix.system.fenxiao.dao.ShopSalesmanGradeDao;
+import com.matrix.system.fenxiao.dao.ShopSalesmanOrderDao;
+import com.matrix.system.fenxiao.entity.ShopSalesmanGrade;
+import com.matrix.system.fenxiao.entity.ShopSalesmanOrder;
 import com.matrix.system.hive.bean.*;
 import com.matrix.system.hive.dao.*;
 import com.matrix.system.hive.service.CodeService;
@@ -21,6 +26,7 @@
 import com.matrix.system.shopXcx.dao.ShopSkuDao;
 import com.rabbitmq.client.DeliverCallback;
 import com.rabbitmq.client.Delivery;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
@@ -78,8 +84,14 @@
     @Autowired
     private ShoppingGoodsDao shoppingGoodsDao;
 
+    @Autowired
+    private ShopSalesmanOrderDao shopSalesmanOrderDao;
+
+    @Autowired
+    private ShopSalesmanGradeDao shopSalesmanGradeDao;
+
     @Transactional(rollbackFor = Exception.class)
-    public void createOrder(ShopOrder orderDto) {
+    public void synchronizationOrderToErp(ShopOrder orderDto) {
 
         //判断是否存在需要同步的产品,只有绑定了erp中产品的才同步
         boolean needTb=false;
@@ -93,6 +105,7 @@
 
             BizUser bizUser = bizUserService.findByOpenId(orderDto.getUserId());
             SysOrder order = new SysOrder();
+            //TODO 根据公司id区分
             SysVipInfo vipInfo = vipDao.selectByPhone(bizUser.getPhoneNumber());
             if(SysVipInfo.UNDEAL_VIP==vipInfo.getIsDeal()){
                 //非成交客户下单更新客户为成交客户
@@ -101,7 +114,7 @@
             }
 
             //获取订单归属门店
-            order.setCompanyId(vipInfo.getCompanyId());
+            order.setCompanyId(orderDto.getCompanyId());
             order.setShopId(Long.parseLong(orderDto.getStoreId()+""));
             //同步的订单订单编号保持一致
             order.setOrderNo(orderDto.getOrderNo());
@@ -116,7 +129,7 @@
             order.setTotal(orderDto.getOrderMoney().doubleValue());
             order.setZkTotal(orderDto.getOrderMoney().doubleValue());
             order.setPayTime(new Date());
-            order.setArrears(0);
+            order.setArrears(0D);
             int i = sysOrderDao.insert(order);
             // 创建订单明细,并计算总价与折扣总价
             int count = 0;
@@ -236,7 +249,96 @@
         //获取订单详情
         List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId));
         order.setDetails(orderDetails);
-        createOrder(order);
+        //同步订单到erp
+        synchronizationOrderToErp(order);
+        //处理分销订单
+        handleExtensionOrder(order);
+
+    }
+
+    /**
+     * 处理分销订单
+     * @param order
+     */
+    @Transactional
+    void handleExtensionOrder(ShopOrder order) {
+        //判读客户是否存在上级,存在则产生上级的收益
+        BizUser bizUser = bizUserService.findByOpenId(order.getUserId());
+        if(StringUtils.isNotBlank(bizUser.getParentOpenId())){
+            LogUtil.debug("处理分销订单userOpid={}",bizUser.getOpenId());
+            //存在上级
+            ShopSalesmanOrder salesmanOrder=new ShopSalesmanOrder();
+            salesmanOrder.setCompanyId(order.getCompanyId());
+            salesmanOrder.setCreateBy(MatrixConstance.SYSTEM_USER);
+            salesmanOrder.setUpdateBy(MatrixConstance.SYSTEM_USER);
+            salesmanOrder.setCreateTime(new Date());
+            salesmanOrder.setUpdateTime(new Date());
+            salesmanOrder.setOrderId(Long.parseLong(order.getId()+""));
+            salesmanOrder.setUserId(order.getUserId());
+            salesmanOrder.setSalesUserId(bizUser.getParentOpenId());
+            salesmanOrder.setRevenueType(ShopSalesmanOrder.REVENUE_TYPE_SALES);
+            salesmanOrder.setOrderStatus(ShopSalesmanOrder.STATUS_DJS);
+            Double salesAmount=0D;
+            Double invitationAmount=0D;
+            Double orderTotal=0D;
+            Double zk=1D;
+
+            BizUser salesMan=bizUserService.findByOpenId(bizUser.getParentOpenId());
+            BizUser invitationMan=null;
+            ShopSalesmanOrder invitationOrder=null;
+            boolean hasInvitationMan=StringUtils.isNotBlank(salesMan.getParentOpenId());
+
+            //判断推广员是否还存在上级,存在则计算邀请收益
+            if(hasInvitationMan){
+                invitationMan=bizUserService.findByOpenId(salesMan.getParentOpenId());
+                 invitationOrder=new ShopSalesmanOrder();
+                BeanUtils.copyProperties(salesmanOrder,invitationOrder);
+                invitationOrder.setSalesUserId(invitationMan.getOpenId());
+                invitationOrder.setRevenueType(ShopSalesmanOrder.REVENUE_TYPE_INVITATION);
+            }
+
+            //收益计算
+            for (ShopOrderDetails item:order.getDetails() ){
+                if(StringUtils.isNotBlank(item.getShopProduct().getAttrValues())
+                && item.getShopProduct().getAttrValues().contains("分销商城")){
+                    if(item.getShopSku().getSealesPrice()==null
+                    ||item.getShopSku().getSealesPrice().doubleValue()==0){
+                        //按分销等级计算
+                        ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectById(salesMan.getSalesmanGrade());
+                        salesAmount+=item.getTotalPrice().doubleValue()*(shopSalesmanGrade.getSealesCommission()/100);
+
+                        if(hasInvitationMan){
+                            ShopSalesmanGrade invitationManGrade = shopSalesmanGradeDao.selectById(invitationMan.getSalesmanGrade());
+                            invitationAmount+=item.getTotalPrice().doubleValue()*(invitationManGrade.getInvitationCommission()/100);
+                        }
+
+                    }else{
+                        //按产品设置的推广金额计算
+                        salesAmount+=item.getShopSku().getSealesPrice().doubleValue()*item.getCount();
+                        if(hasInvitationMan){
+                            invitationAmount+=item.getShopSku().getInvitationPrice().doubleValue()*item.getCount();
+                        }
+                    }
+                    orderTotal+=item.getTotalPrice().doubleValue();
+                }
+            }
+            //整体的优惠金额折算成折扣,* 佣金
+            zk=order.getOrderMoney().doubleValue()/orderTotal;
+
+            salesAmount=salesAmount*zk;
+            salesmanOrder.setAmount(salesAmount);
+            shopSalesmanOrderDao.insert(salesmanOrder);
+
+            if(hasInvitationMan){
+                invitationAmount=invitationAmount*zk;
+                invitationOrder.setAmount(invitationAmount);
+                shopSalesmanOrderDao.insert(invitationOrder);
+            }
+
+        }else{
+            LogUtil.debug("不存在父级userOpid={}",bizUser.getOpenId());
+        }
+
 
     }
 }

--
Gitblit v1.9.1