package com.imdroid.sideslope.executor;

import com.imdroid.secapi.client.BeidouClient;
import com.imdroid.secapi.dto.GnssTrxMsg;
import com.imdroid.sideslope.calc.MultiLineGNSSCalcService;
import com.imdroid.sideslope.message.D3F2StopIndicationMessage;
import com.imdroid.sideslope.sal.Device;
import com.imdroid.sideslope.sal.DeviceService;
import com.imdroid.sideslope.service.DataPersistService;
import com.imdroid.common.util.ThreadManager;
import com.imdroid.sideslope.service.WarningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 停止指示消息处理器
 *
 * @author LiGang
 */
@Component
public class D3F2StopIndicationMessageExecutor implements Executor<D3F2StopIndicationMessage, Void> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DataPersistService dataPersistService;

    @Resource(name = "local")
    private DeviceService deviceService;
    @Autowired
    private BeidouClient beidouClient;
    @Autowired
    MultiLineGNSSCalcService multiLineGNSSCalcService;
    @Autowired
    WarningService warningService;

    @Override
    public Void execute(D3F2StopIndicationMessage message) {
        if (logger.isDebugEnabled()) {
            logger.debug("receive d3f2 message of device: "+message.getId());
        }
        // 补齐tenantId
        String deviceId = message.getId();
        Device device = deviceService.findByDeviceId(deviceId);
        if(device == null) return null;
        device.updateRx(message.getHeader(), message.getLen(),1);
        message.setTenantId(device.getTenantId());
        GnssTrxMsg gnssTrxMsg = message.getTrxMsg();
        gnssTrxMsg.setTenantid(device.getTenantId());
        gnssTrxMsg.setD3xxbytes(device.getD3xxbytes());
        gnssTrxMsg.setB562bytes(device.getD341bytes());
        gnssTrxMsg.setSatelliteinuse(device.getSatelitesInUse());

        // 储设备收发字节数统计信息
        ThreadManager.getFixedThreadPool().submit(() -> {
            LocalDateTime uploadTime = multiLineGNSSCalcService.checkUploadTime(
                    device.getDeviceId(), device.getTenantId());
            dataPersistService.saveDeviceTrxStat(message, (uploadTime!=null), device);
            // 通知beidou服务设备休眠
            try {
                beidouClient.onDeviceStop(deviceId, device.getTenantId());
                if (uploadTime != null) {
                    String info = "d342 num:" + device.getD342Count() +
                            ", bytes:" + device.getD342Bytes() +
                            ", d341 included:" + device.getD341In42Count();
                    beidouClient.onGnssUpload(deviceId, device.getTenantId(), uploadTime, info);
                }
            }
            catch (Exception e){

            }
            // 检查告警
            if(device.getDeviceType() == Device.DEVICE_BASE_STATION) {
                warningService.checkD3xxNum(device);
            }
        });

        return null;
    }

    @Override
    public Class<?> getMessageType() {
        return D3F2StopIndicationMessage.class;
    }
}
