package com.mortals.xhx.thread;


import cn.hutool.core.collection.ListUtil;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.AbstractThread;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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


/**
 * 设备消息更新线程
 *
 * @author zxfei
 */
@Slf4j
@Service
@Order(value = 1)
public class DeviceUpdateComsumerThread extends AbstractThread {

    @Autowired
    private ICacheService cacheService;
    @Autowired
    private DeviceService deviceService;

    //
    @Override
    protected int getSleepTime() {
        return 60*1000;
    }

    @Override
    protected void process() {
        log.info("DeviceUpdateComsumerThread process");
        List<DeviceEntity> waitUpdateDeviceList = new ArrayList<>();
        while (true) {
            DeviceEntity deviceEntity = cacheService.lpop(RedisKey.KEY_DEVICE_UPDATE_QUEUE, DeviceEntity.class);
            if (ObjectUtils.isEmpty(deviceEntity)) {
                //  log.info("deviceEntity:{}", deviceEntity == null);
                break;
            }
            waitUpdateDeviceList.add(deviceEntity);
        }
        //分组更新
        if (!ObjectUtils.isEmpty(waitUpdateDeviceList)) {
            log.info("updateDeviceList size:{}", waitUpdateDeviceList.size());
            Map<Long, List<DeviceEntity>> collect = waitUpdateDeviceList.parallelStream().collect(Collectors.groupingBy(x -> x.getId()));
            collect.entrySet().parallelStream().forEach(item->{
                List<DeviceEntity> sortUpdateList = item.getValue().parallelStream().sorted(new Comparator<DeviceEntity>() {
                    @Override
                    public int compare(DeviceEntity o1, DeviceEntity o2) {
                        if (o1.getUpdateTime() == null) return 0;
                        if (o2.getUpdateTime() == null) return 0;
                        long start = o1.getUpdateTime().getTime();
                        long end = o2.getUpdateTime().getTime();
                        if (end > start) {
                            return 1;
                        } else if (end < start) {
                            return -1;
                        } else {
                            return 0;
                        }
                    }
                }).collect(Collectors.toList());

                List<List<DeviceEntity>> partition = ListUtil.partition(sortUpdateList, 50);
                for (List<DeviceEntity> deviceEntityList : partition) {
                    deviceService.update(deviceEntityList);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
/*


            for (DeviceEntity deviceEntity : waitUpdateDeviceList) {
                deviceService.update(deviceEntity);
            }
*/
        }

    }


    @Override
    protected void threadClosed() {
        // TODO Auto-generated method stub
        super.close();
    }


}
