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

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.activityuser.vo.ActivityUserCountVO;
import cn.iocoder.yudao.module.member.controller.admin.activityuser.vo.ActivityUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.activity.vo.AppActivityRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activity.ActivityDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityuser.ActivityUserDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 活动参加人员 Mapper
 *
 * @author 我是秦俊旗
 */
@Mapper
public interface ActivityUserMapper extends BaseMapperX<ActivityUserDO> {

    default PageResult<ActivityUserDO> selectPage(ActivityUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityUserDO>()
                .eqIfPresent(ActivityUserDO::getActivityId, reqVO.getActivityId())
                .eqIfPresent(ActivityUserDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityUserDO::getMobile, reqVO.getMobile())
                .betweenIfPresent(ActivityUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityUserDO::getId));
    }

    List<ActivityUserCountVO> countByActivityIds(@Param("activityIds") List<Long> activityIds);

    default PageResult<AppActivityRespVO> selectUserRecordPage(ActivityUserPageReqVO reqVO) {
        Page<AppActivityRespVO> activityAwardsUserRespVOPage = selectJoinPage(new Page<>(reqVO.getPageNo(), reqVO.getPageSize()), AppActivityRespVO.class,
                new MPJLambdaWrapper<ActivityUserDO>()
                        .selectAll(ActivityDO.class)
                        .leftJoin(ActivityDO.class, ActivityDO::getId, ActivityUserDO::getActivityId)
                        .eq(ActivityUserDO::getUserId, reqVO.getUserId())
                        .orderByDesc(ActivityUserDO::getCreateTime)
        );
        return new PageResult<>(activityAwardsUserRespVOPage.getRecords(), activityAwardsUserRespVOPage.getTotal());
    }
}