package com.yonge.cooleshow.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.yonge.cooleshow.mbg.mapper.*;
import com.yonge.cooleshow.mbg.model.*;
import com.yonge.cooleshow.portal.dao.HomeDao;
import com.yonge.cooleshow.portal.domain.FlashPromotionProduct;
import com.yonge.cooleshow.portal.domain.HomeContentResult;
import com.yonge.cooleshow.portal.domain.HomeFlashPromotion;
import com.yonge.cooleshow.portal.dto.ProductCategorySmallVo;
import com.yonge.cooleshow.portal.service.HomeService;
import com.yonge.cooleshow.portal.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页内容管理Service实现类
 * Created by macro on 2019/1/28.
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private CmsSubjectMapper subjectMapper;

    @Autowired
    private OmsCartItemMapper cartItemMapper;

    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList());

        // 获取商品类型
        result.setProductCategoryList(getHomeProductCategoryList());

        //获取热销商品
        result.setHotProductList(getHotProductList());

        // 获取商品分类
        result.setProductAttributeCategoryList(getHomeProductAttributeCategory());

        // 购物车数量
        result.setCount(getHomeCartItemCount());
        //获取推荐品牌
        // result.setBrandList(homeDao.getRecommendBrandList(0,6));
        //获取秒杀信息
        // result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        // result.setNewProductList(homeDao.getNewProductList(0,4));

        //获取推荐专题
        // result.setSubjectList(homeDao.getRecommendSubjectList(0,4));
        return result;
    }

    private List<PmsProduct> getHotProductList() {
        PmsProductExample example = new PmsProductExample();
        example.setOrderByClause("sort desc");
        example.createCriteria().andDeleteStatusEqualTo(0)
                .andPublishStatusEqualTo(1)
                .andRecommandStatusEqualTo(1)
                .andVerifyStatusEqualTo(1);
        return productMapper.selectByExample(example);
    }

    private long getHomeCartItemCount() {
        OmsCartItemExample example = new OmsCartItemExample();
        // todo 用户id
        // example.createCriteria().andDeleteStatusEqualTo(0).andMemberIdEqualTo(memberId);
        example.createCriteria().andDeleteStatusEqualTo(0);
        return cartItemMapper.countByExample(example);
    }

    private  List<PmsProductAttributeCategory>  getHomeProductAttributeCategory() {
        PageHelper.startPage(1, 4);
        PmsProductAttributeCategoryExample example = new PmsProductAttributeCategoryExample();
        example.setOrderByClause("id desc");
        List<PmsProductAttributeCategory> pmsProductCategories = productAttributeCategoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(pmsProductCategories)) {
            return new ArrayList<>();
        }
        return pmsProductCategories;
    }


    private List<ProductCategorySmallVo> getHomeProductCategoryList() {
        PageHelper.startPage(1, 4);
        PmsProductCategoryExample example = new PmsProductCategoryExample();
        example.setOrderByClause("sort desc");
        example.createCriteria().andParentIdEqualTo(0L)
                .andShowStatusEqualTo(1)
                .andNavStatusEqualTo(1);
        List<PmsProductCategory> pmsProductCategories = productCategoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(pmsProductCategories)) {
            return new ArrayList<>();
        }
        return pmsProductCategories.stream()
                .map( pmsProductCategory ->  {
                    ProductCategorySmallVo productCategorySmallVo = new ProductCategorySmallVo();
                    BeanUtils.copyProperties(pmsProductCategory,productCategorySmallVo);
                    return productCategorySmallVo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<PmsProduct> recommendProductList(Integer pageSize, Integer pageNum) {
        // TODO: 2019/1/29 暂时默认推荐所有商品
        PageHelper.startPage(pageNum,pageSize);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria()
                .andDeleteStatusEqualTo(0)
                .andPublishStatusEqualTo(1);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<PmsProductCategory> getProductCateList(Long parentId) {
        PmsProductCategoryExample example = new PmsProductCategoryExample();
        example.createCriteria()
                .andShowStatusEqualTo(1)
                .andParentIdEqualTo(parentId);
        example.setOrderByClause("sort desc");
        return productCategoryMapper.selectByExample(example);
    }

    @Override
    public List<CmsSubject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andShowStatusEqualTo(1);
        if(cateId!=null){
            criteria.andCategoryIdEqualTo(cateId);
        }
        return subjectMapper.selectByExample(example);
    }

    @Override
    public List<PmsProduct> hotProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeDao.getHotProductList(offset, pageSize);
    }

    @Override
    public List<PmsProduct> newProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeDao.getNewProductList(offset, pageSize);
    }

    private HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
        Date now = new Date();
        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
        if (flashPromotion != null) {
            //获取当前秒杀场次
            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
            if (flashPromotionSession != null) {
                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
                if(nextSession!=null){
                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
                }
                //获取秒杀商品
                List<FlashPromotionProduct> flashProductList = homeDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
                homeFlashPromotion.setProductList(flashProductList);
            }
        }
        return homeFlashPromotion;
    }

    //获取下一个场次信息
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeGreaterThan(date);
        sessionExample.setOrderByClause("start_time asc");
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }

    private List<SmsHomeAdvertise> getHomeAdvertiseList() {
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andTypeEqualTo(1).andStatusEqualTo(1);
        example.setOrderByClause("sort desc");
        return advertiseMapper.selectByExample(example);
    }

    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
        Date currDate = DateUtil.getDate(date);
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        example.createCriteria()
                .andStatusEqualTo(1)
                .andStartDateLessThanOrEqualTo(currDate)
                .andEndDateGreaterThanOrEqualTo(currDate);
        List<SmsFlashPromotion> flashPromotionList = flashPromotionMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(flashPromotionList)) {
            return flashPromotionList.get(0);
        }
        return null;
    }

    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
        Date currTime = DateUtil.getTime(date);
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeLessThanOrEqualTo(currTime)
                .andEndTimeGreaterThanOrEqualTo(currTime);
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }
}
