|
|
package com.yohoufo.order.service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
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;
|
...
|
...
|
@@ -9,26 +21,26 @@ 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.yohobuy.ufo.model.order.resp.PageResp;
|
|
|
import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
|
|
|
import com.yohoufo.common.caller.UfoServiceCaller;
|
|
|
import com.yohoufo.common.utils.TimeUtils;
|
|
|
import com.yohoufo.dal.order.*;
|
|
|
import com.yohoufo.dal.order.model.*;
|
|
|
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;
|
|
|
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;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
|
...
|
...
|
@@ -102,6 +114,7 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
sw = new SellerWallet();
|
|
|
sw.setUid(orderInfo.getUid());
|
|
|
sw.setAmount(orderInfo.getAmount());
|
|
|
sw.setLockAmount(BigDecimal.ZERO);
|
|
|
sw.setStatus(1);
|
|
|
sw.setType(1);
|
|
|
sw.setCreateTime(TimeUtils.getTimeStampSecond());
|
...
|
...
|
@@ -141,8 +154,12 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(orderInfo.getAmount());
|
|
|
swd.setIsBatch(0);
|
|
|
swd.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
swd.setUpdateTime(0);
|
|
|
JSONObject att = new JSONObject();
|
|
|
att.put("payment", orderInfo.getPayment());
|
|
|
swd.setAttachValue(att.toJSONString());
|
|
|
logger.info("充值后,处理增加钱包明细,bean={}", swd);
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
logger.info("充值后,处理增加钱包明细成功,orderCode={}", orderInfo.getOrderCode());
|
...
|
...
|
@@ -293,25 +310,86 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
return buildOrderInfo(buyerOrder);
|
|
|
}
|
|
|
|
|
|
public com.yohoufo.common.ApiResponse getMerchantWalletInfo(Integer uid) {
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
|
|
|
if (sw == null) {
|
|
|
return new com.yohoufo.common.ApiResponse(400, "钱包不存在", null);
|
|
|
}
|
|
|
if (sw.getStatus() == null || sw.getStatus() == 0) {
|
|
|
return new com.yohoufo.common.ApiResponse(400, "钱包不可用", null);
|
|
|
}
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("all", sw.getAmount().add(sw.getLockAmount()));
|
|
|
result.put("used", sw.getLockAmount());
|
|
|
result.put("avail", sw.getAmount());
|
|
|
return new com.yohoufo.common.ApiResponse(200, "查询成功", result);
|
|
|
}
|
|
|
|
|
|
public com.yohoufo.common.ApiResponse getMerchantWalletDetailInfo(Integer uid, int page, int limit) {
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
|
|
|
if (sw == null) {
|
|
|
return new com.yohoufo.common.ApiResponse(400, "钱包不存在", null);
|
|
|
}
|
|
|
if (sw.getStatus() == null || sw.getStatus() == 0) {
|
|
|
return new com.yohoufo.common.ApiResponse(400, "钱包不可用", null);
|
|
|
}
|
|
|
int count = sellerWalletDetailMapper.selectUserDetailCount(uid);
|
|
|
PageResp<JSONObject> pageResp = new PageResp<>();
|
|
|
pageResp.setPage(page);
|
|
|
pageResp.setPageSize(limit);
|
|
|
pageResp.setTotal(count);
|
|
|
pageResp.setPagetotal((int) Math.ceil(count * 1.0 / limit));
|
|
|
if (count == 0) {
|
|
|
pageResp.setData(new ArrayList<>());
|
|
|
return new com.yohoufo.common.ApiResponse(200, "查询成功", pageResp);
|
|
|
} else {
|
|
|
List<SellerWalletDetail> resultList = sellerWalletDetailMapper.selectUserDetailCount(uid,
|
|
|
(page - 1) * limit, count);
|
|
|
List<JSONObject> array = new ArrayList<>();
|
|
|
pageResp.setData(array);
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
|
|
|
for (SellerWalletDetail detail : resultList) {
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("amount", detail.getAmount());
|
|
|
result.put("type", sw.getType());
|
|
|
result.put("info", SellerWalletDetail.Type.of(sw.getType()).getName());
|
|
|
result.put("time", sdf.format(new Date(1000L * detail.getCreateTime())));
|
|
|
array.add(result);
|
|
|
}
|
|
|
return new com.yohoufo.common.ApiResponse(200, "查询成功", pageResp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 支付保证金:总账修改
|
|
|
public SellerWallet useEarnest(Integer uid, BigDecimal money) {
|
|
|
return changeEarnest(uid, money, "支付保证金", 2);
|
|
|
public SellerWallet useEarnest(Integer uid, BigDecimal money, String attach) {
|
|
|
return changeEarnest(uid, money, 0L, attach, SellerWalletDetail.Type.PUBLISH);
|
|
|
}
|
|
|
|
|
|
// 修改价格保证金差价:总账修改
|
|
|
public SellerWallet changePriceUseEarnest(Integer uid, BigDecimal money) {
|
|
|
return changeEarnest(uid, money, "支付保证金(修改价格)", 4);
|
|
|
public SellerWallet changePriceUseEarnest(Integer uid, BigDecimal money, String attach) {
|
|
|
if(money.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
return changeEarnest(uid, money, 0L, attach, SellerWalletDetail.Type.ADD_PRICE);
|
|
|
} else {
|
|
|
return changeEarnest(uid, money, 0L, attach, SellerWalletDetail.Type.SUBTRACT_PRICE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 退还保证金:总账修改
|
|
|
public SellerWallet refundEarnest(Integer uid, BigDecimal money) {
|
|
|
return changeEarnest(uid, money, "退还保证金", 3);
|
|
|
// 卖家下架、系统下架、鉴定通过、买家取消
|
|
|
public SellerWallet refundEarnest(Integer uid, BigDecimal money, String attach, long orderCode, SellerWalletDetail.Type type) {
|
|
|
return changeEarnest(uid, money, orderCode, attach, type);
|
|
|
}
|
|
|
|
|
|
// 处罚保证金:总账修改
|
|
|
// 卖家取消、鉴定失败
|
|
|
public SellerWallet punishEarnest(Integer uid, BigDecimal money, String attach, SellerWalletDetail.Type type) {
|
|
|
return changeEarnest(uid, money, 0L, attach, type);
|
|
|
}
|
|
|
|
|
|
// 支付保证金:明细记录+orders_pay记录
|
|
|
public void useEarnestAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList) {
|
|
|
for(OrderInfo oi : orderList) {
|
|
|
SellerWalletDetail swd = addUseEarnestDetail(sw, oi.getOrderCode(), oi.getAmount(), 1);
|
|
|
SellerWalletDetail swd = addWalletDetail(sw, oi.getOrderCode(), oi.getAmount(), 1, SellerWalletDetail.Type.PUBLISH);
|
|
|
addOrdersPay(swd);
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -319,15 +397,21 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
// 修改价格:明细记录+orders_pay金额修改
|
|
|
public void changePriceAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList) {
|
|
|
for(OrderInfo oi : orderList) {
|
|
|
SellerWalletDetail swd = addChangePriceWalletDetail(sw, oi.getOrderCode(), oi.getAmount(), 1);
|
|
|
SellerWalletDetail.Type t;
|
|
|
if (orderList.get(0).getAmount().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
t = SellerWalletDetail.Type.ADD_PRICE;
|
|
|
} else {
|
|
|
t = SellerWalletDetail.Type.SUBTRACT_PRICE;
|
|
|
}
|
|
|
SellerWalletDetail swd = addWalletDetail(sw, oi.getOrderCode(), oi.getAmount(), 1, t);
|
|
|
changePriceUpdateOrdersPay(swd);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 下架商品:明细记录+退款表记录
|
|
|
public void unshelveAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList) {
|
|
|
// 下架商品(卖家+系统):明细记录+退款表记录
|
|
|
public void unshelveAddWalletDetail(SellerWallet sw, List<OrderInfo> orderList, SellerWalletDetail.Type type) {
|
|
|
for(OrderInfo oi : orderList) {
|
|
|
SellerWalletDetail swd = refundEarnestDetail(sw, oi.getOrderCode(), oi.getAmount(), 1);
|
|
|
SellerWalletDetail swd = addWalletDetail(sw, oi.getOrderCode(), oi.getAmount(), 1, type);
|
|
|
addRefundRecord(swd, oi.getAmount());
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -345,23 +429,6 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
return paymentService.transAllEarnest(orderCode, uid, money, alipayAccount);
|
|
|
}
|
|
|
|
|
|
// 退还保证金
|
|
|
public SellerWalletDetail refundEarnestDetail(SellerWallet sw, long orderCode, BigDecimal amount, Integer isBatch) {
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(3); // 单笔退还
|
|
|
swd.setUid(sw.getUid());
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(amount);
|
|
|
swd.setIsBatch(isBatch);
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
swd.setUpdateTime(0);
|
|
|
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);
|
...
|
...
|
@@ -391,7 +458,7 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
// 增加充值记录
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(5);// 退还所有保证金
|
|
|
swd.setType(SellerWalletDetail.Type.MERCHANT_EXIT.getValue());// 退还所有保证金
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(amount);
|
...
|
...
|
@@ -404,47 +471,30 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
return amount;
|
|
|
}
|
|
|
|
|
|
// 支付保证金:明细
|
|
|
private SellerWalletDetail addUseEarnestDetail(SellerWallet sw, long orderCode, BigDecimal amount, Integer isBatch) {
|
|
|
Integer uid = sw.getUid();
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(2);
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(amount);
|
|
|
swd.setIsBatch(isBatch);
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
swd.setUpdateTime(0);
|
|
|
logger.info("支付保证金,钱包明细记录,bean={}", swd);
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
logger.info("支付保证金,钱包明细记录成功,bean={}", swd);
|
|
|
return swd;
|
|
|
}
|
|
|
|
|
|
// 修改价格支付保证金:明细
|
|
|
private SellerWalletDetail addChangePriceWalletDetail(SellerWallet sw, long orderCode, BigDecimal amount, Integer isBatch) {
|
|
|
// 增加:明细
|
|
|
private SellerWalletDetail addWalletDetail(SellerWallet sw, long orderCode, BigDecimal amount, Integer isBatch, SellerWalletDetail.Type type) {
|
|
|
Integer uid = sw.getUid();
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(4);
|
|
|
swd.setType(type.getValue());
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(amount);
|
|
|
swd.setIsBatch(isBatch);
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
swd.setUpdateTime(0);
|
|
|
logger.info("修改价格支付保证金,钱包明细记录,bean={}", swd);
|
|
|
logger.info("增加保证金明细({}),bean={}", type.getName(), swd);
|
|
|
sellerWalletDetailMapper.insert(swd);
|
|
|
logger.info("修改价格支付保证金,钱包明细记录成功,bean={}", swd);
|
|
|
logger.info("增加保证金明细({}),bean={}", type.getName(), swd);
|
|
|
return swd;
|
|
|
}
|
|
|
|
|
|
private SellerWallet changeEarnest(Integer uid, BigDecimal money, String message, int type) {
|
|
|
logger.info("{}修改钱包表入口,uid={},money={}", message, uid, money);
|
|
|
private SellerWallet changeEarnest(Integer uid, BigDecimal money, long orderCode, String attach, SellerWalletDetail.Type type) {
|
|
|
|
|
|
logger.info("{}修改钱包表入口,uid={},money={}", type.getName(), uid, money);
|
|
|
String message = type.getName();
|
|
|
// 使用钱包余额,支付保证金
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
|
|
|
|
|
|
if (sw == null) {
|
|
|
logger.error(message + "失败,钱包不存在,uid={}", uid);
|
|
|
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "钱包不存在uid=" + uid+", 金额为:"+money);
|
...
|
...
|
@@ -455,15 +505,34 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "钱包不可用uid=" + uid+", 金额为:"+money);
|
|
|
return null;
|
|
|
}
|
|
|
if ((type == 2 || type == 4) && sw.getAmount().compareTo(money) < 0) {
|
|
|
if ((type == SellerWalletDetail.Type.PUBLISH || type == SellerWalletDetail.Type.ADD_PRICE) && sw.getAmount().compareTo(money) < 0) {
|
|
|
logger.error(message + "{}失败,钱包余额不足,uid={}", money, uid);
|
|
|
CommonAlarmEventPublisher.publish(message + "失败", "ufo.order.changePrice", "钱包余额不足uid=" + uid+", 金额为:"+money+", 账户余额为:"+sw.getAmount());
|
|
|
return null;
|
|
|
}
|
|
|
if (type == 2 || type == 4) {
|
|
|
|
|
|
sw.setAmount(null);
|
|
|
sw.setLockAmount(null);
|
|
|
|
|
|
// 减去保证金
|
|
|
if (type == SellerWalletDetail.Type.PUBLISH
|
|
|
|| type == SellerWalletDetail.Type.ADD_PRICE
|
|
|
|| type == SellerWalletDetail.Type.SUBTRACT_PRICE) {
|
|
|
sw.setAmount(money.multiply(new BigDecimal("-1")));
|
|
|
} else {
|
|
|
sw.setLockAmount(money);
|
|
|
} else if (type == SellerWalletDetail.Type.SELLER_OFF
|
|
|
|| type == SellerWalletDetail.Type.SYSTEM_OFF
|
|
|
|| type == SellerWalletDetail.Type.APPRAISE_OK
|
|
|
|| type == SellerWalletDetail.Type.BUYER_CANCEL_DELIVERY
|
|
|
|| type == SellerWalletDetail.Type.BUYER_CANCEL_NO_DELIVERY) {
|
|
|
// 增加保证金
|
|
|
sw.setAmount(money);
|
|
|
sw.setLockAmount(money.multiply(new BigDecimal("-1")));
|
|
|
} else if (type == SellerWalletDetail.Type.SELLER_CANCEL
|
|
|
|| type == SellerWalletDetail.Type.SELLER_OVER_TIME
|
|
|
|| type == SellerWalletDetail.Type.APPRAISE_FAIL) {
|
|
|
// 处罚保证金
|
|
|
sw.setLockAmount(money.multiply(new BigDecimal("-1")));
|
|
|
}
|
|
|
sw.setUpdateTime(TimeUtils.getTimeStampSecond());
|
|
|
logger.info("{}修改钱包表余额,bean={}", sw);
|
...
|
...
|
@@ -475,12 +544,13 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { |
|
|
logger.info("{}修改钱包表余额成功,bean={}", sw);
|
|
|
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(0L);
|
|
|
swd.setType(type);
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(type.getValue());
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(sw.getId());
|
|
|
swd.setAmount(money);
|
|
|
swd.setIsBatch(0);
|
|
|
swd.setAttachValue(attach);
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
swd.setUpdateTime(0);
|
|
|
logger.info("增加钱包明细汇总记录,bean={}", swd);
|
...
|
...
|
|