package org.ts.ddcs.db.impl;

import org.ts.ddcs.db.DatabaseService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author dylan
 */
@Service("DatabaseService")
@Transactional
public class DatabaseServiceImpl implements DatabaseService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void saveRtu(String rtuCode) {
//        String sql ="SELECT * FROM t_base_rtu_info WHERE RTU_CD='"+rtuCode+"'";
//        List<Map<String,Object>> list =jdbcTemplate.queryForList(sql);
//        if (null != list && list.size()>0){
//
//        }else{
//           sql ="INSERT INTO `t_base_rtu_info`(`RTU_CD`) VALUES ('"+rtuCode+"')";
//           jdbcTemplate.update(sql);
//        }
    }

    @Override
    public void saveDatagram(String rtuCode, Date logTime, String datagram) {
        String sql = "INSERT INTO `t_data_datagram`(`RTU_CODE`,`DATAGRAM`,`LOG_TIME`) VALUES (?,?,?)";
        jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                int index = 0;
                preparedStatement.setString(++index, rtuCode);
                preparedStatement.setString(++index, datagram);
                preparedStatement.setTimestamp(++index, new Timestamp(logTime.getTime()));
            }
        });
    }

    @Override
    public long datagramCount(Date startTime, Date endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "SELECT count(d.ID) num FROM t_data_datagram d  ";
        sql += " WHERE 1=1 ";
        sql += " AND ( d.LOG_TIME >= '" + sdf.format(startTime) + "' AND  d.LOG_TIME < '" + sdf.format(endTime) + "')";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (null != list && list.size() > 0) {
            Map<String, Object> m = list.get(0);
            return (long) m.get("num");
        }
        return 0;
    }

    @Override
    public void updateLastTime(String rtuCode, Date time) {
        //更新实时状态表
        String sql = "select rtu_code from  rtu_realtime_status where rtu_code = '" + rtuCode + "' ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (list.size() > 0) {
            sql = "UPDATE  rtu_realtime_status  ";
            sql += " SET last_up_time=?,update_time=? ";
            sql += " WHERE rtu_code=? ";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setTimestamp(++index, new Timestamp(time.getTime()));
                    preparedStatement.setTimestamp(++index, new Timestamp(System.currentTimeMillis()));
                    preparedStatement.setString(++index, rtuCode);
                }
            });
        } else {
            sql = "INSERT INTO rtu_realtime_status  (rtu_code,last_up_time) VALUES (?,?)";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, rtuCode);
                    preparedStatement.setTimestamp(++index, new Timestamp(time.getTime()));
                }
            });
        }
    }


}
