package com.matrix.system.shopXcx.api.action; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.tools.LogUtil; import com.matrix.biz.bean.BizUser; import com.matrix.system.common.bean.BusParameterSettings; import com.matrix.system.common.bean.CustomerDataDictionary; import com.matrix.system.common.dao.BusParameterSettingsDao; import com.matrix.system.common.dao.CustomerDataDictionaryDao; import com.matrix.component.redis.RedisUserLoginUtils; import com.matrix.system.common.constance.AppConstance; 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 com.matrix.component.tools.WxUtils; import com.matrix.system.shopXcx.api.service.WxShopRefundRecordService; import com.matrix.system.shopXcx.api.tools.SMSTools; 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 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 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 modifyMap = new HashMap<>(); modifyMap.put("id", orderId); modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_APPLY_MONEYBACK); shopOrderDao.updateByMap(modifyMap); //从数据字典获取需要发送的手机号码和短信提醒内容 BusParameterSettings busParameterSetting1 =busParameterSettingsDao.selectCompanyParamByCode("MSG_ALERT_CONTENT_REFUND",17L); String msg = busParameterSetting1.getParamValue(); //TODO 写死微商城17L公司id BusParameterSettings busParameterSetting= busParameterSettingsDao.selectCompanyParamByCode("MSG_ALERT_MOBILE",17L); String phons = busParameterSetting.getParamValue(); phons.replace(',',','); List result = Arrays.asList(phons.split(",")); if(CollectionUtils.isNotEmpty(result)){ for(String phon : result){ smsTools.sendMsg(phon, msg + "退订回T" ); } } 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 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 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 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 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 dataDictionaries = dataDictionaryDao.selectByParentCode(parentCode,17L); AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataDictionaries); return result; } }