package com.caimei.www.service.page.impl;


import com.caimei.www.mapper.BaseDao;
import com.caimei.www.pojo.page.*;
import com.caimei.www.service.page.BaseService;
import com.caimei.www.utils.ImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/6/16
 */
@Slf4j
@Service
public class BaseServiceImpl implements BaseService {
    @Resource
    private BaseDao baseDao;
    @Value("${caimei.wwwDomain}")
    private String domain;
    /**
     * 获取搜索热门关键字
     */
    @Override
    @Cacheable(value = "getSearchHotWord", key="'www'", unless="#result == null")
    public List<String> getSearchHotWord() {
        List<String> hotSearch = baseDao.getSearchKeyword();
        if (!CollectionUtils.isEmpty(hotSearch) && hotSearch.size() > 8) {
            return hotSearch.parallelStream()
                .filter(str -> !StringUtils.isEmpty(str)).limit(8)
                .collect(Collectors.toList());
        } else {
            return hotSearch;
        }
    }

    /**
     * 获取头部菜单
     */
    @Override
    @Cacheable(value = "getNavMenu", key="'www'", unless="#result == null")
    public List<TopMenu> getNavMenu() {
        List<TopMenu> menuList = baseDao.getTopMenus();
        menuList.forEach(item -> {
            List<SubMenu> subList = baseDao.getSubMenus(item.getId());
            if (subList.size() > 0) {
                // 设置老图片路径
                subList.forEach(sub -> {
                     sub.setImage(ImageUtil.getImageURL("", sub.getImage(), 0, domain));
                });
            }
            item.setSubMenus(subList);
        });
        return menuList;
    }

    /**
     * 底部帮助页
     */
    @Override
    @Cacheable(value = "getHelpPages", key="'www'", unless="#result == null")
    public List<BaseLink> getHelpPages() {
        List<BaseLink> typeList = baseDao.getHelpPageTypes();
        typeList.forEach(item -> {
            List<BaseLink> pageList = baseDao.getHelpPagesByType(item.getId());
            item.setLinkList(pageList);
        });
        return typeList;
    }

    /**
     * 友情链接
     */
    @Override
    @Cacheable(value = "getFriendLinks", key="'www'", unless="#result == null")
    public List<BaseLink> getFriendLinks() {
        return baseDao.getFriendLinks();
    }

}
