package com.gyee.wisdom.dao.sql.things;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.timeseries.BasicTsPoint;
import com.gyee.wisdom.common.data.timeseries.TsDataType;
import com.gyee.wisdom.common.data.timeseries.TsPoint;
import com.gyee.wisdom.dao.sql.point.entity.*;
import com.gyee.wisdom.dao.sql.point.repository.*;
import com.gyee.wisdom.dao.sql.things.entity.TsPointEntity;
import com.gyee.wisdom.dao.sql.things.repository.TsPointRepository;
import com.gyee.wisdom.dao.things.IThingsPointDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;


@Component
public class JpaThingsPointDao implements IThingsPointDao {

    @Autowired
    private TsPointRepository tsPointRepository;


    @Override
    public List<TsPoint> findTsPointByIds(String... tagNames) {
        return DataUtil.convertDataList(tsPointRepository.findAllByIdIn(tagNames));
    }

    @Override
    public TsPoint findTsPointById(String tagName) {
        Optional<TsPointEntity> obj = tsPointRepository.findById(tagName);
        if (obj.isPresent())
            return obj.get().toData();

        return null;
    }

    @Override
    public List<TsPoint> findTsPointByUnformCodes(String thingType, String thingId, String... uCodes) {
        return DataUtil.convertDataList(tsPointRepository.findAllByThingTypeAndThingIdAndUniformCodeIn(thingType, thingId, uCodes));
    }

    @Override
    public List<TsPoint> findTsPointByThingIds(String thingType, String uniformCode, String... thingIds) {
        return DataUtil.convertDataList(tsPointRepository.findAllByThingTypeAndUniformCodeAndThingIdIn(thingType, uniformCode, thingIds));
    }

    @Override
    public   List<TsPoint> getAllTsPoint(){
        return DataUtil.convertDataList(tsPointRepository.findAll());
    }

}
