| | |
| | | package com.matrix.system.shopXcx.mqTask; |
| | | |
| | | |
| | | import com.matrix.biz.bean.BizUser; |
| | | import com.matrix.biz.service.BizUserService; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.matrix.core.constance.MatrixConstance; |
| | | import com.matrix.core.tools.LogUtil; |
| | | import com.matrix.core.tools.StringUtils; |
| | | import com.matrix.system.common.bean.BusParameterSettings; |
| | | import com.matrix.system.common.dao.BusParameterSettingsDao; |
| | | import com.matrix.system.fenxiao.constant.FenxiaoSettingConstant; |
| | | 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.SysVipInfo; |
| | | import com.matrix.system.hive.dao.SysOrderDao; |
| | | import com.matrix.system.hive.dao.SysOrderItemDao; |
| | | import com.matrix.system.hive.dao.SysVipInfoDao; |
| | | import com.matrix.system.hive.service.CodeService; |
| | | import com.matrix.system.hive.service.ShoppingGoodsService; |
| | | import com.matrix.system.hive.service.SysOrderService; |
| | | import com.matrix.system.hive.service.SysVipInfoService; |
| | | import com.matrix.system.shopXcx.bean.ShopOrder; |
| | | import com.matrix.system.shopXcx.bean.ShopOrderDetails; |
| | | import com.matrix.system.shopXcx.bean.ShopProduct; |
| | | import com.matrix.system.shopXcx.dao.ShopOrderDao; |
| | | import com.matrix.system.shopXcx.dao.ShopOrderDetailsDao; |
| | | import com.matrix.system.shopXcx.dao.ShopSkuDao; |
| | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 微商城订单同步到erp系统 |
| | | * 分销订单创建 |
| | | */ |
| | | @Component |
| | | public class SalesOrderTask implements DeliverCallback { |
| | |
| | | ShopOrderDetailsDao shopOrderDetailsDao; |
| | | |
| | | @Autowired |
| | | BizUserService bizUserService; |
| | | SysVipInfoService sysVipInfoService; |
| | | |
| | | |
| | | @Autowired |
| | |
| | | private ShopSalesmanGradeDao shopSalesmanGradeDao; |
| | | |
| | | |
| | | |
| | | @Override |
| | | public void handle(String consumerTag, Delivery message) throws IOException { |
| | | |
| | |
| | | @Transactional |
| | | void handleExtensionOrder(ShopOrder order) { |
| | | //判读客户是否存在上级,存在则产生上级的收益 |
| | | BizUser bizUser = bizUserService.findByOpenId(order.getUserId()); |
| | | if(StringUtils.isNotBlank(bizUser.getParentOpenId())){ |
| | | LogUtil.debug("处理分销订单userOpid={}",bizUser.getOpenId()); |
| | | //存在上级 |
| | | SysVipInfo sysVipInfo = sysVipInfoService.findById(order.getUserId()); |
| | | LogUtil.debug("处理分销订单userOpid={}",sysVipInfo.getOpenId()); |
| | | //判断是否重复生成 |
| | | QueryWrapper queryWrapper=new QueryWrapper(); |
| | | queryWrapper.eq("order_id",order.getId()); |
| | | Integer orderCount = shopSalesmanOrderDao.selectCount(queryWrapper); |
| | | if(orderCount==0){ |
| | | ShopSalesmanOrder salesmanOrder=new ShopSalesmanOrder(); |
| | | salesmanOrder.setCompanyId(order.getCompanyId()); |
| | | salesmanOrder.setCreateBy(MatrixConstance.SYSTEM_USER); |
| | |
| | | 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 selfAmount=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); |
| | | } |
| | | |
| | | //收益计算 |
| | | //分销订单总额 |
| | | Double orderTotal=0D; |
| | | 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){ |
| | | if(item.getShopProduct().getAbleSales()==ShopProduct.ABLE_SALES){ |
| | | orderTotal+=item.getTotalPrice().doubleValue(); |
| | | } |
| | | } |
| | | //自购返佣 |
| | | Integer isSales = sysVipInfo.getIsSales(); |
| | | if(SysVipInfo.IS_SALES == isSales){ |
| | | for (ShopOrderDetails item:order.getDetails() ){ |
| | | if(item.getShopProduct().getAbleSales()==ShopProduct.ABLE_SALES){ |
| | | //分销规则是否开启自购返佣 |
| | | BusParameterSettings zgfy = parameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_ZGFY, sysVipInfo.getCompanyId()); |
| | | //按分销等级计算 |
| | | 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(); |
| | | ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectById(sysVipInfo.getSalesmanGrade()); |
| | | if(ObjectUtil.isNotEmpty(zgfy)) { |
| | | //获取商品的自购返佣价格,如果没有设置,在去获取当前推广人的等级的自购返佣比例 |
| | | if(StrUtil.isNotEmpty(zgfy.getParamValue()) && |
| | | FenxiaoSettingConstant.FX_ZGFY_YES.equals(zgfy.getParamValue())) { |
| | | if(item.getShopSku().getSelfPrice() == null |
| | | ||item.getShopSku().getSealesPrice().doubleValue() == 0){ |
| | | selfAmount+=item.getTotalPrice().doubleValue()*(shopSalesmanGrade.getSelfCommission()/100); |
| | | }else { |
| | | //按产品设置的自购返佣金额计算 |
| | | selfAmount+=item.getShopSku().getSelfPrice().doubleValue()*item.getCount(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | orderTotal+=item.getTotalPrice().doubleValue(); |
| | | } |
| | | //自购返佣记录 |
| | | if(selfAmount > 0) { |
| | | //整体的优惠金额折算成折扣,* 佣金 |
| | | zk=order.getOrderMoney().doubleValue()/orderTotal; |
| | | selfAmount=selfAmount*zk; |
| | | salesmanOrder.setAmount(selfAmount); |
| | | salesmanOrder.setSalesUserId(order.getUserId()); |
| | | shopSalesmanOrderDao.insert(salesmanOrder); |
| | | } |
| | | } |
| | | if(salesAmount>0){ |
| | | //整体的优惠金额折算成折扣,* 佣金 |
| | | zk=order.getOrderMoney().doubleValue()/orderTotal; |
| | | salesAmount=salesAmount*zk; |
| | | salesmanOrder.setAmount(salesAmount); |
| | | shopSalesmanOrderDao.insert(salesmanOrder); |
| | | |
| | | if(hasInvitationMan){ |
| | | invitationAmount=invitationAmount*zk; |
| | | //存在上级 |
| | | if(sysVipInfo.getRecommendId()!=null) { |
| | | SysVipInfo salesMan=sysVipInfoService.findById(sysVipInfo.getRecommendId()); |
| | | Double salesAmount=0D; |
| | | Double invitationAmount=0D; |
| | | SysVipInfo invitationMan=null; |
| | | ShopSalesmanOrder invitationOrder=null; |
| | | boolean hasInvitationMan=salesMan.getRecommendId()!=null; |
| | | //判断推广员是否还存在上级,存在则计算邀请收益 |
| | | if(hasInvitationMan){ |
| | | invitationMan=sysVipInfoService.findById(salesMan.getRecommendId()); |
| | | invitationOrder=new ShopSalesmanOrder(); |
| | | BeanUtils.copyProperties(salesmanOrder,invitationOrder); |
| | | invitationOrder.setSalesUserId(invitationMan.getId()); |
| | | invitationOrder.setRevenueType(ShopSalesmanOrder.REVENUE_TYPE_INVITATION); |
| | | } |
| | | //收益计算 |
| | | for (ShopOrderDetails item:order.getDetails() ){ |
| | | if(item.getShopProduct().getAbleSales()==ShopProduct.ABLE_SALES){ |
| | | //按分销等级计算 |
| | | ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectById(salesMan.getSalesmanGrade()); |
| | | //推广提成,按分销等级计算或者按产品设置的推广金额计算 |
| | | if(item.getShopSku().getSealesPrice()==null |
| | | ||item.getShopSku().getSealesPrice().doubleValue()==0){ |
| | | salesAmount+=item.getTotalPrice().doubleValue()*(shopSalesmanGrade.getSealesCommission()/100); |
| | | }else{ |
| | | salesAmount+=item.getShopSku().getSealesPrice().doubleValue()*item.getCount(); |
| | | } |
| | | //邀请提成,获取商品的邀请提成价格和是否有上级推广人,如果没有设置,在去获取当前推广人的等级的自购返佣比例 |
| | | if(hasInvitationMan) { |
| | | if(item.getShopSku().getInvitationPrice() == null |
| | | ||item.getShopSku().getInvitationPrice().doubleValue() == 0){ |
| | | //获取上级的推广等级设置 |
| | | ShopSalesmanGrade invitationManGrade = shopSalesmanGradeDao.selectById(invitationMan.getSalesmanGrade()); |
| | | invitationAmount+=item.getTotalPrice().doubleValue()*(invitationManGrade.getInvitationCommission()/100); |
| | | }else { |
| | | //按产品设置的邀请提成价格计算 |
| | | invitationAmount+=item.getShopSku().getInvitationPrice().doubleValue()*item.getCount(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | //推广提成记录 |
| | | if(salesAmount>0){ |
| | | //整体的优惠金额折算成折扣,* 佣金 |
| | | zk=order.getOrderMoney().doubleValue()/orderTotal; |
| | | salesAmount=salesAmount*zk; |
| | | salesmanOrder.setAmount(salesAmount); |
| | | salesmanOrder.setSalesUserId(sysVipInfo.getRecommendId()); |
| | | shopSalesmanOrderDao.insert(salesmanOrder); |
| | | } |
| | | //邀请提成记录 |
| | | if(invitationAmount > 0) { |
| | | //整体的优惠金额折算成折扣,* 佣金 |
| | | invitationAmount=invitationAmount*zk; |
| | | invitationOrder.setAmount(invitationAmount); |
| | | shopSalesmanOrderDao.insert(invitationOrder); |
| | | } |
| | | } |
| | | }else{ |
| | | LogUtil.debug("不存在父级userOpid={}",sysVipInfo.getOpenId()); |
| | | } |
| | | }else{ |
| | | LogUtil.debug("不存在父级userOpid={}",bizUser.getOpenId()); |
| | | LogUtil.debug("重复订单id={}",order.getId()); |
| | | } |
| | | |
| | | |
| | | } |
| | | } |