package com.kym.service.awoara.entity.event.handle;

import com.kym.entity.miniapp.WashDevice;
import com.kym.service.awoara.entity.event.DeviceStateObject;
import com.kym.service.awoara.entity.event.MessageBody;
import com.kym.service.miniapp.WashDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 设备状态更新事件事件处理
 *
 * @author skyline
 */
@Slf4j
@Component
public class DeviceStateEventHandler implements AwoaraEventHandler<DeviceStateObject> {

    private final WashDeviceService washDeviceService;

    public DeviceStateEventHandler(WashDeviceService washDeviceService) {
        this.washDeviceService = washDeviceService;
    }

    @Override
    public void handle(MessageBody<DeviceStateObject> message) {
        log.info(message.toString());
        log.info("DeviceStateEventHandler");

        // 获取设备信息 todo 要从topic中获取
        var productKey = "";
        var deviceName = "";

        var deviceState = message.getPayload().getData().getDevice_state();

        // 状态更新逻辑
        washDeviceService.lambdaUpdate()
                .set(WashDevice::getUptimeMs, deviceState.getState())
                .set(WashDevice::getState, deviceState.getState())
                .set(WashDevice::getFsmState, deviceState.getFsm_state())
                .set(WashDevice::getHasWater, deviceState.getHas_water())
                .set(WashDevice::getHasFoam, deviceState.getHas_foam())
                .set(WashDevice::getTemperatureChip, deviceState.getTemperature_chip())
                .eq(WashDevice::getProductKey, productKey)
                .eq(WashDevice::getDeviceName, deviceName)
                .update();
    }
}
