/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.wechat.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wechat.common.Constants;
import com.wechat.common.utils.DateUtils;
import com.wechat.common.utils.StringsUtils;
import com.wechat.dao.MdeGoodsDao;
import com.wechat.dao.MdeOrdersDao;
import com.wechat.dao.RedisGoodsSaleDao;
import com.wechat.global.base.BaseService;
import com.wechat.model.dbEntity.MdeGoods;
import com.wechat.model.dto.GoodsDetailDto;
import com.wechat.model.dto.GoodsListDto;
import com.wechat.model.dto.UpTimeGroupDto;


/**
 * 单表生成Service
 * 
 * @author jadyn.wu
 * @version 2019-08-28
 */
@Service
public class MdeGoodsService extends BaseService{
	
	@Autowired
	private MdeGoodsDao mdeGoodsDao;
	
	@Autowired
	private RedisGoodsSaleDao goodsRedisDao;
	
	@Autowired
	private MdeOrdersDao mdeOrdersDao;
	
	/**
	 * 获取上架时间分组
	 * @return
	 */
	public List<UpTimeGroupDto> getUpTimeGroup() {
		//获取上架时间分组
		List<UpTimeGroupDto> upTimeGroupList = mdeGoodsDao.getUpTimeGroup();
		return upTimeGroupList;
	}
	
	/**
	 * 获取显示的上架时间
	 * @return
	 */
	public UpTimeGroupDto getUpTimeShow() {
		// 获取显示的上架时间
		UpTimeGroupDto showTime = mdeGoodsDao.getUpTimeShow(new Date());
		return showTime;
	}

	/**
	 * 根据上架时间和上架状态获取商品列表
	 * 
	 * @param upTime
	 * @param onSale
	 * @return
	 */
	public List<GoodsListDto> getGoodsListByUpTime(Date upTime, String onSale, String lang) {
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("upTime", upTime);
		map.put("onSale", onSale);
		
		Date date = new Date();
		if(upTime == null && "1".equals(onSale))
		{
			//热销数据
			date = DateUtils.getFirstTimeOfDay(date);
		}
		else if(upTime == null && "2".equals(onSale))
		{
			//预热数据
			date = DateUtils.getFirstTimeOfDay(DateUtils.addDay(date, 1));
		}
		map.put("date", date);
		map.put("lang", lang);
		List<GoodsListDto> goodsList = mdeGoodsDao.getGoodsListByUpTime(map);
		
		return goodsList;
	}
	
	/**
	 * 获取商品详情
	 * 
	 * @return
	 */
	public GoodsDetailDto getGoodsInfo(int goodsId, String lang) {
		if(StringsUtils.isEmpty(lang))
		{
			lang= Constants.LANG_CN;
		}
		GoodsDetailDto goodsDetail = mdeGoodsDao.getGoodsDetail(goodsId,lang);
		//设置销量
		goodsDetail.setSale(getGoodsSale(goodsId));
		
		return goodsDetail;
	}
	
	/**
	 * 获取商品全部详情
	 * 
	 * @return
	 */
	public MdeGoods getGoodsAllInfo(int goodsId) {

		MdeGoods goodsDetail = mdeGoodsDao.getGoodsAllInfo(goodsId);
		
		return goodsDetail;
	}
	
	
	/**
	 * 设置商品销量
	 * 
	 * @return
	 */
	public void setGoodsSale(List<GoodsListDto> goodsList) {
		for(GoodsListDto dto: goodsList)
		{
			dto.setSale(getGoodsSale(dto.getGoodsId()));
		}
	}
	
	/**
	 * 获取商品销量
	 * 
	 * @return
	 */
	public Integer getGoodsSale(int goodsId) {
		//商品销量
		Integer saleNum = null;
		String saleStr = "";
		
		//从缓存中获取商品销量
		try{
			saleStr = goodsRedisDao.selectSaleNum(goodsId);
			if(StringsUtils.isEmpty(saleStr))
			{
				//缓存为空的情况下获取数据库数据
				saleNum = mdeOrdersDao.getGoodsSaleNums(goodsId);
				//存入缓存 并设置30天过期
				goodsRedisDao.insertSaleNum(goodsId, saleNum);
			} else {
				saleNum = Integer.valueOf(saleStr);
			}
		} catch (Exception e) {
			log.error("***********redis获取商品库存错误***********",e);
			//缓存出错的情况下获取数据库数据
			saleNum = mdeOrdersDao.getGoodsSaleNums(goodsId);
		}

		return saleNum;
	}
	

}