package com.gyee.edge.gateway.config.read;


import com.gyee.edge.gateway.config.point.PointMapper;
import com.gyee.edge.gateway.config.sqlite.Database;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Component
public class ReadPointSqLiteData {
    @Autowired
    private Database db;

    public List<PointMapper> readData() {
        List<PointMapper> pointMapperList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from point";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                PointMapper pointMapper = new PointMapper();
                pointMapper.setRatio(rs.getDouble("ratio"));
                pointMapper.setDataSource(rs.getString("datasource"));
                pointMapper.setMessageKey(rs.getInt("messagekey"));
                pointMapper.setSourceAddress(rs.getInt("sourceaddress"));
                pointMapper.setMessageId(rs.getString("messageid"));
                pointMapper.setProtocolSource(rs.getString("protocolsource"));
                pointMapperList.add(pointMapper);
            }
            System.out.println(pointMapperList.get(0));
            db.releaseResource();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pointMapperList;
    }
}
