package com.hwrj.cloud.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.common.exception.ApiException;
import com.hwrj.cloud.portal.dao.PmsProductDao;
import com.hwrj.cloud.portal.domain.*;
import com.hwrj.cloud.portal.mapper.*;
import com.hwrj.cloud.portal.model.*;
import com.hwrj.cloud.portal.dao.HomeDao;
import com.hwrj.cloud.portal.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.Oneway;
import java.util.Date;
import java.util.List;

/**
 * 首页内容管理Service实现类
 * Created by macro on 2019/1/28.
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeDao homeDao;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private CmsSubjectMapper subjectMapper;

    @Autowired
    private PmsProductDao pmsProductDao;
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private UmsRollBuyMapper umsRollBuyMapper;

    @Autowired
    private CouponService couponService;

    @Autowired
    private UmsCompanyInfoService umsCompanyInfoService;

    @Autowired
    private OmsOrderService omsOrderService;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        List<SmsHomeAdvertise> smsHomeAdvertise = couponService.getHomeAdvertiseList();
        result.setAdvertiseList(smsHomeAdvertise);

        /**
         * 获取弹窗广告
         */
        List<SmsHomeAdvertise> advertise = couponService.advertise();
        if (advertise != null&&advertise.size()>0){
            result.setPromote(advertise);
        }
//        result.setAdvertiseList(couponFeign.getHomeAdvertiseList().getData());
        //获取推荐品牌
//        result.setBrandList(homeDao.getRecommendBrandList(0,6));
        //获取秒杀信息
//        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        result.setNewProductList(homeDao.getNewProductList(0, 4));
        //获取人气推荐
        result.setHotProductList(homeDao.getHotProductList(0, 4));
        List<UmsCompanyInfoDto> infoDtos = umsCompanyInfoService.searchCompanyInfo(null, 1, 5);
        result.setCompanyInfos(infoDtos);

        //获取推荐专题
        List<CmsSubject> cmsSubjects = subjectService.getRecommendSubjectList(0, 4);
        result.setSubjectList(cmsSubjects);
//        result.setSubjectList(subjectFeign.getRecommendSubjectList(0, 4).getData());

        //获取个人订单
        List<OmsOrderDetail> order = omsOrderService.getOwnNoReciveOrder();
        result.setOwnOrderDetails(order);


        return result;
    }

    @Override
    public List<PmsProduct> recommendProductList(Integer pageSize, Integer pageNum) {
        // TODO:  暂时默认推荐所有商品
        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);
        criteria.andDelFlagEqualTo(0);
        example.setOrderByClause("sort desc");
        if (cateId != null) {
            criteria.andCategoryIdEqualTo(cateId);
        }
        return subjectMapper.selectByExample(example);
    }

    @Override
    public List<PmsProduct> getSubjectProductList(Long subjectId, Integer pageSize, Integer pageNum) {
        if (subjectId == null){
            throw  new ApiException("非法操作");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<PmsProduct> list = pmsProductDao.subjectProduct(subjectId);

        return list;
    }

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

    @Override
    public List<PmsProduct> hotProductPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return homeDao.getHotProductPage();
    }

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

    @Override
    public List<UmsRollBuy> rollBuy(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        UmsRollBuyExample example = new UmsRollBuyExample();

        return umsRollBuyMapper.selectByExample(example);
    }

    private HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
        Date now = new Date();
        SmsFlashPromotion flashPromotion = couponService.getFlashPromotion(now);
//        SmsFlashPromotion flashPromotion = couponFeign.getFlashPromotion(now).getData();
        if (flashPromotion != null) {
            //获取当前秒杀场次
            SmsFlashPromotionSession flashPromotionSession = couponService.getFlashPromotionSession(now);
//            SmsFlashPromotionSession flashPromotionSession = couponFeign.getFlashPromotionSession(now).getData();
            if (flashPromotionSession != null) {
                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextSession = couponService.getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
//                SmsFlashPromotionSession nextSession = couponFeign.getNextFlashPromotionSession(homeFlashPromotion.getStartTime()).getData();
                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;
    }


}
