xiaoyong931011
2023-04-13 0e0738fea4275eb50c1014dea40ac5cf9f8cf845
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
@@ -16,7 +16,10 @@
import cc.mrbird.febs.mall.service.IApiMallMemberService;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.ICommonService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.mall.vo.*;
import cc.mrbird.febs.pay.model.BrandWCPayRequestData;
import cc.mrbird.febs.pay.service.IXcxPayService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
@@ -25,6 +28,7 @@
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -63,6 +67,8 @@
    private final MallRegisterAppealMapper mallRegisterAppealMapper;
    private final MallTeamLeaderMapper mallTeamLeaderMapper;
    private final SpringContextHolder springContextHolder;
    private final MallAgentRecordMapper mallAgentRecordMapper;
    private final IMallMoneyFlowService mallMoneyFlowService;
    @Value("${spring.profiles.active}")
@@ -193,7 +199,9 @@
    public String generateAsaToken(String token) {
        RSA rsa = new RSA(null, AppContants.PUBLIC_KEY);
        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
//        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
        //去掉时间戳
        return rsa.encryptBase64(token, KeyType.PublicKey);
    }
    @Override
@@ -229,10 +237,11 @@
        MallMember mallMember = this.baseMapper.selectById(id);
        MallMemberVo mallMemberVo = MallMemberConversion.INSTANCE.entityToVo(mallMember);
        MallMember referMember = this.baseMapper.selectInfoByInviteId(mallMember.getReferrerId());
        if (referMember != null) {
            mallMemberVo.setReferrerName(referMember.getName());
        if(StrUtil.isNotEmpty(mallMember.getReferrerId())){
            MallMember referMember = this.baseMapper.selectInfoByInviteId(mallMember.getReferrerId());
            if (referMember != null) {
                mallMemberVo.setReferrerName(referMember.getName());
            }
        }
        if (StrUtil.isNotBlank(mallMember.getTradePassword())) {
@@ -244,23 +253,28 @@
            mallMemberVo.setHasPayment(1);
        }
//        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.AGENT_LEVEL, mallMember.getLevel());
//        if (dic != null) {
//            mallMemberVo.setLevelName(dic.getDescription());
//        }
        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(mallMemberVo.getId());
        mallMemberVo.setBalance(wallet.getBalance());
        mallMemberVo.setScore(wallet.getScore());
        mallMemberVo.setPrizeScore(wallet.getPrizeScore());
        mallMemberVo.setTotalCost(mallOrderInfoMapper.selectTotalAmount(id));
        //用户是否是团长
        List<MallTeamLeader> mallTeamLeaders = mallTeamLeaderMapper.selectListByMemberIdAndState(mallMember.getId(), MallTeamLeader.STATE_YES);
        if(CollUtil.isNotEmpty(mallTeamLeaders)){
            mallMemberVo.setIsTeamLeader(1);
        }else{
            mallMemberVo.setIsTeamLeader(2);
        //代理状态
        MallAgentRecord mallAgentRecordIng = mallAgentRecordMapper.selectByMemberIdAndState(id, MallAgentRecord.APPLY_ING);
        MallAgentRecord mallAgentRecordAgree = mallAgentRecordMapper.selectByMemberIdAndState(id, MallAgentRecord.APPLY_AGREE);
        MallAgentRecord mallAgentRecordDisagree = mallAgentRecordMapper.selectByMemberIdAndState(id, MallAgentRecord.APPLY_DISAGREE);
        if(ObjectUtil.isNotEmpty(mallAgentRecordIng)){
            mallMemberVo.setIsAgent(1);
        }else if(ObjectUtil.isNotEmpty(mallAgentRecordAgree)){
            mallMemberVo.setIsAgent(2);
            if(StrUtil.isNotEmpty(mallAgentRecordAgree.getAgentLevel())){
                DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode("AGENT_LEVEL_APPLY", mallAgentRecordAgree.getAgentLevel());
                mallMemberVo.setAgentLevelName(dataDictionaryCustom.getDescription());
            }
        }else if(ObjectUtil.isNotEmpty(mallAgentRecordDisagree)){
            mallMemberVo.setIsAgent(3);
        }
        return new FebsResponse().success().data(mallMemberVo);
    }
@@ -428,7 +442,7 @@
        Long id = LoginUserUtil.getLoginUser().getId();
        MallMember member = this.baseMapper.selectById(id);
        member.setBindPhone(accountAndCodeDto.getAccount());
        member.setPhone(accountAndCodeDto.getAccount());
        this.baseMapper.updateById(member);
    }
