package com.inspect.metadata.mqtt.service.impl;

import com.inspect.metadata.mqtt.domain.HrWarn;
import com.inspect.metadata.mqtt.mapper.HrWarnMapper;
import com.inspect.metadata.mqtt.service.IHrWarnService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class IHrWarnServiceImpl implements IHrWarnService {
    private final HrWarnMapper warnMapper;

    @Override
    public List<HrWarn> selectWarnList(HrWarn warn) {
        return warnMapper.selectWarnList(warn);
    }

    @Override
    public Integer countWarnThreeDay(HrWarn hrWarn) {
        return warnMapper.countWarnThreeDay(hrWarn);
    }

    @Override
    public Integer insertWarn(HrWarn warn) {
        return warnMapper.insertWarn(warn);
    }

    @Override
    public Integer handleWarn(HrWarn warn) {
        log.info("warn: {}", warn);
        HrWarn activeWarn = warnMapper.selectWarn(warn);
        log.info("activeWarn: {}", activeWarn);
        // 五分钟内连续上报归为同一条记录更新，否则新增一条记录
        if (activeWarn.getUpdateTime() != null && Instant.now().isAfter(activeWarn.getUpdateTime().toInstant().plusSeconds(5 * 60))) {
            return warnMapper.insertWarn(warn);
        } else {
            return warnMapper.updateWarn(activeWarn);
        }
    }
}
