package com.gyee.dataadapter.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gyee.dataadapter.dao.TsPoint;
import com.gyee.dataadapter.dao.ViewTspointMapper;
import com.gyee.dataadapter.entity.ViewTspoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TsPointService {

    //key:测点
    private Map<String, TsPoint> tagMap = new HashMap<>();

    @Resource
    private ViewTspointMapper tsPointMapper;
    private final LambdaQueryWrapper<ViewTspoint> wrapper = new LambdaQueryWrapper<>();

    public List<TsPoint> getTsPoint(String... tagNames) {
        List<TsPoint> tsPoints = new ArrayList<>();
        for (String tagName : tagNames) {
            tsPoints.add(getTsPoint(tagName));
        }
        return tsPoints;
    }

    public TsPoint getTsPoint(String tagName) {
        TsPoint tsPoint = tagMap.get(tagName);
        if (tsPoint == null) {
            wrapper.clear();
            wrapper.eq(ViewTspoint::getId, tagName);
            ViewTspoint one = tsPointMapper.selectOne(wrapper, false);
            tagMap.put(tagName, one);
            return one;
        }
        return tsPoint;
    }
}
