package org.ts.ddcs.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.Entity.WqRiverEntity;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Service.SwService;
import org.ts.ddcs.Service.WqService;
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.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("WqService")
@Transactional
@SuppressWarnings("all")
public class WqServiceImpl implements WqService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void SaveWqHighData(WqRiverEntity entity) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "select  * from  wq_river where STCD='" + entity.getSTCD() + "' and TM='" + timeFormat.format(new Date(entity.getTM().getTime())) + "'";
        List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
        if (records != null && records.size() > 0) {

            sql = "UPDATE  wq_river  set HIGH=?,DT=?,TS=? where STCD=? and TM=?";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, entity.getHIGH());
                    preparedStatement.setTimestamp(++index, entity.getDT());
                    preparedStatement.setTimestamp(++index, entity.getTS());
                    preparedStatement.setString(++index, entity.getSTCD());
                    preparedStatement.setTimestamp(++index, entity.getTM());
                }
            });
        }else{
            sql = "INSERT INTO wq_river  (STCD,TM,HIGH,DT,TS) VALUES (?,?,?,?,?)";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, entity.getSTCD());
                    preparedStatement.setTimestamp(++index, entity.getTM());
                    preparedStatement.setString(++index, entity.getHIGH());
                    preparedStatement.setTimestamp(++index, entity.getDT());
                    preparedStatement.setTimestamp(++index, entity.getTS());
                }
            });
        }
    }

    @Override
    public void SaveWqSpeedData(WqRiverEntity entity) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "select  * from  wq_river where STCD='" + entity.getSTCD() + "' and TM='" + timeFormat.format(new Date(entity.getTM().getTime())) + "'";
        List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
        if (records != null && records.size() > 0) {

            sql = "UPDATE  wq_river  set SPEED=?,DT=?,TS=? where STCD=? and TM=?";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, entity.getSPEED());
                    //preparedStatement.setString(++index, entity.getSPEED());
                    preparedStatement.setTimestamp(++index, entity.getDT());
                    preparedStatement.setTimestamp(++index, entity.getTS());
                    preparedStatement.setString(++index, entity.getSTCD());
                    preparedStatement.setTimestamp(++index, entity.getTM());
                }
            });
        }else{
            sql = "INSERT INTO wq_river  (STCD,TM,SPEED,DT,TS) VALUES (?,?,?,?,?)";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, entity.getSTCD());
                    preparedStatement.setTimestamp(++index, entity.getTM());
                    preparedStatement.setString(++index, entity.getSPEED());
                   // preparedStatement.setString(++index, entity.getSPEED());
                    preparedStatement.setTimestamp(++index, entity.getDT());
                    preparedStatement.setTimestamp(++index, entity.getTS());
                }
            });
        }
    }

    @Override
    public List<Map<String, Object>> QuerySpeedList(String rtuCode, int minValue) {
        try {
            String sql ="SELECT r.SPEED FROM wq_river r WHERE r.STCD='"+rtuCode+"' and r.SPEED is not null ORDER BY TM DESC LIMIT "+ minValue;
            List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
            return list;
        }catch (Exception e){
            LogHelper.error(e);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> QuerySpeedList(String rtuCode,String tm ) {
        try {
            String sql ="SELECT * FROM wq_river r WHERE r.STCD='"+rtuCode+"' and r.SPEED is not null  and TM >= '" + tm  + "' ORDER BY TM ";
            List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
            return list;
        }catch (Exception e){
            LogHelper.error(e);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> QuerySpeedListByTm(String rtuCode, int limit,String tm ) {
        try {
            String sql ="SELECT * FROM wq_river r WHERE r.STCD='"+rtuCode+"' and r.SPEED is not null  and TM <= '" + tm  + "' ORDER BY TM DESC  LIMIT "+ limit;
            List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
            return list;
        }catch (Exception e){
            LogHelper.error(e);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> QuerySpeedListAll(String rtuCode, int limit) {
        try {
            String sql ="SELECT * FROM wq_river r WHERE r.STCD='"+rtuCode+"' and r.SPEED is not null ORDER BY TM DESC  LIMIT "+ limit;
            List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
            return list;
        }catch (Exception e){
            LogHelper.error(e);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> QueryCorrectSpeedList(String rtuCode,String tm, int limit) {
        try {
            String sql ="SELECT r.SPEED_CORRECT FROM wq_river r WHERE r.STCD='"+rtuCode+"' and r.SPEED_CORRECT is not null and r.TM < '" + tm  + "' ORDER BY TM DESC LIMIT "+ limit;
            List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
            return list;
        }catch (Exception e){
            LogHelper.error(e);
        }
        return null;
    }

    @Override
    public void UpdateSpeedValue(String rtuCode, Timestamp tm, String value) {
       String  sql = "UPDATE  wq_river  set SPEED_CORRECT=? where STCD=? and TM=?";
        jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                int index = 0;
                preparedStatement.setString(++index,value);
                preparedStatement.setString(++index, rtuCode);
                preparedStatement.setTimestamp(++index,tm);
            }
        });
    }
}
