package com.ifish.serviceImpl;

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

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

import com.ifish.dao.IfishGoodsDao;
import com.ifish.dto.IfishGoodsDto;
import com.ifish.dto.JsonResult;
import com.ifish.entity.IfishGoods;
import com.ifish.enums.IfishShopPlaceEnum;
import com.ifish.enums.SubDirectoryEnum;
import com.ifish.service.CacheService;
import com.ifish.service.IfishGoodsService;
import com.ifish.util.IfishFilePath;
import com.ifish.util.ResultUtil;

/**
 * 商品管理service
 * @author Administrator
 *
 */
@Service
@Transactional
public class IfishGoodsServiceImpl implements IfishGoodsService {

	@Autowired
	private IfishGoodsDao ifishGoodsDao;
	@Autowired
	private CacheService cacheService;
	
	/**
	 * 根据展示位置获取商品
	 */
	@Override
	public JsonResult<?> getIfishGoodsByGoodsPlace(IfishShopPlaceEnum shopPlaceEnum) {
		//商品信息list
		List<IfishGoods> ifishGoodsList = cacheService.getIfishGoodsList(shopPlaceEnum.getKey());
		//封装返回
		List<IfishGoodsDto> dtoList = new ArrayList<IfishGoodsDto>();
		for (IfishGoods ifishGoods : ifishGoodsList) {
			//商品信息DTO
			IfishGoodsDto dto = new IfishGoodsDto(ifishGoods.getGoodsId(), ifishGoods.getShopName(), ifishGoods.getGoodsName(), 
					ifishGoods.getGoodsLink(), ifishGoods.getOriPrice(), ifishGoods.getPrePrice());
			dto.setIsTaobaoLink(ifishGoods.getIsTaobaoLink());
			dtoList.add(dto);
			//商品图片地址
			String goodsPicture = IfishFilePath.getPath(SubDirectoryEnum.ifishGoods,ifishGoods.getGoodsPicture());
			dto.setGoodsPicture(goodsPicture);
		}
		return ResultUtil.success(dtoList);
	}

	/**
	 * 增加商品点击数
	 */
	@Override
	public JsonResult<?> addClickNum(Integer goodsId) {
		IfishGoods ifishGoods = ifishGoodsDao.get(goodsId);
		if(ifishGoods!=null){
			Integer clickNum = ifishGoods.getClickNum();
			ifishGoods.setClickNum(++clickNum);
			ifishGoodsDao.update(ifishGoods);
		}
		return ResultUtil.success();
	}

}
