package com.zksy.environment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.environment.domain.po.StationHour;
import com.zksy.environment.mapper.StationHourMapper;
import com.zksy.environment.service.StationHourService;
import com.zksy.environment.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * <p>
 * 遥测站小时报-报送以小时为基本单位的历史数据和实时数据 服务实现类
 * </p>
 *
 * @author Yang
 * @since 2023-12-19
 */
@Slf4j
@Service
public class StationHourServiceImpl extends ServiceImpl<StationHourMapper, StationHour> implements StationHourService {
    @Override
    public boolean saveStationHour(String oldDataStr,String dataStr) {
        boolean flag = false;
        try {
            StationHour stationHour = new StationHour();
            stationHour.setId(UUID.randomUUID().toString().replace("-", ""));
            stationHour.setMessage(oldDataStr);
            stationHour.setCreateTime(LocalDateTime.now());

            //功能码
            String functionCode = oldDataStr.substring(20, 22);
            stationHour.setFunctionCode(functionCode);
            //发报时间
            String dataTime = dataStr.substring(0, 12);
            //拼接年份
            String sendingMessageTime = "20"+dataTime;
            stationHour.setSendingMessageTime(DateTimeUtil.parseDateTime(sendingMessageTime));
            //测站地址
            int stationAddressIndex = dataStr.indexOf("F1F1");
            // 如果找到了并且后面有10个字符
            if (stationAddressIndex != -1 && stationAddressIndex + 14 <= dataStr.length()) {
                // 提取后面的10个字符
                String result = dataStr.substring(stationAddressIndex + 4, stationAddressIndex + 14);
                stationHour.setMeasuringStationAddress(result);
            }
            //测站类型
            Pattern pattern = Pattern.compile("F0F0", Pattern.CASE_INSENSITIVE);
            String[] f0F0s = pattern.split(dataStr);
            String f0F0 = f0F0s[0];
            if (f0F0.length() >= 2) {
                // 取出前两位数字
                String prefix = f0F0.substring(f0F0.length() - 2);
                stationHour.setMeasuringStationType(prefix);
            }
            //观测时间(到分钟)
            String f0F01 = f0F0s[1];
            if (f0F01.length() >= 10) {
                // 取出前面10位数字
                String digits = f0F01.substring(0, 10);
                //拼接年份和秒
                String observeTime = "20"+digits+"00";
                stationHour.setObserveTime(DateTimeUtil.parseDateTime(observeTime));
            }
            //水温
            // 寻找 "ffa01" 的位置
            int ffa01Index = dataStr.indexOf("FFA01");
            if (ffa01Index != -1) {
                // 在 "ffa01" 之前查找 "0311"
                int waterTemperatureIndex = dataStr.lastIndexOf("0311", ffa01Index - 1);
                // 如果找到了并且后面有4个字符
                if (waterTemperatureIndex != -1 && waterTemperatureIndex + 4 < dataStr.length()) {
                    // 提取后面的4个字符
                    String result = dataStr.substring(waterTemperatureIndex + 4, waterTemperatureIndex + 8);
                    int parseInt = Integer.parseInt(result);
                    double numberDouble = (double) parseInt / 10;
                    stationHour.setWaterTemperature(numberDouble);
                }
                //浊度
                int turbidityIndex = dataStr.lastIndexOf("4919",ffa01Index - 1);
                // 如果找到了并且后面有6个字符
                if (turbidityIndex != -1 && turbidityIndex + 6 < dataStr.length()) {
                    // 提取后面的6个字符1
                    String result = dataStr.substring(turbidityIndex + 4, turbidityIndex + 10);
                    stationHour.setTurbidity(Integer.parseInt(result));
                }
                //电压
                int voltageIndex = dataStr.lastIndexOf("3812",ffa01Index - 1);
                // 如果找到了并且后面有4个字符
                if (voltageIndex != -1 && voltageIndex + 4 < dataStr.length()) {
                    // 提取后面的4个字符
                    String result = dataStr.substring(voltageIndex + 4, voltageIndex + 8);
                    int parseInt = Integer.parseInt(result);
                    double numberDouble = (double) parseInt / 100;
                    stationHour.setVoltage(numberDouble);
                }
            }
            //信号强度
            int signalIndex = dataStr.indexOf("7A08");
            // 如果找到了并且后面有2个字符
            if (signalIndex != -1 && signalIndex + 2 < dataStr.length()) {
                // 提取后面的2个字符
                String result = dataStr.substring(signalIndex + 4, signalIndex + 6);
                stationHour.setSignalStrength(Integer.parseInt(result));
            }
            //设备温度
            int deviceTemperatureIndex = dataStr.indexOf("FFA011");
            // 如果找到了并且后面有4个字符
            if (deviceTemperatureIndex != -1 && deviceTemperatureIndex + 4 < dataStr.length()) {
                // 提取后面的4个字符
                String result = dataStr.substring(deviceTemperatureIndex + 6, deviceTemperatureIndex + 10);
                int parseInt = Integer.parseInt(result);
                double numberDouble = (double) parseInt / 10;
                stationHour.setDeviceTemperature(numberDouble);
            }
            System.out.println("stationHour====="+stationHour);
             flag = super.save(stationHour);
        } catch (Exception e) {
//            log.info("==========数据解析错误==========", dataStr);
            e.printStackTrace();
        }
        return flag;
    }
}
