package com.qiaoba.module.tenant.runner;

import com.qiaoba.api.system.service.SysConfigApiService;
import com.qiaoba.api.system.service.SysDictApiService;
import com.qiaoba.api.tenant.entity.SysTenant;
import com.qiaoba.api.tenant.entity.param.SysTenantParam;
import com.qiaoba.api.tenant.service.SysTenantApiService;
import com.qiaoba.api.tenant.utils.TenantUtil;
import com.qiaoba.common.base.constant.TenantConstant;
import com.qiaoba.common.base.context.BaseContext;
import com.qiaoba.module.tenant.service.SysTenantInitService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 项目启动后同步-[系统配置]
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023-07-01 08:34:20
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class SyncDataToCacheRunner implements ApplicationRunner {

    private final SysTenantApiService sysTenantApiService;
    private final SysTenantInitService sysTenantInitService;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        // 查询主库中所有正常的租户列表
        List<SysTenant> sysTenantList;
        try {
            BaseContext.setTenantId(TenantConstant.DEFAULT_TENANT_ID);
            sysTenantList = sysTenantApiService.selectList(SysTenantParam.buildNormalSelectParam());
        } finally {
            BaseContext.clearTenantId();
        }

        for (SysTenant sysTenant : sysTenantList) {
            sysTenantInitService.syncTenantData2Cache(sysTenant);
        }
    }


}
