| package com.matrix.system.shopXcx.mqTask; | 
|   | 
|   | 
| import com.matrix.biz.bean.BizUser; | 
| import com.matrix.biz.service.BizUserService; | 
| 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.hive.bean.ShoppingGoods; | 
| import com.matrix.system.hive.bean.SysOrder; | 
| import com.matrix.system.hive.bean.SysOrderItem; | 
| 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.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.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.io.IOException; | 
| import java.util.Date; | 
| import java.util.List; | 
|   | 
| /** | 
|  * 微商城订单同步到erp系统 | 
|  */ | 
| public class OrderTask implements DeliverCallback { | 
|   | 
|   | 
|     @Autowired | 
|     ShopOrderDao shopOrderDao; | 
|     @Autowired | 
|     ShopOrderDetailsDao shopOrderDetailsDao; | 
|   | 
|     @Autowired | 
|     BizUserService bizUserService; | 
|   | 
|   | 
|     @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; | 
|   | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public void createOrder(ShopOrder orderDto) { | 
|   | 
|         BizUser bizUser = bizUserService.findByOpenId(orderDto.getUserId()); | 
|         SysOrder order = new SysOrder(); | 
|         SysVipInfo vipInfo = vipDao.selectByPhone(bizUser.getPhoneNumber()); | 
|         if(SysVipInfo.UNDEAL_VIP==vipInfo.getIsDeal()){ | 
|             //非成交客户下单更新客户为成交客户 | 
|             vipInfo.setIsDeal(SysVipInfo.DEAL_VIP); | 
|             vipDao.update(vipInfo); | 
|         } | 
|   | 
|         //获取订单归属门店 | 
|         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.setArrears(0); | 
|         int i = sysOrderDao.insert(order); | 
|         // 创建订单明细,并计算总价与折扣总价 | 
|         // 总价 | 
|         double total = 0; | 
|         double zkTotal = 0; | 
|         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()); | 
|             if (shoppingGoods.getGoodType().equals(Dictionary.SHOPPING_GOODS_TYPE_JJCP)) { | 
|                 orderItem.setAssembleId(shoppingGoods.getAssembleGoods().get(0).getId()); | 
|             } else if (shoppingGoods.getGoodType().equals(Dictionary.SHOPPING_GOODS_TYPE_XM)) { | 
|                 orderItem.setAssembleId(shoppingGoods.getAssembleProj().get(0).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); | 
|   | 
|     } | 
|   | 
|   | 
|   | 
|   | 
|     @Override | 
|     public void handle(String consumerTag, Delivery message) throws IOException { | 
|   | 
|             String orderId = new String(message.getBody(), "UTF-8"); | 
|             LogUtil.debug("收到创建订单任务orderId={}", orderId); | 
|             //获取订单信息 | 
|             ShopOrder order = shopOrderDao.selectById(Integer.valueOf(orderId)); | 
|             //获取订单详情 | 
|             List<ShopOrderDetails> orderDetails = shopOrderDetailsDao.selectByOrderId(Integer.valueOf(orderId)); | 
|             order.setDetails(orderDetails); | 
|             createOrder(order); | 
|   | 
|     } | 
| } |