package cn.iocoder.yudao.module.system.dal.mysql.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.StaffingPlanRecordPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.StaffingPlanRecordDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * 编制计划记录 Mapper
 */
@Mapper
public interface StaffingPlanRecordMapper extends BaseMapperX<StaffingPlanRecordDO> {

    default PageResult<StaffingPlanRecordDO> selectPage(StaffingPlanRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StaffingPlanRecordDO>()
                .eqIfPresent(StaffingPlanRecordDO::getBeginYearMonth, reqVO.getBeginYearMonth())
                .eqIfPresent(StaffingPlanRecordDO::getEndYearMonth, reqVO.getEndYearMonth())
                .eqIfPresent(StaffingPlanRecordDO::getStaffingCount, reqVO.getStaffingCount())
                .inIfPresent(StaffingPlanRecordDO::getTenantId, reqVO.getTenantIds())
                .betweenIfPresent(StaffingPlanRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StaffingPlanRecordDO::getId));
    }

    default Long selectOverlapCount(String beginYearMonth, String endYearMonth, Long tenantId, Long excludeId) {
        LambdaQueryWrapper<StaffingPlanRecordDO> wrapper = new LambdaQueryWrapperX<StaffingPlanRecordDO>()
                .eqIfPresent(StaffingPlanRecordDO::getTenantId, tenantId)
                .le(StaffingPlanRecordDO::getBeginYearMonth, endYearMonth)
                .ge(StaffingPlanRecordDO::getEndYearMonth, beginYearMonth);
        if (excludeId != null) {
            wrapper.ne(StaffingPlanRecordDO::getId, excludeId);
        }
        return selectCount(wrapper);
    }
}

