All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.faasadmin.faas.business.member.service.impl.MemberBussServiceImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021-Now http://faasadmin.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 */

package com.faasadmin.faas.business.member.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import com.faasadmin.faas.business.core.module.system.service.file.SysFileBussService;
import com.faasadmin.faas.business.core.module.system.service.sms.SysSmsCodeBussService;
import com.faasadmin.faas.business.member.dal.dataobject.MemberBussDO;
import com.faasadmin.faas.business.member.dal.mysql.MemberBussMapper;
import com.faasadmin.faas.business.member.service.MemberBussService;
import com.faasadmin.faas.business.member.vo.info.MemberUpdateMobileReqVO;
import com.faasadmin.faas.services.finance.service.balance.SupFinanceBalanceService;
import com.faasadmin.faas.services.finance.vo.balance.SupFinanceBalanceCreateReqVO;
import com.faasadmin.faas.services.member.dal.dataobject.member.SupMemberInfoDO;
import com.faasadmin.faas.services.member.dal.dataobject.oauth.SupMemberOauthDO;
import com.faasadmin.faas.services.member.dal.mysql.member.SupMemberInfoMapper;
import com.faasadmin.faas.services.member.service.oauth.SupMemberOauthService;
import com.faasadmin.faas.services.member.vo.member.SupMemberInfoRegisterReqVO;
import com.faasadmin.faas.services.member.vo.oauth.SupMemberOauthCreateReqVO;
import com.faasadmin.faas.services.member.vo.oauth.SupMemberOauthUpdateReqVO;
import com.faasadmin.faas.services.system.dto.code.SysSmsCodeCheckReqDTO;
import com.faasadmin.faas.services.system.dto.code.SysSmsCodeUseReqDTO;
import com.faasadmin.faas.services.system.enums.sms.SysSmsSceneEnum;
import com.faasadmin.framework.common.enums.CommonStatusEnum;
import com.faasadmin.framework.common.utils.ObjectUtils;
import com.faasadmin.framework.common.uuid.IdUtils;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;

import static com.faasadmin.framework.common.constant.ErrorCodeConstants.USER_NOT_EXISTS;
import static com.faasadmin.framework.common.constant.ErrorCodeConstants.USER_USERNAME_EXISTS;
import static com.faasadmin.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.faasadmin.framework.common.utils.ServletUtils.getClientIP;

/**
 * @version: V1.0
 * @author: fendo
 * @description: 会员业务服务实现
 * @data: 2022-04-21 20:42
 **/
