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

import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.redis.bean.DeviceInfo;
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;

@Slf4j
public class DeviceModifyTask extends RedisTask {
	
	@Setter
	@Getter
	private DeviceInfo deviceInfo;
	
	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");
		DeviceVersionList deviceInRedis = deviceHash.entries().values().stream().filter(deviceVersion ->
				deviceVersion.getDeviceInfo().getId().equals(deviceInfo.getId())).findAny().orElse(null);
		if (null != deviceInRedis) {
			deviceInRedis.setDeviceInfo(deviceInfo);
			deviceHash.put(deviceInfo.getMacAddr(), deviceInRedis);
			deviceHashWithSn.put(deviceInfo.getSerialNo(), deviceInRedis);
		}
	}
}
