package org.ts.ddcs.Service.Impl;

import org.ts.ddcs.Entity.RtuBaseInfo;
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.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Reader;
import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author dylan
 */
@Service("WarningService")
public class WarningServiceImpl implements WarningService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional

    public void warningRecovery(String rtuCode, String warningKind) {
        try {
            Date ct = new Date();
            String sql = "SELECT * FROM t_b_rtu_warning w WHERE w.RTU_CODE ='" + rtuCode + "'  AND w.WARNING_TYPE ='" + warningKind + "' AND w.WARNING_STATUS ='0'";
            List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
            if (records != null && records.size() > 0) {
                Map<String, Object> m = records.get(0);
                sql = "UPDATE  t_b_rtu_warning  SET RECOVERY_TIME=?,WARNING_STATUS=? where ID=?";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setTimestamp(++index, new Timestamp(ct.getTime()));
                        preparedStatement.setString(++index, GBConstants.WARNING_STATUS_RECOVERY);
                        preparedStatement.setString(++index, (String) m.get("ID"));

                    }
                });
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    public List<RtuBaseInfo> warningRtus(String projectId) {
        String sql = "SELECT * FROM t_base_rtu_info r WHERE r.RTU_CD in ( ";
        sql += "SELECT w.RTU_CODE FROM t_b_rtu_warning w LEFT JOIN t_base_rtu_info r2 on r2.RTU_CD = w.RTU_CODE WHERE r2.PROJECT_ID='"+projectId+"' AND  w.RECOVERY_TIME is null GROUP BY w.RTU_CODE";
        sql += " )";


        List<RtuBaseInfo> list = jdbcTemplate.query(sql, new RowMapper<RtuBaseInfo>() {
            @Override
            public RtuBaseInfo mapRow(ResultSet resultSet, int i) throws SQLException {
                RtuBaseInfo rtuBaseInfo = new RtuBaseInfo();
                rtuBaseInfo.setRtuCode(resultSet.getString("RTU_CD"));
                rtuBaseInfo.setRtuName(resultSet.getString("RTU_NAME"));
                return rtuBaseInfo;
            }
        });
        return list;

    }
}
