package com.yolo.keyboard.api.tenantbalance;

import com.yolo.keyboard.dal.dataobject.tenantbalance.TenantBalanceDO;
import com.yolo.keyboard.dal.mysql.tenantbalance.TenantBalanceMapper;
import com.yolo.keyboard.module.system.api.tenantbalance.TenantBalanceApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 租户余额 API 实现类
 *
 * @author ziin
 */
@Service
@Slf4j
public class TenantBalanceApiImpl implements TenantBalanceApi {

    @Resource
    private TenantBalanceMapper tenantBalanceMapper;

    @Override
    public void initTenantBalance(Long tenantId) {
        // 检查是否已存在
        TenantBalanceDO existingBalance = tenantBalanceMapper.selectById(tenantId);
        if (existingBalance != null) {
            log.info("[initTenantBalance] 租户 {} 钱包已存在，跳过初始化", tenantId);
            return;
        }

        // 初始化租户钱包
        TenantBalanceDO balance = new TenantBalanceDO();
        balance.setId(tenantId);
        balance.setBalance(BigDecimal.ZERO);
        balance.setWithdrawableBalance(BigDecimal.ZERO);
        balance.setFrozenAmt(BigDecimal.ZERO);
        balance.setVersion(0);
        tenantBalanceMapper.insert(balance);

        log.info("[initTenantBalance] 租户 {} 钱包初始化成功", tenantId);
    }
}
