package com.mortals.xhx.daemon.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.ap.GlobalSysInfo;
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.base.framework.ws.message.SendToAllRequest;
import com.mortals.xhx.base.framework.ws.util.WebSocketUtil;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DeviceMethodEnum;
import com.mortals.xhx.common.code.DeviceStatusEnum;
import com.mortals.xhx.common.key.ParamKey;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.service.DeviceLogService;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.module.sitestat.model.SitestatEntity;
import com.mortals.xhx.module.sitestat.service.SitestatService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 站点统计任务
 *
 * @author: zxfei
 * @date: 2022/3/9 15:19
 */
@CommonsLog
@Service("SiteStatTask")
public class SiteStatTaskImpl implements ITaskExcuteService {

    @Autowired
    private SitestatService sitestatService;
    @Autowired
    private ISiteFeign siteFeign;



    @Override
    public void excuteTask(ITask task) throws AppException {
        String resp = siteFeign.list(new SitePdu());

        JSONObject jsonObject = JSONObject.parseObject(resp);
        if (jsonObject.getInteger("code") == 1) {
            List<SitePdu> sitePduList = jsonObject.getJSONObject("data").getObject("data", new TypeReference<List<SitePdu>>() {
            });
            sitePduList.stream().forEach(site -> {
                SitestatEntity sitestatEntity = new SitestatEntity();
                sitestatEntity.initAttrValue();
                sitestatEntity.setSiteId(site.getId());
                sitestatEntity.setSiteName(site.getSiteName());
                sitestatEntity.setSiteCode(site.getSiteCode());
                sitestatService.updateSiteStat(sitestatEntity, null);
            });
        }
    }


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

    }


}
