Authored by tanling

充值入驻增加重试

  1 +package com.yohoufo.order.event;
  2 +
  3 +import com.yohobuy.ufo.model.order.bo.OrderInfo;
  4 +import com.yohoufo.common.alarm.Event;
  5 +import lombok.AllArgsConstructor;
  6 +import lombok.Builder;
  7 +import lombok.Data;
  8 +import lombok.NoArgsConstructor;
  9 +
  10 +@Data
  11 +@Builder
  12 +@NoArgsConstructor
  13 +@AllArgsConstructor
  14 +public class EntrySellerRechargeEvent extends Event {
  15 +
  16 + OrderInfo orderInfo;
  17 +}
  1 +package com.yohoufo.order.service;
  2 +
  3 +import com.yohobuy.ufo.model.order.req.EntrySellerRecharge;
  4 +
  5 +public interface IEntrySellerRechargeService {
  6 +
  7 +
  8 +
  9 + /**
  10 + * 首次入驻
  11 + * @param entrySellerRecharge
  12 + */
  13 + void firstEntry(EntrySellerRecharge entrySellerRecharge);
  14 +
  15 +
  16 +
  17 + /**
  18 + * 重新入驻
  19 + * @param entrySellerRecharge
  20 + */
  21 + void againEntry(EntrySellerRecharge entrySellerRecharge);
  22 +
  23 +
  24 +
  25 + /**
  26 + * 充值
  27 + * @param entrySellerRecharge
  28 + */
  29 + void recharge(EntrySellerRecharge entrySellerRecharge);
  30 +
  31 +
  32 +
  33 +
  34 +
  35 +}
@@ -26,6 +26,7 @@ import com.yohoufo.dal.order.*; @@ -26,6 +26,7 @@ import com.yohoufo.dal.order.*;
26 import com.yohoufo.dal.order.model.*; 26 import com.yohoufo.dal.order.model.*;
27 import com.yohoufo.dal.user.model.ZhiMaCert; 27 import com.yohoufo.dal.user.model.ZhiMaCert;
28 import com.yohoufo.order.common.EnterQuitEnum; 28 import com.yohoufo.order.common.EnterQuitEnum;
  29 +import com.yohoufo.order.event.EntrySellerRechargeEvent;
