package com.ruoyi.iot.service.impl;

import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.iot.constants.ReceiveConstants;
import com.ruoyi.iot.domain.CurrentDatastream;
import com.ruoyi.iot.domain.CurrentDeviceData;
import com.ruoyi.iot.domain.IotDeviceInfo;
import com.ruoyi.iot.service.IotConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author wjh
 * 2024/9/29
 */
@Service
@Slf4j
public class IotConverterImpl implements IotConverter {
    @Override
    public IotDeviceInfo toIotDeviceInfo(CurrentDeviceData data1, CurrentDeviceData data2) {
        if (data1 == null && data2 == null) {
            return null;
        }

        // 获取数据点，让不为空的排在前面
        List<CurrentDatastream> ds1 = data1 == null ? data2.getDatastreams() : data1.getDatastreams();
        List<CurrentDatastream> ds2 = data2 == null ? data1.getDatastreams() : data2.getDatastreams();

        String mac = null;
        try {
            mac = data1.getTitle();
        } catch (Exception e) {
            mac = data2.getTitle();
        }

        return this.toIotDeviceInfo(ds1, ds2, mac);
    }

    private IotDeviceInfo toIotDeviceInfo(List<CurrentDatastream> ds1, List<CurrentDatastream> ds2, String mac) {
        if (CollectionUtils.isEmptyElement(ds1) && CollectionUtils.isEmptyElement(ds2)) {
            return null;
        }

        IotDeviceInfo device = IotDeviceInfo.newDefaultInstance();
        device.setMac(mac);

        // 将两个列表混合为一个列表，按照时间正序排序
        List<CurrentDatastream> list = new ArrayList<>();
        if (CollectionUtils.isNotEmptyElement(ds1)) {
            list.addAll(ds1);
        }
        if (CollectionUtils.isNotEmptyElement(ds2)) {
            list.addAll(ds2);
        }
        list.sort(Comparator.comparing(CurrentDatastream::getAt));

        for (CurrentDatastream stream : list) {
            String id = stream.getId(); // 数据点ID
            String value = stream.getValue().toString();    // 数据点取值，取最新的值
            Date at = stream.getAt();

            switch (id) {
                case ReceiveConstants.DS_V:
                    device.setV(NumberUtils.nonNullDecimal(value));
                    break;
                case ReceiveConstants.DS_P:
                    device.setP(NumberUtils.nonNullDecimal(value));
                    break;
                case ReceiveConstants.DS_A:
                    device.setA(NumberUtils.nonNullDecimal(value));
                    break;
                case ReceiveConstants.DS_W:
                    device.setW(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP));
                    break;
                case ReceiveConstants.DS_S:
                    device.setS(Integer.valueOf(value).toString());
                    break;
                case ReceiveConstants.DS_M:
                    device.setM(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000),  2, RoundingMode.HALF_UP));
                    break;
                case ReceiveConstants.DS_SET:
                    device.setSet(this.toSetValue(value));
                    break;
                case ReceiveConstants.DS_TIME:
                    device.setTime(NumberUtils.nonNullDecimal(value));
                    device.setAt(at);
                    break;
                case ReceiveConstants.DS_FW:
                    device.setModel(value);
                    break;
                case ReceiveConstants.DS_SSID:
                    device.setWifi(value);
                    break;
                case ReceiveConstants.DS_VER:
                    device.setVersion(value);
                    break;
                case ReceiveConstants.DS_VXS:
                    device.setVxs(NumberUtils.nonNullDecimal(value, null));
                    break;
                case ReceiveConstants.DS_IMSI:
                    device.setImsi(value);
                    break;
                case ReceiveConstants.DS_WXS:
                    device.setWxs(NumberUtils.nonNullDecimal(value, null));
                    break;
                default:
                    break;
            }
        }
        return device;
    }


    public String toSetValue(String value) {
        try {
            return String.valueOf(Integer.parseInt(value));
        } catch (Exception e) {
            log.error("转换为SetValue时出错:value={}, e={}", value, e.getMessage());
            return null;
        }
    }
}
