Authored by LUOXC

refactor

... ... @@ -94,11 +94,7 @@ public class ErpGWOrderController {
@IgnoreSession
@RequestMapping(value="/manualDealMon")
public ApiResponse manualDealMon(@RequestBody ManualDealRequest req) {
try {
paymentService.manualDeal(req);
} catch (ServiceException e) {
return new ApiResponse.ApiResponseBuilder().code(e.getCode()).data("").message(e.getErrorMessage()).build();
}
paymentService.manualDeal(req);
return new ApiResponse.ApiResponseBuilder().code(200).data("").message("处理成功").build();
}
... ...
... ... @@ -314,11 +314,7 @@ public class PaymentController {
//@RequestMapping(params = "method=ufo.order.manualDealMon")
@RequestMapping(value="/erp/manualDealMon")
public ApiResponse manualDealMon(@RequestBody ManualDealRequest req) {
try {
paymentService.manualDeal(req);
} catch (ServiceException e) {
return new ApiResponse.ApiResponseBuilder().code(e.getCode()).data("").message(e.getErrorMessage()).build();
}
paymentService.manualDeal(req);
return new ApiResponse.ApiResponseBuilder().code(200).data("").message("处理成功").build();
}
... ...
... ... @@ -176,7 +176,7 @@ class BuyerOrderCancelHandler {
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCodeUid(orderCode, uid);
if (Objects.isNull(buyerOrder)) {
log.info("cancel order {} fail, order is not exist", orderCode);
throwServiceException(ServiceError.ORDER_NOT_EXIST);
throwServiceException("订单不存在");
}
this.sellerUid = buyerOrder.getSellerUid();
// 当前状态不可取消订单
... ...
... ... @@ -15,6 +15,7 @@ import com.yohobuy.ufo.model.order.req.ManualDealRequest;
import com.yohobuy.ufo.model.user.resp.AuthorizeResultRespVO;
import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.common.utils.TimeUtils;
import com.yohoufo.dal.order.*;
... ... @@ -66,6 +67,7 @@ import java.util.Objects;
import java.util.function.BiFunction;
import static com.yohoufo.order.common.TransferCase.EARNEST_MONEY_TO_BUYER;
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceExceptionIf;
@Service
... ... @@ -980,30 +982,26 @@ public class PaymentServiceImpl implements IPaymentService {
public void manualDeal(ManualDealRequest req) {
logger.info("manual deal request is {}", req);
Integer tradeBillsId = req.getTradeBillsId();
throwServiceExceptionIf(tradeBillsId == null || tradeBillsId < 1, 400, "流水Id不合法");
throwServiceExceptionIf(req.getOperateUid() == null || req.getOperateUid() < 1, 400, "客服uid不合法");
throwServiceExceptionIf(StringUtils.isBlank(req.getOperateUname()), 400, "客服名称不合法");
throwServiceExceptionIf(tradeBillsId == null || tradeBillsId < 1, "流水Id不合法");
throwServiceExceptionIf(req.getOperateUid() == null || req.getOperateUid() < 1, "客服uid不合法");
throwServiceExceptionIf(StringUtils.isBlank(req.getOperateUname()), "客服名称不合法");
TradeBills tradeBills = tradeBillsMapper.selectByPrimaryKey(tradeBillsId);
throwServiceExceptionIf(tradeBills == null, 400, "流水不存在");
throwServiceExceptionIf(tradeBills.getTradeStatus() == 100, 400, "该流水不是失败的");
throwServiceExceptionIf(tradeBills.getIncomeOutcome() == null || tradeBills.getIncomeOutcome() != 1, 400, "该流水不是退款类型");
throwServiceExceptionIf(tradeBills == null, "流水不存在");
throwServiceExceptionIf(tradeBills.getTradeStatus() == 100, "该流水不是失败的");
throwServiceExceptionIf(tradeBills.getIncomeOutcome() == null || tradeBills.getIncomeOutcome() != 1, "该流水不是退款类型");
// 是否处理过的
throwServiceExceptionIf(Objects.nonNull(tradeBillsMapper.selectByDealRelateId(tradeBillsId)), 400, "该流水已经处理过");
throwServiceExceptionIf(Objects.nonNull(tradeBillsMapper.selectByDealRelateId(tradeBillsId)), "该流水已经处理过");
String logTag = String.format("manual deal orderCode is %s tradeBillId is %s uid is %s",tradeBills.getOrderCode(),tradeBillsId,tradeBills.getUid());
BigDecimal amount;
BigDecimal amount = null;
try {
amount = new BigDecimal(req.getAmount()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
} catch (Exception e) {
throw new ServiceException(400, "转账金额不合法");
}
if (amount.compareTo(new BigDecimal("0.1")) < 0) {
throw new ServiceException(400, "转账金额小于0.1");
}
if (tradeBills.getAmount().compareTo(amount) != 0) {
throw new ServiceException(400, "转账金额有误");
throwServiceException("转账金额不合法");
}
throwServiceExceptionIf(amount.compareTo(new BigDecimal("0.1")) < 0, "转账金额小于0.1");
throwServiceExceptionIf(tradeBills.getAmount().compareTo(amount) != 0, "转账金额有误");
tradeBills.setDealUid(req.getOperateUid());
tradeBills.setDealUserName(req.getOperateUname());
... ... @@ -1014,9 +1012,8 @@ public class PaymentServiceImpl implements IPaymentService {
lockKey.setDealStatus(1);
lockKey.setDealUid(req.getOperateUid());
lockKey.setDealUserName(req.getOperateUname());
if (!tryLock(logTag, lockKey)) {
throw new ServiceException(400, "流水已经处理过id=" + tradeBills.getId());
}
throwServiceExceptionIf(!tryLock(logTag, lockKey), 500, "流水已经处理过id=" + tradeBills.getId());
// 退货款或普通用户退保证金
if (isRefundGoodsMoneyTradeBills(tradeBills) || isRefundEarnestMoneyTradeBills(tradeBills)) {
... ... @@ -1074,7 +1071,7 @@ public class PaymentServiceImpl implements IPaymentService {
// 加新流水
addSuccessTradeBills(tradeBills);
} else {
throw new ServiceException(500, refundBo.getRefundMsg());
throwServiceException(500, refundBo.getRefundMsg());
}
return;
} catch (Exception e) {
... ... @@ -1085,11 +1082,7 @@ public class PaymentServiceImpl implements IPaymentService {
}
alarm("人工处理退款失败", "ufo.order.manualDeal", alarmMsg);
releaseLock(logTag, lockKey);
if (e instanceof ServiceException) {
throw e;
} else {
throw new ServiceException(500, "退款失败[" + tradeBills.getId() + "]");
}
throwServiceException(e, 500, "退款失败[" + tradeBills.getId() + "]");
}
}
... ... @@ -1121,11 +1114,7 @@ public class PaymentServiceImpl implements IPaymentService {
releaseLock(logTag, lockKey);
if (e instanceof ServiceException) {
throw e;
} else {
ServiceExceptions.throwServiceException(500, "转账失败[" + tradeBills.getId() + "]");
}
throwServiceException(e, 500, "转账失败[" + tradeBills.getId() + "]");
}
}
... ... @@ -1176,7 +1165,7 @@ public class PaymentServiceImpl implements IPaymentService {
ordersPayTransferMapper.updateByPrimaryKeySelective(transferSuccess);
}else {
logger.warn("{}, transfer fail {}", logTag,transferResult);
throw new ServiceException(transferResult.getCode(), transferResult.getMsg());
throwServiceException(transferResult.getCode(), transferResult.getMsg());
}
}
... ... @@ -1201,7 +1190,7 @@ public class PaymentServiceImpl implements IPaymentService {
logger.info("{}, the trade bill has updated to transfer waiting", logTag);
} else {
logger.warn("{}, transfer fail {}", logTag,transferResult);
throw new ServiceException(transferResult.getCode(), transferResult.getMsg());
throwServiceException(transferResult.getCode(), transferResult.getMsg());
}
}
... ... @@ -1308,19 +1297,19 @@ public class PaymentServiceImpl implements IPaymentService {
if (!ObjectUtils.allNotNull(ordersPay, ordersPay.getAmount(), ordersPay.getPayment())) {
logger.warn("manual refund paid record not exist.orderCode is {}", orderCode);
ServiceExceptions.throwServiceException("订单[" + orderCode + "],尚未支付!");
ServiceExceptions.throwServiceException(400,"订单[" + orderCode + "],尚未支付!");
}
if(!CollectionUtils.isEmpty(ordersPayRefundMapper.selectByOrderCode(orderCode))) {
logger.warn("manual refund has success before .orderCode is {}", orderCode);
throw new ServiceException(400, "订单[" + orderCode + "],已经退款成功过!");
throwServiceException(400, "订单[" + orderCode + "],已经退款成功过!");
}
// 退款金额大于支付金额
if (ordersPay.getAmount().compareTo(amount) < 0) {
logger.warn("manual refund refundAmount {} > amount {}, orderCode is {}",
ordersPay.getAmount(), amount, orderCode);
ServiceExceptions.throwServiceException("订单[" + orderCode + "],退款金额大于支付金额!");
throwServiceException("订单[" + orderCode + "],退款金额大于支付金额!");
}
// 退款
... ...
package com.yohoufo.order.utils;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.common.exception.UfoServiceException;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* @author LUOXC
... ... @@ -18,29 +15,35 @@ public class ServiceExceptions {
}
public static void throwServiceException(String message) {
ServiceException serviceException = new ServiceException(ServiceError.ORDER_SERVICE_ERROR);
serviceException.setParams(message);
throw serviceException;
throw new UfoServiceException(400, message);
}
public static void throwServiceExceptionIf(Condition condition, int code, String message) {
if (condition.test()) {
throw new ServiceException(code, message);
throw new UfoServiceException(code, message);
}
}
public static void throwServiceExceptionIf(boolean condition, int code, String message) {
if (condition) {
throw new ServiceException(code, message);
throw new UfoServiceException(code, message);
}
}
public static void throwServiceExceptionIf(boolean condition, String message) {
throwServiceExceptionIf(condition, 400, message);
}
public static void throwServiceException(int code, String message) {
throw new ServiceException(code, message);
throw new UfoServiceException(code, message);
}
public static void throwServiceException(ServiceError serviceError) {
throw new ServiceException(serviceError);
public static void throwServiceException(Exception e, int defaultCode, String defaultMessage) {
if (e instanceof UfoServiceException) {
throw (UfoServiceException) e;
} else {
throwServiceException(defaultCode, defaultMessage);
}
}
}
... ...