package com.bonus.core.starter.data;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.data.dataset.Executors;
import com.bonus.core.data.dataset.rule.loader.RouterLoader;
import com.bonus.core.merchant.constant.MerchantStatusEnum;
import com.bonus.core.merchant.entity.MercMerchantInSystem;
import com.bonus.core.merchant.mapper.MercMerchantInSystemMapper;
import com.bonus.core.tenant.TenantLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;


//public class DefaultDataSourceLoader implements RouterLoader, DataSourceLoader, TenantLoader {
@Component
public class DefaultDataSourceLoader implements RouterLoader, TenantLoader {
//   @Autowired
//   @Lazy
//   DataSetMapper dataSetMapper;
   @Autowired
   @Lazy
   MercMerchantInSystemMapper mercMerchantInSystemMapper;
//   @Value("${system.dataset:dataset}")
//   private String dataset;

   public RouterLoader.Router load(Object key) {
      MercMerchantInSystem merchant = (MercMerchantInSystem)Executors.readInSystem(() -> {
         return (MercMerchantInSystem)this.mercMerchantInSystemMapper.selectById((Long)key);
      });
      if (merchant == null) {
         throw new ServiceException("商户[" + String.valueOf(key) + "]不存在");
      } else {
         return new RouterLoader.Router(merchant.getDataset(), merchant.getSchema());
      }
   }

//   public DataSourceLoader.DataSet load(String name) {
//      DS dset = (DS)Executors.readInSystem(() -> {
//         return this.dataSetMapper.queryById(this.dataset, name);
//      });
//      List<MercMerchantInSystem> mercMerchantInSystems = (List)Executors.readInSystem(() -> {
//         return this.mercMerchantInSystemMapper.selectList((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MercMerchantInSystem.class).eq(MercMerchantInSystem::getDataset, name)).eq(MercMerchantInSystem::getStatus, MerchantStatusEnum.NORMAL.key()));
//      });
//      Map<String, Long> schemas = null;
//      if (CollectionUtil.isNotEmpty(mercMerchantInSystems)) {
//         schemas = (Map)mercMerchantInSystems.stream().collect(Collectors.toMap(MercMerchantInSystem::getSchema, MercMerchantInSystem::getTenantId));
//      }
//
//      return new DataSourceLoader.DataSet(name, dset.getMaster(), schemas, dset.getSlave());
//   }
//
//   public List<DataSourceLoader.DataSet> load() {
//      List<DS> dset = (List)Executors.readInSystem(() -> {
//         return this.dataSetMapper.list(this.dataset);
//      });
//      List<MercMerchantInSystem> mercMerchantInSystems = (List)Executors.readInSystem(() -> {
//         return this.mercMerchantInSystemMapper.selectList((Wrapper)Wrappers.lambdaQuery(MercMerchantInSystem.class).eq(MercMerchantInSystem::getStatus, MerchantStatusEnum.NORMAL.key()));
//      });
//      Map<String, Map<String, Long>> ds_schemas = Maps.newHashMap();
//      if (CollectionUtil.isNotEmpty(mercMerchantInSystems)) {
//         ds_schemas = (Map)mercMerchantInSystems.stream().collect(Collectors.groupingBy(MercMerchantInSystem::getDataset, Collectors.toMap(MercMerchantInSystem::getSchema, MercMerchantInSystem::getTenantId)));
//      }
//
//      return (List)dset.stream().map((ds) -> {
//         return new DataSourceLoader.DataSet(ds.getName(), ds.getMaster(), (Map)ds_schemas.get(ds.getName()), ds.getSlave());
//      }).collect(Collectors.toList());
//   }

   public Set listTenant() {
      return Executors.readInSystem(() -> {
         return (Set)this.mercMerchantInSystemMapper
                 .selectList(Wrappers.lambdaQuery(MercMerchantInSystem.class)
                         .select(MercMerchantInSystem::getTenantId)
              .eq(MercMerchantInSystem::getStatus, MerchantStatusEnum.NORMAL.key()))
      .stream().mapToLong(MercMerchantInSystem::getTenantId).boxed().collect(Collectors.toSet());
      });
   }

}
