package com.yaoai.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yaoai.domain.entity.ProjectAsset;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Optional;

@Mapper
public interface ProjectAssetMapper extends BaseMapper<ProjectAsset> {

    default List<ProjectAsset> findByProject(Long projectId) {
        return selectList(Wrappers.<ProjectAsset>lambdaQuery()
                .eq(ProjectAsset::getProjectId, projectId)
                .orderByDesc(ProjectAsset::getCreatedAt));
    }

    default Optional<ProjectAsset> findLatestByProjectAndType(Long projectId, Long tenantId, String assetType) {
        return Optional.ofNullable(selectOne(Wrappers.<ProjectAsset>lambdaQuery()
                .eq(ProjectAsset::getProjectId, projectId)
                .eq(ProjectAsset::getTenantId, tenantId)
                .eq(ProjectAsset::getAssetType, assetType)
                .orderByDesc(ProjectAsset::getCreatedAt)
                .last("LIMIT 1")));
    }

    default Optional<ProjectAsset> findById(Long id, Long tenantId) {
        return Optional.ofNullable(selectOne(Wrappers.<ProjectAsset>lambdaQuery()
                .eq(ProjectAsset::getId, id)
                .eq(ProjectAsset::getTenantId, tenantId)));
    }

    default long countByProject(Long projectId) {
        return selectCount(Wrappers.<ProjectAsset>lambdaQuery()
                .eq(ProjectAsset::getProjectId, projectId));
    }
}
