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

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.workbench.WbAdjustmentSettingDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 工作台定额调整设置 Mapper
 *
 * @author yhy
 */
@Mapper
public interface WbAdjustmentSettingMapper extends BaseMapperX<WbAdjustmentSettingDO> {

    /**
     * 根据分部分项定额节点ID查询调整设置列表
     */
    default List<WbAdjustmentSettingDO> selectListByDivisionId(Long divisionId) {
        return selectList(new LambdaQueryWrapperX<WbAdjustmentSettingDO>()
                .eq(WbAdjustmentSettingDO::getDivisionId, divisionId)
                .orderByAsc(WbAdjustmentSettingDO::getSortOrder));
    }

    /**
     * 根据分部分项定额节点ID列表批量查询
     */
    default List<WbAdjustmentSettingDO> selectListByDivisionIds(List<Long> divisionIds) {
        return selectList(new LambdaQueryWrapperX<WbAdjustmentSettingDO>()
                .in(WbAdjustmentSettingDO::getDivisionId, divisionIds)
                .orderByAsc(WbAdjustmentSettingDO::getSortOrder));
    }

    /**
     * 根据来源调整设置ID查询
     */
    default List<WbAdjustmentSettingDO> selectListBySourceSettingId(Long sourceAdjustmentSettingId) {
        return selectList(new LambdaQueryWrapperX<WbAdjustmentSettingDO>()
                .eq(WbAdjustmentSettingDO::getSourceAdjustmentSettingId, sourceAdjustmentSettingId));
    }

    /**
     * 根据分部分项定额节点ID删除所有调整设置
     */
    default int deleteByDivisionId(Long divisionId) {
        return delete(new LambdaQueryWrapperX<WbAdjustmentSettingDO>()
                .eq(WbAdjustmentSettingDO::getDivisionId, divisionId));
    }

    /**
     * 获取同级最大排序号
     */
    default Integer selectMaxSortOrderByDivisionId(Long divisionId) {
        WbAdjustmentSettingDO maxSortNode = selectOne(new LambdaQueryWrapperX<WbAdjustmentSettingDO>()
                .eq(WbAdjustmentSettingDO::getDivisionId, divisionId)
                .orderByDesc(WbAdjustmentSettingDO::getSortOrder)
                .last("LIMIT 1"));
        return maxSortNode != null ? maxSortNode.getSortOrder() : 0;
    }

    /**
     * 统计分部分项定额节点下的调整设置数量
     */
    default Long selectCountByDivisionId(Long divisionId) {
        return selectCount(new LambdaQueryWrapperX<WbAdjustmentSettingDO>()
                .eq(WbAdjustmentSettingDO::getDivisionId, divisionId));
    }
}
