| 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; | 
|     } | 
|   | 
| } |