package com.yhy.module.core.dal.mysql.quota;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yhy.module.core.dal.dataobject.quota.QuotaUnifiedFeeResourceDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 统一取费子目工料机 Mapper
 */
@Mapper
public interface QuotaUnifiedFeeResourceMapper extends BaseMapperX<QuotaUnifiedFeeResourceDO> {

    /**
     * 根据子定额ID查询工料机列表
     */
    default List<QuotaUnifiedFeeResourceDO> selectListByUnifiedFeeSettingId(Long unifiedFeeSettingId) {
        return selectList(new LambdaQueryWrapperX<QuotaUnifiedFeeResourceDO>()
                .eq(QuotaUnifiedFeeResourceDO::getUnifiedFeeSettingId, unifiedFeeSettingId)
                .orderByAsc(QuotaUnifiedFeeResourceDO::getSortOrder));
    }

    /**
     * 根据子定额ID删除工料机
     */
    default int deleteByUnifiedFeeSettingId(Long unifiedFeeSettingId) {
        return delete(new LambdaQueryWrapperX<QuotaUnifiedFeeResourceDO>()
                .eq(QuotaUnifiedFeeResourceDO::getUnifiedFeeSettingId, unifiedFeeSettingId));
    }

    /**
     * 查询同级最大排序值
     */
    default Integer selectMaxSortOrder(Long unifiedFeeSettingId) {
        QuotaUnifiedFeeResourceDO maxItem = selectOne(new LambdaQueryWrapperX<QuotaUnifiedFeeResourceDO>()
                .eq(QuotaUnifiedFeeResourceDO::getUnifiedFeeSettingId, unifiedFeeSettingId)
                .orderByDesc(QuotaUnifiedFeeResourceDO::getSortOrder)
                .last("LIMIT 1"));
        return maxItem != null ? maxItem.getSortOrder() : 0;
    }

    /**
     * 根据子定额ID和工料机ID查询
     */
    default QuotaUnifiedFeeResourceDO selectByResourceItemId(Long unifiedFeeSettingId, Long resourceItemId) {
        return selectOne(new LambdaQueryWrapperX<QuotaUnifiedFeeResourceDO>()
                .eq(QuotaUnifiedFeeResourceDO::getUnifiedFeeSettingId, unifiedFeeSettingId)
                .eq(QuotaUnifiedFeeResourceDO::getResourceItemId, resourceItemId));
    }
}
