package org.ts.ddcs.Service.Impl;

import org.ts.ddcs.Entity.RtuImageEntity;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Service.JtService;
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.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * @author swp
 */
@Service("JtService")
@Transactional
public class JtServiceImpl implements JtService {

    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void updateLocation(String rtuCode, Map<String, Object> entity) {
        String sql ="select rtu_code from rtu_realtime_status where rtu_code='"+rtuCode+"'";
        List<Map<String,Object>> list =jdbcTemplate.queryForList(sql);
        if (list.size()>0){
             sql = "UPDATE  rtu_realtime_status  SET last_up_time=?,last_lng=?,last_lat=?,update_time=? ";
            sql += " WHERE rtu_code =?";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setTimestamp(++index, new Timestamp(System.currentTimeMillis()));
                    preparedStatement.setString(++index, (String) entity.get("lng"));
                    preparedStatement.setString(++index, (String) entity.get("lat"));
                    preparedStatement.setTimestamp(++index, new Timestamp(System.currentTimeMillis()));
                    preparedStatement.setString(++index, rtuCode);
                }
            });
        }else{
            sql = "INSERT INTO rtu_realtime_status (`rtu_code`,`last_up_time`,`last_lng`, `last_lat`) VALUES (?, ?, ?,?)";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, rtuCode);
                    preparedStatement.setTimestamp(++index, new Timestamp(System.currentTimeMillis()));
                    preparedStatement.setBigDecimal(++index, new BigDecimal((String) entity.get("lng")));
                    preparedStatement.setBigDecimal(++index, new BigDecimal((String) entity.get("lat")));
                }
            });
        }


        sql = "INSERT INTO realtimedata_device_track (`device_code`,`lng`, `lat`) VALUES (?, ?, ?)";
        jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                int index = 0;
                preparedStatement.setString(++index, rtuCode);
                preparedStatement.setBigDecimal(++index, new BigDecimal((String) entity.get("lng")));
                preparedStatement.setBigDecimal(++index, new BigDecimal((String) entity.get("lat")));
            }
        });
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void saveSwImage(String id, String savePath, RtuImageEntity imageEntity, Map<String, Object> location) {
        try {

            String sql = "INSERT INTO t_file (`ID`, `GROUP_ID`, `FILE_NAME`, `EXT_NAME`, `CONTENT_TYPE`, `FILE_SIZE`, `SAVED_PATH`, `STATUS`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, id);
                    preparedStatement.setString(++index, imageEntity.getGroupId());
                    preparedStatement.setString(++index, imageEntity.getFileName());
                    preparedStatement.setString(++index, imageEntity.getFileExt());
                    preparedStatement.setString(++index, imageEntity.getFileType());
                    preparedStatement.setBigDecimal(++index, new BigDecimal(0));
                    preparedStatement.setString(++index, savePath);
                    preparedStatement.setBigDecimal(++index, new BigDecimal(1));
                }
            });

            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();

            sql = "INSERT INTO  st_rtu_camera_image (STCD,TM,GROUP_ID,ADDITIONAL_ID) VALUES(?,?,?,?)";
            jdbcTemplate.update(sql, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement) throws SQLException {
                    int index = 0;
                    preparedStatement.setString(++index, imageEntity.getRtuCode());
                    preparedStatement.setTimestamp(++index, imageEntity.getTm());
                    preparedStatement.setString(++index, imageEntity.getGroupId());
                    preparedStatement.setString(++index, uuid);
                }
            });


            sql = "INSERT INTO  t_camera_additional_info (ID,LAT,LNG,ALTITUDE,SPEED,DIRECTION,REPORT_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, (String) location.get("lat"));
                    preparedStatement.setString(++index, (String) location.get("lng"));
                    preparedStatement.setInt(++index, (int) location.get("altitude"));
                    preparedStatement.setInt(++index, (int) location.get("speed"));
                    preparedStatement.setInt(++index, (int) location.get("direction"));
                    preparedStatement.setTimestamp(++index, new Timestamp(((Date) location.get("reportTime")).getTime()));

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

    }
}
