package com.kym.service.admin.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.kym.entity.admin.EquipmentInfo;
import com.kym.mapper.admin.EquipmentInfoMapper;
import com.kym.service.MyBaseServiceImpl;
import com.kym.service.admin.EquipmentInfoService;
import com.kym.service.cache.KymCache;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * <p>
 * 充电桩桩体设备 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-08-05
 */
@Service
@DS("db-admin")
public class EquipmentInfoServiceImpl extends MyBaseServiceImpl<EquipmentInfoMapper, EquipmentInfo> implements EquipmentInfoService {

    @PostConstruct
    private void init() {
        // 手动切换数据源
        DynamicDataSourceContextHolder.push("db-admin");
        KymCache.INSTANCE.putConnectorId2ParkingNo(list().stream().collect(Collectors.toMap(equipmentInfo -> equipmentInfo.getEquipmentId().concat("1"), EquipmentInfo::getParkingNo)));
        DynamicDataSourceContextHolder.poll();
    }

}
