package com.qqchen.deploy.backend.system.service.impl;

import com.qqchen.deploy.backend.system.entity.Tenant;
import com.qqchen.deploy.backend.framework.annotation.ServiceType;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.system.model.TenantDTO;
import com.qqchen.deploy.backend.system.model.query.TenantQuery;
import com.qqchen.deploy.backend.system.repository.ITenantRepository;
import com.qqchen.deploy.backend.system.service.ITenantService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.qqchen.deploy.backend.framework.annotation.ServiceType.Type.DATABASE;

/**
 * 租户服务实现类
 * 主要功能：
 * 1. 租户的CRUD操作
 * 2. 租户状态管理
 *
 * @author QQChen
 * @version 1.0.0
 */
@Slf4j
@Service
@ServiceType(DATABASE)
public class TenantServiceImpl extends BaseServiceImpl<Tenant, TenantDTO, TenantQuery, Long> implements ITenantService {
    
    @Resource
    private ITenantRepository tenantRepository;
    
    @Override
    @Transactional
    public void updateStatus(Long id, boolean enabled) {
        Tenant tenant = findEntityById(id);
        tenant.setEnabled(enabled);
        tenantRepository.save(tenant);
        log.info("租户[{}]状态已更新为: {}", tenant.getName(), enabled ? "启用" : "禁用");
    }
    
    @Override
    @Transactional(readOnly = true)
    public boolean getStatus(Long id) {
        Tenant tenant = findEntityById(id);
        return tenant.getEnabled();
    }
}