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.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.*;

/**
 * 批量添加机具后执行的任务
 * Created by 姜涛 on 2017/3/29.
 */
@Slf4j
public class DeviceAppendTask extends RedisTask {
	
	@Getter
	private List<DeviceInfo> deviceInfo = new ArrayList<>();
	
	private RedisTemplate redisTemplate = SpringUtils.getBean("redisTemplate", RedisTemplate.class);
	
	@Override
	public void executeTask() {
		log.info("DeviceAppendTask in");
		BoundHashOperations<String, String, DeviceVersionList> deviceHash = redisTemplate.boundHashOps("deviceVersionsMap");
		BoundHashOperations<String, String, DeviceVersionList> deviceHashWithSn = redisTemplate.boundHashOps("deviceVersionsMapWithSn");
        Map<String, DeviceVersionList> deviceHashMap = new HashMap<>();
		Map<String, DeviceVersionList> deviceHashWithSnMap = new HashMap<>();
		deviceInfo.forEach(aDevice -> {
			DeviceVersionList deviceVersions = new DeviceVersionList();
			deviceVersions.setDeviceInfo(aDevice);
			deviceHashMap.put(aDevice.getMacAddr(), deviceVersions);
            deviceHashWithSnMap.put(aDevice.getSerialNo(), deviceVersions);
			log.info("添加机具-" + aDevice.getMacAddr());
		});

        deviceHash.putAll(deviceHashMap);
        deviceHashWithSn.putAll(deviceHashWithSnMap);
		log.info("完成redis存储");
	}
	
	
}
