jyy
2021-03-19 382794b20c47d0f74317b2ac5bb7b6849986166f
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package com.matrix.system.shopXcx.api.action;
 
import com.matrix.biz.bean.BizUser;
import com.matrix.component.redis.RedisUserLoginUtils;
import com.matrix.component.tools.WxUtils;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.system.common.bean.CustomerDataDictionary;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.common.dao.CustomerDataDictionaryDao;
import com.matrix.system.shopXcx.api.service.WxShopRefundRecordService;
import com.matrix.system.shopXcx.api.tools.SMSTools;
import com.matrix.system.shopXcx.bean.ShopDeliveryInfo;
import com.matrix.system.shopXcx.bean.ShopOrder;
import com.matrix.system.shopXcx.bean.ShopRefundRecord;
import com.matrix.system.shopXcx.dao.ShopDeliveryInfoDao;
import com.matrix.system.shopXcx.dao.ShopOrderDao;
import com.matrix.system.shopXcx.dao.ShopRefundRecordDao;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
 
/**
 * @description 退货退款
 * @author jiangyouyao
 * @date 2019-06-16 15:15
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping(value = "wxapi/RefundRecord")
public class WxRefundRecordAction {
    @Autowired
    private RedisUserLoginUtils redisUserLoginUtils;
    @Autowired
    private ShopRefundRecordDao refundRecordDao;
    @Autowired
    private ShopOrderDao shopOrderDao;
    @Autowired
    private CustomerDataDictionaryDao dictionaryDao;
    @Autowired
    private ShopDeliveryInfoDao shopDeliveryInfoDao;
    @Autowired
    private WxShopRefundRecordService refundRecordService;
    @Autowired
    private SMSTools smsTools;
 
    @Autowired
    private BusParameterSettingsDao busParameterSettingsDao;
 
    @Autowired
    private CustomerDataDictionaryDao dataDictionaryDao;
 
 
 
    /**
     * 根据订单ID查询退款金额
     * @param
     * @return
     */
    @PostMapping("/getRefundMoneyByOrderId/{orderId}")
    @ResponseBody
    public AjaxResult getRefundMoneyByOrderId(@PathVariable("orderId") Integer orderId) {
        ShopOrder shopOrder = shopOrderDao.selectById(orderId);
        if(null == shopOrder){
            return new AjaxResult(AjaxResult.STATUS_FAIL, "该订单已不存在!");
        }
        ShopRefundRecord refundRecord = new ShopRefundRecord();
        //退款金额 = 商品价格 - 优惠总金额
        /*Double commodityPrice = shopOrder.getCommodityPrice();
        Double discountAmount = shopOrder.getDiscountAmount();*/
        //Double RefundMoney = commodityPrice - discountAmount;
        BigDecimal money = shopOrder.getOrderMoney();
 
 
        BigDecimal refundMoney = money;
        //如果商品未发货运费也退
        if(ShopOrder.ORDER_STATUS_WAIT_SEND == shopOrder.getOrderStatus()){
           /* Double postage = shopOrder.getPostage();
            BigDecimal b1 = new BigDecimal(Double.toString(postage));
            BigDecimal b2 = new BigDecimal(Double.toString(money));
            double sum= b1.add(b2).doubleValue();*/
            refundMoney = shopOrder.getOrderMoney() ;
        }
        refundRecord.setRefundMoney(refundMoney);
        refundRecord.setOrderId(orderId);
 
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(refundRecord));
        return result;
    }
 
    public Double processOrderMoney(Double orderMoney) {
        DecimalFormat decimalFormat = new DecimalFormat("0.#");
        decimalFormat.setRoundingMode(RoundingMode.UP);
        String moneyStr = decimalFormat.format(orderMoney);
        return Double.valueOf(moneyStr);
    }
 
    /**
     * 接收保存退款退货数据
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/saveRefundRecord")
    public @ResponseBody
    AjaxResult saveRefundRecord(@RequestBody ShopRefundRecord refundRecord) {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        refundRecord.setCreateBy(loginUser.getOpenId());
        refundRecord.setUpdateBy(loginUser.getOpenId());
        refundRecord.setUserId(loginUser.getOpenId());
        refundRecord.setCompanyId(loginUser.getCompanyId());
        refundRecord.setAuditStatus(AppConstance.REFUND_NOT_AUDITED);
        refundRecord.setRefundStatus(AppConstance.REFUND_PROCESSING);
        if(AppConstance.REFUND_GOODS .equals(refundRecord.getRefundType())){
            refundRecord.setRefundGoodsStatus(AppConstance.UNRECEIVED_GOODS);
        }
        refundRecord.setRefundNo(WxUtils.getOrderNum());
        refundRecord.setApplyTime(new Date());
 
        ShopRefundRecord record = new ShopRefundRecord();
        record.setUserId(loginUser.getOpenId());
        record.setOrderId(refundRecord.getOrderId());
        List<ShopRefundRecord> shopRefundRecords = refundRecordDao.selectByModel(record);
        if(CollectionUtils.isNotEmpty(shopRefundRecords)){
            return new AjaxResult(AjaxResult.STATUS_FAIL, "该订单已发起过退款,请勿重复操作!");
        }else {
            int i = refundRecordDao.insert(refundRecord);
 
            if (i == 0) {
                return new AjaxResult(AjaxResult.STATUS_FAIL, "保存失败");
            }
 
            //未发货的订单取消订单不走后台审核直接退款
            List<ShopRefundRecord> refundRecordsList = refundRecordDao.selectByModel(record);
            if(CollectionUtils.isNotEmpty(refundRecordsList)){
                ShopRefundRecord shopRecord = refundRecordsList.get(0);
                Integer orderId = shopRecord.getOrderId();
                ShopOrder order = shopOrderDao.selectById(orderId);
                if (order == null) {
                    return new AjaxResult(AjaxResult.STATUS_FAIL, "未发货直接退款未找到订单信息");
                }
                //未发货
                Boolean flag = false;
                if(ShopOrder.ORDER_STATUS_WAIT_SEND == order.getOrderStatus()){
 
                    //调用退款接口进行退款
                    try{
                        flag = refundRecordService.refundToUser(shopRecord.getId().toString(), shopRecord);
                    }catch (Exception e) {
                        LogUtil.info("退款错误日志----:"+e);
                        return new AjaxResult(AjaxResult.STATUS_FAIL, "退款失败");
                    }
 
                    if(flag){
                        ShopRefundRecord fundRecord = refundRecordDao.selectById(shopRecord.getId());
                        refundRecordService.sendRefundInfoToUser(fundRecord);
                        refundRecordService.updateGroupBuyStatus(orderId.longValue());
                        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "退款成功");
                    }
                    return new AjaxResult(AjaxResult.STATUS_FAIL, "退款失败");
                }
            }
 
            //申请退款成功后将订单表的订单状态改为申请退款:6
            Integer orderId = refundRecord.getOrderId();
            Map<String, Object> modifyMap = new HashMap<>();
            modifyMap.put("id", orderId);
            modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_APPLY_MONEYBACK);
            shopOrderDao.updateByMap(modifyMap);
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, "保存成功");
        }
 
    }
 
    /**
     * 根据用户ID查询退款列表
     * @param
     * @return
     */
    @PostMapping("/findRefundRecord")
    @ResponseBody
    public AjaxResult getRefundRecordByUserId(@RequestBody ShopRefundRecord refundRecord) {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        String userId = loginUser.getOpenId();
        refundRecord.setUserId(userId);
        List<ShopRefundRecord> list = refundRecordDao.selectByRefundUserId(refundRecord);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, list, list.size());
        return result;
    }
 
    /**
     * 退款详情查询
     * @param
     * @return
     */
    @PostMapping("/findRefundRecordById/{id}")
    @ResponseBody
    public AjaxResult findRefundRecordById(@PathVariable("id") Integer id) {
        ShopRefundRecord shopRefundRecord = refundRecordDao.selectRefundById(id);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(shopRefundRecord));
        return result;
    }
 
    /**
     * 填写物流单号接口
     * @param
     * @return
     */
    @PostMapping("/updateRefundWaybillNo")
    @ResponseBody
    public AjaxResult updateRefundWaybillNo(@RequestBody ShopRefundRecord refundRecord) {
        Map<String, Object> modifyMap = new HashMap<>();
        ShopRefundRecord shopRefundRecord = refundRecordDao.selectById(refundRecord.getId());
        if(null != shopRefundRecord){
            if(AppConstance.REFUND_GOODS .equals(shopRefundRecord.getRefundType())){
                modifyMap.put("id", refundRecord.getId());
                modifyMap.put("refundWaybillNo", refundRecord.getRefundWaybillNo());
                modifyMap.put("logisticsCompany", refundRecord.getLogisticsCompany());
                modifyMap.put("refundTransactionNo", refundRecord.getRefundTransactionNo());
            }else {
                return new AjaxResult(AjaxResult.STATUS_FAIL, "只有退货退款类型才能添加物流单号");
            }
        }else {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "无此条退货退款数据");
 
        }
        int flag = 0;
        if (modifyMap.size() > 0) {
            flag = refundRecordDao.updateByMap(modifyMap);
        }
        if (flag == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "保存失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "保存成功");
    }
 
    /**
     * 取消退款接口
     * @param
     * @return
     */
    @PostMapping("/cancelRefund/{id}")
    @ResponseBody
    public AjaxResult cancelRefund(@PathVariable("id") Integer id) {
        //将订单状态改为已取消
        Map<String, Object> modifyMap = new HashMap<>();
        ShopRefundRecord shopRefundRecord = refundRecordDao.selectById(id);
        Integer orderId = shopRefundRecord.getOrderId();
        ShopDeliveryInfo shopDeliveryInfo = shopDeliveryInfoDao.selectByOrderId(orderId);
        ShopOrder order = shopOrderDao.selectById(orderId);
        //如果运单号不等于null就将订单状态改为待收货3,为null就改为待配送2
        if(null != order){
            //如果是门店自提就将订单状态改为待收货3
            if(AppConstance.SHIPPING_METHOD_SELF .equals(order.getShippingMethod())){
                modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_RECEIVE);
            }
            if(AppConstance.SHIPPING_METHOD_LOGISTING .equals(order.getShippingMethod())  && null != shopDeliveryInfo.getWaybillNo()){
                modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_RECEIVE);
            }
            if(AppConstance.SHIPPING_METHOD_LOGISTING .equals(order.getShippingMethod()) && null == shopDeliveryInfo.getWaybillNo()){
                modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_SEND);
            }
            modifyMap.put("id", orderId);
            int i = shopOrderDao.updateByMap(modifyMap);
        }
        //将退款表申请退款状态改为用户已取消
        Map<String, Object> refundMap = new HashMap<>();
        refundMap.put("id", id);
        refundMap.put("refundStatus", AppConstance.REFUND_USER_CANCEL);
        int flag = refundRecordDao.updateByMap(refundMap);
        if (flag == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "取消退款失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "取消退款成功");
    }
 
    /**
     * 查询退款原因接口(数据字典维护)
     * @param
     * @return
     */
    @PostMapping("/findRefundReason")
    @ResponseBody
    public AjaxResult findRefundReason() {
        String parentCode = "refund_reason";
 
        List<CustomerDataDictionary> dataDictionaries = dataDictionaryDao.selectByParentCode(parentCode,17L);
 
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataDictionaries);
        return result;
    }
 
}