|
|
package com.yohoufo.order.service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.ufo.model.order.bo.OrderInfo;
|
|
|
import com.yohobuy.ufo.model.order.common.OrderCodeType;
|
|
|
import com.yohobuy.ufo.model.order.common.OrderStatus;
|
|
|
import com.yohobuy.ufo.model.order.common.TabType;
|
|
|
import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
|
|
|
import com.yohoufo.common.utils.TimeUtils;
|
|
|
import com.yohoufo.dal.order.EntrySellerRechargeOrderMapper;
|
|
|
import com.yohoufo.dal.order.OrdersPayMapper;
|
|
|
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.SellerWallet;
|
|
|
import com.yohoufo.dal.order.model.SellerWalletDetail;
|
|
|
import com.yohoufo.order.common.Payment;
|
|
|
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;
|
|
|
|
|
|
@Service
|
|
|
public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
private static final int TYPE_FIRST_RECHARGE = 1;
|
|
|
|
|
|
@Autowired
|
|
|
SellerWalletMapper sellerWalletMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerWalletDetailMapper sellerWalletDetailMapper;
|
|
|
|
|
|
@Autowired
|
|
|
EntrySellerRechargeOrderMapper entrySellerRechargeOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
OrdersPayMapper ordersPayDao;
|
|
|
|
|
|
@Autowired
|
|
|
private CacheCleaner cacheCleaner;
|
|
|
|
|
|
/**
|
|
|
* 更新订单状态
|
|
|
* @param orderInfo
|
|
|
*/
|
|
|
public void updateOrderStatusPaid(OrderInfo orderInfo) {
|
|
|
EntrySellerRechargeOrder order = new EntrySellerRechargeOrder();
|
|
|
order.setOrderCode(orderInfo.getOrderCode());
|
|
|
order.setStatus(OrderStatus.HAS_PAYED.getCode());
|
|
|
order.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
// order.setPayment(orderInfo.getPayment());
|
|
|
entrySellerRechargeOrderMapper.updateByOrderCode(order);
|
|
|
orderInfo.setStatus(OrderStatus.HAS_PAYED.getCode());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void processAfterPay(OrderInfo orderInfo) {
|
|
|
// 首次充值,调用认证成功接口
|
|
|
if (orderInfo.getType() == TYPE_FIRST_RECHARGE) {
|
|
|
// TODO
|
|
|
}
|
|
|
BigDecimal amount = orderInfo.getAmount();
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(orderInfo.getUid(), 1);
|
|
|
if (orderInfo.getType() == TYPE_FIRST_RECHARGE) {
|
|
|
if (sw != null) {
|
|
|
logger.error("首次充值保证金余额{},发现钱包已存在,uid={}", amount, orderInfo.getUid());
|
|
|
CommonAlarmEventPublisher.publish("首次充值保证金余额失败", "ufo.order.recharge", "钱包已存在:uid=" + orderInfo.getUid() + ",充值订单号为:" + orderInfo.getOrderCode()+", 充值金额为:"+amount);
|
|
|
return;
|
|
|
}
|
|
|
// 首次充值,创建钱包信息
|
|
|
sw = new SellerWallet();
|
|
|
sw.setUid(orderInfo.getUid());
|
|
|
sw.setAmount(orderInfo.getAmount());
|
|
|
sw.setStatus(1);
|
|
|
sw.setType(1);
|
|
|
sw.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
sw.setUpdateTime(0);
|
|
|
sellerWalletMapper.insert(sw);
|
|
|
} else {
|
|
|
// 修改钱包余额
|
|
|
if (sw == null) {
|
|
|
logger.error("充值保证金余额{},钱包不存在,uid={}", amount, orderInfo.getUid());
|
|
|
CommonAlarmEventPublisher.publish("充值保证金余额失败", "ufo.order.recharge", "钱包不存在uid=" + orderInfo.getUid() + ",充值订单号为:" + orderInfo.getOrderCode()+", 充值金额为:"+amount);
|
|
|
return;
|
|
|
}
|
|
|
if (sw.getStatus() == null || sw.getStatus() == 0) {
|
|
|
logger.error("充值保证金余额{},钱包不可用,uid={}", amount, orderInfo.getUid());
|
|
|
CommonAlarmEventPublisher.publish("充值保证金余额失败", "ufo.order.recharge", "钱包不可用uid=" + orderInfo.getUid() + ",充值订单号为:" + orderInfo.getOrderCode()+", 充值金额为:"+amount);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
sw.setAmount(orderInfo.getAmount());
|
|
|
sw.setUpdateTime(TimeUtils.getTimeStampSecond());
|
|
|
if (sellerWalletMapper.addMoney(sw) == 0) {
|
|
|
logger.error("充值保证金余额{},更新表返回0,uid={}", amount, orderInfo.getUid());
|
|
|
CommonAlarmEventPublisher.publish("充值保证金余额失败", "ufo.order.recharge", "更新表返回0,uid=" + orderInfo.getUid() + ",充值订单号为:" + orderInfo.getOrderCode()+", 充值金额为:"+amount);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 增加充值记录
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderInfo.getOrderCode());
|
|
|
swd.setType(1);
|
|
|
swd.setUid(orderInfo.getUid());
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(orderInfo.getAmount());
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
|
|
|
cacheCleaner.delete(Arrays.asList(
|
|
|
CacheKeyBuilder.orderListKey(orderInfo.getUid(), TabType.BUY.getValue()),
|
|
|
CacheKeyBuilder.orderDetailKey(orderInfo.getUid(), TabType.BUY.getValue(), orderInfo.getOrderCode())));
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 订单是否是待支付状态
|
|
|
* @param orderInfo
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean isOrderWaitingPay(OrderInfo orderInfo){
|
|
|
|
|
|
if (orderInfo.getStatus() != null && orderInfo.getStatus().intValue() == OrderStatus.WAITING_PAY.getCode()){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 订单是 已支付的订单
|
|
|
* @param orderInfo
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean isOrderPaid(OrderInfo orderInfo){
|
|
|
|
|
|
if (orderInfo.getStatus() != null && orderInfo.getStatus().intValue() == OrderStatus.HAS_PAYED.getCode()){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
final static List<Integer> canRefundStatusList = Arrays.asList(OrderStatus.SELLER_CANCEL_AFTER_PAY.getCode(),
|
|
|
OrderStatus.SEND_OUT_TIMEOUT.getCode(), OrderStatus.CHECKING_FAKE.getCode());
|
|
|
|
|
|
@Override
|
|
|
public boolean canRefund(OrderInfo orderInfo) {
|
|
|
if (orderInfo.getStatus() != null && canRefundStatusList.contains(orderInfo.getStatus().intValue())){
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 保存前回预支付结果
|
|
|
* @param orderInfo
|
|
|
* @param response
|
|
|
*/
|
|
|
public void saveAppPayRequest(OrderInfo orderInfo, PrepayResponse response) {
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取前回的访问结果
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
public PrepayResponse getAppPayRequest(PaymentRequest request) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 检查状态
|
|
|
* @param orderInfo
|
|
|
*/
|
|
|
public void checkOrderStatus(OrderInfo orderInfo){
|
|
|
// 订单不是未付款的状态
|
|
|
if (orderInfo.getStatus() == null || orderInfo.getStatus().intValue() != OrderStatus.WAITING_PAY.getCode()){
|
|
|
logger.warn("payment status not allow {}, orderCode is {}, uid is {}", orderInfo.getStatus(),
|
|
|
orderInfo.getOrderCode(), orderInfo.getUid());
|
|
|
throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 预支付方式更新
|
|
|
* @param orderInfo
|
|
|
*/
|
|
|
public void updatePayment(OrderInfo orderInfo, int payment){
|
|
|
|
|
|
EntrySellerRechargeOrder order = new EntrySellerRechargeOrder();
|
|
|
order.setOrderCode(orderInfo.getOrderCode());
|
|
|
order.setUid(orderInfo.getUid());
|
|
|
// 更新预支付方式
|
|
|
order.setPayment(payment);
|
|
|
order.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
entrySellerRechargeOrderMapper.updateByOrderCode(order);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取订单情报
|
|
|
* @param orderCode
|
|
|
* @param uid
|
|
|
* @return
|
|
|
*/
|
|
|
public OrderInfo getOrderInfo(long orderCode, int uid) {
|
|
|
|
|
|
EntrySellerRechargeOrder order = entrySellerRechargeOrderMapper.selectByOrderCodeUid(orderCode, uid);
|
|
|
|
|
|
if (order == null){
|
|
|
logger.warn("getOrderInfo order not exist, orderCode is {}, uid is {}", orderCode, uid);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
return buildOrderInfo(order);
|
|
|
}
|
|
|
|
|
|
private OrderInfo buildOrderInfo(EntrySellerRechargeOrder order) {
|
|
|
OrderInfo orderInfo = OrderInfo.builder()
|
|
|
.id(order.getId())
|
|
|
.uid(order.getUid())
|
|
|
.orderCode(order.getOrderCode())
|
|
|
.payment(order.getPayment())
|
|
|
.status(order.getStatus())
|
|
|
.amount(order.getAmount())
|
|
|
.createTime(order.getCreateTime())
|
|
|
.orderCodeType(OrderCodeType.BUYER_TYPE)
|
|
|
.sourceStatus(order.getStatus())
|
|
|
.type(order.getType())
|
|
|
.build();
|
|
|
|
|
|
return orderInfo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取订单情报
|
|
|
* @param orderCode
|
|
|
* @return
|
|
|
*/
|
|
|
public OrderInfo getOrderInfo(long orderCode) {
|
|
|
EntrySellerRechargeOrder buyerOrder = entrySellerRechargeOrderMapper.selectByOrderCode(orderCode);
|
|
|
if (buyerOrder == null){
|
|
|
logger.warn("getOrderInfo order not exist, orderCode is {}", orderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
return buildOrderInfo(buyerOrder);
|
|
|
}
|
|
|
|
|
|
// 支付保证金:总账修改
|
|
|
public SellerWallet useEarnest(Integer uid, BigDecimal money) {
|
|
|
return changeEarnest(uid, money, "支付保证金");
|
|
|
}
|
|
|
|
|
|
// 修改价格保证金差价:总账修改
|
|
|
public SellerWallet changePriceUseEarnest(Integer uid, BigDecimal money) {
|
|
|
return changeEarnest(uid, money, "支付保证金(修改价格)");
|
|
|
}
|
|
|
|
|
|
// 退还保证金:总账修改
|
|
|
public SellerWallet cancelRefundEarnest(Integer uid, BigDecimal money) {
|
|
|
return changeEarnest(uid, money, "退还保证金");
|
|
|
}
|
|
|
|
|
|
// 支付保证金:明细
|
|
|
public void addUseEarnestDetail(SellerWallet sw, BigDecimal money) {
|
|
|
long orderCode = 0;
|
|
|
BigDecimal amount = null;
|
|
|
Integer uid = sw.getId();
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(2);
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(amount);
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
}
|
|
|
|
|
|
// 退还保证金
|
|
|
public void 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());
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
}
|
|
|
|
|
|
// 退出入驻:归还所有保证金 总账+明细
|
|
|
public BigDecimal returnEarnest(Integer uid, long 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();
|
|
|
|
|
|
if (sw.getStatus() == null || sw.getStatus() == 0) {
|
|
|
logger.error("退还所有保证金{},钱包不可用,uid={}", amount, uid);
|
|
|
CommonAlarmEventPublisher.publish("退还所有保证金失败", "ufo.order.recharge", "钱包不可用uid=" + uid+", 退还金额为:"+amount);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
sw.setAmount(amount);
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
// 增加充值记录
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(5);// 退还所有保证金
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(amount);
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
|
|
|
return amount;
|
|
|
}
|
|
|
|
|
|
private SellerWallet changeEarnest(Integer uid, BigDecimal money, String message) {
|
|
|
// 使用钱包余额,支付保证金
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
|
|
|
|
|
|
if (sw == null) {
|
|
|
logger.error(message + "失败,钱包不存在,uid={}", uid);
|
|
|
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.recharge", "钱包不存在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);
|
|
|
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());
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
sw.setAmount(money.multiply(new BigDecimal("-1")));
|
|
|
sw.setUpdateTime(TimeUtils.getTimeStampSecond());
|
|
|
if (sellerWalletMapper.addMoney(sw) == 0) {
|
|
|
logger.error(message + "失败,(并发)更新表返回0,uid={}", uid);
|
|
|
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.recharge", "(并发)更新表返回0,uid=" + uid + ", 金额为:"+ money);
|
|
|
return null;
|
|
|
}
|
|
|
return sw;
|
|
|
}
|
|
|
|
|
|
public void addOrdersPay(SellerWalletDetail swd) {
|
|
|
// 增加支付记录
|
|
|
OrdersPay ordersPay = new OrdersPay();
|
|
|
ordersPay.setOrderCode(swd.getOrderCode());
|
|
|
ordersPay.setUid(swd.getUid());
|
|
|
ordersPay.setPayment(Payment.WALLET.getCode());
|
|
|
ordersPay.setAmount(swd.getAmount());
|
|
|
ordersPay.setPayOrderCode(String.valueOf(swd.getOrderCode()));
|
|
|
ordersPay.setSerialNo(Integer.toString(swd.getId()));
|
|
|
ordersPay.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
|
|
|
JSONObject attach = new JSONObject();
|
|
|
attach.put("order_type", OrderCodeType.SELLER_TYPE.getType());
|
|
|
ordersPay.setAttach(attach.toJSONString());
|
|
|
ordersPayDao.insertSelective(ordersPay);
|
|
|
}
|
|
|
|
|
|
public void changePriceUpdateOrdersPay(SellerWalletDetail swd) {
|
|
|
// 增加支付记录
|
|
|
OrdersPay ordersPay = new OrdersPay();
|
|
|
ordersPay.setOrderCode(swd.getOrderCode());
|
|
|
ordersPay.setUid(swd.getUid());
|
|
|
ordersPay.setAmount(swd.getAmount());
|
|
|
String message = "修改保证金order_pay记录金额";
|
|
|
if (ordersPayDao.addMoney(ordersPay) == 0) {
|
|
|
logger.error(message + "{}失败,修改DB返回记录数为0,orderCode={}", swd.getAmount(), swd.getOrderCode());
|
|
|
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePriceOrdersPay", "修改DB返回记录数为0,orderCode=" + swd.getOrderCode()+", 金额为:"+swd.getAmount());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|