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

import com.neusoft.smart.pos.advertisement.persistent.domain.MediaResource;
import com.neusoft.smart.pos.advertisement.persistent.domain.MediaResourceExample;
import com.neusoft.smart.pos.advertisement.persistent.mapper.MediaResourceMapper;
import com.neusoft.smart.pos.dto.advertisement.request.MediaResourceStrategyRequest;
import com.neusoft.smart.pos.dto.advertisement.request.MediaStrategyRequest;
import com.neusoft.smart.pos.dto.device.request.DeviceRequest;
import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.redis.bean.DeviceMedia;
import com.neusoft.smart.pos.redis.bean.DeviceMedias;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新建完策略后更新redis的任务
 * Created by 姜涛 on 2017/3/29.
 */
@Slf4j
public class MediaStrategyTask extends RedisTask {
    
    @Setter
    @Getter
    private MediaResourceStrategyRequest mediaResourceStrategyRequest;
    
    private RedisTemplate redisTemplate = SpringUtils.getBean("redisTemplate", RedisTemplate.class);
    
    
    @Autowired
    private MediaResourceMapper mediaResourceMapper = SpringUtils.getBean("mediaResourceMapper", MediaResourceMapper.class);
    
    @Override
    public void executeTask() {
        BoundHashOperations<String, String, DeviceMedias> deviceHash = redisTemplate.boundHashOps("deviceMediaMap");
        
        List<Integer> mediaIdList = mediaResourceStrategyRequest.getStrategyList().stream().map(MediaStrategyRequest::getMediaResourceId).collect(Collectors.toList());
        MediaResourceExample example = new MediaResourceExample();
        example.createCriteria().andIdIn(mediaIdList);
        Map<Integer, MediaResource> mediaResourceMap = mediaResourceMapper.selectByExample(example).stream().collect(Collectors.toMap(MediaResource::getId, MediaResource -> MediaResource));
        
        List<Integer> audioIdList = mediaResourceStrategyRequest.getStrategyList().stream().map(MediaStrategyRequest::getBackgroundAudioId).filter(t -> t != null).collect(Collectors.toList());
        Map<Integer, MediaResource> audioResourceMap = new HashMap<>();
        if (!audioIdList.isEmpty()) {
            MediaResourceExample example2 = new MediaResourceExample();
            example2.createCriteria().andIdIn(audioIdList);
            audioResourceMap = mediaResourceMapper.selectByExample(example2).stream().collect(Collectors.toMap(MediaResource::getId, MediaResource -> MediaResource));
        }
        
        
        for (DeviceRequest device : mediaResourceStrategyRequest.getDeviceList()) {
            DeviceMedias deviceMedias = new DeviceMedias();
            List<DeviceMedia> deviceMediaList = new ArrayList<>();
            for (MediaStrategyRequest mediaStrategy : mediaResourceStrategyRequest.getStrategyList()) {
                MediaResource mediaResource = mediaResourceMap.get(mediaStrategy.getMediaResourceId());
                DeviceMedia deviceMedia = new DeviceMedia();
                deviceMedia.setFileType(mediaResource.getFileType());
                deviceMedia.setMediaFileCode(mediaResource.getMediaUuid());
                deviceMedia.setMediaType(mediaResource.getMediaType());
                deviceMedia.setMediaUrl(mediaResource.getMediaUrl());
                deviceMedia.setDisplayType(mediaResource.getDisplayType());
                deviceMedia.setPlayLength(mediaStrategy.getPlayLength());
                if (null != mediaStrategy.getShowTimeBegin()) {
                    deviceMedia.setShowTimeBegin(DateFormatUtils.format(mediaStrategy.getShowTimeBegin(), "HH:mm:ss"));
                }
                if (null != mediaStrategy.getShowTimeEnd()) {
                    deviceMedia.setShowTimeEnd(DateFormatUtils.format(mediaStrategy.getShowTimeEnd(), "HH:mm:ss"));
                }
                if (null != mediaStrategy.getBackgroundAudioId()) {
                    MediaResource audio = audioResourceMap.get(mediaStrategy.getBackgroundAudioId());
                    deviceMedia.setBgAudioFileCode(audio.getMediaUuid());
                    deviceMedia.setBgAudioFileType(audio.getFileType());
                    deviceMedia.setBgAudioFileUrl(audio.getMediaUrl());
                }
                deviceMediaList.add(deviceMedia);
            }
            deviceMedias.setMediaList(deviceMediaList);
            deviceMedias.setCacheTime(mediaResourceStrategyRequest.getCacheTime());
            
            deviceHash.put(device.getSerialNo(), deviceMedias);
            
            log.info(device.getSerialNo() + "执行了更新资源redis任务");
        }
        
    }
    
}