@@ -436,15 +450,7 @@
    public BigDecimal canMoney() {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        BigDecimal profit = mallMoneyFlowMapper.selectProfitByDateAndMemberId(memberId);
        BigDecimal canMoney = wallet.getCommission();
        if (profit != null) {
            if(canMoney.compareTo(BigDecimal.ZERO) > 0) {
                canMoney = canMoney.subtract(profit);
            }
        }
        return canMoney;
        return wallet.getBalance().setScale(2,BigDecimal.ROUND_DOWN);
    }
    @Override
@@ -616,8 +622,8 @@
                }
            }
            String token = IdUtil.simpleUUID();
            redisUtils.set(token, JSONObject.toJSONString(mallMember), 360000);
            redisUtils.set(redisKey, token, 360000);
            redisUtils.set(token, JSONObject.toJSONString(mallMember), -1);
            redisUtils.set(redisKey, token, -1);
            Map<String, Object> authInfo = new HashMap<>();
            authInfo.put("token", token);
            authInfo.put("appid", xcxProperties.getXcxAppid());
@@ -648,7 +654,7 @@
        if(StrUtil.isNotEmpty(avatarUrl)){
            mallMember.setAvatar(avatarUrl);
        }
        mallMember.setSex(apiXcxSaveInfoDto.getGender() == 1 ? "女" : "男");
//        mallMember.setSex(1 == apiXcxSaveInfoDto.getGender() ? "女" : "男");
        this.baseMapper.updateById(mallMember);
        return new FebsResponse().success();
    }
