package com.gyee.alarm.service.realtimelibrary;

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicPowerstation;
import com.gyee.alarm.model.auto.ProBasicPowerstationPoint;
import com.gyee.alarm.util.PointUtil;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
@author   谢生杰
@date   2022/12/7-15:54
*/
@Service
public class SafetyDayService {
    @Resource
    private IEdosUtil edosUtil;

    public void saveSafetyday() throws Exception {

        Date currentDate = DateUtils.getCurrentDate();

        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;

        List<PointData> resultList = new ArrayList<>();

        wpls.stream().forEach(wp->{
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = wppointmap.get(wp.getId());
            ProBasicPowerstationPoint powerstationPoint = powerstationPointMap.get(ContantXk.AQTS);
            if (StringUtils.isNotNull(powerstationPoint)){
                double aqts = 0;
                try {
                    aqts = edosUtil.getSectionData(powerstationPoint, currentDate.getTime()).getPointValueInDouble();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                aqts ++;
                PointData pointData = PointUtil.createPointData(currentDate, aqts, powerstationPoint.getNemCode(), powerstationPoint.getName());
                resultList.add(pointData);
            }
        });

        edosUtil.sendMultiPoint(resultList);
    }
}