29 import com.yohoufo.order.event.InviteRecordEvent; 30 import com.yohoufo.order.event.InviteRecordEvent;
30 import com.yohoufo.order.model.request.PaymentRequest; 31 import com.yohoufo.order.model.request.PaymentRequest;
31 import com.yohoufo.order.model.response.PrepayResponse; 32 import com.yohoufo.order.model.response.PrepayResponse;
@@ -121,186 +122,16 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService { @@ -121,186 +122,16 @@ public class MerchantOrderPaymentService extends AbstractOrderPaymentService {
121 return result; 122 return result;
122 } 123 }
123 124
124 - private SellerWallet processFirstTimeCharge(SellerWallet sw, OrderInfo orderInfo){  
125 - Long orderCode = orderInfo.getOrderCode();  
126 - BigDecimal amount = orderInfo.getAmount();  
127 - Integer uid = orderInfo.getUid();  
128 -  
129 - ZhiMaCert cert = null;  
130 - try {  
131 - ApiResponse<?> resp = ufoServiceCaller.call("ufo.user.zhiMaCertInfo", ApiResponse.class, uid);  
132 - cert = (ZhiMaCert) resp.getData();  
133 - } catch (Exception e) {  
134 - CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "没有芝麻认证信息:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);  
135 - logger.error("重新入驻首次充值保证金={},没有芝麻认证信息,uid={}", amount, uid);  
136 - return sw;  
137 - }  
138 - if(cert == null || StringUtils.isBlank(cert.getCertNo())) {  
139 - CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "芝麻认证信息有误:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);  
140 - logger.error("重新入驻首次充值保证金={},没有芝麻认证信息有误,uid={}", amount, uid);  
141 - return sw;  
142 - }  
143 - String certNo = cert.getCertNo();  
144 -  
145 - if (sw != null) {  
146 - logger.info("重新入驻首次充值保证金={},已入驻次数为,uid={}", amount, uid, sw.getJoinTimes());  
147 - if (sw.getStatus() == null || sw.getStatus() != 0) {  
148 - CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "入驻状态异常:uid=" + uid + ",充值订单号为:" + orderCode + ", 充值金额为:" + amount);  
149 - logger.error("重新入驻首次充值保证金={},发现入驻状态异常,uid={},times={}", amount, uid, sw.getJoinTimes());  
150 - return sw;  
151 - }  
152 - if (!canRejoin(uid, sw, cert)) {  
153 - CommonAlarmEventPublisher.publish("重新入驻首次充值保证金失败", "ufo.order.recharge", "超过入最大驻次数:uid=" + uid + ",充值订单号为:" + orderCode+", 充值金额为:"+amount);  
154 - logger.error("重新入驻首次充值保证金={},发现已超过最大入驻次数,uid={},times={}", amount, uid, sw.getJoinTimes());  
155 - return sw;  
156 - }  
157 - // 更新钱包  
158 - logger.info("重新入驻首次充值保证金={},更新钱包信息,uid={}", amount, uid);  
159 - int exe = sellerWalletMapper.rejoin(uid, amount, null, TimeUtils.getTimeStampSecond(), sw.getJoinTimes() + 1);  
160 - if (exe == 0) {  
161 - CommonAlarmEventPublisher.publish("重新入驻修改钱包失败", "ufo.order.recharge", "更新钱包不成功:uid=" + uid + ",充值订单号为:" + orderCode + ", 充值金额为:" + amount+ ",钱包信息为:"+sw);  
162 - logger.info("重新入驻失败,首次充值保证金={},更新钱包信息失败,uid={}, 钱包信息={}", amount, uid, sw);  
163 - return sw;  
164 - }  
165 - // 数据清理  
166 - // 钱包明细  
167 - logger.info("重新入驻orderCode={},清理历史钱包明细数据,uid={}", orderCode, uid);  
168 - int clearCount = sellerWalletDetailMapper.clearUserHistoryData(uid);  
169 - logger.info("重新入驻orderCode={},清理历史钱包明细数据={},uid={}", orderCode, clearCount, uid);  
170 - // 充值信息  
171 - logger.info("重新入驻orderCode={},清理充值表数据,uid={}", orderCode, uid);  
172 - clearCount = entrySellerRechargeOrderMapper.clearUserHistoryData(uid, orderCode);  
173 - logger.info("重新入驻orderCode={},清理充值表数据={},uid={}", orderCode, clearCount, uid);  
174 - // 入驻信息  
175 - logger.info("重新入驻orderCode={},清理历史入驻表数据,uid={}", orderCode, uid);  
176 - clearCount = storedSellerService.removeStoredSeller(uid);  
177 - logger.info("重新入驻orderCode={},清理历史入驻表数据={},uid={}", orderCode, clearCount, uid);  
178 125
179 - } else {  
180 - // 首次充值,创建钱包信息  
181 - sw = new SellerWallet();  
182 - sw.setUid(uid);  
183 - sw.setAmount(amount);  
184 - sw.setLockAmount(BigDecimal.ZERO);  
185 - sw.setStatus(1);  
186 - sw.setType(1);  
187 - sw.setCreateTime(TimeUtils.getTimeStampSecond());  
188 - sw.setUpdateTime(0);  
189 - sw.setJoinTimes(0);  
190 - logger.info("首次充值后回调,处理总账表新增账号,bean={}", sw);  
191 - sellerWalletMapper.insert(sw);  
192 - logger.info("首次充值后回调,处理总账表新增账号成功,bean={}", sw);  
193 -  
194 - // 首次入驻  
195 - InviteRecordEvent inviteRecordEvent = InviteRecordEvent.builder().uid(uid).orderCode(orderCode).eventType(EnterQuitEnum.ENTER).build();  
196 - EventBusPublisher.publishEvent(inviteRecordEvent);  
197 - }  
198 - addJoinHistory(uid, certNo);  
199 - return sw;  
200 - }  
201 126
202 @Override 127 @Override
203 public void processAfterPay(OrderInfo orderInfo) { 128 public void processAfterPay(OrderInfo orderInfo) {
204 - logger.info("充值后回调,处理processAfterPay,orderInfo={}", orderInfo);  
205 - Long orderCode = orderInfo.getOrderCode();  
206 - BigDecimal amount = orderInfo.getAmount();  
207 - Integer payment = orderInfo.getPayment();  
208 - tradeBillsService.addPayRechargeEnsureRecord(orderInfo.getUid(), orderCode, payment, orderInfo.getAmount());  
209 -  
210 - BigDecimal beforeAmount = BigDecimal.ZERO;  
211 - BigDecimal beforeLockAmount = BigDecimal.ZERO;  
212 -  
213 - BigDecimal afterAmount = BigDecimal.ZERO;  
214 - BigDecimal afterLockAmount = BigDecimal.ZERO;  
215 -  
216 - Integer uid = orderInfo.getUid();  
217 129
218 - SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);  
219 - logger.info("充值后回调,处理总账表,orderCode={}", orderCode);  
220 - if (orderInfo.getType() == TYPE_FIRST_RECHARGE) {  
221 - sw = processFirstTimeCharge(sw, orderInfo);  
222 - afterAmount = amount;  
223 - } else {  
224 - // 修改钱包余额  
225 - if (sw == null) {  
226 - logger.error("充值保证金余额{},钱包不存在,uid={}", amount, uid);  
227 - CommonAlarmEventPublisher.publish("充值保证金余额失败", "ufo.order.recharge", "钱包不存在uid=" + uid + ",充值订单号为:" + orderCode +", 充值金额为:"+amount);  
228 - return;  
229 - }  
230 - if (sw.getStatus() == null || sw.getStatus() == 0) {  
231 - logger.error("充值保证金余额{},钱包不可用,uid={}", amount, uid);  
232 - CommonAlarmEventPublisher.publish("充值保证金余额失败", "ufo.order.recharge", "钱包不可用uid=" + uid + ",充值订单号为:" + orderCode +", 充值金额为:"+amount);  
233 - return;  
234 - }  
235 -  
236 - beforeAmount = sw.getAmount();  
237 - beforeLockAmount = sw.getLockAmount();  
238 - EntrySellerType sellerType = sellerService.getEntrySellerType(uid);  
239 - if (sellerType == EntrySellerType.SPECIAL_SUPER) {  
240 - // 白名单:充入lock账户  
241 - sw.setLockAmount(amount);  
242 - sw.setAmount(null);  
243 - afterAmount = beforeAmount;  
244 - afterLockAmount = beforeLockAmount.add(amount);  
245 - } else {  
246 - sw.setLockAmount(null);  
247 - sw.setAmount(amount);  
248 - afterAmount = beforeAmount.add(amount);  
249 - afterLockAmount = beforeLockAmount;  
250 - }  
251 -  
252 - sw.setUpdateTime(TimeUtils.getTimeStampSecond());  
253 - logger.info("后期充值,处理总账表增加余额,bean={}", sw);  
254 - if (sellerWalletMapper.addMoney(sw) == 0) {  
255 - logger.error("充值保证金余额{},更新表返回0,uid={}", amount, uid);  
256 - CommonAlarmEventPublisher.publish("充值保证金余额失败", "ufo.order.recharge", "更新表返回0,uid=" + uid + ",充值订单号为:" + orderCode +", 充值金额为:"+amount);  
257 - return;  
258 - }  
259 - }  
260 - logger.info("修改钱包表余额成功,orderCode={}", orderCode);  
261 -  
262 - // 增加充值记录  
263 - SellerWalletDetail swd = new SellerWalletDetail();  
264 - swd.setOrderCode(orderCode);  
265 - swd.setType(1);  
266 - swd.setUid(uid);  
267 - swd.setWalletId(sw.getId());  
268 - swd.setAmount(amount);  
269 - swd.setIsBatch(0);  
270 - swd.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());  
271 - swd.setCreateTime(TimeUtils.getTimeStampSecond());  
272 - swd.setUpdateTime(0);  
273 - swd.setAvailAmount(afterAmount);  
274 - swd.setLockAmount(afterLockAmount);  
275 - swd.setIsSet(1);  
276 - MerchantOrderAttachInfo att = new MerchantOrderAttachInfo();  
277 - att.setPayment(orderInfo.getPayment());  
278 - if (att != null) {  
279 - att.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());  
280 - att.setTypeName(SellerWalletDetail.Type.RE_CHARGE.getName());  
281 - }  
282 - swd.setAttachValue(att==null?"":JSON.toJSONString(att));  
283 - logger.info("充值后,处理增加钱包明细,bean={}", swd);  
284 - sellerWalletDetailMapper.insert(swd);  
285 - logger.info("充值后,处理增加钱包明细成功,orderCode={}", orderCode);  
286 -  
287 - // 身份升级、恢复等  
288 - BigDecimal allMoney = afterAmount.add(afterLockAmount);  
289 - logger.info("卖家{}此充值完成身份处理:剩余金额={},orderCode={}", allMoney, uid);  
290 - sellerDealAfterPay(uid, orderCode, allMoney); 130 + EntrySellerRechargeEvent rechargeEvent = new EntrySellerRechargeEvent(orderInfo);
  131 + EventBusPublisher.publishEvent(rechargeEvent);
