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.DeviceVersionList;
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.List;

/**
 * 批量修改机具状态执行的任务
 * Created by 姜涛 on 2017/3/29.
 */
@Slf4j
public class DeviceStatusTask extends RedisTask {
	
	@Setter
	private Integer status;
	
	@Setter
	private Integer storeId;
	
	@Setter
	private List<Integer> deviceIds;
	
	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");
		deviceIds.forEach(id -> {
			DeviceVersionList deviceVersions = deviceHash.values().stream().filter((deviceVersionList) -> deviceVersionList.getDeviceInfo().getId().equals(id)).findAny().orElse(null);
			if (null != deviceVersions) {
				if (null != this.status) {
					deviceVersions.getDeviceInfo().setStatus(this.status);
					log.info("机具-" + deviceVersions.getDeviceInfo().getMacAddr() + " 修改状态-" + status);
				}
				if (null != this.storeId) {
					deviceVersions.getDeviceInfo().setStoreId(this.storeId);
					log.info("机具-" + deviceVersions.getDeviceInfo().getMacAddr() + " 修改门店-" + storeId);
				}
				deviceHash.put(deviceVersions.getDeviceInfo().getMacAddr(), deviceVersions);
				deviceHashWithSn.put(deviceVersions.getDeviceInfo().getSerialNo(), deviceVersions);
			}
		});
		log.info("完成修改机具状态redis存储");
	}
	
}
