package com.sqx.modules.utils;

import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 邀请码生成解密工具类
 * @author fang
 * @date 2020/7/8
 */
@Component
public class InvitationCodeUtil {


    private static UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        InvitationCodeUtil.userService = userService;
    }

    /**
     * 根据ID生成六位随机码
     * @param id ID
     * @return 随机码
     */
    public static String toSerialCode(long id) {
        while (true){
            int t = (int) ((Math.random() * 9 + 1) * 100000);
            String s = String.valueOf(t);
            UserEntity userEntity = userService.queryByInvitationCode(s);
            if(userEntity==null){
                return s;
            }
        }
    }

    /**
     * 根据随机码生成ID
     * @param code 随机码
     * @return ID
     */
    public static long codeToId(String code) {
        UserEntity userEntity = userService.queryByInvitationCode(code);
        return userEntity.getUserId();
    }






}