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;


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

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void SaveFlowRateData(FlowRateEntity flowRateEntity) {
        try {
            //SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyyMMddHHmmss");
            String 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());
                }
            });

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