Authored by caoyan

Merge branch 'dev_order' of http://git.yoho.cn/ufo/yohoufo-fore into dev_order

... ... @@ -258,4 +258,12 @@ public class OrdersPayRefund {
public void setOrderType(int orderType) {
this.orderType = orderType;
}
@Override
public String toString() {
return "OrdersPayRefund [orderType=" + orderType + ", id=" + id + ", orderCode=" + orderCode + ", serialNo="
+ serialNo + ", amount=" + amount + ", status=" + status + ", createTime=" + createTime
+ ", updateTime=" + updateTime + ", refundCode=" + refundCode + "]";
}
}
\ No newline at end of file
... ...
... ... @@ -2,6 +2,7 @@ package com.yohoufo.order.service;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
... ... @@ -23,10 +24,12 @@ import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.common.utils.TimeUtils;
import com.yohoufo.dal.order.EntrySellerRechargeOrderMapper;
import com.yohoufo.dal.order.OrdersPayMapper;
import com.yohoufo.dal.order.OrdersPayRefundMapper;
import com.yohoufo.dal.order.SellerWalletDetailMapper;
import com.yohoufo.dal.order.SellerWalletMapper;
import com.yohoufo.dal.order.model.EntrySellerRechargeOrder;
import com.yohoufo.dal.order.model.OrdersPay;
import com.yohoufo.dal.order.model.OrdersPayRefund;
import com.yohoufo.dal.order.model.SellerWallet;
import com.yohoufo.dal.order.model.SellerWalletDetail;
import com.yohoufo.order.common.Payment;
... ... @@ -34,6 +37,7 @@ import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.response.PrepayResponse;
import com.yohoufo.order.service.cache.CacheCleaner;
import com.yohoufo.order.service.cache.CacheKeyBuilder;
import com.yohoufo.order.service.impl.PaymentServiceImpl;
@Service
public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
... ... @@ -52,12 +56,18 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
@Autowired
OrdersPayMapper ordersPayDao;
@Autowired
OrdersPayRefundMapper ordersPayRefundMapper;
@Autowired
private CacheCleaner cacheCleaner;
@Autowired
UfoServiceCaller ufoServiceCaller;
@Autowired
PaymentServiceImpl paymentService;
/**
* 更新订单状态
... ... @@ -305,10 +315,96 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
return changeEarnest(uid, money, "退还保证金");
}
// 支付保证金:明细记录+orders_pay记录
public void useEarnestAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList) {
for(OrderInfo oi : orderList) {
SellerWalletDetail swd = addUseEarnestDetail(sw, oi.getOrderCode(), oi.getAmount());
addOrdersPay(swd);
}
}
// 修改价格:明细记录+orders_pay金额修改
public void changePriceAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList) {
for(OrderInfo oi : orderList) {
SellerWalletDetail swd = addChangePriceWalletDetail(sw, oi.getOrderCode(), oi.getAmount());
changePriceUpdateOrdersPay(swd);
}
}
// 下架商品:明细记录+退款表记录
public void unshelveAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList) {
for(OrderInfo oi : orderList) {
SellerWalletDetail swd = refundEarnestDetail(sw, oi.getOrderCode(), oi.getAmount());
addRefundRecord(swd, oi.getAmount());
}
}
// 商家退出入驻,钱包清空+转账
public boolean returnAllEarnest(Integer uid, long orderCode, String alipayAccount) {
BigDecimal money = returnEarnest(uid, orderCode);
if (money == null) {
return false;
}
return paymentService.transAllEarnest(orderCode, uid, money, alipayAccount);
}
// 退还保证金
public SellerWalletDetail refundEarnestDetail(SellerWallet sw, long orderCode, BigDecimal amount) {
SellerWalletDetail swd = new SellerWalletDetail();
swd.setOrderCode(orderCode);
swd.setType(3); // 单笔退还
swd.setUid(sw.getUid());
swd.setWalletId(sw.getId());
swd.setAmount(amount);
swd.setCreateTime(TimeUtils.getTimeStampSecond());
logger.info("退还保证金,钱包明细记录,bean={}", swd);
sellerWalletDetailMapper.insert(swd);
logger.info("退还保证金,钱包明细记录成功,bean={}", swd);
return swd;
}
// 退出入驻:归还所有保证金 总账+明细
private BigDecimal returnEarnest(Integer uid, long orderCode) {
logger.info("商家退出,退还保证金,钱包归零处理开始,uid={},orderCode={}", uid, orderCode);
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
if (sw == null) {
logger.error("退还所有保证金,钱包不存在,uid={}", uid);
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.changeEarnest", "钱包不存在uid=" + uid);
return null;
}
BigDecimal amount = sw.getAmount();
logger.info("商家退出,退还保证金,钱包归零处理,钱包信息={}", sw);
if (sw.getStatus() == null || sw.getStatus() == 0) {
logger.error("退还所有保证金{},钱包不可用,uid={}", amount, uid);
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.changeEarnest", "钱包不可用uid=" + uid+", 退还金额为:"+amount);
return null;
}
sw.setUpdateTime(TimeUtils.getTimeStampSecond());
if (sellerWalletMapper.returnMoney(sw) == 0) {
logger.error("退还所有证金失败,更新表返回0,uid={}", uid);
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.changeEarnest", "更新表返回0,uid=" + uid + ", 退还金额为:"+ amount);
return null;
}
logger.info("商家退出,退还保证金,钱包归零处理,余额扣除成功,钱包信息={}", sw);
// 增加充值记录
SellerWalletDetail swd = new SellerWalletDetail();
swd.setOrderCode(orderCode);
swd.setType(5);// 退还所有保证金
swd.setUid(uid);
swd.setWalletId(sw.getId());
swd.setAmount(amount);
swd.setCreateTime(TimeUtils.getTimeStampSecond());
logger.info("商家退出,退还保证金,钱包归零处理,增加扣减明细,bean={}", swd);
sellerWalletDetailMapper.insert(swd);
return amount;
}
// 支付保证金:明细
public void addUseEarnestDetail(SellerWallet sw, BigDecimal money) {
long orderCode = 0;
BigDecimal amount = null;
private SellerWalletDetail addUseEarnestDetail(SellerWallet sw, long orderCode, BigDecimal amount) {
Integer uid = sw.getId();
SellerWalletDetail swd = new SellerWalletDetail();
swd.setOrderCode(orderCode);
... ... @@ -320,63 +416,24 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
logger.info("支付保证金,钱包明细记录,bean={}", swd);
sellerWalletDetailMapper.insert(swd);
logger.info("支付保证金,钱包明细记录成功,bean={}", swd);
return swd;
}
// 退还保证金
public SellerWalletDetail refundEarnestDetail(SellerWallet sw, long orderCode, BigDecimal amount) {
// 修改价格支付保证金:明细
private SellerWalletDetail addChangePriceWalletDetail(SellerWallet sw, long orderCode, BigDecimal amount) {
Integer uid = sw.getId();
SellerWalletDetail swd = new SellerWalletDetail();
swd.setOrderCode(orderCode);
swd.setType(3); // 单笔退还
swd.setUid(sw.getUid());
swd.setType(4);
swd.setUid(uid);
swd.setWalletId(sw.getId());
swd.setAmount(amount);
swd.setCreateTime(TimeUtils.getTimeStampSecond());
logger.info("退还保证金,钱包明细记录,bean={}", swd);
logger.info("修改价格支付保证金,钱包明细记录,bean={}", swd);
sellerWalletDetailMapper.insert(swd);
logger.info("退还保证金,钱包明细记录成功,bean={}", swd);
logger.info("修改价格支付保证金,钱包明细记录成功,bean={}", swd);
return swd;
}
// 退出入驻:归还所有保证金 总账+明细
public BigDecimal returnEarnest(Integer uid, long orderCode) {
logger.info("商家退出,退还保证金,钱包归零处理开始,uid={},orderCode={}", uid, orderCode);
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
if (sw == null) {
logger.error("退还所有保证金,钱包不存在,uid={}", uid);
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.recharge", "钱包不存在uid=" + uid);
return null;
}
BigDecimal amount = sw.getAmount();
logger.info("商家退出,退还保证金,钱包归零处理,钱包信息={}", sw);
if (sw.getStatus() == null || sw.getStatus() == 0) {
logger.error("退还所有保证金{},钱包不可用,uid={}", amount, uid);
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.recharge", "钱包不可用uid=" + uid+", 退还金额为:"+amount);
return null;
}
sw.setUpdateTime(TimeUtils.getTimeStampSecond());
if (sellerWalletMapper.returnMoney(sw) == 0) {
logger.error("退还所有证金失败,更新表返回0,uid={}", uid);
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.recharge", "更新表返回0,uid=" + uid + ", 退还金额为:"+ amount);
return null;
}
logger.info("商家退出,退还保证金,钱包归零处理,余额扣除成功,钱包信息={}", sw);
// 增加充值记录
SellerWalletDetail swd = new SellerWalletDetail();
swd.setOrderCode(orderCode);
swd.setType(5);// 退还所有保证金
swd.setUid(uid);
swd.setWalletId(sw.getId());
swd.setAmount(amount);
swd.setCreateTime(TimeUtils.getTimeStampSecond());
logger.info("商家退出,退还保证金,钱包归零处理,增加扣减明细,bean={}", swd);
sellerWalletDetailMapper.insert(swd);
return amount;
}
}
private SellerWallet changeEarnest(Integer uid, BigDecimal money, String message) {
logger.info("{}修改钱包表入口,uid={},money={}", message, uid, money);
... ... @@ -385,17 +442,17 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
if (sw == null) {
logger.error(message + "失败,钱包不存在,uid={}", uid);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.recharge", "钱包不存在uid=" + uid+", 金额为:"+money);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "钱包不存在uid=" + uid+", 金额为:"+money);
return null;
}
if (sw.getStatus() == null || sw.getStatus() == 0) {
logger.error(message + "{}失败,钱包不可用,uid={}", money, uid);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.recharge", "钱包不可用uid=" + uid+", 金额为:"+money);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "钱包不可用uid=" + uid+", 金额为:"+money);
return null;
}
if (sw.getAmount().compareTo(money) < 0) {
logger.error(message + "{}失败,钱包余额不足,uid={}", money, uid);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.recharge", "钱包余额不足uid=" + uid+", 金额为:"+money+", 账户余额为:"+sw.getAmount());
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "钱包余额不足uid=" + uid+", 金额为:"+money+", 账户余额为:"+sw.getAmount());
return null;
}
... ... @@ -404,14 +461,14 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
logger.info("{}修改钱包表余额,bean={}", sw);
if (sellerWalletMapper.addMoney(sw) == 0) {
logger.error(message + "失败,(并发)更新表返回0,uid={}", uid);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.recharge", "(并发)更新表返回0,uid=" + uid + ", 金额为:"+ money);
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "(并发)更新表返回0,uid=" + uid + ", 金额为:"+ money);
return null;
}
logger.info("{}修改钱包表余额成功,bean={}", sw);
return sw;
}
public void addOrdersPay(SellerWalletDetail swd) {
private void addOrdersPay(SellerWalletDetail swd) {
// 增加支付记录
OrdersPay ordersPay = new OrdersPay();
ordersPay.setOrderCode(swd.getOrderCode());
... ... @@ -430,7 +487,7 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
logger.info("增加支付记录orders_pay成功,orderCode={}", swd.getOrderCode());
}
public int changePriceUpdateOrdersPay(SellerWalletDetail swd) {
private int changePriceUpdateOrdersPay(SellerWalletDetail swd) {
// 增加支付记录
OrdersPay ordersPay = new OrdersPay();
ordersPay.setOrderCode(swd.getOrderCode());
... ... @@ -447,4 +504,19 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
return result;
}
private void addRefundRecord(SellerWalletDetail swd, BigDecimal amount) {
OrdersPayRefund newRecord = new OrdersPayRefund();
newRecord.setOrderCode(swd.getOrderCode());
newRecord.setRefundCode(Integer.toString(swd.getWalletId()));
newRecord.setAmount(amount);
newRecord.setStatus((byte) 0);
newRecord.setSerialNo(Integer.toString(swd.getId()));
newRecord.setOrderType(0);
int timeStamp = (int) (new Date().getTime() / 1000);
newRecord.setCreateTime(timeStamp);
newRecord.setUpdateTime(timeStamp);
logger.info("商品下架,添加退款记录,bean={}", newRecord);
ordersPayRefundMapper.insert(newRecord);
logger.info("商品下架,添加退款记录成功,orderCode={}", swd.getOrderCode());
}
}
... ...
... ... @@ -616,6 +616,114 @@ public class PaymentServiceImpl implements IPaymentService {
}
}
@Database(ForceMaster = true)
public boolean transAllEarnest(long orderCode, Integer uid, BigDecimal amount, String alipayAccount){
logger.info("退还商家所有保证金,转账开始,orderCode = {}, uid={}, amount={}, alipayAccount={}", orderCode, uid, amount, alipayAccount);
int now = (int) (System.currentTimeMillis()/1000);
// 查看是否已经有转账记录
OrdersPayTransfer exist = ordersPayTransferMapper.selectByBuyerOrderCode(orderCode);
if (exist != null) {
logger.warn("transAllEarnestErr OrdersPayTransfer has exist, orderCode is {}", orderCode);
throw new ServiceException(400, "订单已经处理过");
}
OrdersPayTransfer transfer = new OrdersPayTransfer();
transfer.setBuyerOrderCode(orderCode);
transfer.setSellerOrderCode(orderCode);
transfer.setAlipayTradeId("");
transfer.setAlipayTradeResult("");
transfer.setAlipayAccount(alipayAccount);
transfer.setTransferType(6);
transfer.setUid(uid);
transfer.setAmount(amount);
transfer.setCreateTime(now);
transfer.setStatus(0);
try {
logger.info("transAllEarnest插入初始化转账信息orderCode is {}", orderCode);
ordersPayTransferMapper.insert(transfer);
} catch (Exception e) {
logger.warn("transAllEarnestErr insert ordersPayTransfer failed, orderCode is {}, msg is {}", orderCode, e.getMessage());
throw new ServiceException(400, "交易记录创建失败");
}
// 增加流水记录
TradeBills record = new TradeBills();
record.setUid(uid);
record.setOrderCode(orderCode);
record.setUserType(2);
record.setPayType(1);
record.setTradeType(1);
record.setIncomeOutcome(1);
record.setTradeStatus(0);
record.setCreateTime(now);
record.setAmount(amount);
record.setSystemAmount(amount.multiply(new BigDecimal("-1")));
if (amount == null || amount.compareTo(new BigDecimal("0.1")) < 0) {
logger.warn("transAllEarnestErr transferMon计算费用结果为 {}, 小于0.1或为null", amount);
alarm("转账金额小于0.1或为null", "ufo.order.transAllEarnest", "退还商家入驻所有金额,订单号:" + orderCode + "操作类型(" + 6 + ")计算金额结果为" + amount);
record.setTradeStatus(202);
addTradeBills(record);
throw new ServiceException(400, "不合法的金额:" + amount);
}
if (StringUtils.isBlank(alipayAccount)) {
logger.warn("transAllEarnestErr uid {} 支付宝账号不合法", alipayAccount);
record.setTradeStatus(201);
addTradeBills(record);
throw new ServiceException(400, "uid[" + uid + "]支付宝账号不合法");
}
logger.info("transAllEarnest参数检查成功!");
// 转账
try {
logger.info("transAllEarnest开始调用阿里接口参数buyerOrderCode={}, alipayAccount={}, transferAmount={}", orderCode, alipayAccount, amount);
JSONObject jsonObject = alipayService.transferMoney(Long.toString(orderCode), alipayAccount, amount);
if (jsonObject == null) {
logger.warn("transAllEarnestErr 转账失败 , orderCode is {}", orderCode);
transfer.setStatus(3);
throw new ServiceException(500, "转账失败:阿里接口返回null");
}
transfer.setAlipayTradeResult(jsonObject.toJSONString());
// 成功
Integer code = null;
String orderId = null;
if (jsonObject.containsKey("code")
&& (code = jsonObject.getInteger("code")) == 10000
&& jsonObject.containsKey("order_id")
&& StringUtils.isNotBlank(orderId = jsonObject.getString("order_id"))) {
logger.info("转账成功,targeAccount is {}, amount is {}", alipayAccount, amount);
transfer.setAlipayTradeId(orderId);
transfer.setStatus(1);
return true;
} else {
logger.warn("transAllEarnestErr 返回code或者order_id不是成功状态,code={}, orderId={}", code, orderId);
throw new ServiceException(500, "转账失败:code或者order_id不是成功状态");
}
} catch (Exception e) {
logger.warn("transAllEarnestErr 转账失败 , orderCode is {}, msg is {}", orderCode, e.getMessage());
alarm("转账失败", "ufo.order.transAllEarnest", "订单号:" + orderCode + "操作类型(" + 6 + ")转账失败,msg=" + e.getMessage());
transfer.setStatus(3);
if(e instanceof ServiceException) {
throw new ServiceException(((ServiceException) e).getCode(), e.getMessage());
} else {
throw new ServiceException(500, "转账失败");
}
} finally {
logger.info("transAllEarnest最后更新状态 status= {}", transfer.getStatus());
transfer.setUpdateTime(now);
ordersPayTransferMapper.updateByPrimaryKeySelective(transfer);
// 100:成功;201:没有支付宝账号;202:金额不合法;299:转账失败
record.setTradeStatus(transfer.getStatus() == 1 ? 100 : 299);
addTradeBills(record);
logger.info("transAllEarnest最后更新状态完成,转账结束, orderCode is {}!", orderCode);
}
}
/**
* 专门提供给鉴定通过,不通过的校验
* @param request
... ...