package org.ts.ddcs.service.Impl;

import org.ts.ddcs.entity.RtuStatusEntity;
import org.ts.ddcs.entity.WarningInfo;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.service.WarningService;
import org.ts.ddcs.utils.GBConstants;
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.Propagation;
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;
import java.util.UUID;

/**
 * @author dylan
 */
@Service("WarningService")
@Transactional
public class WarningServiceImpl implements WarningService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public WarningInfo getLastWarning(String rtuCode, String warningType) {
        String sql = "SELECT  * FROM t_b_rtu_warning w  WHERE w.RTU_CODE='" + rtuCode + "' AND w.WARNING_TYPE='" + warningType + "' AND w.WARNING_STATUS = '0' ";
        List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
        if (null != records && records.size() > 0) {
            Map<String, Object> m = records.get(0);
            WarningInfo warningInfo = new WarningInfo();
            warningInfo.setId((String) m.get("ID"));
            return warningInfo;
        }
        return null;
    }

    @Override
    public void saveWarning(WarningInfo warningInfo) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = timeFormat.parse(warningInfo.getWarningCheckTime());
            String rtuCode = warningInfo.getRtuCode();
            String sql = "SELECT  w.ID, w.RTU_CODE FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + warningInfo.getRtuCode() + "'  AND w.WARNING_TYPE ='" + warningInfo.getWarningType() + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
            if (records != null && records.size() > 0) {
                Map<String, Object> record = records.get(0);
                sql = "UPDATE  t_b_rtu_warning  SET WARNING_INFO=?,WARNING_CONTENT=?,WARNING_CHECK_TIME=? where ID=?";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, warningInfo.getWarningInfo());
                        preparedStatement.setString(++index, warningInfo.getWarningContent());
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, (String) record.get("ID"));
                    }
                });
            } else {
                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                sql = "INSERT INTO t_b_rtu_warning  (ID,RTU_CODE,HAPPEN_TIME,WARNING_STATUS,WARNING_TYPE,WARNING_INFO,WARNING_CONTENT,WARNING_CHECK_TIME) VALUES (?,?,?,?,?,?,?,?)";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, uuid);
                        preparedStatement.setString(++index, warningInfo.getRtuCode());
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, warningInfo.getWarningStatus());
                        preparedStatement.setString(++index, warningInfo.getWarningType());
                        preparedStatement.setString(++index, warningInfo.getWarningInfo());
                        preparedStatement.setString(++index, warningInfo.getWarningContent());
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                    }
                });
            }
            sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='1' where RTU_CD='" + warningInfo.getRtuCode() + "'";
            jdbcTemplate.update(sql);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void warningRecovery(WarningInfo warningInfo) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = timeFormat.parse(warningInfo.getWarningCheckTime());
            Date lastTm = timeFormat.parse(warningInfo.getLastTime());
            String sql = "SELECT  *  FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + warningInfo.getRtuCode() + "'  AND w.WARNING_TYPE ='" + warningInfo.getWarningType() + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
            if (records != null && records.size() > 0) {
                for (Map<String, Object> m : records) {
                    sql = "UPDATE  t_b_rtu_warning  SET WARNING_CONTENT=?,RECOVERY_TIME=?,WARNING_CHECK_TIME=?,WARNING_STATUS=? where ID=?";
                    jdbcTemplate.update(sql, new PreparedStatementSetter() {
                        @Override
                        public void setValues(PreparedStatement preparedStatement) throws SQLException {
                            int index = 0;
                            preparedStatement.setString(++index, warningInfo.getWarningContent());
                            preparedStatement.setTimestamp(++index, new Timestamp(lastTm.getTime()));
                            preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                            preparedStatement.setString(++index, "1");
                            preparedStatement.setString(++index, (String) m.get("ID"));
                        }
                    });
                }
            }
            sql = "SELECT  w.RTU_CODE FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + warningInfo.getRtuCode() + "'  AND w.WARNING_TYPE !='" + warningInfo.getWarningType() + "' AND w.WARNING_STATUS ='0'";
            records = jdbcTemplate.queryForList(sql);
            if (null == records || records.size() == 0) {
                sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='0' where RTU_CD='" + warningInfo.getRtuCode() + "'";
                jdbcTemplate.update(sql);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void RtuAcWarnningHappen(RtuStatusEntity entity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = new Date(entity.getPickTime().getTime());
            Date checkTime = new Date();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE ='" + GBConstants.WARNING_TYPE_RTU_FAULT_AC + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> warningList = jdbcTemplate.queryForList(sql);
            if (null != warningList && warningList.size() > 0) {
                Map<String, Object> record = warningList.get(0);
                sql = "UPDATE  t_b_rtu_warning  SET WARNING_INFO=?,WARNING_CONTENT=?,WARNING_CHECK_TIME=? where ID=?";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, "交流电断电故障");
                        preparedStatement.setString(++index, "{}");
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, (String) record.get("ID"));
                    }
                });
            } else {
                String id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                sql = "INSERT INTO t_b_rtu_warning  (ID,RTU_CODE,HAPPEN_TIME,WARNING_STATUS,WARNING_TYPE,WARNING_INFO,WARNING_CONTENT,WARNING_CHECK_TIME) VALUES (?,?,?,?,?,?,?,?)";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, id);
                        preparedStatement.setString(++index, entity.getStcd());
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, "0");
                        preparedStatement.setString(++index, GBConstants.WARNING_TYPE_RTU_FAULT_AC);
                        preparedStatement.setString(++index, "交流电断电故障");
                        preparedStatement.setString(++index, "{}");
                        preparedStatement.setTimestamp(++index, new Timestamp(checkTime.getTime()));
                    }
                });
            }
            sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='1' where RTU_CD='" + entity.getStcd() + "'";
            jdbcTemplate.update(sql);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void RtuAcWarnningRecovery(RtuStatusEntity entity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = new Date(entity.getPickTime().getTime());
            Date checkTime = new Date();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE ='" + GBConstants.WARNING_TYPE_RTU_FAULT_AC + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> warningList = jdbcTemplate.queryForList(sql);
            if (null != warningList && warningList.size() > 0) {
                for (Map<String, Object> m : warningList) {
                    sql = "UPDATE  t_b_rtu_warning  SET WARNING_STATUS='1' ,RECOVERY_TIME=?  WHERE ID =?";
                    jdbcTemplate.update(sql, new PreparedStatementSetter() {
                        @Override
                        public void setValues(PreparedStatement preparedStatement) throws SQLException {
                            int index = 0;
                            preparedStatement.setTimestamp(++index, new Timestamp(new Date().getTime()));
                            preparedStatement.setString(++index, (String) m.get("id"));
                        }
                    });
                }
            }
            sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE !='" + GBConstants.WARNING_TYPE_RTU_FAULT_AC + "' AND w.WARNING_STATUS ='0'";
            warningList = jdbcTemplate.queryForList(sql);
            if (null == warningList || warningList.size() == 0) {
                sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='0' where RTU_CD='" + entity.getStcd() + "'";
                jdbcTemplate.update(sql);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void RtuBvWarnningHappen(RtuStatusEntity entity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = new Date(entity.getPickTime().getTime());
            Date checkTime = new Date();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE ='" + GBConstants.WARNING_TYPE_RTU_FAULT_BV + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> warningList = jdbcTemplate.queryForList(sql);
            if (null != warningList && warningList.size() > 0) {
                Map<String, Object> record = warningList.get(0);
                sql = "UPDATE  t_b_rtu_warning  SET WARNING_INFO=?,WARNING_CONTENT=?,WARNING_CHECK_TIME=? where ID=?";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, "电池电压故障");
                        preparedStatement.setString(++index, "{}");
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, (String) record.get("ID"));
                    }
                });
            } else {
                String id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                sql = "INSERT INTO t_b_rtu_warning  (ID,RTU_CODE,HAPPEN_TIME,WARNING_STATUS,WARNING_TYPE,WARNING_INFO,WARNING_CONTENT,WARNING_CHECK_TIME) VALUES (?,?,?,?,?,?,?,?)";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, id);
                        preparedStatement.setString(++index, entity.getStcd());
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, "0");
                        preparedStatement.setString(++index, GBConstants.WARNING_TYPE_RTU_FAULT_BV);
                        preparedStatement.setString(++index, "电池电压故障");
                        preparedStatement.setString(++index, "{}");
                        preparedStatement.setTimestamp(++index, new Timestamp(checkTime.getTime()));
                    }
                });
            }
            sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='1' where RTU_CD='" + entity.getStcd() + "'";
            jdbcTemplate.update(sql);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void RtuBvWarnningRecovery(RtuStatusEntity entity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = new Date(entity.getPickTime().getTime());
            Date checkTime = new Date();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE ='" + GBConstants.WARNING_TYPE_RTU_FAULT_BV + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> warningList = jdbcTemplate.queryForList(sql);
            if (null != warningList && warningList.size() > 0) {
                for (Map<String, Object> m : warningList) {
                    sql = "UPDATE  t_b_rtu_warning  SET WARNING_STATUS='1' ,RECOVERY_TIME=?  WHERE ID =?";
                    jdbcTemplate.update(sql, new PreparedStatementSetter() {
                        @Override
                        public void setValues(PreparedStatement preparedStatement) throws SQLException {
                            int index = 0;
                            preparedStatement.setTimestamp(++index, new Timestamp(new Date().getTime()));
                            preparedStatement.setString(++index, (String) m.get("id"));
                        }
                    });
                }
            }
            sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE !='" + GBConstants.WARNING_TYPE_RTU_FAULT_BV + "' AND w.WARNING_STATUS ='0'";
            warningList = jdbcTemplate.queryForList(sql);
            if (null == warningList || warningList.size() == 0) {
                sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='0' where RTU_CD='" + entity.getStcd() + "'";
                jdbcTemplate.update(sql);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void RtuSeWarnningHappen(RtuStatusEntity entity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = new Date(entity.getPickTime().getTime());
            Date checkTime = new Date();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE ='" + GBConstants.WARNING_TYPE_RTU_FAULT_SE + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> warningList = jdbcTemplate.queryForList(sql);
            if (null != warningList && warningList.size() > 0) {
                Map<String, Object> record = warningList.get(0);
                sql = "UPDATE  t_b_rtu_warning  SET WARNING_INFO=?,WARNING_CONTENT=?,WARNING_CHECK_TIME=? where ID=?";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, "水位计故障");
                        preparedStatement.setString(++index, "{}");
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, (String) record.get("ID"));
                    }
                });
            } else {
                String id = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                sql = "INSERT INTO t_b_rtu_warning  (ID,RTU_CODE,HAPPEN_TIME,WARNING_STATUS,WARNING_TYPE,WARNING_INFO,WARNING_CONTENT,WARNING_CHECK_TIME) VALUES (?,?,?,?,?,?,?,?)";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, id);
                        preparedStatement.setString(++index, entity.getStcd());
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, "0");
                        preparedStatement.setString(++index, GBConstants.WARNING_TYPE_RTU_FAULT_SE);
                        preparedStatement.setString(++index, "水位计故障");
                        preparedStatement.setString(++index, "{}");
                        preparedStatement.setTimestamp(++index, new Timestamp(checkTime.getTime()));
                    }
                });
            }
            sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='1' where RTU_CD='" + entity.getStcd() + "'";
            jdbcTemplate.update(sql);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void RtuSeWarnningRecovery(RtuStatusEntity entity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date ct = new Date(entity.getPickTime().getTime());
            Date checkTime = new Date();
            String se = entity.getWaterSe();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE ='" + GBConstants.WARNING_TYPE_RTU_FAULT_SE + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> warningList = jdbcTemplate.queryForList(sql);
            if (null != warningList && warningList.size() > 0) {
                for (Map<String, Object> m : warningList) {
                    sql = "UPDATE  t_b_rtu_warning  SET WARNING_STATUS='1' ,RECOVERY_TIME=?  WHERE ID =?";
                    jdbcTemplate.update(sql, new PreparedStatementSetter() {
                        @Override
                        public void setValues(PreparedStatement preparedStatement) throws SQLException {
                            int index = 0;
                            preparedStatement.setTimestamp(++index, new Timestamp(new Date().getTime()));
                            preparedStatement.setString(++index, (String) m.get("id"));
                        }
                    });
                }
            }
            sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + entity.getStcd() + "'  AND w.WARNING_TYPE !='" + GBConstants.WARNING_TYPE_RTU_FAULT_SE + "' AND w.WARNING_STATUS ='0'";
            warningList = jdbcTemplate.queryForList(sql);
            if (null == warningList || warningList.size() == 0) {
                sql = "UPDATE  t_base_rtu_info  SET WARNING_STATUS='0' where RTU_CD='" + entity.getStcd() + "'";
                jdbcTemplate.update(sql);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
