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

import com.neusoft.smart.pos.dto.device.request.ConfigStrategyGroupRequest;
import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.redis.bean.DeviceConfig;
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.ArrayList;
import java.util.List;

@Slf4j
public class ConfigStrategyTask extends RedisTask {
	
	@Setter
	@Getter
	private ConfigStrategyGroupRequest configStrategyGroupRequest;
	
	private RedisTemplate redisTemplate = SpringUtils.getBean("redisTemplate", RedisTemplate.class);
	
	
	@Override
	public void executeTask() {
		BoundHashOperations<String, String, List<DeviceConfig>> deviceHash = redisTemplate.boundHashOps("deviceConfigMap");
		BoundHashOperations<String, String, List<DeviceConfig>> deviceHashWithSn = redisTemplate.boundHashOps("deviceConfigMapWithSn");
		configStrategyGroupRequest.getDeviceList().forEach(device -> {
			List<DeviceConfig> deviceConfigs = new ArrayList<>();
			configStrategyGroupRequest.getStrategyList().forEach(strategy -> {
				DeviceConfig deviceConfig = new DeviceConfig();
				deviceConfig.setFileCode(strategy.getConfigFileCode());
				deviceConfig.setName(strategy.getConfigFileName());
				deviceConfig.setPath(strategy.getConfigUrl());
				deviceConfigs.add(deviceConfig);
			});
			deviceHash.put(device.getMacAddr(), deviceConfigs);
			deviceHashWithSn.put(device.getSerialNo(), deviceConfigs);
			log.info(device.getMacAddr() + "执行了配置文件策略redis任务");
		});
	}
}
