package com.zksy.manhole.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.manhole.domain.ManholeData;
import com.zksy.manhole.mapper.ManholeDataMapper;
import com.zksy.manhole.service.ManholeDataService;
import com.zksy.manhole.utils.AlarmUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @version 1.0
 * @project dh-server-micro
 * @description
 * @date 2025/2/20 10:55:03
 */
@Service
public class ManholeDataServiceImpl extends ServiceImpl<ManholeDataMapper, ManholeData> implements ManholeDataService {
    @Autowired
    private ManholeDataMapper manholeDataMapper;
    @Autowired
    private AlarmUtil alarmUtil;
    //@Autowired
    //private BaseDevicesService baseDevicesService;
    @Autowired
    private RestTemplate restTemplate;

    @Value("${websocket.url:http://127.0.0.1:8103}")
    private String BASE_URL;
    @Override
    public Integer saveManholeData(ManholeData result) {
        Integer resultData = manholeDataMapper.insert(result);
        // 更新设备状态为在线
        //baseDevicesService.getByDeviceNumberStatus(result.getImeiCardNumber(),0,1);

        /*if(resultData > 0 && Integer.parseInt(result.getTiltAngle()) > 60) {
            String url = BASE_URL + "/push/pushAll?msg=" + result.getTiltAngle();
            try {
                // 调用目标接口并获取响应实体
                ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
                System.out.println("webSocket响应：" + response.getBody());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/

        if (resultData > 0) {
            checkAndSaveAlarm(result);
        }

        return resultData;
    }

    private void checkAndSaveAlarm(ManholeData result) {
        String deviceCode = result.getImeiCardNumber();
        String deviceName = "井盖设备";
        String deviceType = "manhole";

        if (result.getTemperatureValue() != null && !result.getTemperatureValue().isEmpty()) {
            try {
                alarmUtil.checkAndSaveAlarm(deviceName, deviceCode, deviceType,
                        "温度预警", "WARN-TEMPERATURE", 50.0,
                        new BigDecimal(result.getTemperatureValue()),
                        "井盖温度报警");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (result.getTiltAngle() != null && !result.getTiltAngle().isEmpty() 
                && result.getAngleAlarmThreshold() != null && !result.getAngleAlarmThreshold().isEmpty()) {
            try {
                double tiltAngle = Double.parseDouble(result.getTiltAngle());
                double threshold = Double.parseDouble(result.getAngleAlarmThreshold());
                alarmUtil.checkAndSaveAlarm(deviceName, deviceCode, deviceType,
                        "倾斜预警", "WARN-TILT", threshold,
                        BigDecimal.valueOf(tiltAngle),
                        "井盖倾斜报警");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if ("1".equals(result.getAlarmStatus())) {
            alarmUtil.saveAlarm(deviceName, deviceCode, deviceType,
                    "设备报警", "WARN-DEVICE-ALARM", null,
                    null, "井盖设备状态报警");
        }

        if ("1".equals(result.getWaterInfiltrationAlarmStatus())) {
            alarmUtil.saveAlarm(deviceName, deviceCode, deviceType,
                    "水浸预警", "WARN-WATER-INFILTRATION", null,
                    null, "井盖水浸报警");
        }

        if ("1".equals(result.getWaterLevelAlarmStatus())) {
            alarmUtil.saveAlarm(deviceName, deviceCode, deviceType,
                    "水位预警", "WARN-WATER-LEVEL", null,
                    null, "井盖水位报警");
        }
    }
}
