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

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ss.device.domain.DeviceBO;
import com.ruoyi.ss.device.domain.dto.DeviceRegisterDTO;
import com.ruoyi.ss.device.service.DeviceConverter;
import com.ruoyi.ss.model.domain.ModelVO;
import com.ruoyi.ss.model.domain.enums.ModelTag;
import com.ruoyi.ss.model.service.IModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author qzz
 * 2024/7/15
 */
@Service
public class DeviceConverterImpl implements DeviceConverter {

    @Autowired
    private IModelService modelService;

    private final String WIFI_MAC = "D8";

    private final String FOUR_G_MAC = "4C";

    @Override
    public DeviceBO toBOByRegister(DeviceRegisterDTO dto) {

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

        // 添加
        DeviceBO device = new DeviceBO();
        device.setDeviceName("未命名");
        device.setMac(dto.getMac());
        device.setSn(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;
    }

}
