PC-202403201522\Administrator
2024-05-13 62a8a6efdffa32d494427e6cd0657a39ea0a0a1d
src/main/java/com/nq/service/impl/UserServiceImpl.java
@@ -13,6 +13,9 @@
import com.nq.pojo.*;
import com.nq.pojo.reponse.RUserAssets;
import com.nq.service.*;
import com.nq.utils.UserPointUtil;
import com.nq.utils.email.CodeUtil;
import com.nq.utils.email.EmailService;
import com.nq.utils.timeutil.DateTimeUtil;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.SymmetricCryptoUtil;
@@ -27,6 +30,7 @@
import com.nq.vo.indexposition.IndexPositionVO;
import com.nq.vo.position.PositionProfitVO;
import com.nq.vo.position.PositionVO;
import com.nq.vo.position.UserPositionVO;
import com.nq.vo.stock.StockListVO;
import com.nq.vo.user.UserInfoVO;
@@ -43,8 +47,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
@Service("iUserService")
public class UserServiceImpl implements IUserService {
@@ -117,25 +123,42 @@
    @Autowired
    ISiteMessageService iSiteMessageService;
    @Autowired
    private UserPositionMapper userPositionMapper;
    @Autowired
    IPriceServices priceServices;
    @Resource
    ApplyLeverMapper applyLeverMapper;
    @Autowired
    EmailService emailService;
    @Autowired
    CodeUtil codeUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;
    public ServerResponse reg(String yzmCode, String agentCode, String phone, String userPwd, HttpServletRequest request) {
        if (StringUtils.isBlank(agentCode) || StringUtils.isBlank(phone) ||
                StringUtils.isBlank(userPwd) || StringUtils.isBlank(yzmCode)) {
    public ServerResponse reg(String yzmCode, String agentCode, String phone, String userPwd,String email, HttpServletRequest request) {
        if (StringUtils.isAnyBlank(agentCode,phone,userPwd,yzmCode,email)) {
            return ServerResponse.createByErrorMsg("注册失败。该参数不能为空",request);
        }
        Assert.isTrue(userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getEmail,email)) == 0,
                "Registration failed, the mobile mail number has been registered");
        String redisCode = redisTemplate.opsForValue().get(codeUtil.KEY_PREFIX + yzmCode);
        if (redisCode == null || !yzmCode.equals(redisCode)) {
            return ServerResponse.createByErrorMsg("Verification code error");
        }
        /*
        String keys = "AliyunSmsCode:" + phone;
        String redis_yzm = RedisShardedPoolUtils.get(keys);
        log.info("redis_yzm = {},yzmCode = {}", redis_yzm, yzmCode);
        if (!yzmCode.equals(redis_yzm) && !"6666".equals(yzmCode)) {
            return ServerResponse.createByErrorMsg("由于验证码不正确,注册失败。过程",request);
        }
        }*/
        AgentUser agentUser = this.iAgentUserService.findByCode(agentCode);
@@ -158,7 +181,7 @@
        user.setAgentName(agentUser.getAgentName());
        user.setPhone(phone);
        user.setUserPwd(SymmetricCryptoUtil.encryptPassword(userPwd));
        user.setEmail(email);
        user.setAccountType(Integer.valueOf(0));
        user.setIsLock(Integer.valueOf(1));
@@ -286,9 +309,30 @@
        String userJson = RedisShardedPoolUtils.get(loginToken);
        User user = (User) JsonUtil.string2Obj(userJson, User.class);
        User dbuser = this.userMapper.selectById(user.getId());
        SiteSetting siteSetting = iSiteSettingService.getSiteSetting();
        UserInfoVO userInfoVO = assembleUserInfoVO(dbuser, siteSetting);
        ApplyLever applyLever = applyLeverMapper.selectOne(new LambdaQueryWrapper<ApplyLever>()
                .eq(ApplyLever::getUserId, user.getId())
                .eq(ApplyLever::getState, 1)
                .orderByDesc(ApplyLever::getCreateTime)
                .last(" limit 1"));
        if(null == applyLever || applyLever.getLever().equals("1")){
            userInfoVO.setSiteLever("1");
        }else{
            userInfoVO.setSiteLever(leverSplit(applyLever.getLever()));
        }
        return ServerResponse.createBySuccess(userInfoVO);
    }
    public String leverSplit(String lever){
        String levers = "1/2/5/10";
        String[] parts = levers.split("/");
        int index = Arrays.asList(parts).indexOf(lever);
        if (index != -1) {
            return String.join("/", Arrays.copyOfRange(parts, 0, index + 1));
        }
        return null;
    }
@@ -484,19 +528,20 @@
            rUserAssets.setCumulativeProfitAndLoss(hProfitAndLose.toString());
            rUserAssets.setHandlingCharge(hMoney.toString());
            rUserAssets.setProfitAndLoss(profitAndLose.toString());
            rUserAssets.setIsZf(userAssets.getIsZf());
            rUserAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().toString());
            BigDecimal rate = rateServices.currencyRate(
                    EStockType.getEStockTypeByCode(userAssets.getAccectType()),EStockType.US);
           BigDecimal  availableBalanceUSD = amt;
           if(amt.compareTo(BigDecimal.ZERO)>0){
               availableBalanceUSD = amt.multiply(rate);
           }
            BigDecimal  availableBalanceUSD = amt;
            if(amt.compareTo(BigDecimal.ZERO)>0){
                availableBalanceUSD = amt.multiply(rate);
            }
            BigDecimal  freezeMoneyUSD = freeMoney;
           if(freeMoney.compareTo(BigDecimal.ZERO)>0){
               freezeMoneyUSD  = freeMoney.multiply(rate);
           }
            if(freeMoney.compareTo(BigDecimal.ZERO)>0){
                freezeMoneyUSD  = freeMoney.multiply(rate);
            }
            BigDecimal  totleMoneyUSD = totalAssets;
            if(totalAssets.compareTo(BigDecimal.ZERO)>0){
@@ -527,7 +572,9 @@
            rUserAssets.setCumulativeProfitAndLossUSD(cumulativeProfitAndLossUSD.setScale(s,BigDecimal.ROUND_UP).toString());
            rUserAssets.setHandlingChargeUSD(handlingChargeUSD.setScale(s,BigDecimal.ROUND_UP).toString());
            rUserAssets.setProfitAndLossUSD(profitAndLossUSD.setScale(s,BigDecimal.ROUND_UP).toString());
            rUserAssets.setProfitAndLoss(getProfitAndLose(userId,userAssets.getAccectType()).toString());
            BigDecimal decimal = new BigDecimal(rUserAssets.getTotalMoney()).add(new BigDecimal(rUserAssets.getProfitAndLoss()));
            rUserAssets.setTotalMoney(decimal.toString());
            AllProfitAndLose = AllProfitAndLose.add(profitAndLossUSD);
            allTotalAssets = allTotalAssets.add(totleMoneyUSD);
            allAmt = allAmt.add(availableBalanceUSD);
@@ -566,6 +613,28 @@
        return ServerResponse.createBySuccess(rUserAssetsList);
    }
    public BigDecimal getProfitAndLose(Integer userId,String  accectType){
        List<UserPosition> userPositions;
        userPositions = userPositionMapper.
                findMyPositionByCodeAndSpell(userId,
                        "","",
                        0, accectType);
        List<UserPositionVO> userPositionVOS = Lists.newArrayList();
        if (userPositions.size() > 0) {
            for (UserPosition position : userPositions) {
                UserPositionVO userPositionVO = UserPointUtil.assembleUserPositionVO(position, priceServices.getNowPrice(position.getStockCode()));
                userPositionVOS.add(userPositionVO);
            }
        }
        BigDecimal profitAndLose = BigDecimal.ZERO;
        for (UserPositionVO f : userPositionVOS) {
            profitAndLose = profitAndLose.add(f.getProfitAndLose());
        }
        return profitAndLose;
    }
    @Override
    public ServerResponse transfer(String fromType, String toType, String amt,HttpServletRequest request) {
        User user =  userService.getCurrentUser(request);
@@ -575,7 +644,11 @@
            return ServerResponse.createByErrorMsg("余额不足",request);
        }
        userAssetsServices.availablebalanceChange(fromType,user.getId(), EUserAssets.TRANSFER,amtBig.negate(),fromType+"/"+toType,"");
        amtBig = rateServices.currencyRate(EStockType.getEStockTypeByCode(fromType),EStockType.getEStockTypeByCode(toType)).multiply(amtBig);
        if(fromType.equals("US")){
            amtBig = (new BigDecimal(83.5)).multiply(amtBig);
        }else{
            amtBig = rateServices.currencyRate(EStockType.getEStockTypeByCode(fromType),EStockType.getEStockTypeByCode(toType)).multiply(amtBig);
        }
        userAssetsServices.availablebalanceChange(toType,user.getId(),EUserAssets.TRANSFER,amtBig.setScale(5,RoundingMode.HALF_DOWN),fromType+"/"+toType,"");
        return ServerResponse.createBySuccess();
    }
@@ -701,7 +774,7 @@
    }
    public ServerResponse addSimulatedAccount(Integer agentId, String phone, String pwd, String amt, Integer accountType, HttpServletRequest request) {
    public ServerResponse addSimulatedAccount(Integer agentId, String phone, String pwd,Integer accountType, HttpServletRequest request) {
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(pwd)) {
            return ServerResponse.createByErrorMsg("The parameter cannot be null");
        }
@@ -712,11 +785,6 @@
            return ServerResponse.createByErrorMsg("The phone number is registered");
        }
        if ((new BigDecimal(amt)).compareTo(new BigDecimal("200000")) == 1) {
            return ServerResponse.createByErrorMsg("The phone number is registered");
        }
        amt = "0";   //代理后台添加用户时金额默认为0
        User user = new User();
        user.setAccountType(accountType);
        user.setPhone(phone);
@@ -725,6 +793,7 @@
        user.setIsActive(Integer.valueOf(0));
        user.setRegTime(new Date());
        user.setUserPwd(SymmetricCryptoUtil.encryptPassword(pwd));
        user.setLever("1");
        if (accountType.intValue() == 1) {
            user.setNickName("模拟用户");
        }
@@ -1070,6 +1139,14 @@
        queryWrapper.eq("user_id",user.getId());
        return  ServerResponse.createBySuccess(mapper.selectList(queryWrapper));
    }
    @Override
    public ServerResponse sendMailCode(String MailCode) {
        if (MailCode == null || MailCode.equals("")) {
            return ServerResponse.createByErrorMsg("The mobile mail number and password cannot be empty");
        }
        emailService.sendSimpleMail(MailCode, "barclays register code", codeUtil.generateVerificationCode());
        return ServerResponse.createBySuccessMsg("send code success!");
    }
}