@@ -696,11 +702,140 @@
            authInfo.put("token", token);
            authInfo.put("member", mallMember);
            authInfo.put("rasToken", generateAsaToken(token));
            return new FebsResponse().success().message("登陆成功");
            return new FebsResponse().success().data(authInfo);
        }
        return new FebsResponse().fail().message("验证码错误");
    }
    @Override
    public FebsResponse xcxOpen(ApiXcxOpenDto apiXcxOpenDto) {
        DataDictionaryCustom rangeSwitch = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANGE_SWITCH.getType(), DataDictionaryEnum.RANGE_SWITCH.getCode());
        if(StrUtil.isNotBlank(rangeSwitch.getValue()) && "1".equals(rangeSwitch.getValue())){
            if(ObjectUtil.isNull(apiXcxOpenDto.getLongitude()) || ObjectUtil.isNull(apiXcxOpenDto.getLatitude())){
                return new FebsResponse().fail().message("请授权位置信息");
            }
            Double longitude = apiXcxOpenDto.getLongitude();
            Double latitude = apiXcxOpenDto.getLatitude();
            DataDictionaryCustom rangeSize = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANGE_SIZE.getType(), DataDictionaryEnum.RANGE_SIZE.getCode());
            if(ObjectUtil.isEmpty(rangeSize)){
                return new FebsResponse().success().data(2);
            }
            if(StrUtil.isBlank(rangeSize.getValue())){
                return new FebsResponse().success().data(2);
            }
            //方位大小,换成单位:米
            Integer value = Integer.parseInt(rangeSize.getValue()) * 1000;
            //根据经纬度获取周围团长的距离
            MallTeamLeader mallTeamLeader = mallTeamLeaderMapper.selectLeaderByLonAndLat(longitude, latitude);
            if(ObjectUtil.isEmpty(mallTeamLeader)){
                return new FebsResponse().success().data(2);
            }
            Double distance = mallTeamLeader.getDistance();
            if(value <= distance){
                return new FebsResponse().success().data(2);
            }
        }
        return new FebsResponse().success().data(1);
    }
    private final IXcxPayService iXcxPayService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse rechargeWallet(ApiRechargeWalletDto apiRechargeWalletDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        BigDecimal amount = apiRechargeWalletDto.getAmount();
        if(BigDecimal.ZERO.compareTo(amount)>0){
            return new FebsResponse().fail().message("请输入正确的充值金额");
        }
        Integer type = apiRechargeWalletDto.getType();
        if(2 == type){
            //成为合伙人的充值金额
            MallAgentRecord mallAgentRecord = mallAgentRecordMapper.selectById(apiRechargeWalletDto.getAgentApplyId());
            BigDecimal agentPrice = mallAgentRecord.getAmount();
//            DataDictionaryCustom agentPriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.PRICE_AMOUNT.getType(),
//                    DataDictionaryEnum.PRICE_AMOUNT.getCode());
//            String agentPrice = agentPriceDic.getValue();
//            BigDecimal price = new BigDecimal(agentPrice);
            if(agentPrice.compareTo(amount) != 0){
                return new FebsResponse().fail().message("成为合伙人的金额为"+agentPrice);
            }
        }
        String rechargeNo = "CZ_"+MallUtils.getOrderNum();
        apiRechargeWalletDto.setRechargeNo(rechargeNo);
        apiRechargeWalletDto.setMemberId(memberId);
        BrandWCPayRequestData brandWCPayRequestData = null;
        try {
            brandWCPayRequestData = iXcxPayService.startRechargeWallet(apiRechargeWalletDto);
        } catch (Exception e) {
            throw new FebsException("支付失败");
        }
        mallMoneyFlowService.addMoneyFlow(
                memberId,
                amount,
                MoneyFlowTypeEnum.RECHARGE.getValue(),
                rechargeNo,
                FlowTypeEnum.BALANCE.getValue(),
                "余额充值",1);
        String wxResultStr = JSONUtil.toJsonStr(brandWCPayRequestData);
        String payResultStr = brandWCPayRequestData.getPrepay_id();
        Map<String, Object> map = new HashMap<>();
        map.put("orderInfo", payResultStr);
        map.put("wxResultStr", wxResultStr);
        return new FebsResponse().success().data(map).message("充值即将到账");
    }
    @Override
    public void updateMemberAgent(Long memberId,String levelCode) {
        mallAgentRecordMapper.updateStateByMemberId(memberId);
        //更新用户表中的LEVEL
        MallMember mallMember = this.baseMapper.selectById(memberId);
        mallMember.setLevel(levelCode);
        this.baseMapper.updateById(mallMember);
    }
    @Override
    public FebsResponse agentDetail() {
        DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.AGENT_DETAILS.getType(), DataDictionaryEnum.AGENT_DETAILS.getCode());
        Map<String, Object> map = new HashMap<>();
        if(ObjectUtil.isNotEmpty(dataDictionaryCustom)){
            map.put("agentDetail", dataDictionaryCustom.getValue());
        }
        return new FebsResponse().success().data(map);
    }
    @Override
    public FebsResponse activityInfo() {
        DataDictionaryCustom activityBulletinDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.ACTIVITY_BULLETIN.getType(), DataDictionaryEnum.ACTIVITY_BULLETIN.getCode());
        DataDictionaryCustom giveAmountDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.GIVE_AMOUNT.getType(), DataDictionaryEnum.GIVE_AMOUNT.getCode());
        DataDictionaryCustom giveStateDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.GIVE_STATE.getType(), DataDictionaryEnum.GIVE_STATE.getCode());
        Map<String, Object> map = new HashMap<>();
        if(ObjectUtil.isNotEmpty(activityBulletinDic)){
            map.put("activityBulletin",
                    ObjectUtil.isEmpty(activityBulletinDic.getValue()) ? "暂无活动" : activityBulletinDic.getValue());
        }
        if(ObjectUtil.isNotEmpty(giveAmountDic)){
            map.put("giveAmount",
                    ObjectUtil.isEmpty(giveAmountDic.getValue()) ? 0 : giveAmountDic.getValue());
        }
        map.put("giveState",giveStateDic.getValue());
        return new FebsResponse().success().data(map);
    }
    @Override
    public FebsResponse agentApplyInfo() {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        ApiMallAgentRecordVo apiMallAgentRecordVo = mallAgentRecordMapper.selectApiMallAgentRecordVoByMemberIdAndState(memberId,MallAgentRecord.APPLY_ING);
        return new FebsResponse().success().data(apiMallAgentRecordVo);
    }
    private  String getXcxLoginUrl(String code) {
        String wechatLoginUrl =xcxProperties.getWecharLoginUrl();
        return String.format(wechatLoginUrl, xcxProperties.getXcxAppid(), xcxProperties.getXcxSecret(), code);