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

import cn.hutool.core.util.StrUtil;
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.activityawardsuser.vo.ActivityAwardsUserCountVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityawards.ActivityAwardsDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityawardsuser.ActivityAwardsUserDO;
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 ActivityAwardsUserMapper extends BaseMapperX<ActivityAwardsUserDO> {

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

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

    default PageResult<ActivityAwardsUserRespVO> selectJoinPage(ActivityAwardsUserPageReqVO reqVO) {
        Page<ActivityAwardsUserRespVO> activityAwardsUserRespVOPage = selectJoinPage(new Page<>(reqVO.getPageNo(), reqVO.getPageSize()), ActivityAwardsUserRespVO.class,
                new MPJLambdaWrapper<ActivityAwardsUserDO>()
                        .selectAll(ActivityAwardsUserDO.class)
                        .select(ActivityAwardsDO::getAwardsName, ActivityAwardsDO::getPrize)
                        .leftJoin(ActivityAwardsDO.class, ActivityAwardsDO::getId, ActivityAwardsUserDO::getAwardsId)
                        .eq(reqVO.getActivityId() != null, ActivityAwardsUserDO::getActivityId, reqVO.getActivityId())
                        .eq(reqVO.getAwardsId() != null, ActivityAwardsUserDO::getAwardsId, reqVO.getAwardsId())
                        .eq(StrUtil.isNotBlank(reqVO.getAwardsStatus()), ActivityAwardsUserDO::getAwardsStatus,reqVO.getAwardsStatus())
                        .eq(StrUtil.isNotBlank(reqVO.getMobile()), ActivityAwardsUserDO::getMobile, reqVO.getMobile())
                        .orderByAsc(ActivityAwardsDO::getSortNum)
        );
        return new PageResult<>(activityAwardsUserRespVOPage.getRecords(), activityAwardsUserRespVOPage.getTotal());
    }

}