子订单退款
This commit is contained in:
parent
bd5dbb500d
commit
29a09a7a58
|
|
@ -102,8 +102,7 @@ public class OrderController extends BaseController {
|
||||||
private IAfterServiceRecordService afterServiceRecordService;
|
private IAfterServiceRecordService afterServiceRecordService;
|
||||||
@Resource
|
@Resource
|
||||||
private OrderBehaviorService orderBehaviorService;
|
private OrderBehaviorService orderBehaviorService;
|
||||||
|
@Resource
|
||||||
@Autowired
|
|
||||||
private IWxMsgService wxMsgService;
|
private IWxMsgService wxMsgService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1026,13 +1025,6 @@ public class OrderController extends BaseController {
|
||||||
// 查询子单加价记录
|
// 查询子单加价记录
|
||||||
FinancialChangeRecord financialChangeRecord = financialChangeRecordService.selectNotPayRecordByDetailId(detail.getId());
|
FinancialChangeRecord financialChangeRecord = financialChangeRecordService.selectNotPayRecordByDetailId(detail.getId());
|
||||||
BigDecimal changeMoney = financialChangeRecord == null ? BigDecimal.ZERO : financialChangeRecord.getChangeMoney();
|
BigDecimal changeMoney = financialChangeRecord == null ? BigDecimal.ZERO : financialChangeRecord.getChangeMoney();
|
||||||
// 查询子单加价记录
|
|
||||||
// FinancialChangeRecord changeRecordQry = new FinancialChangeRecord();
|
|
||||||
// changeRecordQry.setOrderDetailId(detail.getId());
|
|
||||||
// List<FinancialChangeRecord> financialChangeRecords = financialChangeRecordService.selectFinancialChangeRecordList(changeRecordQry);
|
|
||||||
// if (CollectionUtils.isNotEmpty(financialChangeRecords)) {
|
|
||||||
// detailPayMoney = detailPayMoney.add(financialChangeRecords.get(0).getChangeMoney());
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 地址信息
|
// 地址信息
|
||||||
CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
|
CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
|
||||||
|
|
@ -1051,7 +1043,7 @@ public class OrderController extends BaseController {
|
||||||
|
|
||||||
// 查询是否超时
|
// 查询是否超时
|
||||||
OrderTimeoutRecord orderTimeoutRecord = orderFineRecordService.selectByDetailIdAndStatus(detail.getId(), detail.getOrderStatus());
|
OrderTimeoutRecord orderTimeoutRecord = orderFineRecordService.selectByDetailIdAndStatus(detail.getId(), detail.getOrderStatus());
|
||||||
Boolean isOverTime = orderTimeoutRecord == null ? false : true;
|
Boolean isOverTime = orderTimeoutRecord != null;
|
||||||
|
|
||||||
if (this.checkIsOnlyServer(orderMaster.getId(), detail.getWorkerId())) {
|
if (this.checkIsOnlyServer(orderMaster.getId(), detail.getWorkerId())) {
|
||||||
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(detail.getOrderMasterId());
|
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(detail.getOrderMasterId());
|
||||||
|
|
@ -1260,7 +1252,7 @@ public class OrderController extends BaseController {
|
||||||
|
|
||||||
// 查询是否超时
|
// 查询是否超时
|
||||||
OrderTimeoutRecord orderTimeoutRecord = orderFineRecordService.selectByDetailIdAndStatus(detail.getId(), detail.getOrderStatus());
|
OrderTimeoutRecord orderTimeoutRecord = orderFineRecordService.selectByDetailIdAndStatus(detail.getId(), detail.getOrderStatus());
|
||||||
Boolean isOverTime = orderTimeoutRecord == null ? false : true;
|
Boolean isOverTime = orderTimeoutRecord != null;
|
||||||
if (Boolean.TRUE.equals(orderDetail.getIsOverTime()) && !isOverTime) {
|
if (Boolean.TRUE.equals(orderDetail.getIsOverTime()) && !isOverTime) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -1281,7 +1273,7 @@ public class OrderController extends BaseController {
|
||||||
orderListResponse.setTotalMoney(financialDetail.getTotalMoney());
|
orderListResponse.setTotalMoney(financialDetail.getTotalMoney());
|
||||||
orderListResponse.setPayMoney(financialDetail.getPayMoney());
|
orderListResponse.setPayMoney(financialDetail.getPayMoney());
|
||||||
orderListResponse.setWorkerName(worker.getName());
|
orderListResponse.setWorkerName(worker.getName());
|
||||||
orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
|
orderListResponse.setWorkerPhone(worker.getPhone());
|
||||||
orderListResponse.setWorkerId(detail.getWorkerId());
|
orderListResponse.setWorkerId(detail.getWorkerId());
|
||||||
orderListResponse.setCustomerName(customerAddress.getName());
|
orderListResponse.setCustomerName(customerAddress.getName());
|
||||||
orderListResponse.setCustomerPhone(customerAddress.getPhone());
|
orderListResponse.setCustomerPhone(customerAddress.getPhone());
|
||||||
|
|
|
||||||
|
|
@ -739,8 +739,8 @@ public class OrderDetailController extends BaseController {
|
||||||
*/
|
*/
|
||||||
@PostMapping("/cancel")
|
@PostMapping("/cancel")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public AjaxResult cancel(Long orderDetailId) {
|
public AjaxResult applyForCancel(Long orderDetailId) throws BaseAdaPayException {
|
||||||
orderDetailService.cancel(orderDetailId);
|
orderDetailService.applyForCancel(orderDetailId);
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -752,7 +752,7 @@ public class OrderDetailController extends BaseController {
|
||||||
*/
|
*/
|
||||||
@PostMapping("/cancel/agree")
|
@PostMapping("/cancel/agree")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public AjaxResult cancelAgree(Long orderDetailId, Integer agree) {
|
public AjaxResult cancelAgree(Long orderDetailId, Integer agree) throws BaseAdaPayException {
|
||||||
orderDetailService.cancelAgree(orderDetailId, agree);
|
orderDetailService.cancelAgree(orderDetailId, agree);
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package com.ghy.web.controller.order;
|
package com.ghy.web.controller.order;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
|
||||||
import com.ghy.common.annotation.Log;
|
import com.ghy.common.annotation.Log;
|
||||||
import com.ghy.common.constant.UserConstants;
|
import com.ghy.common.constant.UserConstants;
|
||||||
import com.ghy.common.core.controller.BaseController;
|
import com.ghy.common.core.controller.BaseController;
|
||||||
|
|
@ -9,7 +7,8 @@ import com.ghy.common.core.domain.AjaxResult;
|
||||||
import com.ghy.common.core.page.PageDomain;
|
import com.ghy.common.core.page.PageDomain;
|
||||||
import com.ghy.common.core.page.TableDataInfo;
|
import com.ghy.common.core.page.TableDataInfo;
|
||||||
import com.ghy.common.core.page.TableSupport;
|
import com.ghy.common.core.page.TableSupport;
|
||||||
import com.ghy.common.enums.*;
|
import com.ghy.common.enums.BusinessType;
|
||||||
|
import com.ghy.common.enums.ImgType;
|
||||||
import com.ghy.common.utils.ExceptionUtil;
|
import com.ghy.common.utils.ExceptionUtil;
|
||||||
import com.ghy.common.utils.StringUtils;
|
import com.ghy.common.utils.StringUtils;
|
||||||
import com.ghy.common.utils.poi.ExcelUtil;
|
import com.ghy.common.utils.poi.ExcelUtil;
|
||||||
|
|
@ -35,7 +34,10 @@ import com.ghy.payment.domain.FinancialChangeRecord;
|
||||||
import com.ghy.payment.domain.FinancialDetail;
|
import com.ghy.payment.domain.FinancialDetail;
|
||||||
import com.ghy.payment.domain.FinancialMaster;
|
import com.ghy.payment.domain.FinancialMaster;
|
||||||
import com.ghy.payment.domain.OrderTimeoutRecord;
|
import com.ghy.payment.domain.OrderTimeoutRecord;
|
||||||
import com.ghy.payment.service.*;
|
import com.ghy.payment.service.FinancialChangeRecordService;
|
||||||
|
import com.ghy.payment.service.FinancialDetailService;
|
||||||
|
import com.ghy.payment.service.FinancialMasterService;
|
||||||
|
import com.ghy.payment.service.OrderFineRecordService;
|
||||||
import com.ghy.system.domain.SysArea;
|
import com.ghy.system.domain.SysArea;
|
||||||
import com.ghy.system.service.ISysAreaService;
|
import com.ghy.system.service.ISysAreaService;
|
||||||
import com.ghy.web.pojo.vo.OrderListResponse;
|
import com.ghy.web.pojo.vo.OrderListResponse;
|
||||||
|
|
@ -50,15 +52,12 @@ import com.huifu.adapay.core.exception.BaseAdaPayException;
|
||||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
@ -80,8 +79,6 @@ public class OrderMasterController extends BaseController {
|
||||||
@Resource
|
@Resource
|
||||||
private GoodsAreaService goodsAreaService;
|
private GoodsAreaService goodsAreaService;
|
||||||
@Resource
|
@Resource
|
||||||
private AdapayService adapayService;
|
|
||||||
@Resource
|
|
||||||
private WorkerService workerService;
|
private WorkerService workerService;
|
||||||
@Resource
|
@Resource
|
||||||
private IWorkerCertificationService workerCertificationService;
|
private IWorkerCertificationService workerCertificationService;
|
||||||
|
|
@ -737,7 +734,7 @@ public class OrderMasterController extends BaseController {
|
||||||
*/
|
*/
|
||||||
@PostMapping("/cancel")
|
@PostMapping("/cancel")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public AjaxResult cancel(Long orderMasterId) {
|
public AjaxResult cancel(Long orderMasterId) throws BaseAdaPayException {
|
||||||
orderMasterService.cancel(orderMasterId);
|
orderMasterService.cancel(orderMasterId);
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
@ -833,77 +830,4 @@ public class OrderMasterController extends BaseController {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 撤销支付(适用于延迟分账的订单)
|
|
||||||
*
|
|
||||||
* @param orderMasterId 主订单ID
|
|
||||||
*/
|
|
||||||
@PostMapping("reverse")
|
|
||||||
@ResponseBody
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public AjaxResult reverse(Long orderMasterId) throws BaseAdaPayException {
|
|
||||||
OrderMaster orderMaster = orderMasterService.selectById(orderMasterId);
|
|
||||||
Assert.notNull(orderMaster, "找不到对应的订单");
|
|
||||||
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMasterId);
|
|
||||||
Assert.notNull(financialMaster, "找不到订单");
|
|
||||||
Assert.isTrue(!PayStatus.WAIT_PAY.getCode().equals(financialMaster.getPayStatus()), "订单未支付");
|
|
||||||
Assert.isTrue(!PayStatus.CANCEL.getCode().equals(financialMaster.getPayStatus()), "订单已取消");
|
|
||||||
Assert.isTrue(!PayStatus.REFUND.getCode().equals(financialMaster.getPayStatus()), "订单已退款");
|
|
||||||
boolean paid = PayStatus.PAID.getCode().equals(financialMaster.getPayStatus()) ||
|
|
||||||
PayStatus.PAYED_ADD.getCode().equals(financialMaster.getPayStatus());
|
|
||||||
Assert.isTrue(paid, "订单未支付");
|
|
||||||
Assert.hasText(financialMaster.getPaymentId(), "找不到订单的支付记录,请联系管理员");
|
|
||||||
|
|
||||||
// 改主订单的订单状态和支付状态
|
|
||||||
orderMasterService.updateStatus(orderMasterId, OrderStatus.CANCEL.code());
|
|
||||||
orderMasterService.updatePayStatus(orderMasterId, PayStatus.REFUND.getCode());
|
|
||||||
// 改子订单的订单状态和支付状态
|
|
||||||
OrderDetail odUpdate = new OrderDetail();
|
|
||||||
odUpdate.setOrderMasterId(orderMasterId);
|
|
||||||
odUpdate.setPayStatus(PayStatus.REFUND.getCode());
|
|
||||||
odUpdate.setOrderStatus(OrderStatus.CANCEL.code());
|
|
||||||
orderDetailService.updateByOrderMasterId(odUpdate);
|
|
||||||
|
|
||||||
// 主订单金额=订单原价+加价
|
|
||||||
BigDecimal payMoney = financialMaster.getPayMoney();
|
|
||||||
List<FinancialChangeRecord> fcrList = financialChangeRecordService.selectByMasterId(orderMasterId);
|
|
||||||
for (FinancialChangeRecord fcr : fcrList) {
|
|
||||||
// 把已支付的加价单退了 并从主单金额里减去
|
|
||||||
if (PayStatus.PAID.getCode().equals(fcr.getPayStatus())) {
|
|
||||||
financialChangeRecordService.refund(orderMaster.getDeptId(), fcr);
|
|
||||||
payMoney = payMoney.subtract(fcr.getChangeMoney());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
List<FinancialDetail> fdList = financialDetailService.selectByFinancialMasterId(financialMaster.getId());
|
|
||||||
for (FinancialDetail fd : fdList) {
|
|
||||||
// 如果有已退款或正在退款的子财务单 从主单金额里减去
|
|
||||||
if (PayStatus.REFUND.getCode().equals(fd.getPayStatus()) ||
|
|
||||||
PayStatus.REFUNDING.getCode().equals(fd.getPayStatus()) ||
|
|
||||||
PayStatus.REVERSING.getCode().equals(fd.getPayStatus())) {
|
|
||||||
payMoney = payMoney.subtract(fd.getPayMoney());
|
|
||||||
} else if (PayStatus.PAID.getCode().equals(fd.getPayStatus()) ||
|
|
||||||
PayStatus.PAYED_ADD.getCode().equals(fd.getPayStatus())) {
|
|
||||||
// 修改子单状态
|
|
||||||
financialDetailService.updatePayStatus(fd.getId(), PayStatus.REVERSING.getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payMoney.compareTo(BigDecimal.ZERO) < 1) {
|
|
||||||
logger.info("无需退款 reverseAmt={}", payMoney);
|
|
||||||
}
|
|
||||||
|
|
||||||
String reverseAmt = payMoney.setScale(2, RoundingMode.DOWN).toString();
|
|
||||||
JSONObject response = adapayService.payReverse(orderMaster.getDeptId(), financialMaster.getPaymentId(), reverseAmt, RefundType.ROM);
|
|
||||||
if (AdapayStatusEnum.succeeded.code.equals(response.getString("status")) ||
|
|
||||||
AdapayStatusEnum.pending.code.equals(response.getString("status"))) {
|
|
||||||
// 主订单退款 直接修改主财务单状态为退款中
|
|
||||||
String reverseId = response.getString("id");
|
|
||||||
financialMaster.setPayStatus(PayStatus.REVERSING.getCode());
|
|
||||||
financialMaster.setReverseId(reverseId);
|
|
||||||
financialMasterService.updateFinancialMaster(financialMaster);
|
|
||||||
} else {
|
|
||||||
logger.error("FM[{}]撤销支付失败: {}", financialMaster.getId(), response.toJSONString());
|
|
||||||
}
|
|
||||||
return AjaxResult.success();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ public interface OrderDetailService {
|
||||||
*
|
*
|
||||||
* @param orderDetailId 子订单ID
|
* @param orderDetailId 子订单ID
|
||||||
*/
|
*/
|
||||||
void cancel(Long orderDetailId);
|
void applyForCancel(Long orderDetailId) throws BaseAdaPayException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 师傅审核取消子订单申请
|
* 师傅审核取消子订单申请
|
||||||
|
|
@ -87,7 +87,7 @@ public interface OrderDetailService {
|
||||||
* @param orderDetailId 子订单ID
|
* @param orderDetailId 子订单ID
|
||||||
* @param agree 0=不同意 1=同意
|
* @param agree 0=不同意 1=同意
|
||||||
*/
|
*/
|
||||||
void cancelAgree(Long orderDetailId, Integer agree);
|
void cancelAgree(Long orderDetailId, Integer agree) throws BaseAdaPayException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过主订单ID批量更新子订单
|
* 通过主订单ID批量更新子订单
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ public interface OrderMasterService {
|
||||||
*
|
*
|
||||||
* @param orderMasterId 主订单ID
|
* @param orderMasterId 主订单ID
|
||||||
*/
|
*/
|
||||||
void cancel(Long orderMasterId);
|
void cancel(Long orderMasterId) throws BaseAdaPayException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 师傅同意取消主订单
|
* 师傅同意取消主订单
|
||||||
|
|
|
||||||
|
|
@ -211,7 +211,7 @@ public class OrderDetailServiceImpl implements OrderDetailService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void cancel(Long orderDetailId) {
|
public void applyForCancel(Long orderDetailId) throws BaseAdaPayException {
|
||||||
OrderDetail orderDetail = selectById(orderDetailId);
|
OrderDetail orderDetail = selectById(orderDetailId);
|
||||||
Assert.notNull(orderDetail, "找不到对应的子订单");
|
Assert.notNull(orderDetail, "找不到对应的子订单");
|
||||||
|
|
||||||
|
|
@ -236,11 +236,7 @@ public class OrderDetailServiceImpl implements OrderDetailService {
|
||||||
case 1: // 待接单和待排期状态可直接取消
|
case 1: // 待接单和待排期状态可直接取消
|
||||||
orderDetailMapper.updateStatus(orderDetailId, OrderStatus.CANCEL.code());
|
orderDetailMapper.updateStatus(orderDetailId, OrderStatus.CANCEL.code());
|
||||||
// 发起退款
|
// 发起退款
|
||||||
try {
|
|
||||||
refund(orderDetail);
|
refund(orderDetail);
|
||||||
} catch (BaseAdaPayException e) {
|
|
||||||
logger.error("子订单[code={}]退款失败", orderDetail.getCode(), e);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 2: // 待上门状态需要师傅同意才能取消
|
case 2: // 待上门状态需要师傅同意才能取消
|
||||||
orderDetailMapper.updateStatus(orderDetailId, OrderStatus.GOING_CANCEL.code());
|
orderDetailMapper.updateStatus(orderDetailId, OrderStatus.GOING_CANCEL.code());
|
||||||
|
|
@ -256,37 +252,63 @@ public class OrderDetailServiceImpl implements OrderDetailService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子单全额退款
|
||||||
|
*
|
||||||
|
* @param orderDetail
|
||||||
|
* @throws BaseAdaPayException
|
||||||
|
*/
|
||||||
private void refund(OrderDetail orderDetail) throws BaseAdaPayException {
|
private void refund(OrderDetail orderDetail) throws BaseAdaPayException {
|
||||||
FinancialDetail financialDetail = financialDetailService.selectByOrderDetailId(orderDetail.getId());
|
Long orderDetailId = orderDetail.getId();
|
||||||
BigDecimal payMoney = financialDetail.getPayMoney();
|
OrderMaster orderMaster = orderMasterMapper.selectById(orderDetail.getOrderMasterId());
|
||||||
|
Assert.notNull(orderMaster, "OrderMaster is null !");
|
||||||
|
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMaster.getId());
|
||||||
|
Assert.notNull(financialMaster, "FinancialMaster is null !");
|
||||||
|
|
||||||
// 把子订单对应的子财务单状态更新为已退款
|
// 退款金额
|
||||||
FinancialDetail update = new FinancialDetail();
|
BigDecimal refundMoney = BigDecimal.ZERO;
|
||||||
update.setId(financialDetail.getId());
|
|
||||||
update.setPayStatus(PayStatus.REFUND.getCode());
|
|
||||||
financialDetailService.updateFinancialDetail(update);
|
|
||||||
|
|
||||||
if (BigDecimal.ZERO.compareTo(payMoney) > -1) {
|
List<FinancialChangeRecord> fcrList = financialChangeRecordService.selectByDetailIds(orderDetailId.toString());
|
||||||
logger.info("子订单[code={}]待退款金额<=0,不需要退款", orderDetail.getCode());
|
// 先把这个子单的加价单全退了
|
||||||
|
for (FinancialChangeRecord fcr : fcrList) {
|
||||||
|
financialChangeRecordService.refund(orderDetail.getDeptId(), fcr);
|
||||||
|
// 子单对应的财务单里也加上了加价单的金额 所以这里减掉
|
||||||
|
refundMoney = refundMoney.subtract(fcr.getChangeMoney());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
List<FinancialDetail> fdList = financialDetailService.selectListByOrderDetailId(orderDetailId);
|
||||||
|
for (FinancialDetail fd : fdList) {
|
||||||
|
if (PayStatus.REFUND.getCode().equals(fd.getPayStatus()) ||
|
||||||
|
PayStatus.REFUNDING.getCode().equals(fd.getPayStatus()) ||
|
||||||
|
PayStatus.REVERSING.getCode().equals(fd.getPayStatus())) {
|
||||||
|
// 减去退款中和已退款的子财务单金额
|
||||||
|
refundMoney = refundMoney.subtract(fd.getPayMoney());
|
||||||
|
} else if (PayStatus.PAID.getCode().equals(fd.getPayStatus())) {
|
||||||
|
refundMoney = refundMoney.add(fd.getPayMoney());
|
||||||
|
}
|
||||||
|
financialDetailService.updatePayStatus(fd.getId(), PayStatus.REFUND.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BigDecimal.ZERO.compareTo(refundMoney) > -1) {
|
||||||
|
logger.info("子订单[id={}]待退款金额<=0,不需要退款", orderDetailId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发起退款
|
// 发起退款
|
||||||
OrderMaster orderMaster = orderMasterMapper.selectById(orderDetail.getOrderMasterId());
|
String refundAmt = AdapayUtils.bigDecimalToString(refundMoney);
|
||||||
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMaster.getId());
|
|
||||||
String refundAmt = AdapayUtils.bigDecimalToString(payMoney);
|
|
||||||
JSONObject response = adapayService.payReverse(orderMaster.getDeptId(), financialMaster.getPaymentId(), refundAmt, RefundType.ROD);
|
JSONObject response = adapayService.payReverse(orderMaster.getDeptId(), financialMaster.getPaymentId(), refundAmt, RefundType.ROD);
|
||||||
logger.debug("子订单退款结果: " + response.toJSONString());
|
logger.debug("子订单退款结果: " + response.toJSONString());
|
||||||
boolean status = AdapayStatusEnum.pending.code.equals(response.getString("status")) ||
|
boolean status = AdapayStatusEnum.pending.code.equals(response.getString("status")) ||
|
||||||
AdapayStatusEnum.succeeded.code.equals(response.getString("status"));
|
AdapayStatusEnum.succeeded.code.equals(response.getString("status"));
|
||||||
// 如果退款失败 这里携带失败原因抛出异常 回滚订单状态
|
// 如果退款失败 这里携带失败原因抛出异常 回滚订单状态
|
||||||
Assert.isTrue(status, response.getString("error_msg"));
|
Assert.isTrue(status, response.getString("error_msg"));
|
||||||
logger.info("子订单[code={}]退款成功", orderDetail.getCode());
|
logger.info("子订单[id={}]退款成功", orderDetailId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void cancelAgree(Long orderDetailId, Integer agree) {
|
public void cancelAgree(Long orderDetailId, Integer agree) throws BaseAdaPayException {
|
||||||
OrderDetail orderDetail = selectById(orderDetailId);
|
OrderDetail orderDetail = selectById(orderDetailId);
|
||||||
Assert.notNull(orderDetail, "找不到对应的子订单");
|
Assert.notNull(orderDetail, "找不到对应的子订单");
|
||||||
if (Integer.valueOf(1).equals(agree)) {
|
if (Integer.valueOf(1).equals(agree)) {
|
||||||
|
|
@ -294,11 +316,7 @@ public class OrderDetailServiceImpl implements OrderDetailService {
|
||||||
// 修改主订单状态为已取消
|
// 修改主订单状态为已取消
|
||||||
orderDetailMapper.updateStatus(orderDetailId, OrderStatus.CANCEL.code());
|
orderDetailMapper.updateStatus(orderDetailId, OrderStatus.CANCEL.code());
|
||||||
// 发起退款
|
// 发起退款
|
||||||
try {
|
|
||||||
refund(orderDetail);
|
refund(orderDetail);
|
||||||
} catch (BaseAdaPayException e) {
|
|
||||||
logger.error("子订单[code={}]退款失败", orderDetail.getCode(), e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 师傅不同意取消订单
|
// 师傅不同意取消订单
|
||||||
Integer orderStatus = orderDetail.getOrderStatus();
|
Integer orderStatus = orderDetail.getOrderStatus();
|
||||||
|
|
@ -528,7 +546,8 @@ public class OrderDetailServiceImpl implements OrderDetailService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int changePrice(Long orderDetailId, BigDecimal changeMoney, Integer type, String remark) throws Exception {
|
public int changePrice(Long orderDetailId, BigDecimal changeMoney, Integer type, String remark) throws
|
||||||
|
Exception {
|
||||||
OrderDetail orderDetail = orderDetailMapper.selectById(orderDetailId);
|
OrderDetail orderDetail = orderDetailMapper.selectById(orderDetailId);
|
||||||
if (orderDetail == null) {
|
if (orderDetail == null) {
|
||||||
throw new BaseException("子单不存在!");
|
throw new BaseException("子单不存在!");
|
||||||
|
|
@ -556,12 +575,13 @@ public class OrderDetailServiceImpl implements OrderDetailService {
|
||||||
// 创建改单记录
|
// 创建改单记录
|
||||||
financialChangeRecord = new FinancialChangeRecord();
|
financialChangeRecord = new FinancialChangeRecord();
|
||||||
financialChangeRecord.setOrderDetailId(orderDetailId);
|
financialChangeRecord.setOrderDetailId(orderDetailId);
|
||||||
|
financialChangeRecord.setOrderMasterId(orderDetail.getOrderMasterId());
|
||||||
financialChangeRecord.setChangeMoney(changeMoney);
|
financialChangeRecord.setChangeMoney(changeMoney);
|
||||||
financialChangeRecord.setStatus(0);
|
financialChangeRecord.setStatus(0);
|
||||||
financialChangeRecord.setPayStatus(0);
|
financialChangeRecord.setPayStatus(0);
|
||||||
financialChangeRecord.setType(type);
|
financialChangeRecord.setType(type);
|
||||||
financialChangeRecord.setRemark(remark);
|
financialChangeRecord.setRemark(remark);
|
||||||
affectedRows = financialChangeRecordService.insertFinancialChangeRecord(financialChangeRecord);
|
affectedRows = financialChangeRecordService.insert(financialChangeRecord);
|
||||||
}
|
}
|
||||||
if (changeMoney.compareTo(BigDecimal.ZERO) != 0 && affectedRows != 1) {
|
if (changeMoney.compareTo(BigDecimal.ZERO) != 0 && affectedRows != 1) {
|
||||||
throw new Exception("加价记录新增或者修改失败");
|
throw new Exception("加价记录新增或者修改失败");
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,11 @@ import com.ghy.order.mapper.OrderMasterMapper;
|
||||||
import com.ghy.order.request.AppOrderRequest;
|
import com.ghy.order.request.AppOrderRequest;
|
||||||
import com.ghy.order.service.OrderDetailService;
|
import com.ghy.order.service.OrderDetailService;
|
||||||
import com.ghy.order.service.OrderMasterService;
|
import com.ghy.order.service.OrderMasterService;
|
||||||
|
import com.ghy.payment.domain.FinancialChangeRecord;
|
||||||
import com.ghy.payment.domain.FinancialDetail;
|
import com.ghy.payment.domain.FinancialDetail;
|
||||||
import com.ghy.payment.domain.FinancialMaster;
|
import com.ghy.payment.domain.FinancialMaster;
|
||||||
import com.ghy.payment.service.AdapayService;
|
import com.ghy.payment.service.AdapayService;
|
||||||
|
import com.ghy.payment.service.FinancialChangeRecordService;
|
||||||
import com.ghy.payment.service.FinancialDetailService;
|
import com.ghy.payment.service.FinancialDetailService;
|
||||||
import com.ghy.payment.service.FinancialMasterService;
|
import com.ghy.payment.service.FinancialMasterService;
|
||||||
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
||||||
|
|
@ -30,6 +32,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
@ -56,6 +59,8 @@ public class OrderMasterServiceImpl implements OrderMasterService {
|
||||||
private FinancialMasterService financialMasterService;
|
private FinancialMasterService financialMasterService;
|
||||||
@Resource
|
@Resource
|
||||||
private FinancialDetailService financialDetailService;
|
private FinancialDetailService financialDetailService;
|
||||||
|
@Resource
|
||||||
|
private FinancialChangeRecordService financialChangeRecordService;
|
||||||
|
|
||||||
private static final AtomicLong INDEX = new AtomicLong(1L);
|
private static final AtomicLong INDEX = new AtomicLong(1L);
|
||||||
|
|
||||||
|
|
@ -274,7 +279,7 @@ public class OrderMasterServiceImpl implements OrderMasterService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void cancel(Long orderMasterId) {
|
public void cancel(Long orderMasterId) throws BaseAdaPayException {
|
||||||
OrderMaster orderMaster = selectById(orderMasterId);
|
OrderMaster orderMaster = selectById(orderMasterId);
|
||||||
Assert.notNull(orderMaster, "找不到对应的订单");
|
Assert.notNull(orderMaster, "找不到对应的订单");
|
||||||
switch (orderMaster.getOrderStatus()) {
|
switch (orderMaster.getOrderStatus()) {
|
||||||
|
|
@ -288,11 +293,7 @@ public class OrderMasterServiceImpl implements OrderMasterService {
|
||||||
update.setOrderStatus(OrderStatus.CANCEL.code());
|
update.setOrderStatus(OrderStatus.CANCEL.code());
|
||||||
orderDetailService.updateByOrderMasterId(update);
|
orderDetailService.updateByOrderMasterId(update);
|
||||||
// 发起退款
|
// 发起退款
|
||||||
try {
|
|
||||||
refund(orderMaster);
|
refund(orderMaster);
|
||||||
} catch (BaseAdaPayException e) {
|
|
||||||
logger.error("订单[code={}]退款失败", orderMaster.getCode(), e);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 2: // 待上门状态需要师傅同意才能取消
|
case 2: // 待上门状态需要师傅同意才能取消
|
||||||
updateStatus(orderMasterId, OrderStatus.GOING_CANCEL.code());
|
updateStatus(orderMasterId, OrderStatus.GOING_CANCEL.code());
|
||||||
|
|
@ -347,48 +348,68 @@ public class OrderMasterServiceImpl implements OrderMasterService {
|
||||||
* @param orderMaster 主订单
|
* @param orderMaster 主订单
|
||||||
*/
|
*/
|
||||||
private void refund(OrderMaster orderMaster) throws BaseAdaPayException {
|
private void refund(OrderMaster orderMaster) throws BaseAdaPayException {
|
||||||
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMaster.getId());
|
Assert.notNull(orderMaster, "找不到对应的订单");
|
||||||
// 支付状态
|
Long orderMasterId = orderMaster.getId();
|
||||||
Integer payStatus = financialMaster.getPayStatus();
|
FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMasterId);
|
||||||
if (!payStatus.equals(1)) {
|
Assert.notNull(financialMaster, "找不到订单");
|
||||||
// 如果状态不是已支付 则不需要退款
|
Assert.isTrue(!PayStatus.WAIT_PAY.getCode().equals(financialMaster.getPayStatus()), "订单未支付");
|
||||||
return;
|
Assert.isTrue(!PayStatus.CANCEL.getCode().equals(financialMaster.getPayStatus()), "订单已取消");
|
||||||
}
|
Assert.isTrue(!PayStatus.REFUND.getCode().equals(financialMaster.getPayStatus()), "订单已退款");
|
||||||
List<FinancialDetail> financialDetails = financialDetailService.selectByFinancialMasterId(financialMaster.getId());
|
boolean paid = PayStatus.PAID.getCode().equals(financialMaster.getPayStatus()) ||
|
||||||
|
PayStatus.PAYED_ADD.getCode().equals(financialMaster.getPayStatus());
|
||||||
|
Assert.isTrue(paid, "订单未支付");
|
||||||
|
Assert.hasText(financialMaster.getPaymentId(), "找不到订单的支付记录,请联系管理员");
|
||||||
|
|
||||||
// 主订单实付金额 - 已退款金额 = 待退款金额
|
// 改主订单的订单状态和支付状态
|
||||||
|
updateStatus(orderMasterId, OrderStatus.CANCEL.code());
|
||||||
|
updatePayStatus(orderMasterId, PayStatus.REFUND.getCode());
|
||||||
|
// 改子订单的订单状态和支付状态
|
||||||
|
OrderDetail odUpdate = new OrderDetail();
|
||||||
|
odUpdate.setOrderMasterId(orderMasterId);
|
||||||
|
odUpdate.setPayStatus(PayStatus.REFUND.getCode());
|
||||||
|
odUpdate.setOrderStatus(OrderStatus.CANCEL.code());
|
||||||
|
orderDetailService.updateByOrderMasterId(odUpdate);
|
||||||
|
|
||||||
|
// 主订单金额=订单原价+加价
|
||||||
BigDecimal payMoney = financialMaster.getPayMoney();
|
BigDecimal payMoney = financialMaster.getPayMoney();
|
||||||
for (FinancialDetail fd : financialDetails) {
|
List<FinancialChangeRecord> fcrList = financialChangeRecordService.selectByMasterId(orderMasterId);
|
||||||
if (FinancialDetailType.RETURN_FEE.getCode().equals(fd.getFinancialDetailType())) {
|
for (FinancialChangeRecord fcr : fcrList) {
|
||||||
// 如果有退款子财务单 则减去子财务单的已退款金额
|
// 把已支付的加价单退了 并从主单金额里减去
|
||||||
|
if (PayStatus.PAID.getCode().equals(fcr.getPayStatus())) {
|
||||||
|
financialChangeRecordService.refund(orderMaster.getDeptId(), fcr);
|
||||||
|
payMoney = payMoney.subtract(fcr.getChangeMoney());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
List<FinancialDetail> fdList = financialDetailService.selectByFinancialMasterId(financialMaster.getId());
|
||||||
|
for (FinancialDetail fd : fdList) {
|
||||||
|
// 如果有已退款或正在退款的子财务单 从主单金额里减去
|
||||||
|
if (PayStatus.REFUND.getCode().equals(fd.getPayStatus()) ||
|
||||||
|
PayStatus.REFUNDING.getCode().equals(fd.getPayStatus()) ||
|
||||||
|
PayStatus.REVERSING.getCode().equals(fd.getPayStatus())) {
|
||||||
payMoney = payMoney.subtract(fd.getPayMoney());
|
payMoney = payMoney.subtract(fd.getPayMoney());
|
||||||
|
} else if (PayStatus.PAID.getCode().equals(fd.getPayStatus()) ||
|
||||||
|
PayStatus.PAYED_ADD.getCode().equals(fd.getPayStatus())) {
|
||||||
|
// 修改子单状态
|
||||||
|
financialDetailService.updatePayStatus(fd.getId(), PayStatus.REVERSING.getCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BigDecimal.ZERO.compareTo(payMoney) > -1) {
|
if (payMoney.compareTo(BigDecimal.ZERO) < 1) {
|
||||||
logger.info("订单[code={}]待退款金额<=0,不需要退款", orderMaster.getCode());
|
logger.info("无需退款 reverseAmt={}", payMoney);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 把主财务单状态设置为已退款
|
String reverseAmt = payMoney.setScale(2, RoundingMode.DOWN).toString();
|
||||||
FinancialMaster fmUpdate = new FinancialMaster();
|
JSONObject response = adapayService.payReverse(orderMaster.getDeptId(), financialMaster.getPaymentId(), reverseAmt, RefundType.ROM);
|
||||||
fmUpdate.setId(financialMaster.getId());
|
if (AdapayStatusEnum.succeeded.code.equals(response.getString("status")) ||
|
||||||
fmUpdate.setPayStatus(PayStatus.REFUND.getCode());
|
AdapayStatusEnum.pending.code.equals(response.getString("status"))) {
|
||||||
financialMasterService.updateFinancialMaster(fmUpdate);
|
// 主订单退款 直接修改主财务单状态为退款中
|
||||||
// 把所有子财务单状态设置为已退款
|
String reverseId = response.getString("id");
|
||||||
FinancialDetail fdUpdate = new FinancialDetail();
|
financialMaster.setPayStatus(PayStatus.REVERSING.getCode());
|
||||||
fdUpdate.setFinancialMasterId(financialMaster.getId());
|
financialMaster.setReverseId(reverseId);
|
||||||
fdUpdate.setPayStatus(PayStatus.REFUND.getCode());
|
financialMasterService.updateFinancialMaster(financialMaster);
|
||||||
financialDetailService.updateByFinancialMasterId(fdUpdate);
|
} else {
|
||||||
|
logger.error("FM[{}]撤销支付失败: {}", financialMaster.getId(), response.toJSONString());
|
||||||
String refundAmt = AdapayUtils.bigDecimalToString(payMoney);
|
}
|
||||||
JSONObject response = adapayService.payReverse(orderMaster.getDeptId(), financialMaster.getPaymentId(), refundAmt, RefundType.ROM);
|
|
||||||
logger.debug("退款结果: " + response.toJSONString());
|
|
||||||
boolean status = AdapayStatusEnum.pending.code.equals(response.getString("status")) ||
|
|
||||||
AdapayStatusEnum.succeeded.code.equals(response.getString("status"));
|
|
||||||
// 如果退款失败 这里携带失败原因抛出异常 回滚订单状态
|
|
||||||
Assert.isTrue(status, response.getString("error_msg"));
|
|
||||||
logger.info("订单[code={}]退款成功", orderMaster.getCode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.ghy.payment.domain;
|
||||||
|
|
||||||
import com.ghy.common.annotation.Excel;
|
import com.ghy.common.annotation.Excel;
|
||||||
import com.ghy.common.core.domain.BaseEntity;
|
import com.ghy.common.core.domain.BaseEntity;
|
||||||
|
import com.ghy.common.enums.FinancialDetailType;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
|
@ -49,7 +50,10 @@ public class FinancialDetail extends BaseEntity {
|
||||||
@Excel(name = "实付金额", cellType = Excel.ColumnType.STRING)
|
@Excel(name = "实付金额", cellType = Excel.ColumnType.STRING)
|
||||||
private BigDecimal payMoney;
|
private BigDecimal payMoney;
|
||||||
|
|
||||||
@Excel(name = "财务子单类型,1师傅转派/2多级分销/3平台抽成/4撤销支付或退款/5超时罚金", cellType = Excel.ColumnType.NUMERIC)
|
/**
|
||||||
|
* {@link FinancialDetailType}
|
||||||
|
*/
|
||||||
|
@Excel(name = "财务子单类型", cellType = Excel.ColumnType.NUMERIC)
|
||||||
private Integer financialDetailType;
|
private Integer financialDetailType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ public interface FinancialChangeRecordService {
|
||||||
* @param financialChangeRecord 改价记录
|
* @param financialChangeRecord 改价记录
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
int insertFinancialChangeRecord(FinancialChangeRecord financialChangeRecord);
|
int insert(FinancialChangeRecord financialChangeRecord);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改改价记录
|
* 修改改价记录
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ public class FinancialChangeRecordServiceImpl implements FinancialChangeRecordSe
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int insertFinancialChangeRecord(FinancialChangeRecord financialChangeRecord) {
|
public int insert(FinancialChangeRecord financialChangeRecord) {
|
||||||
financialChangeRecord.setLeftMoney(financialChangeRecord.getChangeMoney());
|
financialChangeRecord.setLeftMoney(financialChangeRecord.getChangeMoney());
|
||||||
return financialChangeRecordMapper.insertFinancialChangeRecord(financialChangeRecord);
|
return financialChangeRecordMapper.insertFinancialChangeRecord(financialChangeRecord);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue