| package com.matrix.system.shopXcx.api.action; | 
|   | 
| import com.matrix.component.rabbitmq.RabiitMqTemplate; | 
| import com.matrix.component.redis.RedisUserLoginUtils; | 
| import com.matrix.component.wechat.externalInterface.protocol.paramProtocol.BrandWCPayRequestData; | 
| import com.matrix.config.RabbitMqConfig; | 
| import com.matrix.core.constance.SystemErrorCode; | 
| import com.matrix.core.exception.GlobleException; | 
| import com.matrix.core.pojo.AjaxResult; | 
| import com.matrix.core.tools.LogUtil; | 
| import com.matrix.system.common.constance.AppConstance; | 
| import com.matrix.system.hive.bean.MoneyCardUse; | 
| import com.matrix.system.hive.bean.MoneyCardUseFlow; | 
| import com.matrix.system.hive.bean.SysVipInfo; | 
| import com.matrix.system.hive.dao.MoneyCardUseDao; | 
| import com.matrix.system.hive.dao.MoneyCardUseFlowDao; | 
| import com.matrix.system.hive.dao.ShoppingGoodsDao; | 
| import com.matrix.system.hive.dao.SysVipInfoDao; | 
| import com.matrix.system.score.dao.ScoreVipDetailDao; | 
| import com.matrix.system.shopXcx.api.pojo.OrderInfoQueryPOJO; | 
| import com.matrix.system.shopXcx.api.pojo.ShopOrderDto; | 
| import com.matrix.system.shopXcx.api.service.OrderCouponGroupService; | 
| import com.matrix.system.shopXcx.api.service.ShoppingCartService; | 
| import com.matrix.system.shopXcx.api.service.WXShopOrderService; | 
| import com.matrix.system.shopXcx.api.service.WxShopCouponService; | 
| import com.matrix.system.shopXcx.bean.ShopActivitiesGroupJoin; | 
| import com.matrix.system.shopXcx.bean.ShopActivitiesGroupJoinUser; | 
| import com.matrix.system.shopXcx.bean.ShopOrder; | 
| import com.matrix.system.shopXcx.bean.ShopPayLog; | 
| import com.matrix.system.shopXcx.dao.*; | 
| import com.matrix.system.shopXcx.mqTask.MQTaskRouting; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Controller; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.validation.annotation.Validated; | 
| import org.springframework.web.bind.annotation.*; | 
|   | 
| import java.math.BigDecimal; | 
| import java.util.*; | 
|   | 
|   | 
| /** | 
|  * @author jyy | 
|  * @description 订单控制器 | 
|  * @date 2019-06-10 10:58 | 
|  */ | 
| @Controller | 
| @RequestMapping(value = "wxapi/shopOrder") | 
| public class WXShopOrderAction { | 
|   | 
|     @Autowired | 
|     private WXShopOrderService shopOrderService; | 
|   | 
|     @Autowired | 
|     WxShopCouponService wxShopCouponService; | 
|   | 
|   | 
|     @Autowired | 
|     ShoppingCartService shoppingCartService; | 
|   | 
|   | 
|     @Autowired | 
|     private RedisUserLoginUtils redisUserLoginUtils; | 
|   | 
|     @Autowired | 
|     ShopCouponRecordDao shopCouponRecordDao; | 
|   | 
|     @Autowired | 
|     ShopSkuDao shopSkuDao; | 
|   | 
|     @Autowired | 
|     ShopProductDao shopProductDao; | 
|   | 
|     @Autowired | 
|     OrderCouponGroupService orderCouponGroupService; | 
|   | 
|     @Autowired | 
|     private ShopPayLogDao shopPayLogDao; | 
|   | 
|     @Autowired | 
|     ShopOrderDao shopOrderDao; | 
|   | 
|     @Autowired | 
|     SysVipInfoDao sysVipInfoDao; | 
|   | 
|     @Autowired | 
|     private ShopActivitiesGroupJoinUserDao shopActivitiesGroupJoinUserDao; | 
|   | 
|     @Autowired | 
|     private ShopActivitiesGroupJoinDao shopActivitiesGroupJoinDao; | 
|   | 
|     @Autowired | 
|     private RabiitMqTemplate rabiitMqTemplate; | 
|   | 
|     @Autowired | 
|     ShoppingGoodsDao shoppingGoodsDao; | 
|   | 
|     @Autowired | 
|     MoneyCardUseFlowDao moneyCardUseFlowDao; | 
|   | 
|     @Autowired | 
|     MoneyCardUseDao moneyCardUseDao; | 
|   | 
|     @Value("${evn}") | 
|     private String evn; | 
|   | 
|     /** | 
|      * 计算购物车订单价格 | 
|      * | 
|      * @param shopOrderDto | 
|      * @return | 
|      * @throws Exception | 
|      */ | 
|     @PostMapping(value = "/calculationCartOrder") | 
|     public | 
|     @ResponseBody | 
|     AjaxResult calculationCartOrder(@RequestBody ShopOrderDto shopOrderDto) throws Exception { | 
|         return shopOrderService.buildDiscountExplain(shopOrderDto); | 
|     } | 
|   | 
|   | 
|     @Autowired | 
|     private ScoreVipDetailDao scoreVipDetailDao; | 
|   | 
|     /** | 
|      * 新增微信支付订单 | 
|      * | 
|      * @param shopOrderDto | 
|      * @return | 
|      */ | 
|     @PostMapping(value = "/addShopOrder") | 
|     public | 
|     @ResponseBody | 
|     AjaxResult addShopOrder(@RequestBody @Validated ShopOrderDto shopOrderDto) throws Exception { | 
|   | 
|         ShopOrder shopOrder = shopOrderService.createShopOrder(shopOrderDto); | 
|         AjaxResult result = AjaxResult.buildSuccessInstance("订单创建成功"); | 
|         result.putInMap("orderId", shopOrder.getId()); | 
|         if (shopOrderDto.getPayMethod().equals(ShopOrder.SHOP_ORDER_PAY_METHOD_WX)) { | 
|             BrandWCPayRequestData payData = shopOrderService.startPayment(shopOrder); | 
|             result.setRows(Arrays.asList(payData)); | 
|         } | 
|         return result; | 
|     } | 
|   | 
|   | 
|   | 
|     /** | 
|      * 余额支付确认 | 
|      * | 
|      * @param orderId | 
|      * @return | 
|      * @throws Exception | 
|      */ | 
|     @GetMapping(value = "/moneyCardPay/{orderId}") | 
|     @ResponseBody | 
|     @Transactional | 
|     public   AjaxResult moneyCardPay(@PathVariable Integer orderId) throws Exception { | 
|   | 
|         ShopOrder order = shopOrderDao.selectById(orderId); | 
|         if (order != null | 
|                 && ShopOrder.SHOP_ORDER_PAY_METHOD_VIPCARD == order.getPayMethod() | 
|                 && ShopOrder.ORDER_PAY_STATUS_WAIT == order.getPayStatus()) { | 
|   | 
|             MoneyCardUse moneyCardUse = moneyCardUseDao.selectVipCard(order.getUserId()); | 
|             Double balance=moneyCardUse.getGiftMoney() + moneyCardUse.getRealMoney(); | 
|             if(balance>=order.getOrderMoney().doubleValue()){ | 
|                 //扣减会员卡余额 | 
|                 if(moneyCardUse.getRealMoney()>=order.getOrderMoney().doubleValue()){ | 
|                     moneyCardUse.setRealMoney(moneyCardUse.getRealMoney()-order.getOrderMoney().doubleValue()); | 
|                 }else{ | 
|                     moneyCardUse.setGiftMoney(moneyCardUse.getGiftMoney()-(order.getOrderMoney().doubleValue()- moneyCardUse.getRealMoney() )); | 
|                     moneyCardUse.setRealMoney(0D); | 
|                 } | 
|                 moneyCardUseDao.update(moneyCardUse); | 
|                 //插入扣减记录 | 
|                 MoneyCardUseFlow moneyCardUseFlow=new MoneyCardUseFlow(); | 
|                 moneyCardUseFlow.setTotal(order.getOrderMoney().doubleValue()); | 
|                 moneyCardUseFlow.setCarUseId(moneyCardUse.getId()); | 
|                 moneyCardUseFlow.setVipId(order.getUserId()); | 
|                 moneyCardUseFlow.setType(MoneyCardUseFlow.USE_TYPE_CONSUMER); | 
|                 moneyCardUseFlow.setOrderNo(order.getOrderNo()); | 
|                 moneyCardUseFlow.setCreateTime(new Date()); | 
|                 moneyCardUseFlow.setBalance(moneyCardUse.getGiftMoney()+moneyCardUse.getRealMoney()); | 
|                 moneyCardUseFlow.setCardName(moneyCardUse.getCardName()); | 
|                 moneyCardUseFlowDao.insert(moneyCardUseFlow); | 
|   | 
|                 //构建需要修改订单信息Map | 
|                 Map<String, Object> modifyMap = new HashMap<>(); | 
|                 modifyMap.put("id", order.getId()); | 
|                 //设置微信支付状态为已支付 | 
|                 modifyMap.put("payResult", ShopOrder.ORDER_WX_STATUS_PAY_SUCCESS); | 
|                 //设置支付状态为支付成功 | 
|                 modifyMap.put("payStatus", ShopOrder.ORDER_PAY_STATUS_SUCCESS); | 
|                 //如果是商品订单则进入待发货 | 
|                 modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_SEND); | 
|                 shopOrderDao.updateByMap(modifyMap); | 
|   | 
|                 // 判断该订单是否为拼团产生的订单 | 
|                 ShopActivitiesGroupJoinUser joinUser = shopActivitiesGroupJoinUserDao.selectGroupJoinUserByOrderId(order.getId().longValue()); | 
|                 if (joinUser != null) { | 
|                     joinUser.setIsHasPay(ShopActivitiesGroupJoinUser.IS_HAS_PAY_Y); | 
|                     shopActivitiesGroupJoinUserDao.updateByModel(joinUser); | 
|   | 
|                     ShopActivitiesGroupJoin groupJoin = shopActivitiesGroupJoinDao.selectGroupAndPriceById(joinUser.getGjId()); | 
|                     List<ShopActivitiesGroupJoinUser> joinUserList = shopActivitiesGroupJoinUserDao.selectGroupJoinUserListByGjId(joinUser.getGjId()); | 
|                     LogUtil.info("#当前已支付完成:{}#", joinUserList.size()); | 
|                     // 若拼团中已支付的数量等于几人团预设值,则更新拼团状态为拼团成功 | 
|                     if (groupJoin.getGroupPrice().getGpCount() == joinUserList.size()) { | 
|                         groupJoin.setGjStatus(ShopActivitiesGroupJoin.ACTIVITIES_JOIN_SUCCESS); | 
|                     } else { | 
|                         if (joinUser.getUserId().equals(groupJoin.getGjHeadId())) { | 
|                             groupJoin.setGjStatus(ShopActivitiesGroupJoin.ACTIVITIES_JOIN_ING); | 
|                         } | 
|                     } | 
|                     shopActivitiesGroupJoinDao.updateByModel(groupJoin); | 
|   | 
|                     SysVipInfo sysVipInfo = sysVipInfoDao.selectByPhone(order.getUserTel(),order.getCompanyId()); | 
|                     if (sysVipInfo != null) { | 
|                         sysVipInfo.setIsDeal(SysVipInfo.DEAL_VIP); | 
|                         sysVipInfoDao.update(sysVipInfo); | 
|                     } | 
|                 } | 
|   | 
|                 // 根据订单类型创建不同的处理任务 | 
|                 rabiitMqTemplate.sendTopicMsg(RabbitMqConfig.MQ_EXCHANGE_TOPIC + evn, MQTaskRouting.CREATE_ORDER + evn, orderId + ""); | 
|                 //支付记录 | 
|                 recordPayLog(Integer.valueOf(orderId), 1, order.getOrderNo(), order.getOrderMoney(), "会员卡余额支付成功", ShopOrder.ORDER_PAY_STATUS_SUCCESS); | 
|   | 
|                 return AjaxResult.buildSuccessInstance("支付成功"); | 
|   | 
|             }else{ | 
|                 return AjaxResult.buildFailInstance("会员卡余额不足"); | 
|             } | 
|   | 
|         } else { | 
|             LogUtil.info("订单状态不为待付款,order status=", order.getOrderStatus()); | 
|             return AjaxResult.buildFailInstance("订单不支持本次结算"); | 
|         } | 
|   | 
|     } | 
|   | 
|   | 
|     private void recordPayLog(Integer orderId, Integer type, String orderNo, BigDecimal payMoney, String payRemark, int payStatus) { | 
|         //设置支付类型为收款 | 
|         ShopPayLog payLog = new ShopPayLog(); | 
|         payLog.setPayType(type); | 
|         payLog.setJoinId(orderId); | 
|         payLog.setPayMoney(payMoney); | 
|         payLog.setPayOrderNo(orderNo); | 
|         payLog.setPayRemark(payRemark); | 
|         payLog.setPayStatus(payStatus); | 
|         payLog.setCreateBy(AppConstance.SYSTEM_USER); | 
|         payLog.setUpdateBy(AppConstance.SYSTEM_USER); | 
|         shopPayLogDao.insert(payLog); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 订单结算 | 
|      * | 
|      * @param orderId 订单ID | 
|      * @return | 
|      */ | 
|     @GetMapping(value = "orderSettlement/{orderId}") | 
|     public | 
|     @ResponseBody | 
|     AjaxResult orderSettlement(@PathVariable("orderId") int orderId) { | 
|         try { | 
|             return shopOrderService.orderSettlement(orderId); | 
|         } catch (Exception e) { | 
|             throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 查询订单支付结果 | 
|      * | 
|      * @param orderId | 
|      * @return | 
|      * @throws InterruptedException | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年9月8日 | 
|      */ | 
|     @GetMapping(value = "/findOrderPayStatus/{orderId}") | 
|     public | 
|     @ResponseBody | 
|     AjaxResult findOrderPayStatus(@PathVariable Integer orderId) throws InterruptedException { | 
|         AjaxResult ajaxResult = shopOrderService.findOrderPayStatus(orderId); | 
|         return ajaxResult; | 
|     } | 
|   | 
|     /** | 
|      * 获取我的订单信息 | 
|      * | 
|      * @return | 
|      */ | 
|     @PostMapping(value = "getMyOrderInfo") | 
|     @ResponseBody | 
|     public AjaxResult getMyOrderInfo(@RequestBody OrderInfoQueryPOJO orderInfoQueryPOJO) { | 
|         AjaxResult ajaxResult = shopOrderService.getMyOrderInfo(orderInfoQueryPOJO); | 
|         return ajaxResult; | 
|     } | 
|   | 
|     /** | 
|      * 通过订单ID获取订单信息 | 
|      * | 
|      * @return | 
|      */ | 
|     @GetMapping(value = "getOrderInfoById/{orderId}") | 
|     @ResponseBody | 
|     public AjaxResult getOrderInfoById(@PathVariable Integer orderId) { | 
|         AjaxResult ajaxResult = shopOrderService.getOrderInfoById(orderId); | 
|         return ajaxResult; | 
|     } | 
|   | 
|     /** | 
|      * 取消订单 | 
|      * | 
|      * @param | 
|      * @return | 
|      */ | 
|     @GetMapping(value = "cancelOrderWhenWaitPay/{orderId}") | 
|     @ResponseBody | 
|     public AjaxResult cancelOrderWhenWaitPay(@PathVariable("orderId") Integer orderId) { | 
|         return shopOrderService.cancelOrderWhenWaitPay(orderId); | 
|     } | 
|   | 
|     /** | 
|      * 删除订单 | 
|      * | 
|      * @param | 
|      * @return | 
|      */ | 
|     @GetMapping(value = "delOrder/{orderId}") | 
|     @ResponseBody | 
|     public AjaxResult delOrderById(@PathVariable("orderId") Integer orderId) { | 
|         return shopOrderService.delOrderById(orderId); | 
|     } | 
|   | 
|     /** | 
|      * 确认收货 | 
|      * | 
|      * @param | 
|      * @return | 
|      */ | 
|     @GetMapping(value = "confirmPackageById/{orderId}") | 
|     @ResponseBody | 
|     public AjaxResult confirmPackageById(@PathVariable("orderId") Integer orderId) { | 
|         return shopOrderService.confirmPackageById(orderId); | 
|     } | 
|   | 
|   | 
|     @GetMapping(value = "/getOrderStatusCount") | 
|     @ResponseBody | 
|     public AjaxResult getOrderStatusCount() { | 
|         SysVipInfo sysVipInfo = redisUserLoginUtils.getLoginUser(SysVipInfo.class); | 
|         AjaxResult result = AjaxResult.buildSuccessInstance( | 
|                 shopOrderDao.selectOrderStatusCount(sysVipInfo.getId())); | 
|         return result; | 
|     } | 
|   | 
| } |