package com.njcn.rdms.module.project.dal.mysql.duealert;

import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.constant.DueAlertConstants;
import com.njcn.rdms.module.project.dal.dataobject.duealert.DueAlertRecordDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

/**
 * 告警发送记录 Mapper。调用方保证 objectIds 非空（空集合不要调用，IN 空集会报错）。
 */
@Mapper
public interface DueAlertRecordMapper extends BaseMapperX<DueAlertRecordDO> {

    /** 查某类对象一批 id 的临期档历史记录（不限日期），调用方按计划日快照内存判重 */
    default List<DueAlertRecordDO> selectApproachingListByObjectIds(String objectType, Collection<Long> objectIds) {
        return selectList(new LambdaQueryWrapperX<DueAlertRecordDO>()
                .eq(DueAlertRecordDO::getObjectType, objectType)
                .eq(DueAlertRecordDO::getAlertType, DueAlertConstants.ALERT_TYPE_APPROACHING)
                .in(DueAlertRecordDO::getObjectId, objectIds));
    }

    /** 查某类对象一批 id 当日已发的逾期档记录（同日防重；昨天的记录自然不命中=每天可再发） */
    default List<DueAlertRecordDO> selectOverdueListByObjectIdsAndAlertDate(String objectType,
                                                                            Collection<Long> objectIds,
                                                                            LocalDate alertDate) {
        return selectList(new LambdaQueryWrapperX<DueAlertRecordDO>()
                .eq(DueAlertRecordDO::getObjectType, objectType)
                .eq(DueAlertRecordDO::getAlertType, DueAlertConstants.ALERT_TYPE_OVERDUE)
                .eq(DueAlertRecordDO::getAlertDate, alertDate)
                .in(DueAlertRecordDO::getObjectId, objectIds));
    }

}
