| package com.matrix.system.shopXcx.mqTask; | 
|   | 
|   | 
| import com.matrix.component.asyncmessage.MessageHandler; | 
| 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.hive.bean.*; | 
| import com.matrix.system.hive.dao.*; | 
| 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.ShopSku; | 
| import com.matrix.system.shopXcx.dao.ShopOrderDao; | 
| import com.matrix.system.shopXcx.dao.ShopOrderDetailsDao; | 
| import com.matrix.system.shopXcx.dao.ShopSkuDao; | 
| import com.rabbitmq.client.DeliverCallback; | 
| import com.rabbitmq.client.Delivery; | 
| import org.apache.logging.log4j.message.Message; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.io.IOException; | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * 微商城订单同步到erp系统 | 
|  */ | 
| @Component | 
| public class OrderTask implements MessageHandler { | 
|   | 
|   | 
|     @Autowired | 
|     ShopOrderDao shopOrderDao; | 
|     @Autowired | 
|     ShopOrderDetailsDao shopOrderDetailsDao; | 
|   | 
|     @Autowired | 
|     SysVipInfoService sysVipInfoService; | 
|   | 
|   | 
|     @Autowired | 
|     private SysVipInfoDao vipDap; | 
|   | 
|     @Autowired | 
|     SysVipInfoDao vipDao; | 
|   | 
|     @Autowired | 
|     CodeService codeService; | 
|   | 
|     @Autowired | 
|     SysOrderDao sysOrderDao; | 
|   | 
|     @Autowired | 
|     ShoppingGoodsService shoppingGoodsService; | 
|   | 
|     @Autowired | 
|     SysOrderItemDao orderItemDao; | 
|   | 
|     @Autowired | 
|     SysOrderService orderService; | 
|   | 
|     @Autowired | 
|     ShopSkuDao shopSkuDao; | 
|   | 
|     @Autowired | 
|     BusParameterSettingsDao parameterSettingsDao; | 
|   | 
|     @Autowired | 
|     private SysOrderFlowDao sysOrderFlowDao; | 
|   | 
|     @Autowired | 
|     private ShoppingGoodsDao shoppingGoodsDao; | 
|   | 
|     @Autowired | 
|     private ShopSalesmanOrderDao shopSalesmanOrderDao; | 
|   | 
|     @Autowired | 
|     private ShopSalesmanGradeDao shopSalesmanGradeDao; | 
|   | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public void synchronizationOrderToErp(ShopOrder orderDto) { | 
|   | 
|         //判断是否存在需要同步的产品,只有绑定了erp中产品的才同步 | 
|         boolean needTb=false; | 
|         for (ShopOrderDetails orderItemDto : orderDto.getDetails()) { | 
|             ShopSku shopSku = shopSkuDao.selectById(orderItemDto.getsId()); | 
|             if (StringUtils.isNotBlank(shopSku.getStockCode())) { | 
|                 needTb=true; | 
|             } | 
|         } | 
|         if(needTb){ | 
|   | 
|             SysVipInfo vipInfo = sysVipInfoService.findById(orderDto.getUserId()); | 
|             SysOrder order = new SysOrder(); | 
|             if(SysVipInfo.UNDEAL_VIP==vipInfo.getIsDeal()){ | 
|                 //非成交客户下单更新客户为成交客户 | 
|                 vipInfo.setIsDeal(SysVipInfo.DEAL_VIP); | 
|                 vipDao.update(vipInfo); | 
|             } | 
|   | 
|             //获取订单归属门店 | 
|             order.setCompanyId(orderDto.getCompanyId()); | 
|             order.setShopId(Long.parseLong(orderDto.getStoreId()+"")); | 
|             //同步的订单订单编号保持一致 | 
|             order.setOrderNo(orderDto.getOrderNo()); | 
|             order.setVipId(vipInfo.getId()); | 
|             order.setOrderTime(new Date()); | 
|             order.setRemark(AppConstance.WX_ORDER_FLAG); | 
|             order.setStaffId(vipInfo.getStaffId()); | 
|             order.setIsCross(2 + ""); | 
|             order.setStatu(Dictionary.ORDER_STATU_YFK); | 
|             order.setCardPay(0.00); | 
|             order.setCashPay(orderDto.getOrderMoney().doubleValue()); | 
|             order.setTotal(orderDto.getOrderMoney().doubleValue()); | 
|             order.setZkTotal(orderDto.getOrderMoney().doubleValue()); | 
|             order.setPayTime(new Date()); | 
|             order.setArrears(0D); | 
|             int i = sysOrderDao.insert(order); | 
|             // 创建订单明细,并计算总价与折扣总价 | 
|             int count = 0; | 
|             // 设置订单条目 | 
|             for (ShopOrderDetails orderItemDto : orderDto.getDetails()) { | 
|   | 
|   | 
|                 ShopSku shopSku = shopSkuDao.selectById(orderItemDto.getsId()); | 
|   | 
|                 if (StringUtils.isBlank(shopSku.getStockCode())) { | 
|                     LogUtil.warn("销售产品未绑定erp中的产品,无法进行同步"); | 
|                 } | 
|   | 
|                 String goodsCode =shopSku.getStockCode(); | 
|                 ShoppingGoods shoppingGoods = shoppingGoodsService.findById(Long.parseLong(goodsCode)); | 
|   | 
|                 if (shoppingGoods == null) { | 
|                     LogUtil.warn("无效的商品id{}", shopSku.getAtrid()); | 
|                 } | 
|                 SysOrderItem orderItem = new SysOrderItem(); | 
|                 orderItem.setOrderId(order.getId()); | 
|                 orderItem.setCount(orderItemDto.getCount()); | 
|                 orderItem.setIsFree(orderItemDto.getPrice().doubleValue() > 0 ? "否" : "是"); | 
|                 orderItem.setType(shoppingGoods.getGoodType()); | 
|                 orderItem.setStatus(Dictionary.ORDER_STATU_YFK); | 
|                 orderItem.setPrice(orderItemDto.getPrice().doubleValue()); | 
|                 orderItem.setZkPrice(orderItemDto.getPrice().doubleValue()); | 
|                 orderItem.setGoodsId(shoppingGoods.getId()); | 
|   | 
|                 // 设置对应产品的id | 
|                 switch (shoppingGoods.getGoodType()) { | 
|                     case Dictionary.SHOPPING_GOODS_TYPE_JJCP: | 
|                         orderItemDao.insert(orderItem); | 
|                         break; | 
|                     // 购买的是单个项目 | 
|                     case Dictionary.SHOPPING_GOODS_TYPE_XM: | 
|                         orderItemDao.insert(orderItem); | 
|                         break; | 
|                     case Dictionary.SHOPPING_GOODS_TYPE_TC: | 
|                         // 每一个套餐都看成一个单独的订单条目 | 
|                         count = orderItem.getCount(); | 
|                         for (int j = 0; j < count; j++) { | 
|                             orderItem.setCount(1); | 
|                             orderItem.setId(null); | 
|                             orderItemDao.insert(orderItem); | 
|                         } | 
|                         break; | 
|                     case Dictionary.SHOPPING_GOODS_TYPE_CZK: | 
|                         // 新增明细 | 
|                         orderItem.setGoodsId(shoppingGoods.getId()); | 
|                         // 每一个充值卡都看成一个单独的订单条目 | 
|                         count = orderItem.getCount(); | 
|                         for (int j = 0; j < count; j++) { | 
|                             orderItem.setCount(1); | 
|                             orderItem.setId(null); | 
|                             orderItemDao.insert(orderItem); | 
|                         } | 
|                         break; | 
|                     case Dictionary.SHOPPING_GOODS_TYPE_ZHK: | 
|                         // 新增明细 | 
|                         orderItem.setGoodsId(shoppingGoods.getId()); | 
|                         // 每一个充值卡都看成一个单独的订单条目 | 
|                         count = orderItem.getCount(); | 
|                         for (int j = 0; j < count; j++) { | 
|                             orderItem.setCount(1); | 
|                             orderItem.setId(null); | 
|                             orderItemDao.insert(orderItem); | 
|                         } | 
|                         break; | 
|                 } | 
|             } | 
|             // 处理收款逻辑 | 
|             SysOrder sourceOrder = sysOrderDao.selectById(order.getId()); | 
|             sourceOrder.setItems(orderItemDao.selectByOrderId(order.getId())); | 
|             // 设置会员充值卡使用情况 | 
|             orderService.addMoneyCardUse(sourceOrder); | 
|   | 
|             // 改变客户项目套餐使用情况 | 
|             orderService.addTaocanProj(sourceOrder); | 
|   | 
|             // 设置销量 | 
|             orderService.setShopSelCount(sourceOrder); | 
|   | 
|             SysOrderFlow flow = new SysOrderFlow(); | 
|             flow.setFlowNo(codeService.getFlowCode() + "-" + i); | 
|             Long goodsId = sourceOrder.getItems().get(0).getGoodsId(); | 
|             ShoppingGoods goods = shoppingGoodsDao.selectById(goodsId); | 
|             flow.setFlowContent(goods.getName() + "等" + sourceOrder.getItems().size() + "件产品"); | 
|   | 
|             flow.setOrderId(sourceOrder.getId()); | 
|             flow.setVipId(sourceOrder.getVipId()); | 
|             flow.setFlowType(SysOrderFlow.FLOW_TYPE_BUY); | 
|   | 
|             flow.setAmount(orderDto.getOrderMoney()); | 
|             flow.setPayMethod("微信"); | 
|   | 
|             flow.setShopId(sourceOrder.getShopId()); | 
|             flow.setCompanyId(sourceOrder.getCompanyId()); | 
|             sysOrderFlowDao.insert(flow); | 
|         }else{ | 
|             LogUtil.info("不存在需要同步的产品"); | 
|         } | 
|   | 
|   | 
|     } | 
|   | 
|   | 
|     @Override | 
|     public String getName() { | 
|         return "微商城订单同步到erp系统"; | 
|     } | 
|   | 
|     @Override | 
|     public String getRouteKey() { | 
|         return AsyncMessageRouting.CREATE_ORDER; | 
|     } | 
|   | 
|     @Override | 
|     public void handle(Map<String,Object> param) { | 
|   | 
|         String orderId = (String) param.get("orderId"); | 
|         LogUtil.debug("收到创建订单任务orderId={}", orderId); | 
|         //获取订单信息 | 
|         ShopOrder order = shopOrderDao.selectById(Integer.valueOf(orderId)); | 
|         //获取订单详情 | 
|         List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId)); | 
|         order.setDetails(orderDetails); | 
|         //同步订单到erp | 
|         synchronizationOrderToErp(order); | 
|   | 
|     } | 
|   | 
| } |