package com.gear.customer.swx.biz.impl;
|
|
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.gear.common.constant.SwxConstons;
|
import com.gear.common.core.redis.RedisCache;
|
import com.gear.common.exception.CustomerException;
|
import com.gear.common.utils.jwt.JWTTokenUtils;
|
import com.gear.common.utils.sign.Md5Utils;
|
import com.gear.customer.swx.biz.SwxBizUser;
|
import com.gear.customer.swx.vo.request.*;
|
import com.gear.customer.swx.vo.response.SwxUserInfoVo;
|
import com.gear.swx.domain.*;
|
import com.gear.swx.service.*;
|
import org.apache.logging.log4j.util.Strings;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
@Component
|
public class SwxBizUserImpl implements SwxBizUser {
|
|
@Autowired
|
private ISwxIdentificationService swxIdentificationService;
|
|
@Autowired
|
private ISwxUserService swxUserService;
|
|
@Autowired
|
private RedisCache redisCache;
|
|
@Autowired
|
private ISwxSettingsService swxSettingsService;
|
|
@Autowired
|
private ISwxRechargeRecordService swxRechargeRecordService;
|
|
@Autowired
|
private ISwxWithdrawalRecordService swxWithdrawalRecordService;
|
|
@Autowired
|
private ISwxNftService swxNftService;
|
|
@Autowired
|
private ISwxMoneyLogService moneyLogService;
|
|
@Autowired
|
private ISwxLoanRecordService swxLoanRecordService;
|
|
@Autowired
|
private ISwxUserLevelService swxUserLevelService;
|
@Override
|
@Transactional
|
public String identification(String userId,SwxUserIdentificationVo swxUserIdentificationVo) throws CustomerException {
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
if (swxUser.getIdefinderStatus() == SwxConstons.SWX_EXAMINE_STATUS_REVIEW||swxUser.getIdefinderStatus() == SwxConstons.SWX_EXAMINE_STATUS_YES){
|
throw new CustomerException("10019");
|
}
|
SwxIdentification swxIdentification = new SwxIdentification();
|
BeanUtils.copyProperties(swxUserIdentificationVo,swxIdentification);
|
swxIdentification.setUserId(swxUser.getId());
|
swxIdentificationService.save(swxIdentification);
|
swxUser.setIdefinderStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
|
swxUserService.updateById(swxUser);
|
return "20001";
|
}
|
|
@Override
|
public SwxUserInfoVo getUserInfo(String userId) throws CustomerException{
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
SwxUserInfoVo info = new SwxUserInfoVo();
|
BeanUtils.copyProperties(swxUser,info);
|
if (!Strings.isEmpty(swxUser.getLevel())){
|
SwxUserLevel userLevel = swxUserLevelService.getById(swxUser.getLevel());
|
if (userLevel != null){
|
info.setLevelLogo(userLevel.getLogo());
|
}
|
}
|
SwxSettings swxSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"virtual_account"));
|
|
if(swxSettings != null && ((Integer.parseInt(swxSettings.getParamValue()))==SwxConstons.NOMARL_STATUS_YES)){
|
long exprise = 7L;
|
//判断当前系统中是否存在此虚拟账户
|
if (redisCache.hasKey("SWX-CUSTOMER-VERTURAL-"+userId)){
|
SwxUserInfoVo infoVo= redisCache.getCacheObject("SWX-CUSTOMER-VERTURAL-"+userId);
|
info.setVirtuallyAmount(infoVo.getVirtuallyAmount() == null ? new BigDecimal(100000) : infoVo.getVirtuallyAmount());
|
info.setVirtuallyBondAmount(infoVo.getVirtuallyBondAmount());
|
}else{
|
info.setVirtuallyAmount(new BigDecimal(1000000));
|
info.setVirtuallyBondAmount(new BigDecimal(0));
|
redisCache.setCacheObject("SWX-CUSTOMER-VERTURAL-"+swxUser.getId(),info,604800L, TimeUnit.SECONDS);
|
}
|
info.setVirtuallyStatus(SwxConstons.NOMARL_STATUS_YES);
|
}
|
info.setInviteNumber(String.valueOf(swxUser.getInviteNumber()));
|
return info;
|
}
|
|
@Override
|
public String recharge(String userId, SwxUserRechargeVo vo) throws CustomerException{
|
if(vo.getAmount() == null || vo.getCurrency() == null || Strings.isEmpty(vo.getType())){
|
throw new CustomerException("10018");
|
}
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
SwxRechargeRecord swxRechargeRecord = new SwxUserRechargeVo();
|
BeanUtils.copyProperties(vo,swxRechargeRecord);
|
swxRechargeRecord.setUserId(swxUser.getId());
|
swxRechargeRecord.setStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
|
swxRechargeRecordService.save(swxRechargeRecord);
|
return "20002";
|
}
|
|
@Override
|
public String withdrawal(String userId, SwxUserWithdrawalVo vo)throws CustomerException {
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
|
SwxSettings swxSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"withdrawal_fee"));
|
BigDecimal fee = BigDecimal.ZERO;
|
if(swxSettings != null && !Strings.isEmpty(swxSettings.getParamValue())){
|
fee = new BigDecimal(swxSettings.getParamValue());
|
}
|
|
SwxWithdrawalRecord swxWithdrawalRecord = new SwxWithdrawalRecord();
|
BeanUtils.copyProperties(vo,swxWithdrawalRecord);
|
swxWithdrawalRecord.setUserId(swxUser.getId());
|
swxWithdrawalRecord.setStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
|
BigDecimal withdrawalFee = swxWithdrawalRecord.getAmount().multiply(fee).divide(new BigDecimal(100));
|
swxWithdrawalRecord.setWithdrawalFee(withdrawalFee);
|
swxWithdrawalRecord.setTrueAmount(swxWithdrawalRecord.getAmount().subtract(withdrawalFee));
|
swxWithdrawalRecordService.save(swxWithdrawalRecord);
|
return "20003";
|
}
|
|
@Override
|
public IPage<SwxWithdrawalRecord> listWithdrwal(String userId,Integer pageNo,Integer pageSize) throws CustomerException{
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
Page<SwxWithdrawalRecord> page = new Page<SwxWithdrawalRecord>(pageNo, pageSize);
|
QueryWrapper<SwxWithdrawalRecord> queryWrapper = new QueryWrapper<>();
|
queryWrapper.lambda().eq(SwxWithdrawalRecord::getUserId,userId);
|
queryWrapper.lambda().orderByDesc(SwxWithdrawalRecord::getCreateTime);
|
IPage<SwxWithdrawalRecord> pageList = swxWithdrawalRecordService.page(page, queryWrapper);
|
return pageList;
|
}
|
|
@Override
|
public IPage<SwxRechargeRecord> listRecharge(String userId,Integer pageNo,Integer pageSize) throws CustomerException{
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
QueryWrapper<SwxRechargeRecord> queryWrapper = new QueryWrapper<>();
|
queryWrapper.lambda().eq(SwxRechargeRecord::getUserId,userId);
|
queryWrapper.lambda().orderByDesc(SwxRechargeRecord::getCreateTime);
|
Page<SwxRechargeRecord> page = new Page<SwxRechargeRecord>(pageNo, pageSize);
|
IPage<SwxRechargeRecord> pageList = swxRechargeRecordService.page(page, queryWrapper);
|
return pageList;
|
}
|
|
@Override
|
public List<SwxNft> listNft(String userId) throws CustomerException{
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
QueryWrapper<SwxNft> queryWrapper = new QueryWrapper<>();
|
queryWrapper.lambda().eq(SwxNft::getUserId,userId);
|
queryWrapper.lambda().orderByDesc(SwxNft::getCreateTime);
|
return swxNftService.list(queryWrapper);
|
}
|
|
@Override
|
@Transactional
|
public String buyNft(String userId, SwxUserBuyNftVo swxUserBuyNftVo) throws CustomerException {
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
SwxNft swxNft = swxNftService.getById(swxUserBuyNftVo.getId());
|
if (swxNft == null || swxNft.getStatus() != SwxConstons.NOMARL_STATUS_YES){
|
throw new CustomerException("10020");
|
}
|
if(Strings.isEmpty(swxUserBuyNftVo.getAddress())){
|
throw new CustomerException("10021");
|
}
|
//判断用户余额是否足够
|
if(swxUser.getAmount().compareTo(swxNft.getCurrentPrice()) < 0){
|
throw new CustomerException("10011");
|
}
|
//插入资金日志
|
SwxMoneyLog moneyLog = new SwxMoneyLog();
|
moneyLog.setInfo("购买nft:"+swxNft.getName());
|
moneyLog.setStatus(SwxConstons.NOMARL_STATUS_YES);
|
moneyLog.setType(SwxConstons.SWX_MONEY_LOG_TYPE_BUYNFT);
|
moneyLog.setSymbol(SwxConstons.SWX_MONEY_TYPE_PAY);
|
moneyLog.setTitle("nft公益");
|
moneyLog.setOldAmount(swxUser.getAmount());
|
//修改用户余额
|
swxUser.setAmount(swxUser.getAmount().subtract(swxNft.getCurrentPrice()));
|
swxUserService.updateById(swxUser);
|
moneyLog.setNowAmount(swxUser.getAmount());
|
moneyLog.setUserId(swxUser.getId());
|
moneyLog.setBusiId(swxNft.getId());
|
moneyLog.setMoney(swxNft.getCurrentPrice());
|
moneyLogService.save(moneyLog);
|
//修改nft状态
|
swxNft.setUserId(userId);
|
swxNft.setStatus(SwxConstons.NOMARL_STATUS_NO);
|
swxNft.setAddress(swxUserBuyNftVo.getAddress());
|
swxNftService.updateById(swxNft);
|
return "20004";
|
}
|
|
@Override
|
public String loan(String userId, SwxUserLoanVo swxUserLoanVo) throws CustomerException{
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
//判断用户是否实名认证
|
if (swxUser.getIdefinderStatus() != SwxConstons.SWX_EXAMINE_STATUS_YES){
|
throw new CustomerException("10022");
|
}
|
|
//获取借款期限和利率
|
if(swxUserLoanVo.getAmount() == null || swxUserLoanVo.getTerm() == null){
|
throw new CustomerException("10023");
|
}
|
|
//获取利率
|
SwxSettings rateSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"loan_rate"));
|
if(rateSettings == null){
|
throw new CustomerException("10018");
|
}
|
|
String rateStr = rateSettings.getParamValue();
|
|
JSONArray jsonArray = JSONArray.parseArray(rateStr);
|
|
BigDecimal rate = null;
|
|
for(int i = 0; i < jsonArray.size(); i++){
|
JSONObject object = jsonArray.getJSONObject(i);
|
Integer term = object.getInteger("days");
|
if (term == swxUserLoanVo.getTerm()){
|
rate = new BigDecimal(object.getString("rate"));
|
break;
|
}
|
}
|
if (rate == null){
|
throw new CustomerException("10018");
|
}
|
|
SwxLoanRecord swxLoanRecord = new SwxLoanRecord();
|
|
BeanUtils.copyProperties(swxUserLoanVo,swxLoanRecord);
|
swxLoanRecord.setUserId(userId);
|
//审核中
|
swxLoanRecord.setStatus(SwxConstons.SWX_EXAMINE_STATUS_REVIEW);
|
swxLoanRecord.setRete(rate);
|
swxLoanRecordService.save(swxLoanRecord);
|
|
|
return "20001";
|
}
|
|
@Override
|
public IPage<SwxLoanRecord> listLoanRecord(String userId,Integer pageNo,Integer pageSize)throws CustomerException {
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
QueryWrapper<SwxLoanRecord> queryWrapper = new QueryWrapper<>();
|
queryWrapper.lambda().eq(SwxLoanRecord::getUserId,userId);
|
queryWrapper.lambda().orderByDesc(SwxLoanRecord::getCreateTime);
|
Page<SwxLoanRecord> page = new Page<SwxLoanRecord>(pageNo, pageSize);
|
IPage<SwxLoanRecord> pageList = swxLoanRecordService.page(page, queryWrapper);
|
return pageList;
|
}
|
|
@Override
|
public String repassword(String userId,SwxUserRegisterVo vo) {
|
if (Strings.isEmpty(vo.getPassword()) || Strings.isEmpty(vo.getRePaossword()) || !vo.getPassword().equals(vo.getRePaossword())){
|
throw new CustomerException("10004");
|
}
|
SwxUser swxUser = swxUserService.getById(userId);
|
if (swxUser == null){
|
throw new CustomerException("10009");
|
}
|
String newPass = Md5Utils.hash(vo.getPassword());
|
swxUser.setPassword(newPass);
|
swxUserService.updateById(swxUser);
|
return "20005";
|
}
|
}
|