package com.community.huiju.service.impl;

import com.community.commom.constant.Constant;
import com.community.commom.utils.DateUtils;
import com.community.huiju.common.perproties.LoginCodePerproties;
import com.community.huiju.dao.ToBannerMapper;
import com.community.huiju.dao.TpCarouselSettingMapper;
import com.community.huiju.model.ToBanner;
import com.community.huiju.service.BannerServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author weichaochao
 * @Title: BannerServiceImpl
 * @date 2018/10/22
 */
@Service("/BannerService")
public class BannerServiceImpl implements BannerServiceI {

    @Autowired
    private ToBannerMapper toBannerMapper;

    @Autowired
    private TpCarouselSettingMapper tpCarouselSettingMapper;

    @Autowired
    private LoginCodePerproties loginCodePerproties;

    @Override
    public ToBanner viewBannerImg(Integer bannerId) {
        return toBannerMapper.selectByCommunityId(bannerId);
    }

    @Override
    public List<ToBanner> viewAllBannerImgAndCommunityId(Integer bannerPosition, Integer communityId) {
        //查询轮播图数量
        Integer bannerNum = tpCarouselSettingMapper.selectCarouselNum(Constant.BANNER, null);

        String nowTime = DateUtils.getDate("yyyy-MM-dd");
        List<ToBanner> bannersList = toBannerMapper.selectAllByNum(nowTime, bannerNum, bannerPosition, communityId);
        bannersList.stream().forEach(banner -> {
            if (banner.getBannerType().equals(Constant.BANNER_TYPE_ARTICLE)){
                banner.setExternalLink(loginCodePerproties.getAppUrl() + "bannerDetail?from=index&id="+banner.getId());
            }
        });
        return bannersList;
    }

    @Override
    public List<ToBanner> viewAllBannerImg(Integer bannerPosition) {
        return viewAllBannerImgAndCommunityId(bannerPosition, null);
    }
}
