package com.neusoft.smart.pos.ota.task;


import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.redis.bean.DeviceVersion;
import com.neusoft.smart.pos.redis.bean.DeviceVersionList;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Collection;

@Slf4j
public class PackagesDeleteTask extends RedisTask {
	
	@Getter
	@Setter
	private Integer packageId;
	
	private RedisTemplate redisTemplate = SpringUtils.getBean("redisTemplate", RedisTemplate.class);
	
	@Override
	public void executeTask() {
		BoundHashOperations<String, String, DeviceVersionList> deviceHash = redisTemplate.boundHashOps("deviceVersionsMap");
		BoundHashOperations<String, String, DeviceVersionList> deviceHashWithSn = redisTemplate.boundHashOps("deviceVersionsMapWithSn");
		Collection<DeviceVersionList> deviceVersionCollection = deviceHash.values();//取出所有设备版本集合
		deviceVersionCollection.forEach(deviceVersionList -> {//循环一下所有设备
			//取出对应更新包id额对象,删除后再放回缓存里
			DeviceVersion deviceVersion = deviceVersionList.getVersionsMap().values().stream().filter(version -> version.getId().equals(packageId)).findAny().orElse(null);
			if (null != deviceVersion) {
				deviceVersionList.getVersionsMap().remove(deviceVersion.getVersionCode());
				deviceHash.put(deviceVersionList.getDeviceInfo().getMacAddr(), deviceVersionList);
				deviceHashWithSn.put(deviceVersionList.getDeviceInfo().getSerialNo(), deviceVersionList);
			}
		});
		
	}
}
