package cn.iocoder.yudao.module.member.dal.mysql.activity;

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.member.controller.admin.activity.vo.ActivityPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activity.ActivityDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 活动 Mapper
 *
 * @author 我是秦俊旗
 */
@Mapper
public interface ActivityMapper extends BaseMapperX<ActivityDO> {

    default PageResult<ActivityDO> selectPage(ActivityPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityDO>()
                .likeIfPresent(ActivityDO::getActivityName, reqVO.getActivityName())
                .leIfPresent(ActivityDO::getStartTime, reqVO.getStartTime())
                .geIfPresent(ActivityDO::getEndTime, reqVO.getStartTime())
                .eqIfPresent(ActivityDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ActivityDO::getImg, reqVO.getImg())
                .eqIfPresent(ActivityDO::getLocation, reqVO.getLocation())
                .eqIfPresent(ActivityDO::getSponsor, reqVO.getSponsor())
                .eqIfPresent(ActivityDO::getLotteryNum, reqVO.getLotteryNum())
                .eqIfPresent(ActivityDO::getReservationStatus, reqVO.getReservationStatus())
                .betweenIfPresent(ActivityDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityDO::getId));
    }

    default PageResult<ActivityDO> selectPageApp(ActivityPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityDO>()
                .likeIfPresent(ActivityDO::getActivityName, reqVO.getActivityName())
                .leIfPresent(ActivityDO::getStartTime, reqVO.getStartTime())
                .geIfPresent(ActivityDO::getEndTime, reqVO.getStartTime())
                .eqIfPresent(ActivityDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ActivityDO::getImg, reqVO.getImg())
                .eqIfPresent(ActivityDO::getLocation, reqVO.getLocation())
                .eqIfPresent(ActivityDO::getSponsor, reqVO.getSponsor())
                .eqIfPresent(ActivityDO::getLotteryNum, reqVO.getLotteryNum())
                .eqIfPresent(ActivityDO::getReservationStatus, reqVO.getReservationStatus())
                .betweenIfPresent(ActivityDO::getCreateTime, reqVO.getCreateTime())
                .last("ORDER BY " +
                        "  CASE " +
                        "    WHEN start_time <= NOW() AND end_time >= NOW() THEN 0  " +
                        "    ELSE 1                                                 " +
                        "  END," +
                        "  end_time ASC "));
    }
}