|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.yoho.core.config.ConfigReader;
|
|
|
import com.yoho.core.dal.datasource.annotation.Database;
|
|
|
import com.yoho.tools.common.beans.ApiResponse;
|
|
|
import com.yohobuy.ufo.model.order.bo.MerchantOrderAttachInfo;
|
|
|
import com.yohobuy.ufo.model.order.common.EntrySellerType;
|
|
|
import com.yohobuy.ufo.model.order.req.EntrySellerRecharge;
|
|
|
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.SellerJoinHistoryMapper;
|
|
|
import com.yohoufo.dal.order.SellerWalletDetailMapper;
|
|
|
import com.yohoufo.dal.order.SellerWalletMapper;
|
|
|
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.user.model.ZhiMaCert;
|
|
|
import com.yohoufo.order.common.EnterQuitEnum;
|
|
|
import com.yohoufo.order.event.InviteRecordEvent;
|
|
|
import com.yohoufo.order.service.IEntrySellerRechargeService;
|
|
|
import com.yohoufo.order.service.IStoredSellerService;
|
|
|
import com.yohoufo.order.service.seller.setting.SellerService;
|
|
|
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 org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class EntrySellerRechargeServiceImpl implements IEntrySellerRechargeService {
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
SellerJoinHistoryMapper sellerJoinHistoryMapper;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
SellerWalletDetailMapper sellerWalletDetailMapper;
|
|
|
|
|
|
@Autowired
|
|
|
UfoServiceCaller ufoServiceCaller;
|
|
|
|
|
|
@Autowired
|
|
|
EntrySellerRechargeOrderMapper entrySellerRechargeOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerWalletMapper sellerWalletMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerService sellerService;
|
|
|
|
|
|
@Autowired
|
|
|
private ConfigReader configReader;
|
|
|
|
|
|
@Autowired
|
|
|
IStoredSellerService storedSellerService;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 充值
|
|
|
* @param entrySellerRecharge
|
|
|
*/
|
|
|
@Database(ForceMaster = true, DataSource = "ufo_order")
|
|
|
public void recharge(EntrySellerRecharge entrySellerRecharge){
|
|
|
|
|
|
int uid = entrySellerRecharge.getUid();
|
|
|
BigDecimal amount = entrySellerRecharge.getAmount();
|
|
|
long orderCode = entrySellerRecharge.getOrderCode();
|
|
|
int payment = entrySellerRecharge.getPayment();
|
|
|
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
|
|
|
if (sw == null){
|
|
|
logger.error("again_entry seller_wallet null ,uid={}", uid);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 1.更新钱包总表的数值
|
|
|
sellerWalletMapper.addMoney(buildRechargeSellerWallet(sw.getId(), uid, amount));
|
|
|
|
|
|
// 2.增加明细
|
|
|
addSellerWalletDetail(orderCode, uid, sw.getId(), amount, payment);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 重新入驻
|
|
|
* @param entrySellerRecharge
|
|
|
*/
|
|
|
@Database(ForceMaster = true, DataSource = "ufo_order")
|
|
|
public void againEntry(EntrySellerRecharge entrySellerRecharge){
|
|
|
|
|
|
int uid = entrySellerRecharge.getUid();
|
|
|
BigDecimal amount = entrySellerRecharge.getAmount();
|
|
|
long orderCode = entrySellerRecharge.getOrderCode();
|
|
|
int payment = entrySellerRecharge.getPayment();
|
|
|
|
|
|
// 1. 检查芝麻认证用户
|
|
|
String againEntryCertNo = geytZhiMaCertNo(uid);
|
|
|
|
|
|
if (StringUtils.isBlank(againEntryCertNo)){
|
|
|
logger.error("again_entry geytZhiMaCertNo null ,uid={}", uid);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
SellerWallet sw = sellerWalletMapper.selectByUidAndType(uid, 1);
|
|
|
if (sw == null){
|
|
|
logger.error("again_entry seller_wallet null ,uid={}", uid);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 2.检查是否达到了 入驻上线
|
|
|
if (!canRejoin(uid, sw.getJoinTimes(), againEntryCertNo)){
|
|
|
logger.error("again_entry joinTimes maxTimes,uid: {},times: {}", uid, sw.getJoinTimes());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 3. 更新 钱包总表
|
|
|
int uptCnt = sellerWalletMapper.rejoin(uid, amount, null, TimeUtils.getTimeStampSecond(), sw.getJoinTimes() + 1);
|
|
|
if (uptCnt == 0){
|
|
|
logger.info("again_entry update seller_wallet uid: {}", uid);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 4. 清理上次入驻的数据
|
|
|
clearOldEntryData(orderCode, uid);
|
|
|
|
|
|
// 5. 增加入驻次数
|
|
|
addJoinHistory(uid, againEntryCertNo);
|
|
|
|
|
|
// 6. 增加明细
|
|
|
addSellerWalletDetail(orderCode, uid, sw.getId(), amount, payment);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 首次入驻
|
|
|
* @param entrySellerRecharge
|
|
|
*/
|
|
|
@Database(ForceMaster = true, DataSource = "ufo_order")
|
|
|
public void firstEntry(EntrySellerRecharge entrySellerRecharge){
|
|
|
|
|
|
int uid = entrySellerRecharge.getUid();
|
|
|
BigDecimal amount = entrySellerRecharge.getAmount();
|
|
|
long orderCode = entrySellerRecharge.getOrderCode();
|
|
|
int payment = entrySellerRecharge.getPayment();
|
|
|
|
|
|
// 1.检查是否是 芝麻认证的用户
|
|
|
String firstEntryCertNo = geytZhiMaCertNo(uid);
|
|
|
|
|
|
if (StringUtils.isBlank(firstEntryCertNo)){
|
|
|
logger.error("frist_entry geytZhiMaCertNo null uid: {}", uid);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 2. 增加钱包总表
|
|
|
SellerWallet sw = addSellerWallet(amount, uid);
|
|
|
|
|
|
// 3.入驻次数记录
|
|
|
addJoinHistory(uid, firstEntryCertNo);
|
|
|
|
|
|
// 4.增加明细
|
|
|
addSellerWalletDetail(orderCode, uid, sw.getId(), amount, payment);
|
|
|
|
|
|
// 附加: 首次入驻(邀请入驻返利)
|
|
|
InviteRecordEvent inviteRecordEvent = InviteRecordEvent.builder().uid(uid).orderCode(orderCode).eventType(EnterQuitEnum.ENTER).build();
|
|
|
EventBusPublisher.publishEvent(inviteRecordEvent);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void clearOldEntryData(Long orderCode, Integer uid) {
|
|
|
// 钱包明细
|
|
|
sellerWalletDetailMapper.clearUserHistoryData(uid);
|
|
|
|
|
|
// 充值信息
|
|
|
entrySellerRechargeOrderMapper.clearUserHistoryData(uid, orderCode);
|
|
|
|
|
|
// 入驻信息
|
|
|
storedSellerService.removeStoredSeller(uid);
|
|
|
}
|
|
|
|
|
|
|
|
|
private String geytZhiMaCertNo(int uid){
|
|
|
|
|
|
ZhiMaCert cert = null;
|
|
|
try {
|
|
|
ApiResponse<?> resp = ufoServiceCaller.call("ufo.user.zhiMaCertInfo", ApiResponse.class, uid);
|
|
|
cert = (ZhiMaCert) resp.getData();
|
|
|
} catch (Exception e) {
|
|
|
logger.error("geytZhiMaCertNo uid {}", uid);
|
|
|
return null;
|
|
|
}
|
|
|
if(cert == null || StringUtils.isBlank(cert.getCertNo())) {
|
|
|
logger.error("geytZhiMaCertNo uid {}", uid);
|
|
|
return null;
|
|
|
}
|
|
|
return cert.getCertNo();
|
|
|
}
|
|
|
|
|
|
|
|
|
private SellerWallet addSellerWallet(BigDecimal amount, Integer uid) {
|
|
|
SellerWallet sw;// 首次充值,创建钱包信息
|
|
|
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);
|
|
|
sellerWalletMapper.insert(sw);
|
|
|
return sw;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int getSellerMaxReJoinTimes() {
|
|
|
String rejoinMaxTimes = configReader.getString("ufo.seller.rejoinMaxTimes", "");
|
|
|
logger.info("getSellerMaxReJoinTimes,value={}", rejoinMaxTimes);
|
|
|
if (StringUtils.isBlank(rejoinMaxTimes)) {
|
|
|
return 0;
|
|
|
}
|
|
|
try {
|
|
|
return Integer.parseInt(rejoinMaxTimes.trim());
|
|
|
} catch (NumberFormatException e) {
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean canRejoin(Integer uid, int sellerWalletJoinTimes, String certNo ) {
|
|
|
|
|
|
int maxReJoin = getSellerMaxReJoinTimes();
|
|
|
if (sellerWalletJoinTimes >= maxReJoin) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return sellerJoinHistoryMapper.selectCountByCertNo(certNo) <= maxReJoin;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void addJoinHistory(Integer uid, String certNo) {
|
|
|
SellerJoinHistory history = new SellerJoinHistory();
|
|
|
history.setCertNo(certNo);
|
|
|
history.setUid(uid);
|
|
|
history.setCreateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
sellerJoinHistoryMapper.insert(history);
|
|
|
}
|
|
|
|
|
|
|
|
|
private int addSellerWalletDetail(long orderCode, int uid, int walletId, BigDecimal amount, int payment){
|
|
|
|
|
|
// 查询总表
|
|
|
SellerWallet sw = sellerWalletMapper.selectByPrimaryKey(walletId);
|
|
|
|
|
|
SellerWalletDetail swd = buildSellerWalletDetail(orderCode, uid, walletId, amount, sw.getAmount(), sw.getLockAmount(), payment);
|
|
|
|
|
|
return sellerWalletDetailMapper.insertSelective(swd);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private SellerWalletDetail buildSellerWalletDetail(long orderCode, int uid, int walletId, BigDecimal amount,
|
|
|
BigDecimal afterAmount, BigDecimal afterLockAmount, int payment){
|
|
|
|
|
|
// 增加充值记录
|
|
|
SellerWalletDetail swd = new SellerWalletDetail();
|
|
|
swd.setOrderCode(orderCode);
|
|
|
swd.setType(1);
|
|
|
swd.setUid(uid);
|
|
|
swd.setWalletId(walletId);
|
|
|
swd.setAmount(amount);
|
|
|
swd.setIsBatch(0);
|
|
|
swd.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());
|
|
|
swd.setCreateTime(TimeUtils.getTimeStampSecond());
|
|
|
swd.setUpdateTime(0);
|
|
|
swd.setAvailAmount(afterAmount);
|
|
|
swd.setLockAmount(afterLockAmount);
|
|
|
swd.setIsSet(1);
|
|
|
MerchantOrderAttachInfo att = new MerchantOrderAttachInfo();
|
|
|
att.setPayment(payment);
|
|
|
if (att != null) {
|
|
|
att.setType(SellerWalletDetail.Type.RE_CHARGE.getValue());
|
|
|
att.setTypeName(SellerWalletDetail.Type.RE_CHARGE.getName());
|
|
|
}
|
|
|
swd.setAttachValue(att==null?"": JSON.toJSONString(att));
|
|
|
|
|
|
return swd;
|
|
|
}
|
|
|
|
|
|
|
|
|
private SellerWallet buildRechargeSellerWallet(int sellerWalletId, int uid, BigDecimal amount){
|
|
|
|
|
|
SellerWallet sw = new SellerWallet();
|
|
|
sw.setId(sellerWalletId);
|
|
|
EntrySellerType sellerType = sellerService.getEntrySellerType(uid);
|
|
|
if (sellerType == EntrySellerType.SPECIAL_SUPER) {
|
|
|
// 白名单:充入lock账户
|
|
|
sw.setLockAmount(amount);
|
|
|
sw.setAmount(null);
|
|
|
} else {
|
|
|
sw.setLockAmount(null);
|
|
|
sw.setAmount(amount);
|
|
|
}
|
|
|
|
|
|
sw.setUpdateTime(TimeUtils.getTimeStampSecond());
|
|
|
return sw;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|