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

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.advert.vo.AdvertPageReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.advert.AdvertDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 广告管理 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface AdvertMapper extends BaseMapperX<AdvertDO> {

    default PageResult<AdvertDO> selectPage(AdvertPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdvertDO>()
                .eqIfPresent(AdvertDO::getAdsenseId, reqVO.getAdsenseId())
                .eqIfPresent(AdvertDO::getSort, reqVO.getSort())
                .eqIfPresent(AdvertDO::getTitle, reqVO.getTitle())
                .eqIfPresent(AdvertDO::getSubTitle, reqVO.getSubTitle())
                .eqIfPresent(AdvertDO::getAdvertUrl, reqVO.getAdvertUrl())
                .eqIfPresent(AdvertDO::getLinkUrl, reqVO.getLinkUrl())
                .eqIfPresent(AdvertDO::getIsDisplay, reqVO.getIsDisplay())
                .eqIfPresent(AdvertDO::getValidType, reqVO.getValidType())
                .betweenIfPresent(AdvertDO::getStaDate, reqVO.getStaDate())
                .betweenIfPresent(AdvertDO::getEndDate, reqVO.getEndDate())
                .betweenIfPresent(AdvertDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AdvertDO::getId));
    }

    default List<AdvertDO> selectAdvertList(Long adsenseId) {
        LambdaQueryWrapperX<AdvertDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(AdvertDO::getAdsenseId, adsenseId);
        wrapperX.eq(AdvertDO::getIsDisplay, 1);
        wrapperX.orderByDesc(AdvertDO::getSort);
        return selectList(wrapperX);
    }

    default List<AdvertDO> selectCanUseAdvertList(Long adsenseId) {
        LambdaQueryWrapperX<AdvertDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(AdvertDO::getAdsenseId, adsenseId);
        wrapperX.eq(AdvertDO::getIsDisplay, 1);
        wrapperX.and(wrapper -> {
            wrapper.eq(AdvertDO::getValidType, 0);
            wrapper.or(wrapper1 -> {
                wrapper1.eq(AdvertDO::getValidType, 1)
                        .ge(AdvertDO::getStaDate, LocalDateTime.now())
                        .le(AdvertDO::getEndDate, LocalDateTime.now());
            });
        });
        wrapperX.orderByAsc(AdvertDO::getSort);
        return selectList(wrapperX);
    }
}
