package org.ts.ddcs.Service.Impl;

import org.ts.ddcs.Entity.*;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Service.SzyService;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 数据入库实现类
 */
@Service("SzyService")
public class SzyServiceImpl implements SzyService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void SaveFlowRateData(FlowRateEntity flowRateEntity) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sql = "select  MP_CD from  wr_mp_q_r where MP_CD='" + flowRateEntity.getMpCode() + "' and TM='" + timeFormat.format(new Date(flowRateEntity.getTm().getTime())) + "'";
            List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
            if (null != records && records.size() > 0) {
//                sql = "UPDATE  t_d_wr_mp_q_r_cache  set MP_Q=?,ACC_W=?,MP_Q_2=?,ACC_W_2=?,TS=? where MP_CD=? AND TM=?";
//                jdbcTemplate.update(sql, new PreparedStatementSetter() {
//                    @Override
//                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
//                        int index = 0;
//                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateFirst());
//                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateFirst());
//                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateSecond());
//                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateSecond());
//                        preparedStatement.setTimestamp(++index, flowRateEntity.getTs());
//                        preparedStatement.setString(++index, flowRateEntity.getMpCode());
//                        preparedStatement.setTimestamp(++index, flowRateEntity.getTm());
//                    }
//                });
            } else {
                sql = "INSERT INTO wr_mp_q_r (ID,MP_CD,TM,MP_Q,ACC_W,MP_Q_2,ACC_W_2,TS) VALUES(?,?,?,?,?,?,?,?) ";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, flowRateEntity.getId());
                        preparedStatement.setString(++index, flowRateEntity.getMpCode());
                        preparedStatement.setTimestamp(++index, flowRateEntity.getTm());
                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateFirst());
                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateFirst());
                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateSecond());
                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateSecond());
                        preparedStatement.setTimestamp(++index, flowRateEntity.getTs());
                    }
                });
            }
            //写入到缓存
            sql = "select  MP_CD  from  t_d_wr_mp_q_r_cache where MP_CD='" + flowRateEntity.getMpCode() + "' ";
            records = jdbcTemplate.queryForList(sql);
            if (null != records && records.size() > 0) {
                sql = "UPDATE  t_d_wr_mp_q_r_cache  set TM=?,MP_Q=?,ACC_W=?,MP_Q_2=?,ACC_W_2=?,TS=? where MP_CD=? ";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setTimestamp(++index, flowRateEntity.getTm());
                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateFirst());
                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateFirst());
                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateSecond());
                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateSecond());
                        preparedStatement.setTimestamp(++index, flowRateEntity.getTs());
                        preparedStatement.setString(++index, flowRateEntity.getMpCode());
                    }
                });
            } else {
                sql = "INSERT INTO t_d_wr_mp_q_r_cache (MP_CD,TM,MP_Q,ACC_W,MP_Q_2,ACC_W_2,TS) VALUES(?,?,?,?,?,?,?) ";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, flowRateEntity.getMpCode());
                        preparedStatement.setTimestamp(++index, flowRateEntity.getTm());
                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateFirst());
                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateFirst());
                        preparedStatement.setString(++index, flowRateEntity.getInstantFlowrateSecond());
                        preparedStatement.setString(++index, flowRateEntity.getAccFlowrateSecond());
                        preparedStatement.setTimestamp(++index, flowRateEntity.getTs());
                    }
                });
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
