package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendEntity;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendQuery;
import com.mortals.xhx.module.alarm.service.AlarmSmsSendService;
import com.mortals.xhx.module.device.model.*;
import com.mortals.xhx.module.device.service.*;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.service.ProductService;
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 org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.math.BigDecimal.ROUND_HALF_DOWN;
import static java.util.stream.Collectors.counting;

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

    @Autowired
    private DeviceService deviceService;

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

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

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

        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())) {
            siteService.find(new SiteQuery()).forEach(item -> {
                deviceService.deviceStat(item.getId(), null);
            });
        }

    }

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

    }

}
