package com.hzs.system.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.enums.EPositionTwo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.system.config.BdAdvertBanner;
import com.hzs.common.domain.system.config.ext.BdAdvertBannerExt;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.mapper.BdAdvertBannerMapper;
import com.hzs.system.config.service.IBdAdvertBannerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 广告banner 服务实现类
 *
 * @author hzs
 * @since 2023-04-13
 */
@Service
public class BdAdvertBannerServiceImpl extends ServiceImpl<BdAdvertBannerMapper, BdAdvertBanner> implements IBdAdvertBannerService {

    @DubboReference
    IMemberServiceApi iMemberServiceApi;
    @Override
    public List<BdAdvertBanner> listData(BdAdvertBanner banner, Long userId) {
        if (null == banner) {
            banner = new BdAdvertBanner();
        }
        CuMember cuMember = iMemberServiceApi.getMember(userId).getData();
        LambdaQueryWrapper<BdAdvertBanner> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(banner.getPosition())) {
            queryWrapper.eq(BdAdvertBanner::getPosition, banner.getPosition());
        } else {
            // 默认首页的banner
            queryWrapper.eq(BdAdvertBanner::getPosition, EPositionTwo.POSITION_ONE.getValue());
        }
        if(ObjectUtil.isEmpty(cuMember)){
            queryWrapper.isNull(BdAdvertBanner::getPkVertex);
        }else{
            queryWrapper.and(qw ->  qw.isNull(BdAdvertBanner::getPkVertex)
                    .or()
                    .eq(ObjectUtil.isNotEmpty(cuMember.getPkVertex()), BdAdvertBanner::getPkVertex, cuMember.getPkVertex()));
        }
        queryWrapper.eq(BdAdvertBanner::getPkCountry, CountryConstants.CHINA_COUNTRY);
        queryWrapper.lt(BdAdvertBanner::getEffectiveDate, new Date());
        queryWrapper.orderByAsc(BdAdvertBanner::getSort);
        return this.list(queryWrapper);
    }

    @Override
    public List<BdAdvertBanner> getList(BdAdvertBannerExt param) {
        return baseMapper.getList(param);
    }

}
