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

import com.njcn.rdms.framework.mybatis.core.dataobject.BaseDO;
import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.dal.dataobject.audit.BizAuditLogDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface BizAuditLogMapper extends BaseMapperX<BizAuditLogDO> {

    default List<BizAuditLogDO> selectListByBiz(String bizType, Long bizId, String actionType, LocalDateTime[] operateTime) {
        return selectList(new LambdaQueryWrapperX<BizAuditLogDO>()
                .eq(BizAuditLogDO::getBizType, bizType)
                .eq(BizAuditLogDO::getBizId, bizId)
                .eqIfPresent(BizAuditLogDO::getActionType, actionType)
                .betweenIfPresent(BaseDO::getCreateTime, operateTime)
                .orderByDesc(BaseDO::getCreateTime)
                .orderByDesc(BizAuditLogDO::getId));
    }

    default List<BizAuditLogDO> selectListByBizAndActions(String bizType, Long bizId, List<String> actionTypes,
                                                          LocalDateTime startTime, LocalDateTime endTime) {
        return selectList(new LambdaQueryWrapperX<BizAuditLogDO>()
                .eq(BizAuditLogDO::getBizType, bizType)
                .eq(BizAuditLogDO::getBizId, bizId)
                .inIfPresent(BizAuditLogDO::getActionType, actionTypes)
                .betweenIfPresent(BaseDO::getCreateTime, startTime, endTime)
                .orderByDesc(BaseDO::getCreateTime)
                .orderByDesc(BizAuditLogDO::getId));
    }

    default List<BizAuditLogDO> selectListByBizType(String bizType, String actionType, LocalDateTime[] operateTime) {
        return selectList(new LambdaQueryWrapperX<BizAuditLogDO>()
                .eq(BizAuditLogDO::getBizType, bizType)
                .eqIfPresent(BizAuditLogDO::getActionType, actionType)
                .betweenIfPresent(BaseDO::getCreateTime, operateTime)
                .orderByDesc(BaseDO::getCreateTime)
                .orderByDesc(BizAuditLogDO::getId));
    }

    default List<BizAuditLogDO> selectListByBizTypeAndActions(String bizType, List<String> actionTypes,
                                                              LocalDateTime startTime, LocalDateTime endTime) {
        return selectList(new LambdaQueryWrapperX<BizAuditLogDO>()
                .eq(BizAuditLogDO::getBizType, bizType)
                .inIfPresent(BizAuditLogDO::getActionType, actionTypes)
                .betweenIfPresent(BaseDO::getCreateTime, startTime, endTime)
                .orderByDesc(BaseDO::getCreateTime)
                .orderByDesc(BizAuditLogDO::getId));
    }

}
