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

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.adsense.vo.AdsensePageReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.adsense.AdsenseDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 广告位管理 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface AdsenseMapper extends BaseMapperX<AdsenseDO> {

    default PageResult<AdsenseDO> selectPage(AdsensePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdsenseDO>()
                .eqIfPresent(AdsenseDO::getAdsenseCode, reqVO.getAdsenseCode())
                .likeIfPresent(AdsenseDO::getAdsenseName, reqVO.getAdsenseName())
                .eqIfPresent(AdsenseDO::getAdsenseType, reqVO.getAdsenseType())
                .betweenIfPresent(AdsenseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AdsenseDO::getId));
    }

    default List<AdsenseDO> selectList(Integer status) {
        LambdaQueryWrapperX<AdsenseDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(AdsenseDO::getIsUse, status);
        queryWrapper.orderByDesc(AdsenseDO::getCreateTime);
        return selectList(queryWrapper);
    }

    default List<AdsenseDO> getAdsenseList(Long id, String code) {
        LambdaQueryWrapperX<AdsenseDO> queryWrapper = new LambdaQueryWrapperX<>();
        if (id != null) {
            queryWrapper.eq(AdsenseDO::getId, id);
        }
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq(AdsenseDO::getAdsenseCode, code);
        }
        queryWrapper.eq(AdsenseDO::getIsUse, 1);
        queryWrapper.orderByDesc(AdsenseDO::getCreateTime);
        return selectList(queryWrapper);
    }

    default AdsenseDO selectByCode(String code) {
        return selectOne(new LambdaQueryWrapperX<AdsenseDO>()
                .eq(AdsenseDO::getAdsenseCode, code)
                .eq(AdsenseDO::getIsUse, 1)
                .last("LIMIT 1"));
    }
}
