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.DeviceMessageStatService;
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 DeviceStatService deviceStatService;
    @Autowired
    private DeviceMessageStatService deviceMessageStatService;


    @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);
            List<SitePdu> siteList = siteFeign.list(sitePdu).getData().getData();
            for (SitePdu item : siteList) {
                deviceStatService.deviceStat(item.getId(), null);
                deviceMessageStatService.deviceMessageStat(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);
                deviceMessageStatService.deviceMessageStat(siteEntity.getId(), null);
            }
        }
        log.info("设备统计任务，生成当天统计数据结束");

    }

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

    }

}
