Authored by LUOXC

香港仓结算

... ... @@ -5,13 +5,14 @@ package com.yohoufo.order.common;
* Created by chao.chen on 2018/10/24.
*/
public enum BillTradeStatus {
NEW(0),
TRANSFER_WAITING(90),
SUCCESS(100),
FAIL(200),
NOT_EXIST_ALI_ACCOUNT(201),
NOT_EXIST_ALIPAY_ACCOUNT(201),
AMOUNT_IS_ILLEGAL(202),
HK_AMOUNT_WAIT_PAYMENT(203),
YH_STORE_AMOUNT_WAIT_PAYMENT(204),
YOHO_STORE_AMOUNT_WAIT_PAYMENT(204),
TRANSFER_FAIL(299);
int code;
... ...
... ... @@ -54,6 +54,8 @@ public interface TopicConstants {
String ORDER_NOT_DELIVER_NOTICE = "order.notDeliver";
//String ORDER_NOT_DELIVER_NOTICE_108 = "order.notDeliver_108";
String HK_AMOUNT_WAIT_PAYMENT = "order.hkAmountWaitPayment";
}
... ...
package com.yohoufo.order.mq.consumer;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.rabbitmq.YhConsumer;
import com.yohoufo.order.event.BuyerConfirmEvent;
import com.yohoufo.order.model.request.OrderRequest;
import com.yohoufo.order.utils.LoggerUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
@Component
public class HkAmountWaitPaymentMessageConsummer implements YhConsumer {
final Logger logger = LoggerUtils.getMqConsumerLogger();
@Override
public void handleMessage(Object o) throws Exception {
logger.info("handler hk amount wait payment message, msg {} ", o);
JSONObject message = JSONObject.parseObject(o.toString());
Integer uid = message.getInteger("uid");
logger.info("handler hk amount wait payment message success, msg {} ", o);
}
}
... ...
... ... @@ -28,10 +28,11 @@ import com.yohoufo.order.model.PayRefundBo;
import com.yohoufo.order.model.PaymentData;
import com.yohoufo.order.model.TransferData;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TranseferCellNode;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.model.response.PaymentConfirmRsp;
import com.yohoufo.order.model.response.PrepayResponse;
import com.yohoufo.order.mq.TopicConstants;
import com.yohoufo.order.mq.producer.TradeMqSender;
import com.yohoufo.order.service.AbstractOrderPaymentService;
import com.yohoufo.order.service.BuyerOrderPaymentService;
import com.yohoufo.order.service.IPaymentService;
... ... @@ -49,7 +50,6 @@ import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
import com.yohoufo.order.service.transfer.TransferResult;
import com.yohoufo.order.utils.LoggerUtils;
import com.yohoufo.order.utils.PaymentHelper;
import lombok.val;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
... ... @@ -57,6 +57,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
... ... @@ -151,6 +152,9 @@ public class PaymentServiceImpl implements IPaymentService {
@Autowired
private TradeBillsService tradeBillsService;
@Autowired
private TradeMqSender tradeMqSender;
/**
* 获取主场的订单service
* @param codeMeta
... ... @@ -576,87 +580,38 @@ public class PaymentServiceImpl implements IPaymentService {
*/
@Database(ForceMaster = true)
public void transferMon(TransferMoneyRequest request){
logger.info("transferMon转账开始,request = {}", request);
long buyerOrderCode = request.getBuyerOrderCode();
String logTag = String.format("transfer money orderCode is %s",buyerOrderCode);
logger.info("transferMon参数检查");
// 订单号check
if (buyerOrderCode < 1L) {
logger.warn("transferMonErr orderCode empty");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
// 类型检查
TransferCase transferCase = TransferCase.getTransferCase(request.getType());
if (transferCase == null) {
logger.warn("transferMonErr transferType invalid");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
TransferMoneyModel transferMoneyModel = new TransferMoneyModel(request).invoke();
String logTag = transferMoneyModel.getLogTag();
long buyerOrderCode = transferMoneyModel.getBuyerOrderCode();
TransferCase transferCase = transferMoneyModel.getTransferCase();
SellerOrder sellerOrder = transferMoneyModel.getSellerOrder();
long sellerOrderCode = transferMoneyModel.getSellerOrderCode();
Integer targetUid = transferMoneyModel.getTargetUid();
int now = transferMoneyModel.getNow();
// 买家订单检查
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(buyerOrderCode);
if (buyerOrder == null) {
logger.warn("transferMonErr getOrderInfo buyer order not exist, orderCode is {}", buyerOrderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), buyerOrderCode);
if (buyerOrderGoods == null) {
logger.warn("transferMonErr buyerOrderGoods not exist, orderCode is {}", buyerOrderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
// 卖家订单检查
Integer skup = buyerOrderGoods.getSkup();
SellerOrder sellerOrder = sellerOrderMapper.selectBySkup(skup);
if (sellerOrder == null){
logger.warn("transferMonErr getOrderInfo seller order not exist, skup is {}", buyerOrderGoods.getSkup());
throw new ServiceException(ServiceError.ORDER_NULL);
}
Integer sellerUid = sellerOrder.getUid();
long sellerOrderCode = sellerOrder.getOrderCode();
penaltyEarnestIfWalletSellerOrder(request.getPenaltyEarnestFunction(), request.getSwdType(), sellerOrder);
val targetUserType = transferCase == EARNEST_MONEY_TO_BUYER ? TargetUserType.buyer : TargetUserType.seller;
val targetUid = targetUserType == TargetUserType.buyer ? buyerOrder.getUid() : sellerUid;
if (targetUid == null || targetUid < 1) {
logger.warn("transferMonErr uid {} 不合法", targetUid);
throw new ServiceException(400, "uid[" + targetUid + "]不合法");
}
int now = (int) (System.currentTimeMillis()/1000);
// 查看是否已经有转账记录
checkTransferExist(buyerOrderCode);
OrdersPayTransfer transfer = createTransfer(buyerOrderCode, sellerOrderCode, transferCase.getCode(), null, targetUid, BigDecimal.ZERO, now);
// 增加流水记录
TradeBills record = new TradeBills();
record.setUid(targetUid);
record.setOrderCode(buyerOrderCode);
record.setUserType(targetUserType.getCode());
record.setPayType(1);
// 如果[全部货款->卖家],否则[补偿款->卖家或买家] 2:货款;3:补偿款
record.setTradeType(transferCase == TransferCase.ALL_GOODS_MONEY_TO_SELLER ? 2 : 3);
record.setIncomeOutcome(1);
record.setTradeStatus(0);
record.setCreateTime(now);
TradeBills record = transferMoneyModel.newTradeBills();
// 转账费
BigDecimal transferAmount = request.getTranseferCellNode().getAmount();
logger.info("transferMon参数检查成功!插入初始化转账信息成功,接下来计算费用");
TranseferCellNode transeferCellNode = request.getTranseferCellNode();
// 算费
BigDecimal transferAmount = transeferCellNode.getAmount();
logger.info("transferMon计算费用结果为 {}", transferAmount);
if (transferAmount == null) {
logger.warn("transferMonErr transferMon计算费用结果为 null, 不合法的金额");
alarm("转账金额不合法", "ufo.order.transferMon", "订单号:" + buyerOrderCode + "操作类型(" + transferCase.getCode() + ")计算金额结果为null");
logger.warn("{}, transfer amount is null", logTag);
alarm("转账金额不合法", "ufo.order.transferMon", "订单号:" + buyerOrderCode + "操作类型(" + transferCase.getCode() + ")计算金额结果为空");
record.setTradeStatus(BillTradeStatus.AMOUNT_IS_ILLEGAL.getCode());
addTradeBills(record);
throw new ServiceException(400, "计算金额错误!:");
}
transfer.setAmount(transferAmount);
logger.info("{}, transfer amount is {}", logTag, transferAmount);
record.setAmount(transferAmount);
// 因为转账支付宝扣费累计计算,使用转账额
// ???因为转账支付宝扣费累计计算,使用转账额
record.setSystemAmount(transferAmount.multiply(new BigDecimal("-1")));
if (transferAmount.compareTo(new BigDecimal("0.1")) < 0) {
logger.warn("transferMonErr transferMon计算费用结果为 {}, 小于0.1", transferAmount);
logger.info("{}, transfer amount is {}, it less than 0.1", logTag, transferAmount);
alarm("转账金额小于0.1", "ufo.order.transferMon", "订单号:" + buyerOrderCode + "操作类型(" + transferCase.getCode() + ")计算金额结果为" + transferAmount);
record.setTradeStatus(BillTradeStatus.AMOUNT_IS_ILLEGAL.getCode());
addTradeBills(record);
... ... @@ -665,29 +620,25 @@ public class PaymentServiceImpl implements IPaymentService {
AuthorizeResultRespVO account = getOneValidAlipayAccountOr(targetUid, invalidAccount -> {
if ("invalid".equals(invalidAccount)) {
record.setTradeStatus(BillTradeStatus.YH_STORE_AMOUNT_WAIT_PAYMENT.getCode());
record.setTradeStatus(BillTradeStatus.YOHO_STORE_AMOUNT_WAIT_PAYMENT.getCode());
} else if ("invalid_hk".equals(invalidAccount)) {
record.setTradeStatus(BillTradeStatus.HK_AMOUNT_WAIT_PAYMENT.getCode());
tradeMqSender.send(TopicConstants.HK_AMOUNT_WAIT_PAYMENT, Collections.singletonMap("uid", record.getUid()));
} else {
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALI_ACCOUNT.getCode());
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALIPAY_ACCOUNT.getCode());
}
addTradeBills(record);
});
if(account == null){
throw new ServiceException(400, "uid[" + targetUid + "]没有获取到有效的支付宝账号");
}
});
if(StringUtils.isNotBlank(account.getAlipayId())) {
transfer.setAlipayAccount(account.getAlipayId());
} else if(StringUtils.isNotBlank(account.getAlipayAccount())) {
transfer.setAlipayAccount(account.getAlipayAccount());
}
tradeBillsMapper.insert(record);
String alipayAccount = StringUtils.isNotBlank(account.getAlipayId()) ? account.getAlipayId() : account.getAlipayAccount();
transfer.setAlipayAccount(alipayAccount);
transfer.setUpdateTime(now);
tradeBillsMapper.insert(record);
transfer.setAmount(transferAmount);
String alipayAccount = transfer.getAlipayAccount();
// 转账
try {
logger.info("{}, transfer alipayAccount={}, transferAmount={}", logTag, alipayAccount, transferAmount);
... ... @@ -758,7 +709,7 @@ public class PaymentServiceImpl implements IPaymentService {
if (StringUtils.isBlank(aliPayAccount.getAlipayAccount()) && StringUtils.isBlank(aliPayAccount.getAlipayId()) ) {
logger.warn("transAllEarnestErr uid {} 支付宝账号不合法", account);
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALI_ACCOUNT.getCode());
record.setTradeStatus(BillTradeStatus.NOT_EXIST_ALIPAY_ACCOUNT.getCode());
addTradeBills(record);
throw new ServiceException(400, "uid[" + uid + "]支付宝账号不合法");
}
... ... @@ -1455,5 +1406,120 @@ public class PaymentServiceImpl implements IPaymentService {
}
private class TransferMoneyModel {
private TransferMoneyRequest request;
private long buyerOrderCode;
private String logTag;
private TransferCase transferCase;
private SellerOrder sellerOrder;
private long sellerOrderCode;
private TargetUserType targetUserType;
private Integer targetUid;
private int now;
public TransferMoneyModel(TransferMoneyRequest request) {
this.request = request;
}
public long getBuyerOrderCode() {
return buyerOrderCode;
}
public String getLogTag() {
return logTag;
}
public TransferCase getTransferCase() {
return transferCase;
}
public SellerOrder getSellerOrder() {
return sellerOrder;
}
public long getSellerOrderCode() {
return sellerOrderCode;
}
public TargetUserType getTargetUserType() {
return targetUserType;
}
public Integer getTargetUid() {
return targetUid;
}
public int getNow() {
return now;
}
public TransferMoneyModel invoke() {
logger.info("transferMon转账开始,request = {}", request);
buyerOrderCode = request.getBuyerOrderCode();
logTag = String.format("transfer money orderCode is %s", buyerOrderCode);
logger.info("transferMon参数检查");
// 订单号check
if (buyerOrderCode < 1L) {
logger.warn("transferMonErr orderCode empty");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
// 类型检查
transferCase = TransferCase.getTransferCase(request.getType());
if (transferCase == null) {
logger.warn("transferMonErr transferType invalid");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
// 买家订单检查
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(buyerOrderCode);
if (buyerOrder == null) {
logger.warn("transferMonErr getOrderInfo buyer order not exist, orderCode is {}", buyerOrderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), buyerOrderCode);
if (buyerOrderGoods == null) {
logger.warn("transferMonErr buyerOrderGoods not exist, orderCode is {}", buyerOrderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
// 卖家订单检查
Integer skup = buyerOrderGoods.getSkup();
sellerOrder = sellerOrderMapper.selectBySkup(skup);
if (sellerOrder == null){
logger.warn("transferMonErr getOrderInfo seller order not exist, skup is {}", buyerOrderGoods.getSkup());
throw new ServiceException(ServiceError.ORDER_NULL);
}
Integer sellerUid = sellerOrder.getUid();
sellerOrderCode = sellerOrder.getOrderCode();
targetUserType = transferCase == EARNEST_MONEY_TO_BUYER ? TargetUserType.buyer : TargetUserType.seller;
targetUid = targetUserType == TargetUserType.buyer ? buyerOrder.getUid() : sellerUid;
if (targetUid == null || targetUid < 1) {
logger.warn("transferMonErr uid {} 不合法", targetUid);
throw new ServiceException(400, "uid[" + targetUid + "]不合法");
}
now = (int) (System.currentTimeMillis() / 1000);
// 查看是否已经有转账记录
checkTransferExist(buyerOrderCode);
return this;
}
public TradeBills newTradeBills(){
// 增加流水记录
TradeBills record = new TradeBills();
record.setUid(targetUid);
record.setOrderCode(buyerOrderCode);
record.setUserType(targetUserType.getCode());
record.setPayType(1);
// 如果[全部货款->卖家],否则[补偿款->卖家或买家] 2:货款;3:补偿款
record.setTradeType(transferCase == TransferCase.ALL_GOODS_MONEY_TO_SELLER ? 2 : 3);
record.setIncomeOutcome(1);
record.setTradeStatus(BillTradeStatus.NEW.getCode());
record.setCreateTime(now);
return record;
}
}
}
... ...
... ... @@ -69,6 +69,11 @@ consumer:
topic: ufo.couponSendWithTradeMqNotify
ratelimit: 20
# 香港卖家待结算
- class: com.yohoufo.order.mq.consumer.HkAmountWaitPaymentMessageConsummer
topic: order.hkAmountWaitPayment
ratelimit: 20
- address: ${rabbit_ufo}
username: ${rabbit_ufo_user}
... ...