package com.neusoft.smart.pos.gateway.service;

import com.neusoft.smart.pos.dto.ota.request.API.DevicePostBaseRequest;
import com.neusoft.smart.pos.dto.ota.response.API.DevicePostBaseResponse;
import com.neusoft.smart.pos.redis.bean.DeviceVersion;
import com.neusoft.smart.pos.redis.bean.DeviceVersionList;

import java.util.Iterator;
import java.util.Map;

public interface UpgradeServiceInterface {
    
    DevicePostBaseResponse solveDeviceRequest(DevicePostBaseRequest devicePostBaseRequest);
    
    default DeviceVersion getNextVersion(DeviceVersionList deviceVersions, String deviceReqVersion, Integer reqSubVersion) {
        DeviceVersion nextVersion = null;
        Iterator<Map.Entry<String, DeviceVersion>> iterator = deviceVersions.getVersionsMap().entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, DeviceVersion> entry = iterator.next();
            //需要判断下如果是熙康PDA带下划线的不执行以下逻辑
            if (entry.getKey().contains("_")) {
                continue;
            }
            Integer tempSubVersion = Integer.parseInt(entry.getValue().getVersionCode().substring(entry.getValue().getVersionCode().lastIndexOf(".") + 1));
            if (entry.getKey().equals(deviceReqVersion)) {//找到版本,把下一个版本取出来
                if (iterator.hasNext()) {
                    nextVersion = iterator.next().getValue();
                }
                break;
            } else if (tempSubVersion > reqSubVersion) { //如果在版本列表中没有找到对应升级策略,把小版本号取出来,取最接近的版本
                nextVersion = entry.getValue();
                break;
            }
        }
        return nextVersion;
    }
}
