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 DeviceStoreTask extends RedisTask {
	
	@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");
		deviceHash.values().forEach(deviceVersionList -> {
			if (storeId.equals(deviceVersionList.getDeviceInfo().getStoreId())) {
				deviceVersionList.getDeviceInfo().setStoreId(null);
				deviceHash.put(deviceVersionList.getDeviceInfo().getMacAddr(), deviceVersionList);
				deviceHashWithSn.put(deviceVersionList.getDeviceInfo().getSerialNo(), deviceVersionList);
			}
			if (deviceIds.contains(deviceVersionList.getDeviceInfo().getId())) {
				deviceVersionList.getDeviceInfo().setStoreId(storeId);
				deviceHash.put(deviceVersionList.getDeviceInfo().getMacAddr(), deviceVersionList);
				deviceHashWithSn.put(deviceVersionList.getDeviceInfo().getSerialNo(), deviceVersionList);
				log.info("机具-" + deviceVersionList.getDeviceInfo().getMacAddr() + " 修改门店-" + storeId);
			}
		});
		log.info("完成修改机具门店redis存储");
	}
	
}
