package com.ifish.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.ifish.dao.AdInfoDao;
import com.ifish.dao.GoldRuleDao;
import com.ifish.dao.GradeDao;
import com.ifish.dao.GradeRuleDao;
import com.ifish.dao.HardwareTypeDao;
import com.ifish.dao.IfishGoodsDao;
import com.ifish.dao.IfishShopDao;
import com.ifish.dao.InformationDao;
import com.ifish.dao.UeditorDao;
import com.ifish.dao.VenderListDao;
import com.ifish.entity.AdInfo;
import com.ifish.entity.GoldTask;
import com.ifish.entity.Grade;
import com.ifish.entity.GradeTask;
import com.ifish.entity.HardwareType;
import com.ifish.entity.IfishGoods;
import com.ifish.entity.IfishShop;
import com.ifish.entity.Information;
import com.ifish.entity.VenderList;
import com.ifish.enums.BooleanEnum;
import com.ifish.enums.SubDirectoryEnum;
import com.ifish.service.CacheService;
import com.ifish.util.IfishFilePath;

/**
 * 缓存数据service
 * @author Administrator
 *
 */
@Service
@Transactional
public class CacheServiceImpl implements CacheService {

	@Autowired
	private GradeDao gradeDao;
	@Autowired
	private AdInfoDao adInfoDao;
	@Autowired
	private UeditorDao ueditorDao;
	@Autowired
	private GoldRuleDao goldRuleDao;
	@Autowired
	private IfishShopDao ifishShopDao;
	@Autowired
	private GradeRuleDao gradeRuleDao;
	@Autowired
	private IfishGoodsDao ifishGoodsDao;
	@Autowired
	private VenderListDao venderListDao;
	@Autowired
	private InformationDao informationDao;
	@Autowired
	private HardwareTypeDao hardwareTypeDao;

	/**
	 * 根据ID获取硬件类型
	 */
	@Cacheable(value="singleCache",key="#id + #root.methodName")
	@Override
	public HardwareType findHardwareTypeById(String id){
		return hardwareTypeDao.get(id);
	}

	/**
	 * 根据ID查找鱼缸厂
	 */
	@Cacheable(value="singleCache",key="#id + #root.methodName")
	@Override
	public VenderList findVenderListById(String id){
		return venderListDao.get(id);
	}

	/**
	 * 获取等级规则
	 * @return
	 */
	@Cacheable(value="singleCache",key="#id + #root.methodName")
	@Override
	public GradeTask getGradeRule(String id){
		return gradeRuleDao.get(id);
	}

	/**
	 * 获取金币规则
	 * @return
	 */
	@Cacheable(value="singleCache",key="#id + #root.methodName")
	@Override
	public GoldTask getGoldRule(String id){
		return goldRuleDao.get(id);
	}
	/**
	 * 获取等级信息
	 * @return
	 */
	@Cacheable(value="listCache",key="#root.methodName")
	@Override
	public List<Grade> getGradeList(){
		//等级信息
		return gradeDao.findByProperty(Order.asc("gradeNum"));
	}
	/**
	 * 获取金币规则信息
	 * @return
	 */
	@Cacheable(value="listCache",key="#root.methodName")
	@Override
	public List<GoldTask> getGoldRuleList(){
		//金币入账规则
		return goldRuleDao.findByProperty(Order.asc("orderNum"),Restrictions.eq("state", BooleanEnum.YES.getKey()));
	}

	/**
	 * 等级规则信息
	 */
	@Cacheable(value="listCache",key="#root.methodName")
	@Override
	public List<GradeTask> getGradeRuleList() {
		//等级规则信息
		return gradeRuleDao.findByProperty(Order.asc("orderNum"),Restrictions.eq("state", BooleanEnum.YES.getKey()));
	}

	/**
	 * 获取资讯信息
	 * @return
	 */
	@Cacheable(value="informationCache",key="#root.methodName")
	@Override
	public List<JSONObject> getInformation(){
		List<Information> informationList = informationDao.findByCriteria(0,1,Order.desc("infoId"),Restrictions.eq("appShow", BooleanEnum.YES.getKey())).getList();
		List<JSONObject> jsonList = new ArrayList<JSONObject>();
		for (Information information : informationList) {
			JSONObject json = new JSONObject();
			jsonList.add(json);
			json.put("infoId", information.getInfoId());
			json.put("appShow", information.getAppShow());
			if(information.getUeditorId()!=null){
				json.put("link", IfishFilePath.getPath(SubDirectoryEnum.ueditorHtml,information.getLink()));
			}
			else{
				json.put("link", information.getLink());
			}
			json.put("title", information.getTitle());
			json.put("isAddUserId", information.getUeditorType());
			json.put("createTime", information.getCreateTime());
			
		}
		return jsonList;
	}

	/**
	 * 广告位信息
	 */
	@Cacheable(value="listCache",key="#adType + #root.methodName")
	@Override
	public List<AdInfo> getAdInfoList(int adType) {
		return adInfoDao.findByProperty(Order.asc("orderNum"),Restrictions.eq("adType", adType),Restrictions.eq("isShow", BooleanEnum.YES.getKey()));
	}

	/**
	 * 商品信息
	 */
	@Cacheable(value="listCache",key="#goodsPlace + #root.methodName")
	@Override
	public List<IfishGoods> getIfishGoodsList(int goodsPlace) {
		List<IfishGoods> ifishGoodsList = ifishGoodsDao.findByProperty(Order.desc("goodsId"),Restrictions.eq("goodsPlace", goodsPlace),Restrictions.eq("isShow", Integer.valueOf(BooleanEnum.YES.getKey())));
		for (IfishGoods ifishGoods : ifishGoodsList) {
			Integer shopId = ifishGoods.getShopId();
			IfishShop ifishShop = ifishShopDao.get(shopId);
			//店铺名称
			if(ifishShop!=null){
				ifishGoods.setShopName(ifishShop.getShopName());
			}
		}
		return ifishGoodsList;
	}
}