@Service
public class MemberBussServiceImpl implements MemberBussService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private SupMemberOauthService supMemberOauthService;

    @Resource
    private SupMemberInfoMapper supMemberInfoMapper;

    @Resource
    private MemberBussMapper memberBussMapper;

    @Resource
    private SupFinanceBalanceService financeBalanceService;

    @Resource
    private SysFileBussService sysFileBussService;

    @Resource
    private SysSmsCodeBussService sysSmsCodeService;

    @Override
    public SupMemberInfoDO createMemberByNickNameIfAbsent(String nickName, String registerIp) {
        return null;
    }

    @Override
    public SupMemberInfoDO createMemberIfAbsent(Long lesseeId, Long appId, String nickName, String avatar, String key, String signature, String platform, String format, String registerIp) {
        // 用户已经存在
        SupMemberInfoDO memberInfoDO = supMemberInfoMapper.selectMemberInfo(lesseeId, appId, nickName, signature);
        if (memberInfoDO != null) {
            return memberInfoDO;
        }
        // 用户不存在,则进行创建
        return this.createMember(lesseeId, appId, nickName, avatar, key, signature, registerIp);
    }

    @Transactional
    @Override
    public SupMemberInfoDO createMemberAndOuathIfAbsent(Long lesseeId, Long appId, String unionId, String openId, String nickName, String avatar, String key, String signature, String platform, String format, String registerIp) {
        // 用户已经存在
        SupMemberOauthDO oauth = supMemberOauthService.getMemberOauthByOpenIdAndUnionId(lesseeId, appId, openId, unionId, platform);
        if (oauth != null) {
            return supMemberInfoMapper.selectMemberInfo(lesseeId, appId, oauth.getMemberId());
        }
        SupMemberInfoDO member = this.createMember(lesseeId, appId, nickName, avatar, key, signature, registerIp);
        this.initMemberData(lesseeId, appId, member.getId());
        SupMemberOauthCreateReqVO createReqVO = new SupMemberOauthCreateReqVO();
        createReqVO.setLesseeId(lesseeId);
        createReqVO.setAppId(appId);
        createReqVO.setSessionId(IdUtils.fastSimpleUUID());
        createReqVO.setMemberId(member.getId());
        createReqVO.setOauthId(openId);
        createReqVO.setUnionId(unionId);
        createReqVO.setPlatform(platform);
        createReqVO.setFormat(format);
        createReqVO.setSessionKey(key);
        createReqVO.setExtendS0(signature);
        supMemberOauthService.create(createReqVO);
        return member;
    }

    @Override
    public SupMemberInfoDO createMemberAndOuathIfAbsent(Long lesseeId, Long appId, String unionId, String openId, String sessionKey, String platform, String registerIp, String sessionId, String expand) {
        // 用户已经存在
        SupMemberOauthDO oauth = supMemberOauthService.getMemberOauthByOpenIdAndUnionId(lesseeId, appId, openId, unionId, platform);
        if (oauth != null) {
            SupMemberInfoDO supMemberInfoDO = supMemberInfoMapper.selectMemberInfo(lesseeId, appId, oauth.getMemberId());
            //更新授权信息
            updateMemberOauthInfo(oauth.getId(), supMemberInfoDO.getId(), unionId, openId, sessionKey, platform, expand);
            return supMemberInfoDO;
        }
        SupMemberInfoDO member = this.createMember(lesseeId, appId, registerIp);
        this.initMemberData(lesseeId, appId, member.getId());
        createMemberOauthInfo(lesseeId, appId, member.getId(), unionId, openId, sessionKey, platform, sessionId, expand);
        return member;
    }

    @Override
    public MemberBussDO getMemberBusinessByUnionIdAndOpenId(Long lesseeId, Long appId, String unionId, String openId, String platform) {
        SupMemberOauthDO oauth = supMemberOauthService.getMemberOauthByOpenIdAndUnionId(lesseeId, appId, openId, unionId, platform);
        if (ObjectUtils.isNotEmptyAndNull(oauth)) {
            return memberBussMapper.selectById(oauth.getMemberId());
        }
        return null;
    }

    @Override
    public MemberBussDO getMemberBusinessByCode(String code) {
        return memberBussMapper.selectMemberBusinessByCode(code);
    }

    @Override
    public MemberBussDO getMemberBusinessById(Long id) {
        return memberBussMapper.selectById(id);
    }

    @Override
    public void createMemberOauthInfo(Long lesseeId, Long appId, Long memberId, String unionId, String openId, String key, String platform, String sessionId, String format) {
        SupMemberOauthCreateReqVO createReqVO = new SupMemberOauthCreateReqVO();
        createReqVO.setLesseeId(lesseeId);
        createReqVO.setAppId(appId);
        createReqVO.setMemberId(memberId);
        createReqVO.setOauthId(openId);
        createReqVO.setSessionId(sessionId);
        createReqVO.setUnionId(unionId);
        createReqVO.setPlatform(platform);
        createReqVO.setSessionKey(key);
        createReqVO.setFormat(format);
        supMemberOauthService.create(createReqVO);
    }

    @Override
    public void createMemberOauthInfo(Long lesseeId, Long appId, Long memberId, String unionId, String openId, String key, String platform, String sessionId) {
        SupMemberOauthCreateReqVO createReqVO = new SupMemberOauthCreateReqVO();
        createReqVO.setLesseeId(lesseeId);
        createReqVO.setAppId(appId);
        createReqVO.setMemberId(memberId);
        createReqVO.setOauthId(openId);
        createReqVO.setSessionId(sessionId);
        createReqVO.setUnionId(unionId);
        createReqVO.setPlatform(platform);
        createReqVO.setSessionKey(key);
        supMemberOauthService.create(createReqVO);
    }

    @Override
    public void updateMemberOauthInfo(Long id, Long memberId, String unionId, String openId, String key, String platform, String format) {
        SupMemberOauthUpdateReqVO createReqVO = new SupMemberOauthUpdateReqVO();
        createReqVO.setId(id);
        createReqVO.setMemberId(memberId);
        createReqVO.setOauthId(openId);
        createReqVO.setUnionId(unionId);
        createReqVO.setPlatform(platform);
        createReqVO.setSessionKey(key);
        createReqVO.setFormat(format);
        supMemberOauthService.update(createReqVO);
    }

    @Override
    public void updateMemberOauthInfo(Long id, Long memberId, String unionId, String openId, String key, String platform) {
        supMemberOauthService.updateMemberOauth(id, memberId, unionId, openId, key, platform);
    }

    @Override
    public void saveOrUpdateMemberAuthInfo(Long lesseeId, Long appId, Long memberId, String unionId, String openId, String nickName, String avatar, String key, String platform, String sessionId, String format) {
        // 用户已经存在
        SupMemberOauthDO oauth = supMemberOauthService.getMemberOauthByOpenIdAndUnionId(lesseeId, appId, openId, unionId, platform);
        if (oauth == null) {
            createMemberOauthInfo(lesseeId, appId, memberId, unionId, openId, key, platform, sessionId, format);
        } else {
            //更新授权信息
            updateMemberOauthInfo(oauth.getId(), memberId, unionId, openId, key, platform, format);
        }
        supMemberInfoMapper.updateNickNameAndAvatarByMemberId(lesseeId, appId, memberId, nickName, avatar);
    }

    @Override
    public SupMemberInfoDO createMember(Long lesseeId, Long appId, String nickName, String avatar, String key, String signature, String registerIp) {
        // 生成密码
        String password = IdUtil.fastSimpleUUID();
        // 插入用户
        SupMemberInfoDO user = new SupMemberInfoDO();
        user.setNickName(nickName);
        user.setAvatar(avatar);
        user.setCode(IdUtil.fastSimpleUUID());
        user.setLesseeId(lesseeId);
        user.setAppId(appId);
        user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
        user.setPassword(passwordEncoder.encode(password)); // 加密密码
        user.setRegisterIp(registerIp);
        user.setExtendS0(key);
        user.setExtendS1(signature);
        supMemberInfoMapper.insert(user);
        return user;
    }

    @Override
    public SupMemberInfoDO createMember(Long lesseeId, Long appId, String registerIp) {
        // 生成密码
        String password = IdUtil.fastSimpleUUID();
        // 插入用户
        SupMemberInfoDO user = new SupMemberInfoDO();
        user.setCode(IdUtil.simpleUUID());
        user.setLesseeId(lesseeId);
        user.setAppId(appId);
        user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
        user.setPassword(passwordEncoder.encode(password)); // 加密密码
        user.setRegisterIp(registerIp);
        supMemberInfoMapper.insert(user);
        return user;
    }

    @Override
    public SupMemberInfoDO createMember(Long lesseeId, Long appId, String code, String password, String registerIp) {
        // 插入用户
        SupMemberInfoDO user = new SupMemberInfoDO();
        user.setCode(code);
        user.setLesseeId(lesseeId);
        user.setAppId(appId);
        user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
        user.setPassword(password); // 加密密码
        user.setRegisterIp(registerIp);
        supMemberInfoMapper.insert(user);
        return user;
    }

    @Override
    public SupMemberInfoDO createMemberByMobileAndPassWord(Long lesseeId, Long appId, String mobile, String password, String nickName, String registerIp) {
        // 插入用户
        SupMemberInfoDO user = new SupMemberInfoDO();
        user.setCode(IdUtil.simpleUUID());
        user.setLesseeId(lesseeId);
        user.setMobile(mobile);
        user.setAppId(appId);
        user.setNickName(nickName);
        user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
        user.setPassword(password); // 加密密码
        user.setRegisterIp(registerIp);
        supMemberInfoMapper.insert(user);
        return user;
    }

    @Transactional
    @Override
    public void initMemberData(Long lesseeId, Long appId, Long memberId) {
        //初始化会员余额表
        SupFinanceBalanceCreateReqVO createReqVO = new SupFinanceBalanceCreateReqVO();
        createReqVO.setLesseeId(lesseeId);
        createReqVO.setAppId(appId);
        createReqVO.setMemberId(memberId);
        createReqVO.setBalance(BigDecimal.ZERO);
        createReqVO.setTotal(BigDecimal.ZERO);
        createReqVO.setConsume(BigDecimal.ZERO);
        createReqVO.setWithdraw(BigDecimal.ZERO);
        createReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        financeBalanceService.createBalance(createReqVO);
    }

    @Transactional
    @Override
    public void register(SupMemberInfoRegisterReqVO reqVO) {
        String mobile = reqVO.getMobile();
        //校验验证码是否正确
        SysSmsCodeCheckReqDTO sysSmsCodeCheckReqDTO = new SysSmsCodeCheckReqDTO();
        sysSmsCodeCheckReqDTO.setMobile(mobile);
        sysSmsCodeCheckReqDTO.setScene(SysSmsSceneEnum.MEMBER_REGISTER_SMS.getScene());
        sysSmsCodeCheckReqDTO.setCode(reqVO.getCode());
        sysSmsCodeService.checkSmsCode(sysSmsCodeCheckReqDTO);
        // 用户已经存在
        SupMemberInfoDO member = supMemberInfoMapper.selectByMobile(mobile);
        if (ObjectUtils.isNotEmptyAndNull(member)) {
            throw exception(USER_USERNAME_EXISTS);
        }
        // 插入用户
        member = new SupMemberInfoDO();
        member.setMobile(mobile);
        //user.setPlatform(reqVO.getPlatform());
        //绑定上级
        if (ObjectUtils.isNotEmptyAndNull(reqVO.getInvitation())) {
            SupMemberInfoDO parentMember = supMemberInfoMapper.selectMemberByInvitation(reqVO.getInvitation());
            member.setParentId(parentMember.getParentId());
        }
        member.setLesseeId(reqVO.getLesseeId());
        member.setAppId(reqVO.getAppId());
        member.setNickName(reqVO.getNickName());
        member.setUserName(reqVO.getUserName());
        member.setAvatar(reqVO.getAvatar());
        member.setCode(reqVO.getNumber());
        member.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
        member.setPassword(reqVO.getPassword()); // 加密密码
        member.setRegisterIp(getClientIP());
        supMemberInfoMapper.insert(member);
        this.initMemberData(reqVO.getLesseeId(), reqVO.getAppId(), member.getId());
    }

    @VisibleForTesting
    public SupMemberInfoDO checkUserExists(Long id) {
        if (id == null) {
            return null;
        }
        SupMemberInfoDO user = supMemberInfoMapper.selectById(id);
        if (user == null) {
            throw exception(USER_NOT_EXISTS);
        }
        return user;
    }

    @Override
    public String updateAvatar(Long userId, InputStream avatarFile) throws Exception {
        this.checkUserExists(userId);
        // 创建文件
        String avatar = sysFileBussService.createFile(IoUtil.readBytes(avatarFile));
        // 更新头像路径
        supMemberInfoMapper.updateById(SupMemberInfoDO.builder().id(userId).avatar(avatar).build());
        return avatar;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMobile(Long userId, MemberUpdateMobileReqVO reqVO) {
        // 检测用户是否存在
        checkUserExists(userId);
        // 校验旧手机和旧验证码
        sysSmsCodeService.useSmsCode(new SysSmsCodeUseReqDTO().setMobile(reqVO.getMobile()).setCode(reqVO.getOldCode()).setScene(SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene()).setUsedIp(getClientIP()));
        // 使用新验证码
        sysSmsCodeService.useSmsCode(new SysSmsCodeUseReqDTO().setMobile(reqVO.getMobile()).setCode(reqVO.getCode()).setScene(SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene()).setUsedIp(getClientIP()));
        // 更新用户手机
        supMemberInfoMapper.updateById(SupMemberInfoDO.builder().id(userId).mobile(reqVO.getMobile()).build());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy