package com.ruoyi.ss.deviceSuit.service.impl;

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ss.device.domain.Device;
import com.ruoyi.ss.device.domain.DeviceBO;
import com.ruoyi.ss.device.domain.dto.DeviceRegisterDTO;
import com.ruoyi.ss.deviceSuit.domain.DeviceSuit;
import com.ruoyi.ss.deviceSuit.service.DeviceSuitConverter;
import com.ruoyi.ss.model.domain.SmModelVO;
import com.ruoyi.ss.model.domain.enums.ModelTag;
import com.ruoyi.ss.model.service.ModelService;
import com.ruoyi.ss.suit.domain.SuitBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wjh
 * 2024/7/31
 */
@Service
public class DeviceSuitConverterImpl implements DeviceSuitConverter {

    @Autowired
    private ModelService modelService;

    private final String WIFI_MAC = "D8";

    private final String FOUR_G_MAC = "4C";


    /**
     * 套餐转为设备套餐关联PO
     *
     * @param suit
     */
    @Override
    public List<DeviceSuit> toPo(SuitBO suit) {
        if (suit == null || suit.getSuitId() == null) {
            return Collections.emptyList();
        }
        List<DeviceSuit> result = new ArrayList<>();

        for (Long deviceId : suit.getDeviceIds()) {
            if (deviceId == null) {
                continue;
            }
            DeviceSuit deviceSuit = new DeviceSuit();
            deviceSuit.setDeviceId(deviceId);
            deviceSuit.setSuitId(suit.getSuitId());
            result.add(deviceSuit);
        }

        return result;
    }

    @Override
    public List<DeviceSuit> toPo(DeviceBO device) {
        if (device == null || device.getDeviceId() == null) {
            return Collections.emptyList();
        }
        List<DeviceSuit> result = new ArrayList<>();

        for (Long suitId : device.getSuitIds()) {
            DeviceSuit deviceSuit = new DeviceSuit();
            deviceSuit.setDeviceId(device.getDeviceId());
            deviceSuit.setSuitId(suitId);
            result.add(deviceSuit);
        }

        return result;

    }

    @Override
    public DeviceBO toBOByRegister(DeviceRegisterDTO dto) {

        SmModelVO model = modelService.selectSmModelByModelId(dto.getModelId());
        ServiceUtil.assertion(model == null, "型号不存在");

        // 添加
        DeviceBO device = new DeviceBO();
        device.setDeviceName("未命名");
        device.setMac(dto.getMac());
        device.setDeviceNo(dto.getSn());
        device.setModelId(dto.getModelId());
        device.setServiceType(model.getServiceType());
        device.setServiceRate(model.getServiceRate());

        // 若为双模版本的型号，则注册4G或WIFI版本MAC
        if (ModelTag.isTwoMac(model.getTags())) {
            if (device.getMac().endsWith(FOUR_G_MAC)) {
                device.setMac2(StringUtils.replaceLast(device.getMac(), FOUR_G_MAC, WIFI_MAC));
            } else {
                // 替换最后两位为4G MAC
                String substring = device.getMac().substring(0, device.getMac().length() - 2);
                device.setMac2(substring + FOUR_G_MAC);
            }
        }

        return device;
    }
}