291 } 132 }
292 133
293 - private void addJoinHistory(Integer uid, String certNo) {  
294 - try {  
295 - SellerJoinHistory history = new SellerJoinHistory();  
296 - history.setCertNo(certNo);  
297 - history.setUid(uid);  
298 - history.setCreateTime((int) (System.currentTimeMillis() / 1000));  
299 - sellerJoinHistoryMapper.insert(history);  
300 - } catch (Exception e) {  
301 - logger.error("增加入驻历史信息出错uid=" + uid + ",certNo=" + certNo, e);  
302 - }  
303 - } 134 +
304 135
305 public boolean canRejoin(Integer uid, SellerWallet sw, ZhiMaCert cert) { 136 public boolean canRejoin(Integer uid, SellerWallet sw, ZhiMaCert cert) {
306 if (sw == null) { 137 if (sw == null) {
  1 +package com.yohoufo.order.service.handler;
  2 +
  3 +
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.github.rholder.retry.*;
  6 +import com.google.common.base.Predicates;
  7 +import com.yoho.core.config.ConfigReader;
  8 +import com.yoho.tools.common.beans.ApiResponse;
  9 +import com.yohobuy.ufo.model.order.bo.MerchantOrderAttachInfo;
  10 +import com.yohobuy.ufo.model.order.bo.OrderInfo;
  11 +import com.yohobuy.ufo.model.order.common.EntrySellerType;
  12 +import com.yohobuy.ufo.model.order.req.EntrySellerRecharge;
  13 +import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
  14 +import com.yohoufo.common.alarm.EventBusPublisher;
  15 +import com.yohoufo.common.alarm.IEventHandler;
  16 +import com.yohoufo.common.caller.UfoServiceCaller;
  17 +import com.yohoufo.common.utils.TimeUtils;
  18 +import com.yohoufo.dal.order.EntrySellerRechargeOrderMapper;
  19 +import com.yohoufo.dal.order.SellerJoinHistoryMapper;
  20 +import com.yohoufo.dal.order.SellerWalletDetailMapper;
  21 +import com.yohoufo.dal.order.SellerWalletMapper;
  22 +import com.yohoufo.dal.order.model.SellerJoinHistory;
  23 +import com.yohoufo.dal.order.model.SellerWallet;
  24 +import com.yohoufo.dal.order.model.SellerWalletDetail;
  25 +import com.yohoufo.dal.user.model.ZhiMaCert;
  26 +import com.yohoufo.order.common.EnterQuitEnum;
  27 +import com.yohoufo.order.event.EntrySellerRechargeEvent;
  28 +import com.yohoufo.order.event.InviteRecordEvent;
  29 +import com.yohoufo.order.service.IEntrySellerRechargeService;
  30 +import com.yohoufo.order.service.IStoredSellerService;
  31 +import com.yohoufo.order.service.impl.TradeBillsService;
  32 +import com.yohoufo.order.service.seller.setting.SellerService;
  33 +import com.yohoufo.order.utils.RetryerUtils;
  34 +import org.apache.commons.lang3.StringUtils;
  35 +import org.slf4j.Logger;
  36 +import org.slf4j.LoggerFactory;
  37 +import org.springframework.beans.factory.annotation.Autowired;
  38 +import org.springframework.stereotype.Component;
  39 +
  40 +import java.math.BigDecimal;
  41 +import java.util.concurrent.ExecutionException;
  42 +import java.util.concurrent.TimeUnit;
  43 +
  44 +@Component
  45 +public class EntrySellerRechargeHandler implements IEventHandler<EntrySellerRechargeEvent> {
  46 +
  47 + private final Logger logger = LoggerFactory.getLogger(getClass());
  48 +
  49 + private static final int TYPE_FIRST_RECHARGE = 1;
  50 +
  51 + @Autowired
  52 + private TradeBillsService tradeBillsService;
  53 +
  54 + @Autowired
  55 + SellerService sellerService;
  56 +
  57 + @Autowired
  58 + SellerWalletMapper sellerWalletMapper;
  59 +
  60 + @Autowired
  61 + IEntrySellerRechargeService entrySellerRechargeService;
  62 +
  63 + @Override
  64 + public void handle(EntrySellerRechargeEvent event) {
  65 +
  66 + OrderInfo orderInfo = event.getOrderInfo();
  67 +
  68 + logger.info("entry_seller_recharge,orderInfo: {}", orderInfo);
  69 + Long orderCode = orderInfo.getOrderCode();
  70 + BigDecimal amount = orderInfo.getAmount();
  71 + Integer payment = orderInfo.getPayment();
  72 + Integer uid = orderInfo.getUid();
  73 +
  74 + tradeBillsService.addPayRechargeEnsureRecord(orderInfo.getUid(), orderCode, payment, orderInfo.getAmount());
  75 +
  76 + EntrySellerRecharge entrySellerRecharge = new EntrySellerRecharge(uid, amount, orderCode, payment);
  77 +
  78 + // 明确 充值场景
  79 + SellerEntryRecharge sellerEntryRecharge = getSellerEntryRecharge(orderInfo.getUid(), orderInfo.getType());
  80 +
  81 + try{
  82 + RetryerUtils.getRetryer().call(()->{
  83 + switch(sellerEntryRecharge){
  84 +
  85 + case FIRST_ENTRY:
  86 +
  87 + logger.error("seller entry firstEntry {}", entrySellerRecharge);
  88 + entrySellerRechargeService.firstEntry(entrySellerRecharge);
  89 + break;
  90 +
  91 + case AGAIN_ENTRY:
  92 +
  93 + logger.error("seller entry againEntry {}", entrySellerRecharge);
  94 + entrySellerRechargeService.againEntry(entrySellerRecharge);
  95 +
  96 + break;
  97 +
  98 + case RECHARGE:
  99 + logger.error("seller entry recharge {}", entrySellerRecharge);
  100 + entrySellerRechargeService.recharge(entrySellerRecharge);
  101 +
  102 + break;
  103 + default:
  104 + logger.error("seller entry recharge no type {}, {}, {}", uid, orderCode, amount);
  105 + break;
  106 +
  107 + }
  108 +
  109 + return true;
  110 + });
  111 + }catch (Exception e){
  112 + logger.error("entry seller recharge e {}", e);
  113 + return;
  114 + }
  115 +
  116 + // 身份升级、恢复等
  117 + SellerWallet sellerWallet = sellerWalletMapper.selectByUidAndType(uid, 1);
  118 + BigDecimal allMoney = sellerWallet.getAmount().add(sellerWallet.getLockAmount());
  119 + logger.info("sellerDealAfterPay before leftMoney {}, uid {}", allMoney, uid);
  120 + sellerDealAfterPay(uid, orderCode, allMoney);
  121 +
  122 + }
  123 +
  124 + private SellerEntryRecharge getSellerEntryRecharge(int uid, int type) {
  125 +
  126 +
  127 + SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
  128 +
  129 + SellerEntryRecharge sellerEntryRecharge = null;
  130 +
  131 + if (type == TYPE_FIRST_RECHARGE){
  132 + if (sw != null){
  133 + sellerEntryRecharge = SellerEntryRecharge.AGAIN_ENTRY;
  134 + }else{
  135 + sellerEntryRecharge = SellerEntryRecharge.FIRST_ENTRY;
  136 + }
  137 + }else{
  138 + sellerEntryRecharge = SellerEntryRecharge.RECHARGE;
  139 + }
  140 + return sellerEntryRecharge;
  141 + }
  142 +
  143 +
  144 + private void sellerDealAfterPay(Integer uid, long orderCode, BigDecimal allMoney) {
  145 + sellerService.processApply(uid, orderCode, allMoney);
  146 + }
  147 +
  148 +
  149 + private enum SellerEntryRecharge{
  150 + FIRST_ENTRY, AGAIN_ENTRY, RECHARGE
  151 + }
  152 +
  153 +}
  1 +package com.yohoufo.order.service.impl;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.yoho.core.config.ConfigReader;
  5 +import com.yoho.core.dal.datasource.annotation.Database;
  6 +import com.yoho.tools.common.beans.ApiResponse;
  7 +import com.yohobuy.ufo.model.order.bo.MerchantOrderAttachInfo;
  8 +import com.yohobuy.ufo.model.order.common.EntrySellerType;
  9 +import com.yohobuy.ufo.model.order.req.EntrySellerRecharge;
  10 +import com.yohoufo.common.alarm.EventBusPublisher;
  11 +import com.yohoufo.common.caller.UfoServiceCaller;
  12 +import com.yohoufo.common.utils.TimeUtils;
  13 +import com.yohoufo.dal.order.EntrySellerRechargeOrderMapper;
  14 +import com.yohoufo.dal.order.SellerJoinHistoryMapper;
  15 +import com.yohoufo.dal.order.SellerWalletDetailMapper;
  16 +import com.yohoufo.dal.order.SellerWalletMapper;
  17 +import com.yohoufo.dal.order.model.SellerJoinHistory;
  18 +import com.yohoufo.dal.order.model.SellerWallet;
  19 +import com.yohoufo.dal.order.model.SellerWalletDetail;
  20 +import com.yohoufo.dal.user.model.ZhiMaCert;
  21 +import com.yohoufo.order.common.EnterQuitEnum;
  22 +import com.yohoufo.order.event.InviteRecordEvent;
  23 +import com.yohoufo.order.service.IEntrySellerRechargeService;
  24 +import com.yohoufo.order.service.IStoredSellerService;
  25 +import com.yohoufo.order.service.seller.setting.SellerService;
  26 +import org.apache.commons.lang3.StringUtils;
  27 +import org.slf4j.Logger;
  28 +import org.slf4j.LoggerFactory;
  29 +import org.springframework.beans.factory.annotation.Autowired;
  30 +import org.springframework.stereotype.Service;
  31 +import org.springframework.transaction.annotation.Transactional;
  32 +
  33 +import java.math.BigDecimal;
  34 +
  35 +@Service
  36 +@Transactional
  37 +public class EntrySellerRechargeServiceImpl implements IEntrySellerRechargeService {
  38 +
  39 +
  40 + private final Logger logger = LoggerFactory.getLogger(getClass());
  41 +
  42 +
  43 + @Autowired
  44 + SellerJoinHistoryMapper sellerJoinHistoryMapper;
  45 +
  46 +
  47 + @Autowired
  48 + SellerWalletDetailMapper sellerWalletDetailMapper;
  49 +
  50 + @Autowired
  51 + UfoServiceCaller ufoServiceCaller;
  52 +
  53 + @Autowired
  54 + EntrySellerRechargeOrderMapper entrySellerRechargeOrderMapper;
  55 +
  56 + @Autowired
  57 + SellerWalletMapper sellerWalletMapper;
  58 +
  59 + @Autowired
  60 + SellerService sellerService;
  61 +
  62 + @Autowired
  63 + private ConfigReader configReader;
  64 +
  65 + @Autowired
  66 + IStoredSellerService storedSellerService;
  67 +
  68 +
  69 + /**
  70 + * 充值
  71 + * @param entrySellerRecharge
  72 + */
  73 + @Database(ForceMaster = true, DataSource = "ufo_order")
  74 + public void recharge(EntrySellerRecharge entrySellerRecharge){
  75 +
  76 + int uid = entrySellerRecharge.getUid();
  77 + BigDecimal amount = entrySellerRecharge.getAmount();
  78 + long orderCode = entrySellerRecharge.getOrderCode();
  79 + int payment = entrySellerRecharge.getPayment();
  80 +
  81 + SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
  82 + if (sw == null){
  83 + logger.error("again_entry seller_wallet null ,uid={}", uid);
  84 + return;
  85 + }
  86 +
  87 + // 1.更新钱包总表的数值
  88 + sellerWalletMapper.addMoney(buildRechargeSellerWallet(sw.getId(), uid, amount));
  89 +
  90 + // 2.增加明细
  91 + addSellerWalletDetail(orderCode, uid, sw.getId(), amount, payment);
  92 + }
  93 +
  94 +
  95 + /**
  96 + * 重新入驻
  97 + * @param entrySellerRecharge
  98 + */
  99 + @Database(ForceMaster = true, DataSource = "ufo_order")
  100 + public void againEntry(EntrySellerRecharge entrySellerRecharge){
  101 +
  102 + int uid = entrySellerRecharge.getUid();
  103 + BigDecimal amount = entrySellerRecharge.getAmount();
  104 + long orderCode = entrySellerRecharge.getOrderCode();
  105 + int payment = entrySellerRecharge.getPayment();
  106 +
  107 + // 1. 检查芝麻认证用户
  108 + String againEntryCertNo = geytZhiMaCertNo(uid);
  109 +
  110 + if (StringUtils.isBlank(againEntryCertNo)){
  111 + logger.error("again_entry geytZhiMaCertNo null ,uid={}", uid);
  112 + return;
  113 + }
  114 +
  115 + SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
  116 + if (sw == null){
  117 + logger.error("again_entry seller_wallet null ,uid={}", uid);
  118 + return;
  119 + }
  120 +
  121 + // 2.检查是否达到了 入驻上线
  122 + if (!canRejoin(uid, sw.getJoinTimes(), againEntryCertNo)){
  123 + logger.error("again_entry joinTimes maxTimes,uid: {},times: {}", uid, sw.getJoinTimes());
  124 + return;
  125 + }
  126 +
  127 + // 3. 更新 钱包总表
  128 + int uptCnt = sellerWalletMapper.rejoin(uid, amount, null, TimeUtils.getTimeStampSecond(), sw.getJoinTimes() + 1);
  129 + if (uptCnt == 0){
  130 + logger.info("again_entry update seller_wallet uid: {}", uid);
  131 + return;
  132 + }
  133 +
  134 + // 4. 清理上次入驻的数据
  135 + clearOldEntryData(orderCode, uid);
  136 +
  137 + // 5. 增加入驻次数
  138 + addJoinHistory(uid, againEntryCertNo);
  139 +
  140 + // 6. 增加明细
  141 + addSellerWalletDetail(orderCode, uid, sw.getId(), amount, payment);
  142 + }
  143 +
  144 +
  145 + /**
  146 + * 首次入驻
  147 + * @param entrySellerRecharge
  148 + */
  149 + @Database(ForceMaster = true, DataSource = "ufo_order")
  150 + public void firstEntry(EntrySellerRecharge entrySellerRecharge){
  151 +
  152 + int uid = entrySellerRecharge.getUid();
  153 + BigDecimal amount = entrySellerRecharge.getAmount();
  154 + long orderCode = entrySellerRecharge.getOrderCode();
  155 + int payment = entrySellerRecharge.getPayment();
  156 +
  157 + // 1.检查是否是 芝麻认证的用户
  158 + String firstEntryCertNo = geytZhiMaCertNo(uid);
  159 +
  160 + if (StringUtils.isBlank(firstEntryCertNo)){
  161 + logger.error("frist_entry geytZhiMaCertNo null uid: {}", uid);
  162 + return;
  163 + }
  164 +
  165 + // 2. 增加钱包总表
  166 + SellerWallet sw = addSellerWallet(amount, uid);
  167 +
  168 + // 3.入驻次数记录
  169 + addJoinHistory(uid, firstEntryCertNo);
  170 +
  171 + // 4.增加明细
  172 + addSellerWalletDetail(orderCode, uid, sw.getId(), amount, payment);
  173 +
  174 + // 附加: 首次入驻(邀请入驻返利)
  175 + InviteRecordEvent inviteRecordEvent = InviteRecordEvent.builder().uid(uid).orderCode(orderCode).eventType(EnterQuitEnum.ENTER).build();
  176 + EventBusPublisher.publishEvent(inviteRecordEvent);
  177 + }
  178 +
  179 +
  180 +
  181 + private void clearOldEntryData(Long orderCode, Integer uid) {
  182 + // 钱包明细
  183 + sellerWalletDetailMapper.clearUserHistoryData(uid);
  184 +
  185 + // 充值信息
  186 + entrySellerRechargeOrderMapper.clearUserHistoryData(uid, orderCode);
  187 +
  188 + // 入驻信息
  189 + storedSellerService.removeStoredSeller(uid);
  190 + }
  191 +
  192 +
  193 + private String geytZhiMaCertNo(int uid){
  194 +
  195 + ZhiMaCert cert = null;
  196 + try {
  197 + ApiResponse<?> resp = ufoServiceCaller.call("ufo.user.zhiMaCertInfo", ApiResponse.class, uid);
  198 + cert = (ZhiMaCert) resp.getData();
  199 + } catch (Exception e) {
  200 + logger.error("geytZhiMaCertNo uid {}", uid);
  201 + return null;
  202 + }
  203 + if(cert == null || StringUtils.isBlank(cert.getCertNo())) {
  204 + logger.error("geytZhiMaCertNo uid {}", uid);
  205 + return null;
  206 + }
  207 + return cert.getCertNo();
  208 + }
  209 +
  210 +
  211 + private SellerWallet addSellerWallet(BigDecimal amount, Integer uid) {
  212 + SellerWallet sw;// 首次充值,创建钱包信息
  213 + sw = new SellerWallet();
  214 + sw.setUid(uid);
  215 + sw.setAmount(amount);
  216 + sw.setLockAmount(BigDecimal.ZERO);
  217 + sw.setStatus(1);
  218 + sw.setType(1);
  219 + sw.setCreateTime(TimeUtils.getTimeStampSecond());
  220 + sw.setUpdateTime(0);
  221 + sw.setJoinTimes(0);
  222 + sellerWalletMapper.insert(sw);
  223 + return sw;
  224 + }
  225 +
  226 +
  227 +
  228 + private int getSellerMaxReJoinTimes() {
  229 + String rejoinMaxTimes = configReader.getString("ufo.seller.rejoinMaxTimes", "");
  230 + logger.info("getSellerMaxReJoinTimes,value={}", rejoinMaxTimes);
  231 + if (StringUtils.isBlank(rejoinMaxTimes)) {
  232 + return 0;
  233 + }
  234 + try {
  235 + return Integer.parseInt(rejoinMaxTimes.trim());
  236 + } catch (NumberFormatException e) {
  237 + return 0;
  238 + }
  239 + }
  240 +
  241 + private boolean canRejoin(Integer uid, int sellerWalletJoinTimes, String certNo ) {
  242 +
  243 + int maxReJoin = getSellerMaxReJoinTimes();
  244 + if (sellerWalletJoinTimes >= maxReJoin) {
  245 + return false;
  246 + }
  247 +
  248 + return sellerJoinHistoryMapper.selectCountByCertNo(certNo) <= maxReJoin;
  249 + }
  250 +
  251 +
  252 + private void addJoinHistory(Integer uid, String certNo) {
  253 + SellerJoinHistory history = new SellerJoinHistory();
  254 + history.setCertNo(certNo);
  255 + history.setUid(uid);
  256 + history.setCreateTime((int) (System.currentTimeMillis() / 1000));
  257 + sellerJoinHistoryMapper.insert(history);
  258 + }
  259 +
  260 +
  261 + private int addSellerWalletDetail(long orderCode, int uid, int walletId, BigDecimal amount, int payment){
  262 +
  263 + // 查询总表
  264 + SellerWallet sw = sellerWalletMapper.selectByPrimaryKey(walletId);
  265 +
  266 + SellerWalletDetail swd = buildSellerWalletDetail(orderCode, uid, walletId, amount, sw.getAmount(), sw.getLockAmount(), payment);
  267 +
  268 + return sellerWalletDetailMapper.insertSelective(swd);
  269 +
  270 + }
  271 +
  272 +
  273 + private SellerWalletDetail buildSellerWalletDetail(long orderCode, int uid, int walletId, BigDecimal amount,
  274 + BigDecimal afterAmount, BigDecimal afterLockAmount, int payment){
  275 +
  276 + // 增加充值记录
  277 + SellerWalletDetail swd = new SellerWalletDetail();
  278 + swd.setOrderCode(orderCode);
  279 + swd.setType(1);
  280 + swd.setUid(uid);
  281 + swd.setWalletId(walletId);
  282 + swd.setAmount(amount);
  283 + swd.setIsBatch(0);
  284 + swd.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());
  285 + swd.setCreateTime(TimeUtils.getTimeStampSecond());
  286 + swd.setUpdateTime(0);
  287 + swd.setAvailAmount(afterAmount);
  288 + swd.setLockAmount(afterLockAmount);
  289 + swd.setIsSet(1);
  290 + MerchantOrderAttachInfo att = new MerchantOrderAttachInfo();
  291 + att.setPayment(payment);
  292 + if (att != null) {
  293 + att.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());
  294 + att.setTypeName(SellerWalletDetail.Type.RE_CHARGE.getName());
  295 + }
  296 + swd.setAttachValue(att==null?"": JSON.toJSONString(att));
  297 +
  298 + return swd;
  299 + }
  300 +
  301 +
  302 + private SellerWallet buildRechargeSellerWallet(int sellerWalletId, int uid, BigDecimal amount){
  303 +
  304 + SellerWallet sw = new SellerWallet();
  305 + sw.setId(sellerWalletId);
  306 + EntrySellerType sellerType = sellerService.getEntrySellerType(uid);
  307 + if (sellerType == EntrySellerType.SPECIAL_SUPER) {
  308 + // 白名单:充入lock账户
  309 + sw.setLockAmount(amount);
  310 + sw.setAmount(null);
  311 + } else {
  312 + sw.setLockAmount(null);
  313 + sw.setAmount(amount);
  314 + }
  315 +
  316 + sw.setUpdateTime(TimeUtils.getTimeStampSecond());
  317 + return sw;
  318 + }
  319 +
  320 +}