| package com.matrix.system.shopXcx.api.service.impl; | 
|   | 
| import com.matrix.component.wechat.externalInterface.common.WechatConfigure; | 
| import com.matrix.core.exception.GlobleException; | 
| import com.matrix.core.tools.DateUtil; | 
| import com.matrix.core.tools.LogUtil; | 
| import com.matrix.system.common.constance.AppConstance; | 
| import com.matrix.component.wechat.externalInterface.weixinUtil.WeixinServiceUtil; | 
| import com.matrix.system.shopXcx.bean.*; | 
| import com.matrix.system.shopXcx.dao.*; | 
| import com.matrix.component.tools.WxTempLateMsgUtil; | 
| import com.matrix.system.shopXcx.api.service.WXShopOrderService; | 
| import com.matrix.system.shopXcx.api.service.WxShopRefundRecordService; | 
| import org.apache.commons.collections.CollectionUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import java.math.BigDecimal; | 
| import java.util.ArrayList; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
| /** | 
|  * @description 退款服务层 | 
|  * @author wlz | 
|  * @date 2019-07-04 10:58 | 
|  */ | 
| @Service("wxShopRefundRecordService") | 
| public class WxShopRefundRecordServiceImpl implements WxShopRefundRecordService { | 
|     @Autowired | 
|     private ShopOrderDao shopOrderDao; | 
|     @Autowired | 
|     private WeixinServiceUtil weixinServiceUtil; | 
|     @Autowired | 
|     private ShopRefundRecordDao shopRefundRecordDao; | 
|     @Autowired | 
|     private ShopCouponRecordDao shopCouponRecordDao; | 
|     @Autowired | 
|     private ShopDeliveryInfoDao shopDeliveryInfoDao; | 
|     @Autowired | 
|     private WXShopOrderService wxShopOrderService; | 
|   | 
|     @Autowired | 
|     private ShopActivitiesGroupJoinUserDao shopActivitiesGroupJoinUserDao; | 
|   | 
|     @Autowired | 
|     private ShopActivitiesGroupJoinDao shopActivitiesGroupJoinDao; | 
|   | 
|     @Value("${wx_pay_debug_onoff}") | 
|     private boolean isDebug; | 
|     @Override | 
|     public Boolean refundToUser(String id, ShopRefundRecord shopRefundRecord) { | 
|         LogUtil.debug("进入退款接口进行退款。。。", id); | 
|         Boolean flag = false; | 
|         //调用退款接口,将钱返给用户 | 
|         ShopOrder shopOrder = shopOrderDao.selectById(shopRefundRecord.getOrderId()); | 
|         if(null == shopOrder){ | 
|             throw new GlobleException("没有找到需要退款的订单信息"); | 
|         } | 
|         //商户订单编号(原订单编号) | 
|         String orderNo = shopOrder.getOrderNo(); | 
|         LogUtil.info("退款商户订单编号为。。。", orderNo); | 
|         //退款编号 | 
|         String refundNo = shopRefundRecord.getRefundNo(); | 
|         LogUtil.info("退款退款编号为。。。", refundNo); | 
|         //订单金额 | 
|         BigDecimal a1 = shopOrder.getOrderMoney(); | 
|         BigDecimal aa = new BigDecimal(100); | 
|         int orMoney = a1.multiply(aa).intValue(); | 
|         LogUtil.info("退款订单金额。。。", orMoney); | 
|         //Double orderMoney = 0.1 * 100; | 
|         //退款金额 | 
|         BigDecimal b1 = shopRefundRecord.getRefundMoney(); | 
|         BigDecimal bb = new BigDecimal(100); | 
|         int reMoney = b1.multiply(bb).intValue(); | 
|         LogUtil.info("退款退款金额。。。", reMoney); | 
|   | 
|         //Double refundMoney = 0.1 * 100; | 
|         //用户ID | 
|   | 
|         if (isDebug) { | 
|             boolean b = weixinServiceUtil.comRefund(orderNo, refundNo, 1, 1, null); | 
|             flag = b; | 
|         } else { | 
|             LogUtil.info("开始调用退款接口。。。退款编号为", refundNo); | 
|             boolean b = weixinServiceUtil.comRefund(orderNo, refundNo, orMoney, reMoney, null); | 
|             flag = b; | 
|         } | 
|   | 
|         if(flag){ | 
|             try{ | 
|                 LogUtil.debug("退款成功,开始修改退款表和订单表状态。。。", id); | 
|                 //将申请退款状态改为成功退款 | 
|                 shopRefundRecordDao.updateRefundStatusByIds(Integer.valueOf(id), AppConstance.REFUND_SUCCESS); | 
|                 //将审核状态改为审核通过 | 
|                 Map<String, Object> modifyRefund = new HashMap<>(); | 
|                 modifyRefund.put("id", Integer.valueOf(id)); | 
|                 modifyRefund.put("auditStatus", AppConstance.REFUND_AUDITED_PASS); | 
|                 shopRefundRecordDao.updateByMap(modifyRefund); | 
|                 //退款成功后同时把优惠券退回到用户的账号中 | 
|                 if (shopOrder.getDiscountAmount() != null && shopOrder.getDiscountAmount().compareTo(BigDecimal.ZERO) >0) { | 
|                     ShopCouponRecord shopCouponRecord = new ShopCouponRecord(); | 
|                     String userIds = shopRefundRecord.getUserId(); | 
|                     shopCouponRecord.setUserId(userIds); | 
|                     shopCouponRecord.setOrderId(shopOrder.getId()); | 
|                     List<ShopCouponRecord> recordList = shopCouponRecordDao.selectByModel(shopCouponRecord); | 
|                     if (CollectionUtils.isNotEmpty(recordList)) { | 
|                         for (ShopCouponRecord record : recordList) { | 
|                             Map<String, Object> modifyMap = new HashMap<>(); | 
|                             modifyMap.put("id", record.getId()); | 
|                             modifyMap.put("isUsing", AppConstance.MY_COUPON_NOT_USE); | 
|                             modifyMap.put("orderId", 0); | 
|                             shopCouponRecordDao.updateByMap(modifyMap); | 
|                         } | 
|                     } | 
|                 } | 
|   | 
|                 //退款成功后加库存减销量 | 
|                 shopRefundRecord = shopRefundRecordDao.selectById(Integer.valueOf(id)); | 
|                 Integer orderId = shopRefundRecord.getOrderId(); | 
|                 Integer orderStatus = shopOrder.getOrderStatus(); | 
|                 Integer refundType = shopRefundRecord.getRefundType(); | 
|                 //物流、待收货、仅退款不要加库存减销量 | 
|                 ShopDeliveryInfo shopDeliveryInfo = shopDeliveryInfoDao.selectByOrderId(orderId); | 
|                 Boolean refundFlag = AppConstance.SHIPPING_METHOD_LOGISTING.equals("物流配送") && null != shopDeliveryInfo.getWaybillNo() && AppConstance.REFUND .equals(refundType) ; | 
|                 if(!refundFlag){ | 
|                     wxShopOrderService.updateStockAndVolumeById(orderId); | 
|                 } | 
|   | 
|                 //将订单表的订单状态改为退款成功,更新退款费用 | 
|                 Map<String, Object> modifyMap = new HashMap<>(); | 
|                 modifyMap.put("id", shopRefundRecord.getOrderId()); | 
|                 modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_MONEYBACK_SUCCESS); | 
|                 modifyMap.put("refundCharge", shopRefundRecord.getRefundMoney()); | 
|                 shopOrderDao.updateByMap(modifyMap); | 
|             }catch (Exception e){ | 
|                 LogUtil.debug("退款成功,修改退款表和订单表状态出错。。。", id); | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|         return flag; | 
|     } | 
|   | 
|     /** | 
|      * 退款成功后发送微信提醒 | 
|      * @param shopRefundRecord | 
|      * @return | 
|      */ | 
|     @Override | 
|     public int sendRefundInfoToUser(ShopRefundRecord shopRefundRecord) { | 
|         ShopOrder order = shopOrderDao.selectById(shopRefundRecord.getOrderId()); | 
|         if (order == null) { | 
|             throw new GlobleException("没有找到需要退款的订单信息"); | 
|         } | 
|         List<String> msg = new ArrayList<>(); | 
|         //退款说明 | 
|         String refundExplain = "退款已经原路返回,具体到账时间可能会有1-3天延迟"; | 
|         msg.add(order.getOrderNo()); | 
|         msg.add(DateUtil.dateToString(shopRefundRecord.getRefundTime(),DateUtil.DATE_FORMAT_SS)); | 
|         msg.add(refundExplain); | 
|         String formId = null; | 
|         if(AppConstance.REFUND_GOODS.equals(shopRefundRecord.getRefundType()) && null != shopRefundRecord.getRefundTransactionNo()){ | 
|             formId = shopRefundRecord.getRefundTransactionNo(); | 
|         }else { | 
|             formId = order.getWxOrderNo().split("=")[1]; | 
|         } | 
|         String page = "pages/refunDetail/refunDetail?id=" + shopRefundRecord.getId() + "&&status=1" + "&&inform=1"; | 
|         int res = WxTempLateMsgUtil.sendWxTemplateMsg(msg, order.getUserId(), | 
|                 page, WxTempLateMsgUtil.REFUND_SUCCESS, formId); | 
|         return res; | 
|     } | 
|   | 
|     @Override | 
|     public void updateGroupBuyStatus(Long orderId) { | 
|         ShopActivitiesGroupJoinUser joinUser = shopActivitiesGroupJoinUserDao.selectGroupJoinUserByOrderId(orderId); | 
|         if (joinUser != null) { | 
|             if (joinUser.getIsHead() == ShopActivitiesGroupJoinUser.USER_IS_HEAD_Y) { | 
|                 joinUser.setIsHasCancel(ShopActivitiesGroupJoinUser.IS_HAS_CANCEL_Y); | 
|                 shopActivitiesGroupJoinUserDao.updateByModel(joinUser); | 
|                 // TODO 这里的逻辑需认真查看 | 
|                 List<ShopActivitiesGroupJoinUser> list = shopActivitiesGroupJoinUserDao.selectGroupJoinUserByPayingAndUnCancel(joinUser.getGjId()); | 
|                 if (CollectionUtils.isEmpty(list)) { | 
|                     ShopActivitiesGroupJoin groupJoin = new ShopActivitiesGroupJoin(); | 
|                     groupJoin.setGjStatus(ShopActivitiesGroupJoin.ACTIVITIES_JOIN_FAIL); | 
|                     groupJoin.setId(joinUser.getGjId()); | 
|                     shopActivitiesGroupJoinDao.updateByModel(groupJoin); | 
|                 } else { | 
|                     ShopActivitiesGroupJoinUser newHead = list.get(0); | 
|                     newHead.setIsHead(ShopActivitiesGroupJoinUser.USER_IS_HEAD_Y); | 
|                     shopActivitiesGroupJoinUserDao.updateByModel(newHead); | 
|   | 
|                     ShopActivitiesGroupJoin groupJoin = new ShopActivitiesGroupJoin(); | 
|                     groupJoin.setId(newHead.getGjId()); | 
|                     groupJoin.setGjHeadId(newHead.getUserId()); | 
|                     shopActivitiesGroupJoinDao.updateByModel(groupJoin); | 
|                 } | 
|             } else { | 
|                 joinUser.setIsHasCancel(ShopActivitiesGroupJoinUser.IS_HAS_CANCEL_Y); | 
|                 shopActivitiesGroupJoinUserDao.updateByModel(joinUser); | 
|             } | 
|         } | 
|     } | 
| } |