package top.continew.admin.distribution.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import top.continew.admin.distribution.service.DistributionService;
import top.continew.admin.user.model.entity.TgUser;
import top.continew.admin.user.service.ITgUserService;
import top.continew.admin.utils.InviteCodeGenerator;
import top.continew.starter.core.validation.CheckUtils;

import java.io.Serializable;

@Service
public class DistributionServiceImpl implements DistributionService {
    @Resource
    private ITgUserService userService;

    @Override
    public String getInviteCode() {
        Object loginId = StpUtil.getLoginId();
        TgUser user = userService.getById((Serializable) loginId);
        CheckUtils.throwIfNull(user, "该账号未登录！");
        String inviteCode = user.getInviteCode();
        if (StrUtil.isBlank(inviteCode)) {
            inviteCode = InviteCodeGenerator.generateInviteCode();
            user.setInviteCode(inviteCode);
            userService.updateById(user);
            return inviteCode;
        }
        return inviteCode;
    }
}
