package com.sptg.gateway.controller.iot.topsail;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.iot.api.beans.ApiResultBean;
import com.sptg.iot.api.dto.AlarmNoticeDto;
import com.sptg.iot.api.entity.IotDevice;
import com.sptg.iot.api.feigns.IotAlarmFeign;
import com.sptg.iot.api.feigns.IotDeviceFeign;
import com.sptg.third.api.entity.topsailiot.TopsailNotice;
import com.sptg.third.api.entity.topsailiot.TopsailNoticeServiceData;
import com.sptg.third.api.entity.topsailiot.TopsailNoticeService;
import com.sptg.third.api.feigns.grid.GridIotFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * @author zwx
 * @create 2020/10/22 23:03
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class TopsailController {

    private final IotAlarmFeign iotAlarmFeign;
    private final IotDeviceFeign iotDeviceFeign;
    private final GridIotFeign gridIotFeign;

    /**
     * {
     * 	"notifyType": "deviceDataChanged",
     * 	"deviceId": "fb5047a6-5d1d-4171-ae11-fb33016a49c9",
     * 	"gatewayId": "fb5047a6-5d1d-4171-ae11-fb33016a49c9",
     * 	"service": {
     * 		"serviceId": "topsailSensorData",
     * 		"serviceType": "topsailSensorData",
     * 		"data": {
     * 			"deviceType": 4,
     * 			"batteryLevel": 99,
     * 			"signalStrength": 13,
     * 			"alarmType": "1",
     * 			"sampleData": "400000",
     * 			"unit": "Mpa",
     * 			"sendTime": "201211123411",
     * 			"configureData": "",
     * 			"dataBody": "7470736C01001D0438363138373830343133363733313630201211123411630D11400000AD5E696F74"
     *                },
     * 		"eventTime": "20201211T043414Z"* 	}
     * }
     */

    /**
     * 消防栓心跳上报接口
     * @param
     * @return
     */
    @PostMapping(value = "/iot/topsail/heartbeat/notice")
    public ApiResultBean alarmNotice(@RequestBody TopsailNotice topsailNotice){
        log.error(JsonUtils.objectToJson(topsailNotice));
        TopsailNoticeService service = topsailNotice.getService();
        TopsailNoticeServiceData data = service.getData();
        String device_id = data.getImei();
        IotDevice iotDevice = this.iotDeviceFeign.getIotDeviceByDeviceId(device_id);
        if(iotDevice == null){
            return ApiResultBean.ok();
        }else{
            iotDevice.setBattery_value(data.getBatteryLevel().toString());
            iotDevice.setSignal_intensity(data.getSignalStrength().toString());
            String sampleData = data.getSampleData();
            String unit = data.getUnit();
            String alarmType = data.getAlarmType();
            Integer first = Integer.parseInt(sampleData.substring(0, 1));
            Float tempValue = Float.parseFloat(sampleData);
            Double v = tempValue / Math.pow(10, first);
            String s = v.toString();
            String monitorValue = s.substring(1, s.length());
            iotDevice.setLastHeartbeatDate(new Date());
            if(alarmType != null){
                if(!alarmType.equals("0")){ //非0表示告警
                    List<AlarmNoticeDto> list = new ArrayList<>();
                    AlarmNoticeDto alarmNoticeDto = new AlarmNoticeDto();
                    alarmNoticeDto.setAlarm_type(data.getAlarmType());
                    alarmNoticeDto.setIs_alarm(1);
                    alarmNoticeDto.setLevel("1");
                    alarmNoticeDto.setReceive_time(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    alarmNoticeDto.setDevice_id(iotDevice.getDevice_id());
                    alarmNoticeDto.setAlarm_value(monitorValue + unit);
                    alarmNoticeDto.setPrefix("");
                    this.getAlarmName(alarmNoticeDto);
                    list.add(alarmNoticeDto);
                    ResultBean<String> resultBean = this.iotAlarmFeign.alarmNotice(list);
                    if(resultBean.getCode().equals("200")){
                        //推送给网格
                        this.gridIotFeign.gridIotAlarmPush(list);
                    }
                }
                List<IotDevice> list = new ArrayList<>();
                iotDevice.setMonitorValue(monitorValue + unit);
                iotDevice.setDevice_data(monitorValue + unit);
                iotDevice.setBattery_value(data.getBatteryLevel().toString());
                iotDevice.setSignal_intensity(data.getSignalStrength().toString());
                list.add(iotDevice);
                //更新社区物联网设备
                this.iotDeviceFeign.updateBatch(list);
                //推送物联网设备到网格
                this.gridIotFeign.gridIotDevicePush(list);
            }
        }
        return ApiResultBean.ok();
    }

    private AlarmNoticeDto getAlarmName(AlarmNoticeDto alarmNoticeDto){
        String alarmType = alarmNoticeDto.getAlarm_type();
        switch (alarmType){
            case "1":
                alarmNoticeDto.setAlarm_desc("阈值下限告警");
                alarmNoticeDto.setType(0);
                break;
            case "2":
                alarmNoticeDto.setAlarm_desc("阈值上限告警");
                alarmNoticeDto.setType(0);
                break;
            case "3":
                alarmNoticeDto.setAlarm_desc("设备故障");
                alarmNoticeDto.setType(1);
                break;
            case "4":
                alarmNoticeDto.setAlarm_desc("波动告警");
                alarmNoticeDto.setType(0);
                break;
            case "5":
                alarmNoticeDto.setAlarm_desc("碰撞告警");
                alarmNoticeDto.setType(1);
                break;
            case "6":
                alarmNoticeDto.setAlarm_desc("倾斜告警");
                alarmNoticeDto.setType(1);
                break;
            case "7":
                alarmNoticeDto.setAlarm_desc("水流告警");
                alarmNoticeDto.setType(0);
                break;
            case "8":
                alarmNoticeDto.setAlarm_desc("进水告警");
                alarmNoticeDto.setType(0);
                break;
            case "9":
                alarmNoticeDto.setAlarm_desc("低电量告警");
                alarmNoticeDto.setType(1);
                break;
            case "10":
                alarmNoticeDto.setAlarm_desc("开盖告警");
                alarmNoticeDto.setType(1);
                break;
            default:
                alarmNoticeDto.setAlarm_desc("");
                alarmNoticeDto.setType(0);
                break;
        }
        return alarmNoticeDto;
    }
}
