package com.usky.fire.service.mqtt.mh;

import com.fasterxml.jackson.core.type.TypeReference;
import com.usky.common.core.util.JsonUtils;
import com.usky.fire.domain.DemCase;
import com.usky.fire.domain.Device;
import com.usky.fire.domain.DeviceAlert;
import com.usky.fire.domain.DeviceInfo;
import com.usky.fire.service.DemCaseService;
import com.usky.fire.service.DeviceAlertService;
import com.usky.fire.service.DeviceInfoService;
import com.usky.fire.service.DeviceService;
import com.usky.fire.service.enums.TopListener;
import com.usky.fire.service.mqtt.MqttStrategy;
import com.usky.fire.service.vo.DeviceAj;
import com.usky.fire.service.vo.MqttBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yq
 * @date 2022/3/4 15:07
 */
@Service("mhwater")
public class MhStrategy implements MqttStrategy {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private DeviceAlertService deviceAlertService;
    @Autowired
    private DemCaseService demCaseService;

    @Override
    public String disposeMessage(MqttBaseVO mqttBaseVO) {

        String topic = mqttBaseVO.getTopic();
        String payload = mqttBaseVO.getData().toString();
        if (TopListener.DEVICE_DETAIL.getCode().equals(topic)) {
            //设备录入
            List<Device> devices = JsonUtils.fromJson(payload, new TypeReference<List<Device>>() {
            });
            deviceService.saveOrUpdateBatch(devices);
        } else if (TopListener.DEVICE_INFO.getCode().equals(topic)) {
            DeviceInfo deviceInfo = JsonUtils.fromJson(payload, DeviceInfo.class);
            deviceInfoService.save(deviceInfo);
        } else if (TopListener.DEVICE_ALERT.getCode().equals(topic)) {
            DeviceAlert deviceAlert = JsonUtils.fromJson(payload, DeviceAlert.class);
            deviceAlertService.save(deviceAlert);
        } else if (TopListener.DEVICE_AJ.getCode().equals(topic)) {
            DeviceAj deviceAj = JsonUtils.fromJson(payload, DeviceAj.class);
            DemCase demCase = new DemCase();
            demCase.setId(deviceAj.getId());
            demCase.setCreateTime(deviceAj.getCreateTime());
            demCase.setCaseDescribe(deviceAj.getAjDescribe());
            demCase.setStreet(deviceAj.getStreet());
            demCase.setAddress(deviceAj.getAddress());
            demCase.setGrid(deviceAj.getGrid());
            demCase.setCaseDescribe(deviceAj.getAjDescribe());
            demCase.setDutyGroup(deviceAj.getDutyGroup());
            demCase.setCaseFlag(deviceAj.getAjFlag());
            demCase.setEndTime(deviceAj.getEndTime());
            demCase.setEnclosure(deviceAj.getEnclosure());
            demCase.setImage(deviceAj.getImage());
            demCase.setDeviceCode(deviceAj.getDeviceCode());
            demCase.setDeviceType(deviceAj.getDeviceType());
            demCase.setDisposePerson(deviceAj.getDisposePerson());
            demCase.setDisposePhone(deviceAj.getDisposePhone());
            demCase.setChargePerson(deviceAj.getChargePerson());
            demCase.setChargePhone(deviceAj.getChargePhone());
            demCaseService.saveOrUpdate(demCase);
        }
        return null;
    }
}
