package cn.iocoder.yudao.module.server.dal.mysql.materialinfo;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.server.dal.dataobject.materialinfo.MaterialInfoDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.server.controller.admin.materialinfo.vo.*;

/**
 * 资料库 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface MaterialInfoMapper extends BaseMapperX<MaterialInfoDO> {

/*    default PageResult<MaterialInfoDO> selectPage(MaterialInfoPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MaterialInfoDO>()
                .eqIfPresent(MaterialInfoDO::getCategoryId, reqVO.getCategoryId())
                .likeIfPresent(MaterialInfoDO::getCategoryName, reqVO.getCategoryName())
                .likeIfPresent(MaterialInfoDO::getName, reqVO.getName())
                .eqIfPresent(MaterialInfoDO::getDescription, reqVO.getDescription())
                .betweenIfPresent(MaterialInfoDO::getExpireTime, reqVO.getExpireTime())
                .eqIfPresent(MaterialInfoDO::getShareType, reqVO.getShareType())
                .eqIfPresent(MaterialInfoDO::getDownloadCount, reqVO.getDownloadCount())
                .eqIfPresent(MaterialInfoDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MaterialInfoDO::getReportRemark, reqVO.getReportRemark())
                .eqIfPresent(MaterialInfoDO::getAttachment, reqVO.getAttachment())
                .betweenIfPresent(MaterialInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialInfoDO::getId));
    }*/

    default PageResult<MaterialInfoDO> selectPage(MaterialInfoPageReqVO reqVO) {
        LambdaQueryWrapperX<MaterialInfoDO> queryWrapper =   new LambdaQueryWrapperX<MaterialInfoDO>()
                .eqIfPresent(MaterialInfoDO::getCategoryId, reqVO.getCategoryId())
                .likeIfPresent(MaterialInfoDO::getCategoryName, reqVO.getCategoryName())
                .likeIfPresent(MaterialInfoDO::getName, reqVO.getName())
                .eqIfPresent(MaterialInfoDO::getDescription, reqVO.getDescription())
                .betweenIfPresent(MaterialInfoDO::getExpireTime, reqVO.getExpireTime())
                .eqIfPresent(MaterialInfoDO::getShareType, reqVO.getShareType())
                .eqIfPresent(MaterialInfoDO::getDownloadCount, reqVO.getDownloadCount())
                .eqIfPresent(MaterialInfoDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MaterialInfoDO::getReportRemark, reqVO.getReportRemark())
                .eqIfPresent(MaterialInfoDO::getAttachment, reqVO.getAttachment())
                .betweenIfPresent(MaterialInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialInfoDO::getId);

        // 如果有共享部门名称查询条件
        if (reqVO.getShareDeptName() != null && !"".equals(reqVO.getShareDeptName().trim())) {
            // 使用 EXISTS 子查询来过滤共享部门
            // 使用 {0} 作为参数占位符，使用表的别名来引用字段
             queryWrapper.inSql(MaterialInfoDO::getId,
                 "SELECT material_id FROM trs_material_share_user WHERE dept_name LIKE CONCAT('%', '" + reqVO.getShareDeptName().trim() + "', '%')");
        }

        return selectPage(reqVO, queryWrapper);
    }

}