package cn.iocoder.lyzsys.module.tjt.dal.mysql.planningquarter;

import cn.iocoder.lyzsys.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.lyzsys.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.lyzsys.module.tjt.dal.dataobject.planningquarter.ProjectPlanningQuarterDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 合约规划季度分配 Mapper
 *
 * @author Codex
 */
@Mapper
public interface ProjectPlanningQuarterMapper extends BaseMapperX<ProjectPlanningQuarterDO> {

    default ProjectPlanningQuarterDO selectByPlanningIdAndDistributionYearAndQuarter(Long planningId,
                                                                                      Integer distributionYear,
                                                                                      Integer quarterNo) {
        return selectParentByPlanningIdAndDistributionYearAndQuarter(planningId, distributionYear, quarterNo);
    }

    default ProjectPlanningQuarterDO selectParentByPlanningIdAndDistributionYearAndQuarter(Long planningId,
                                                                                           Integer distributionYear,
                                                                                           Integer quarterNo) {
        return selectOne(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .eq(ProjectPlanningQuarterDO::getDistributionYear, distributionYear)
                .eq(ProjectPlanningQuarterDO::getQuarterNo, quarterNo)
                .isNull(ProjectPlanningQuarterDO::getGuideDetailId));
    }

    default ProjectPlanningQuarterDO selectByPlanningIdAndGuideDetailIdAndDistributionYearAndQuarter(
            Long planningId, Long guideDetailId, Integer distributionYear, Integer quarterNo) {
        return selectOne(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .eq(ProjectPlanningQuarterDO::getGuideDetailId, guideDetailId)
                .eq(ProjectPlanningQuarterDO::getDistributionYear, distributionYear)
                .eq(ProjectPlanningQuarterDO::getQuarterNo, quarterNo));
    }

    default List<ProjectPlanningQuarterDO> selectListByPlanningId(Long planningId) {
        return selectList(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailSortNo)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getDistributionYear)
                .orderByAsc(ProjectPlanningQuarterDO::getQuarterNo)
                .orderByAsc(ProjectPlanningQuarterDO::getId));
    }

    default List<ProjectPlanningQuarterDO> selectParentListByPlanningId(Long planningId) {
        return selectList(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .isNull(ProjectPlanningQuarterDO::getGuideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getDistributionYear)
                .orderByAsc(ProjectPlanningQuarterDO::getQuarterNo)
                .orderByAsc(ProjectPlanningQuarterDO::getId));
    }

    default List<ProjectPlanningQuarterDO> selectDetailListByPlanningId(Long planningId) {
        return selectList(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .isNotNull(ProjectPlanningQuarterDO::getGuideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailSortNo)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getDistributionYear)
                .orderByAsc(ProjectPlanningQuarterDO::getQuarterNo)
                .orderByAsc(ProjectPlanningQuarterDO::getId));
    }

    default List<ProjectPlanningQuarterDO> selectListByGuideDetailId(Long guideDetailId) {
        return selectList(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getGuideDetailId, guideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getDistributionYear)
                .orderByAsc(ProjectPlanningQuarterDO::getQuarterNo)
                .orderByAsc(ProjectPlanningQuarterDO::getId));
    }

    default List<ProjectPlanningQuarterDO> selectListByGuideDetailIds(Collection<Long> guideDetailIds) {
        if (guideDetailIds == null || guideDetailIds.isEmpty()) {
            return Collections.emptyList();
        }
        return selectList(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .in(ProjectPlanningQuarterDO::getGuideDetailId, guideDetailIds)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailSortNo)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getDistributionYear)
                .orderByAsc(ProjectPlanningQuarterDO::getQuarterNo)
                .orderByAsc(ProjectPlanningQuarterDO::getId));
    }

    default boolean existsParentQuarter(Long planningId, Long excludeId) {
        return selectCount(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .isNull(ProjectPlanningQuarterDO::getGuideDetailId)
                .ne(excludeId != null, ProjectPlanningQuarterDO::getId, excludeId)) > 0;
    }

    default boolean existsGuideDetailQuarter(Long planningId, Long excludeId) {
        return selectCount(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .eq(ProjectPlanningQuarterDO::getPlanningId, planningId)
                .isNotNull(ProjectPlanningQuarterDO::getGuideDetailId)
                .ne(excludeId != null, ProjectPlanningQuarterDO::getId, excludeId)) > 0;
    }

    default List<ProjectPlanningQuarterDO> selectListByPlanningIds(Collection<Long> planningIds) {
        if (planningIds == null || planningIds.isEmpty()) {
            return Collections.emptyList();
        }
        return selectList(new LambdaQueryWrapperX<ProjectPlanningQuarterDO>()
                .in(ProjectPlanningQuarterDO::getPlanningId, planningIds)
                .orderByAsc(ProjectPlanningQuarterDO::getPlanningId)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailSortNo)
                .orderByAsc(ProjectPlanningQuarterDO::getGuideDetailId)
                .orderByAsc(ProjectPlanningQuarterDO::getDistributionYear)
                .orderByAsc(ProjectPlanningQuarterDO::getQuarterNo)
                .orderByAsc(ProjectPlanningQuarterDO::getId));
    }

}
