jyy
2021-03-17 4e35547a189a77525b270f7a5ae6d425e71765ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.matrix.system.shopXcx.api.service.impl;
 
import com.matrix.component.rabbitmq.RabiitMqTemplate;
import com.matrix.component.tools.WxTempLateMsgUtil;
import com.matrix.component.wechat.externalInterface.weixinUtil.WeixinServiceUtil;
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.system.shopXcx.api.service.WXShopOrderService;
import com.matrix.system.shopXcx.api.service.WxShopRefundRecordService;
import com.matrix.system.shopXcx.bean.*;
import com.matrix.system.shopXcx.dao.*;
import com.matrix.system.shopXcx.mqTask.MQTaskRouting;
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;
    @Value("${evn}")
    private  String evn;
 
    @Autowired
    private RabiitMqTemplate rabiitMqTemplate;
 
 
    @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);
 
                rabiitMqTemplate.sendMsg(MQTaskRouting.SEND_UNIFORM_TEMPLATE_MSG+evn,shopOrder.getId()+"");
 
 
            }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);
            }
        }
    }
}