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

import com.caimei.www.pojo.JsonModel;
import com.caimei.www.mapper.HomeDao;
import com.caimei.www.pojo.page.PageFloor;
import com.caimei.www.pojo.page.ImageLink;
import com.caimei.www.pojo.page.ProductList;
import com.caimei.www.service.page.HomeService;
import com.caimei.www.utils.ImageUtil;
import com.caimei.www.utils.PriceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

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

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/6/22
 */
@Slf4j
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private HomeDao homeDao;
    @Value("${caimei.wwwDomain}")
    private String domain;

    /**
     * 首页轮播图
     */
    @Override
    @Cacheable(value = "getHomeBanners", key="'www'", unless="#result == null")
    public JsonModel<List<ImageLink>> getHomeBanners() {
        List<ImageLink> list = homeDao.getHomeBanners();
        return JsonModel.success(list);
    }

    /**
     * 首页推荐专区商品
     */
    @Override
    @Cacheable(value = "getRecommendProducts", key="'www'", unless="#result == null")
    public JsonModel<List<ProductList>> getRecommendProducts() {
        List<ProductList> list = homeDao.getRecommendeds();
        if (list.size() > 0) {
            // 设置价格等级 及 老图片路径
            list.forEach(product -> {
                 product.setPriceGrade(PriceUtil.getpriceGrade(product.getPrice()));
                 product.setPrice(0d);
                 product.setImage(ImageUtil.getImageURL("product", product.getImage(), 0, domain));
            });
        }
        return JsonModel.success(list);
    }

    /**
     * 首页楼层专题数据
     */
    @Override
    public JsonModel<List<PageFloor>> getHomePageFloorData() {
        // 大专题:6,小专题:5,商品专题:4,优质供应商:3
        List<PageFloor> floorList = homeDao.getHomePageFloor();
        floorList.forEach(floor -> {
            if(floor.getType() == 6){
                // 6大专题二级楼层
                List<PageFloor> bigTopicFloors = homeDao.getHomeBigTopicFloor();
                bigTopicFloors.forEach(bigTopic -> {
                    List<ImageLink>  topicData = homeDao.getBigTopicData(bigTopic.getId());
                    bigTopic.setFloorData(topicData);
                });
                floor.setSubFloors(bigTopicFloors);
            } else if (floor.getType() == 5){
                // 5小专题
                List<ImageLink>  topicData5 = homeDao.getSmallTopicData();
                floor.setFloorData(topicData5);
            } else if (floor.getType() == 4){
                // 4商品专题
                List<ImageLink>  topicData4 = homeDao.getCommodityTopicData();
                floor.setFloorData(topicData4);
            } else if (floor.getType() == 3){
                // 3优质供应商
                List<ImageLink>  topicData3 = homeDao.getSuppliersTopData();
                floor.setFloorData(topicData3);
            }
        });
        return JsonModel.success(floorList);
    }

    /**
     * 首页左侧广告图
     */
    @Override
    @Cacheable(value = "getAdvertising", key="'www'", unless="#result == null")
    public JsonModel<List<ImageLink>> getAdvertising() {
        List<ImageLink> list = homeDao.getAdvertising();
        return JsonModel.success(list);
    }

}
