package cn.newfeifan.mall.module.distri.dal.mysql.sharelink;

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.framework.mybatis.core.mapper.BaseMapperX;
import cn.newfeifan.mall.module.distri.controller.app.sharelink.vo.AppShareLinkPageReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.sharelink.ShareLinkDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 分享链接 Mapper
 *
 * @author 非繁人
 */
@Mapper
public interface ShareLinkMapper extends BaseMapperX<ShareLinkDO> {

    default PageResult<ShareLinkDO> selectPage(AppShareLinkPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ShareLinkDO>()
                .likeIfPresent(ShareLinkDO::getName, reqVO.getName())
                .eqIfPresent(ShareLinkDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ShareLinkDO::getCreateUserId, reqVO.getCreateUserId())
                .eqIfPresent(ShareLinkDO::getCategory, reqVO.getCategory())
                .eqIfPresent(ShareLinkDO::getObjectId, reqVO.getObjectId())
                .eqIfPresent(ShareLinkDO::getRegisterUserCount, reqVO.getRegisterUserCount())
                .betweenIfPresent(ShareLinkDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ShareLinkDO::getId));
    }

    default ShareLinkDO selectByCategory(Integer category, Long loginUserId, Long objectId){
        LambdaQueryWrapperX<ShareLinkDO> wrapper = new LambdaQueryWrapperX<ShareLinkDO>()
                .eq(ShareLinkDO::getCategory, category)
                .eq(ShareLinkDO::getCreateUserId, loginUserId);
        if(objectId == null){
            wrapper.isNull(ShareLinkDO::getObjectId);
        }else{
            wrapper.eq(ShareLinkDO::getObjectId, objectId);
        }
        return selectOne(wrapper);
    }
}