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

import com.neusoft.smart.pos.dto.advertisement.response.MediaStrategyResponse;
import com.neusoft.smart.pos.dto.device.request.DeviceQueryRequest;
import com.neusoft.smart.pos.dto.device.response.DeviceResponse;
import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteAdvertisementService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteDeviceService;
import com.neusoft.smart.pos.redis.bean.DeviceMedias;
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;
import java.util.stream.Collectors;

@Slf4j
public class MediaStrategyCancelTask extends RedisTask {
	
	@Setter
	private Integer groupId;
	
	private RedisTemplate redisTemplate = SpringUtils.getBean("redisTemplate", RedisTemplate.class);
	
	private RemoteAdvertisementService remoteAdvertisementService = SpringUtils.getBean(RemoteAdvertisementService.class);
	
	private RemoteDeviceService remoteDeviceService = SpringUtils.getBean(RemoteDeviceService.class);
	
	@Override
	public void executeTask() {
		List<MediaStrategyResponse> strategyResponseList = remoteAdvertisementService.getRelation(groupId).getResult();
		
		List<Integer> deviceIds = strategyResponseList.stream().map(MediaStrategyResponse::getDeviceId).collect(Collectors.toList());
		DeviceQueryRequest deviceQueryRequest = new DeviceQueryRequest();
		deviceQueryRequest.setIsPage(false);
		deviceQueryRequest.setIds(deviceIds);
		List<DeviceResponse> deviceList = remoteDeviceService.fetchList(deviceQueryRequest).getResult().getRows();
		BoundHashOperations<String, String, DeviceMedias> deviceHash = redisTemplate.boundHashOps("deviceMediaMap");

        deviceList.forEach(device -> deviceHash.delete(device.getSerialNo()));
		log.info("执行了取消策略任务");
	}
	
}
