Authored by chenchao

optimized

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 java.util.Map;
import java.util.Objects;
import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.order.common.EnterQuitEnum;
import com.yohoufo.order.event.InviteRecordEvent;
import com.yohoufo.order.service.impl.TradeBillsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.common.utils.DateUtil;
... ... @@ -31,31 +12,33 @@ import com.yohobuy.ufo.model.order.bo.OrderInfo;
import com.yohobuy.ufo.model.order.common.EntrySellerType;
import com.yohobuy.ufo.model.order.common.OrderCodeType;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import com.yohobuy.ufo.model.order.common.Payment;
import com.yohobuy.ufo.model.order.resp.EntryThreshold;
import com.yohobuy.ufo.model.order.resp.PageResp;
import com.yohobuy.ufo.model.user.resp.AuthorizeResultRespVO;
import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
import com.yohoufo.common.alarm.EventBusPublisher;
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.SellerJoinHistoryMapper;
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.SellerJoinHistory;
import com.yohoufo.dal.order.model.SellerWallet;
import com.yohoufo.dal.order.model.SellerWalletDetail;
import com.yohoufo.dal.order.model.StoredSeller;
import com.yohoufo.dal.order.*;
import com.yohoufo.dal.order.model.*;
import com.yohoufo.dal.user.model.ZhiMaCert;
import com.yohobuy.ufo.model.order.common.Payment;
import com.yohoufo.order.common.EnterQuitEnum;
import com.yohoufo.order.event.InviteRecordEvent;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.response.PrepayResponse;
import com.yohoufo.order.service.impl.PaymentServiceImpl;
import com.yohoufo.order.service.impl.SellerService;
import com.yohoufo.order.service.impl.TradeBillsService;
import org.apache.commons.lang3.StringUtils;
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.text.SimpleDateFormat;
import java.util.*;
@Service
public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
... ... @@ -139,6 +122,84 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
return result;
}
private void processFirstTimeCharge(SellerWallet sw, OrderInfo orderInfo){
Long orderCode = orderInfo.getOrderCode();
BigDecimal amount = orderInfo.getAmount();
Integer uid = orderInfo.getUid();
ZhiMaCert cert = null;
try {
ApiResponse<?> resp = ufoServiceCaller.call("ufo.user.zhiMaCertInfo", ApiResponse.class, uid);
cert = (ZhiMaCert) resp.getData();
} catch (Exception e) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "没有芝麻认证信息:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);
logger.error("重新入驻首次充值保证金={},没有芝麻认证信息,uid={}", amount, uid);
return;
}
if(cert == null || StringUtils.isBlank(cert.getCertNo())) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "芝麻认证信息有误:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);
logger.error("重新入驻首次充值保证金={},没有芝麻认证信息有误,uid={}", amount, uid);
return;
}
String certNo = cert.getCertNo();
if (sw != null) {
logger.info("重新入驻首次充值保证金={},已入驻次数为,uid={}", amount, uid, sw.getJoinTimes());
if (sw.getStatus() == null || sw.getStatus() != 0) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "入驻状态异常:uid=" + uid + ",充值订单号为:" + orderCode + ", 充值金额为:" + amount);
logger.error("重新入驻首次充值保证金={},发现入驻状态异常,uid={},times={}", amount, uid, sw.getJoinTimes());
return;
}
if (!canRejoin(uid, sw, cert)) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "超过入最大驻次数:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);
logger.error("重新入驻首次充值保证金={},发现已超过最大入驻次数,uid={},times={}", amount, uid, sw.getJoinTimes());
return;
}
// 更新钱包
logger.info("重新入驻首次充值保证金={},更新钱包信息,uid={}", amount, uid);
int exe = sellerWalletMapper.rejoin(uid, amount, null, TimeUtils.getTimeStampSecond(), sw.getJoinTimes() + 1);
if (exe == 0) {
CommonAlarmEventPublisher.publish("重新入驻修改钱包失败", "ufo.order.recharge", "更新钱包不成功:uid=" + uid + ",充值订单号为:" + orderCode + ", 充值金额为:" + amount+ ",钱包信息为:"+sw);
logger.info("重新入驻失败,首次充值保证金={},更新钱包信息失败,uid={}, 钱包信息={}", amount, uid, sw);
return;
}
// 数据清理
// 钱包明细
logger.info("重新入驻orderCode={},清理历史钱包明细数据,uid={}", orderCode, uid);
int clearCount = sellerWalletDetailMapper.clearUserHistoryData(uid);
logger.info("重新入驻orderCode={},清理历史钱包明细数据={},uid={}", orderCode, clearCount, uid);
// 充值信息
logger.info("重新入驻orderCode={},清理充值表数据,uid={}", orderCode, uid);
clearCount = entrySellerRechargeOrderMapper.clearUserHistoryData(uid, orderCode);
logger.info("重新入驻orderCode={},清理充值表数据={},uid={}", orderCode, clearCount, uid);
// 入驻信息
logger.info("重新入驻orderCode={},清理历史入驻表数据,uid={}", orderCode, uid);
clearCount = storedSellerService.removeStoredSeller(uid);
logger.info("重新入驻orderCode={},清理历史入驻表数据={},uid={}", orderCode, clearCount, uid);
} else {
// 首次充值,创建钱包信息
sw = new SellerWallet();
sw.setUid(uid);
sw.setAmount(amount);
sw.setLockAmount(BigDecimal.ZERO);
sw.setStatus(1);
sw.setType(1);
sw.setCreateTime(TimeUtils.getTimeStampSecond());
sw.setUpdateTime(0);
sw.setJoinTimes(0);
logger.info("首次充值后回调,处理总账表新增账号,bean={}", sw);
sellerWalletMapper.insert(sw);
logger.info("首次充值后回调,处理总账表新增账号成功,bean={}", sw);
// 首次入驻
InviteRecordEvent inviteRecordEvent = InviteRecordEvent.builder().uid(uid).orderCode(orderCode).eventType(EnterQuitEnum.ENTER).build();
EventBusPublisher.publishEvent(inviteRecordEvent);
}
addJoinHistory(uid, certNo);
}
@Override
public void processAfterPay(OrderInfo orderInfo) {
logger.info("充值后回调,处理processAfterPay,orderInfo={}", orderInfo);
... ... @@ -147,92 +208,20 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
Integer payment = orderInfo.getPayment();
tradeBillsService.addPayRechargeEnsureRecord(orderInfo.getUid(), orderInfo.getSkup(), orderCode, payment,
orderInfo.getAmount());
BigDecimal beforeAmount = BigDecimal.ZERO;
BigDecimal beforeLockAmount = BigDecimal.ZERO;
BigDecimal afterAmount = BigDecimal.ZERO;
BigDecimal afterLockAmount = BigDecimal.ZERO;
Integer uid = orderInfo.getUid();
BigDecimal beforeAmount = BigDecimal.ZERO;
BigDecimal beforeLockAmount = BigDecimal.ZERO;
BigDecimal afterAmount = BigDecimal.ZERO;
BigDecimal afterLockAmount = BigDecimal.ZERO;
Integer uid = orderInfo.getUid();
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
logger.info("充值后回调,处理总账表,orderCode={}", orderCode);
if (orderInfo.getType() == TYPE_FIRST_RECHARGE) {
ZhiMaCert cert = null;
try {
ApiResponse<?> resp = ufoServiceCaller.call("ufo.user.zhiMaCertInfo", ApiResponse.class, uid);
cert = (ZhiMaCert) resp.getData();
} catch (Exception e) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "没有芝麻认证信息:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);
logger.error("重新入驻首次充值保证金={},没有芝麻认证信息,uid={}", amount, uid);
return;
}
if(cert == null || StringUtils.isBlank(cert.getCertNo())) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "芝麻认证信息有误:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);
logger.error("重新入驻首次充值保证金={},没有芝麻认证信息有误,uid={}", amount, uid);
return;
}
String certNo = cert.getCertNo();
if (sw != null) {
logger.info("重新入驻首次充值保证金={},已入驻次数为,uid={}", amount, uid, sw.getJoinTimes());
if (sw.getStatus() == null || sw.getStatus() != 0) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "入驻状态异常:uid=" + uid + ",充值订单号为:" + orderCode + ", 充值金额为:" + amount);
logger.error("重新入驻首次充值保证金={},发现入驻状态异常,uid={},times={}", amount, uid, sw.getJoinTimes());
return;
}
if (!canRejoin(uid, sw, cert)) {
CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "超过入最大驻次数:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);
logger.error("重新入驻首次充值保证金={},发现已超过最大入驻次数,uid={},times={}", amount, uid, sw.getJoinTimes());
return;
}
// 更新钱包
logger.info("重新入驻首次充值保证金={},更新钱包信息,uid={}", amount, uid);
int exe = sellerWalletMapper.rejoin(uid, amount, null, TimeUtils.getTimeStampSecond(), sw.getJoinTimes() + 1);
if (exe == 0) {
CommonAlarmEventPublisher.publish("重新入驻修改钱包失败", "ufo.order.recharge", "更新钱包不成功:uid=" + uid + ",充值订单号为:" + orderCode + ", 充值金额为:" + amount+ ",钱包信息为:"+sw);
logger.info("重新入驻失败,首次充值保证金={},更新钱包信息失败,uid={}, 钱包信息={}", amount, uid, sw);
return;
}
// 数据清理
// 钱包明细
logger.info("重新入驻orderCode={},清理历史钱包明细数据,uid={}", orderCode, uid);
int clearCount = sellerWalletDetailMapper.clearUserHistoryData(uid);
logger.info("重新入驻orderCode={},清理历史钱包明细数据={},uid={}", orderCode, clearCount, uid);
// 充值信息
logger.info("重新入驻orderCode={},清理充值表数据,uid={}", orderCode, uid);
clearCount = entrySellerRechargeOrderMapper.clearUserHistoryData(uid, orderCode);
logger.info("重新入驻orderCode={},清理充值表数据={},uid={}", orderCode, clearCount, uid);
// 入驻信息
logger.info("重新入驻orderCode={},清理历史入驻表数据,uid={}", orderCode, uid);
clearCount = storedSellerService.removeStoredSeller(uid);
logger.info("重新入驻orderCode={},清理历史入驻表数据={},uid={}", orderCode, clearCount, uid);
} else {
// 首次充值,创建钱包信息
sw = new SellerWallet();
sw.setUid(uid);
sw.setAmount(amount);
sw.setLockAmount(BigDecimal.ZERO);
sw.setStatus(1);
sw.setType(1);
sw.setCreateTime(TimeUtils.getTimeStampSecond());
sw.setUpdateTime(0);
sw.setJoinTimes(0);
logger.info("首次充值后回调,处理总账表新增账号,bean={}", sw);
sellerWalletMapper.insert(sw);
logger.info("首次充值后回调,处理总账表新增账号成功,bean={}", sw);
// 首次入驻
InviteRecordEvent inviteRecordEvent = InviteRecordEvent.builder().uid(uid).orderCode(orderCode).eventType(EnterQuitEnum.ENTER).build();
EventBusPublisher.publishEvent(inviteRecordEvent);
}
addJoinHistory(uid, certNo);
afterAmount = amount;
processFirstTimeCharge(sw, orderInfo);
afterAmount = amount;
} else {
// 修改钱包余额
if (sw == null) {
... ... @@ -1040,7 +1029,7 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
newRecord.setStatus((byte) 0);
newRecord.setSerialNo(Integer.toString(swd.getId()));
newRecord.setOrderType(0);
int timeStamp = (int) (new Date().getTime() / 1000);
int timeStamp = DateUtil.currentTimeSeconds();
newRecord.setCreateTime(timeStamp);
newRecord.setUpdateTime(timeStamp);
logger.info("商品下架,添加退款记录,bean={}", newRecord);
... ...