package com.zbkj.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.zbkj.common.constants.Constants;
import com.zbkj.common.model.home.Home;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.*;
import com.zbkj.common.utils.RedisUtil;
import com.zbkj.common.vo.HomeProductVo;
import com.zbkj.common.vo.HomeVo;
import com.zbkj.common.vo.product.ProductDetailVo;
import com.zbkj.service.dao.HomeDao;
import com.zbkj.service.dao.HomeProductsDao;
import com.zbkj.service.service.EbHomeService;
import com.zbkj.service.service.StoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class EbHomeServiceImpl extends ServiceImpl<HomeDao, Home> implements EbHomeService {

    @Autowired private RedisUtil redisUtil;
    @Autowired private HomeDao dao;
    @Autowired private HomeProductsDao homeProductsDao;
    @Autowired private StoreProductService productService;

    @Override
    public List<HomeVo> getIndexHomeList(Integer storeId) {
        List<HomeVo> homeVos = dao.selectHomeType(Constants.HOME_TYPE_1, storeId);
        for (HomeVo homeVo : homeVos) {
            // 默认展示5个
            PageParamRequest fpPage = new PageParamRequest();
            fpPage.setPage(1);
            fpPage.setLimit(5);
            SetMealFloorProRequest fp = new SetMealFloorProRequest();
            fp.setHomeId(homeVo.getId().toString());
            List<HomeProductVo> fpResult = selectPageSetMealFloorPro(fp, fpPage);
            homeVo.setProducts(fpResult);
        }
        return homeVos;
    }

    @Override
    public List<SetMealResponse> selectPageSetMealType(SetMealRequest entity, PageParamRequest page) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        return dao.selectPageSetMealType(entity);
    }

    @Override
    public List<HomeVo> selectPageSetMealFloor(SetMealFloorRequest entity, PageParamRequest page) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<HomeVo> list = dao.selectPageSetMealFloor(entity);
        PageHelper.clearPage();
        for (HomeVo home : list) {
            // 默认展示6个
            PageParamRequest fpPage = new PageParamRequest();
            fpPage.setPage(1);
            fpPage.setLimit(6);
            SetMealFloorProRequest fp = new SetMealFloorProRequest();
            fp.setHomeId(home.getId().toString());
            List<HomeProductVo> fpResult = selectPageSetMealFloorPro(fp, fpPage);
            home.setProducts(CommonPage.restPage(fpResult));
        }
        return list;
    }

    @Override
    public List<HomeProductVo> selectPageSetMealFloorPro(SetMealFloorProRequest entity, PageParamRequest page) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<HomeProductVo> list = homeProductsDao.selectHomeProduct(Integer.parseInt(entity.getHomeId()));
        PageHelper.clearPage();
        Map<Integer, ProductDetailVo> skuDetails = productService.getSkuDetails(list.stream().map(HomeProductVo::getProductId).collect(Collectors.toList()));
        for (HomeProductVo product : list) {
            ProductDetailVo skuDetail = skuDetails.get(product.getProductId());
            product.setProductValue(skuDetail.getProductValue());
            product.setProductAttr(skuDetail.getProductAttr());
            product.setProductInfo(skuDetail.getProductInfo());
        }
        return list;
    }

}
