package com.yc.ship.module.miniapplet.dal.mysql.content;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.miniapplet.controller.admin.content.vo.ContentPageReqVO;
import com.yc.ship.module.miniapplet.controller.app.content.vo.AppContentPageReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.content.ContentDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 目录内容 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface ContentMapper extends BaseMapperX<ContentDO> {

    default PageResult<ContentDO> selectPage(ContentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ContentDO>()
                .likeIfPresent(ContentDO::getName, reqVO.getName())
                .eqIfPresent(ContentDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ContentDO::getContent, reqVO.getContent())
                .eqIfPresent(ContentDO::getCatalogueId, reqVO.getCatalogueId())
                .eqIfPresent(ContentDO::getIntroduce, reqVO.getIntroduce())
                .eqIfPresent(ContentDO::getKeyword, reqVO.getKeyword())
                .eqIfPresent(ContentDO::getCover, reqVO.getCover())
                .eqIfPresent(ContentDO::getVideo, reqVO.getVideo())
                .eqIfPresent(ContentDO::getUrl, reqVO.getUrl())
                .eqIfPresent(ContentDO::getReleaseStatus, reqVO.getReleaseStatus())
                .betweenIfPresent(ContentDO::getReleaseTime, reqVO.getReleaseTime())
                .betweenIfPresent(ContentDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ContentDO::getId));
    }

    default List<ContentDO> getContentList(Long catalogueId, Integer releaseStatus) {
        LambdaQueryWrapperX<ContentDO> queryWrapperX = new LambdaQueryWrapperX<>();
        if (catalogueId != null) {
            queryWrapperX.eqIfPresent(ContentDO::getCatalogueId, catalogueId);
        }
        if (releaseStatus != null) {
            queryWrapperX.eq(ContentDO::getReleaseStatus, releaseStatus);
        }
        queryWrapperX.orderByDesc(ContentDO::getReleaseTime);
        return selectList(queryWrapperX);
    }

    /**
     * 获取小程序内容分页
     * @param reqVO
     * @return
     */
    default PageResult<ContentDO> selectAppContentPage(AppContentPageReqVO reqVO, Long catalogueId) {
        LambdaQueryWrapperX<ContentDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(ContentDO::getReleaseStatus, 1);
        wrapperX.eq(ContentDO::getCatalogueId, catalogueId);
        wrapperX.and(StringUtils.isNotBlank(reqVO.getKeyword()), i -> {
            i.like(ContentDO::getName, reqVO.getKeyword())
                    .or()
                    .like(ContentDO::getTitle, reqVO.getKeyword())
                    .or()
                    .like(ContentDO::getContent, reqVO.getKeyword())
                    .or()
                    .like(ContentDO::getIntroduce, reqVO.getKeyword())
                    .or()
                    .like(ContentDO::getKeyword, reqVO.getKeyword());
        });
        return selectPage(reqVO, wrapperX);
    }
}
