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

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.project.ProjectStatusLogDO;
import org.apache.ibatis.annotations.Mapper;

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

@Mapper
public interface ProjectStatusLogMapper extends BaseMapperX<ProjectStatusLogDO> {

    default ProjectStatusLogDO selectEarliestByProjectIdAndActionType(Long projectId, String actionType) {
        if (projectId == null || actionType == null || actionType.isBlank()) {
            return null;
        }
        List<ProjectStatusLogDO> list = selectList(new LambdaQueryWrapperX<ProjectStatusLogDO>()
                .eq(ProjectStatusLogDO::getProjectId, projectId)
                .eq(ProjectStatusLogDO::getActionType, actionType.trim())
                .orderByAsc(BaseDO::getCreateTime)
                .orderByAsc(ProjectStatusLogDO::getId)
                .last("LIMIT 1"));
        return list.isEmpty() ? null : list.get(0);
    }

    default List<ProjectStatusLogDO> selectListByProjectId(Long projectId, String actionType, LocalDateTime[] operateTime) {
        return selectList(new LambdaQueryWrapperX<ProjectStatusLogDO>()
                .eq(ProjectStatusLogDO::getProjectId, projectId)
                .eqIfPresent(ProjectStatusLogDO::getActionType, actionType)
                .betweenIfPresent(BaseDO::getCreateTime, operateTime)
                .orderByDesc(BaseDO::getCreateTime)
                .orderByDesc(ProjectStatusLogDO::getId));
    }

}
