package com.mortals.xhx.daemon.task;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.common.code.PlatformTypeEnum;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.device.service.DeviceStatService;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.model.SiteQuery;
import com.mortals.xhx.module.site.service.SiteService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备统计任务，生成当天统计数据
 *
 * @author: zxfei
 * @date: 2022/3/9 15:19
 */
@CommonsLog
@Service("DeviceTotalStatTask")
public class DeviceTotalStatTaskImpl implements ITaskExcuteService {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceStatService deviceStatService;

    @Value("${platform.type:cloud}")
    private String platFormType;//版本，默认云服务版本

    @Autowired
    private SiteService siteService;
    @Autowired
    private ISiteFeign siteFeign;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("设备统计任务，生成当天统计数据开始");
        if (platFormType.equalsIgnoreCase(PlatformTypeEnum.CLOUD.getValue())) {
            //统计所有站点
            SitePdu sitePdu = new SitePdu();
            sitePdu.setSize(-1);
            siteFeign.list(sitePdu).getData().getData().parallelStream().forEach(item -> {
                //deviceService.deviceStat(item.getId(), null);
            });
        } else if (platFormType.equalsIgnoreCase(PlatformTypeEnum.STANDALONE.getValue())) {
            List<SiteEntity> siteEntities = siteService.find(new SiteQuery());
            for (SiteEntity siteEntity : siteEntities) {


                //判断如果当前节点无设备 则不统计
                deviceStatService.deviceStat(siteEntity.getId(), null);
            }
        }
        log.info("设备统计任务，生成当天统计数据结束");

    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }

}
