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

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 com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceBookPageReqVO;
import com.yhy.module.core.dal.dataobject.infoprice.InfoPriceBookDO;
import com.yhy.module.core.enums.infoprice.InfoPricePublishStatusEnum;
import org.apache.ibatis.annotations.Mapper;

/**
 * 信息价册 Mapper
 *
 * @author yhy
 */
@Mapper
public interface InfoPriceBookMapper extends BaseMapperX<InfoPriceBookDO> {

    /**
     * 分页查询信息价册
     *
     * @param reqVO 查询条件
     * @return 分页结果
     */
    default PageResult<InfoPriceBookDO> selectPage(InfoPriceBookPageReqVO reqVO) {
        LambdaQueryWrapperX<InfoPriceBookDO> wrapper = new LambdaQueryWrapperX<InfoPriceBookDO>()
                .eqIfPresent(InfoPriceBookDO::getTreeNodeId, reqVO.getTreeNodeId())
                .likeIfPresent(InfoPriceBookDO::getName, reqVO.getName())
                .eqIfPresent(InfoPriceBookDO::getPublishStatus, reqVO.getPublishStatus())
                .betweenIfPresent(InfoPriceBookDO::getStartTime, reqVO.getStartTimeBegin(), reqVO.getStartTimeEnd())
                .betweenIfPresent(InfoPriceBookDO::getEndTime, reqVO.getEndTimeBegin(), reqVO.getEndTimeEnd())
                .orderByDesc(InfoPriceBookDO::getStartTime);
        // 筛选有附件的记录
        if (Boolean.TRUE.equals(reqVO.getAttachment())) {
            wrapper.isNotNull(InfoPriceBookDO::getAttachment)
                   .ne(InfoPriceBookDO::getAttachment, "");
        }
        return selectPage(reqVO, wrapper);
    }

    /**
     * 统计节点下的信息价册数量
     *
     * @param treeNodeId 树节点ID
     * @return 数量
     */
    default Long countByTreeNodeId(Long treeNodeId) {
        return selectCount(new LambdaQueryWrapperX<InfoPriceBookDO>()
                .eq(InfoPriceBookDO::getTreeNodeId, treeNodeId));
    }

    /**
     * 分页查询信息价册（排除指定租户）
     *
     * @param reqVO 查询条件
     * @param excludeTenantId 要排除的租户ID
     * @return 分页结果
     */
    default PageResult<InfoPriceBookDO> selectPageExcludeTenant(InfoPriceBookPageReqVO reqVO, Long excludeTenantId) {
        if (reqVO.getTreeNodeId() == null) {
            return PageResult.empty(0L);
        }
        LambdaQueryWrapperX<InfoPriceBookDO> wrapper = new LambdaQueryWrapperX<InfoPriceBookDO>()
                .eq(InfoPriceBookDO::getTreeNodeId, reqVO.getTreeNodeId())
                .eq(InfoPriceBookDO::getPublishStatus, InfoPricePublishStatusEnum.COMPLETED.getCode())
                .likeIfPresent(InfoPriceBookDO::getName, reqVO.getName())
                .eqIfPresent(InfoPriceBookDO::getPublishStatus, reqVO.getPublishStatus())
                .betweenIfPresent(InfoPriceBookDO::getStartTime, reqVO.getStartTimeBegin(), reqVO.getStartTimeEnd())
                .betweenIfPresent(InfoPriceBookDO::getEndTime, reqVO.getEndTimeBegin(), reqVO.getEndTimeEnd())
                .orderByDesc(InfoPriceBookDO::getStartTime);
        // 筛选有附件的记录
        if (Boolean.TRUE.equals(reqVO.getAttachment())) {
            wrapper.isNotNull(InfoPriceBookDO::getAttachment)
                   .ne(InfoPriceBookDO::getAttachment, "");
        }
        if (excludeTenantId != null) {
            wrapper.ne(InfoPriceBookDO::getTenantId, excludeTenantId);

            wrapper.isNotNull(InfoPriceBookDO::getStartTime)
                    .isNotNull(InfoPriceBookDO::getEndTime);

            wrapper.apply(
                    "NOT EXISTS (" +
                            " SELECT 1" +
                            " FROM yhy_info_price_book b" +
                            " WHERE b.tenant_id = {0}" +
                            "   AND b.tree_node_id = yhy_info_price_book.tree_node_id" +
                            "   AND b.start_time IS NOT NULL" +
                            "   AND b.end_time IS NOT NULL" +
                            "   AND b.deleted = 0" +
                            "   AND NOT (yhy_info_price_book.end_time < b.start_time OR yhy_info_price_book.start_time > b.end_time)" +
                            ")",
                    excludeTenantId);
        }
        return selectPage(reqVO, wrapper);
    }
}
