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

import com.gyee.wisdom.common.data.topic.TopicPoint;
import com.gyee.wisdom.dao.sql.topic.entity.PointEntity;
import com.gyee.wisdom.dao.sql.topic.repository.PointRepository;
import com.gyee.wisdom.dao.topic.ITopicPointDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @descrition:
 * @author:Wanghs
 * @date:2018-07-31
 */
@Component
public class JpaPointDao implements ITopicPointDao {

    @Autowired
    private PointRepository pointRepository;

    @Override
    public TopicPoint findByPoint(String point) {
        return pointRepository.findById(point).get().toData();
    }

    @Override
    public List<TopicPoint> findByPoints(List<String> pointList) {
        List<PointEntity> entityList = pointRepository.findAllById(pointList);
        List<TopicPoint> list = new ArrayList<>();
        for (int i = 0; i < entityList.size(); i++) {
            list.add(entityList.get(i).toData());
        }
        return list;
    }


    @Override
    public List<TopicPoint> findByStationIdAndPointTypeAndUniformCode(String stationId, String pointType, String uniformCode) {
        List<PointEntity> list = pointRepository.findByStationIdAndPointTypeAndUniformCode(stationId, pointType, uniformCode);
        List<TopicPoint> resultList = new ArrayList<>();
        list.forEach((k) -> {
            resultList.add(k.toData());
        });
        return resultList;
    }

    @Override
    public List<TopicPoint> findByStationIdAndPointTypeAndWindturbineId(String stationId, String pointType, String windturbineId) {
        List<PointEntity> list = pointRepository.findByStationIdAndPointTypeAndWindturbineId(stationId, pointType, windturbineId);
        List<TopicPoint> resultList = new ArrayList<>();
        list.forEach((k) -> {
            resultList.add(k.toData());
        });
        return resultList;
    }

    @Override
    public List<TopicPoint> findByPointNameLike(String pointName) {
        List<PointEntity> list = pointRepository.findByPointNameLike(pointName);
        List<TopicPoint> resultList = new ArrayList<>();
        list.forEach((k) -> {
            resultList.add(k.toData());
        });
        return resultList;
    }

    @Override
    public List<TopicPoint> findByUniformCodes(List<String> uniformCodeList) {
        List<PointEntity> list = pointRepository.findAllByUniformCodeIn(uniformCodeList);
        List<TopicPoint> resultList = new ArrayList<>();
        list.forEach((k) -> {
            resultList.add(k.toData());
        });
        return resultList;
    }


}
