package com.iamberry.wechat.service.cart;

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

import com.iamberry.wechat.core.entity.product.*;
import com.iamberry.wechat.core.entity.search.SearchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.cart.ShopOrderItemDto;
import com.iamberry.wechat.face.cart.ProductInfoService;
import com.iamberry.wechat.service.StaticCacheMemory;
import com.iamberry.wechat.service.cart.dao.CartDao;
import com.iamberry.wechat.service.cart.dao.ProductInfoDao;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {
	
	@Autowired
	private ProductInfoDao productInfoDao;
	
	@Autowired
	private CartDao cartDao;

	
	public List<Product> selectPageProduct(int pageSize, int pageNO, String productName,Integer typeId) {
		// TODO Auto-generated method stub
		PaperBean bean = new PaperBean();
		if (pageNO == 1) {
			bean.setMinNum((pageNO-1)*pageSize); 
		} else if (pageNO > 1) {
			bean.setMinNum((pageNO-1)*pageSize+1); 
		}
		bean.setMaxNum(pageSize);
		if (null != productName && !"".equals(productName)) {
			bean.setStr(productName);
		}
		if (null != typeId) {
			bean.setOtherInt(typeId);
		}
		return productInfoDao.selectPageProduct(bean);
	}


	public List<ProductFittings> selectPageProductFittings(int pageSize, int pageNO, Integer typeId) {
		// TODO Auto-generated method stub
		PaperBean bean = new PaperBean();
		if (pageNO == 1) {
			bean.setMinNum((pageNO-1)*pageSize);
		} else if (pageNO > 1) {
			bean.setMinNum((pageNO-1)*pageSize+1);
		}
		bean.setMaxNum(pageSize);
		if (null != typeId) {
			bean.setOtherInt(typeId);
		}
		return productInfoDao.selectPageProductFittings(bean);
	}


	
	public Product getProductByProductId(Integer productId) {
		// TODO Auto-generated method stub
		return productInfoDao.getProductByProductId(productId);
	}

	
	public List<Product> getProductByProductName(String productName) {
		// TODO Auto-generated method stub
		return productInfoDao.getProductByProductName(productName);
	}

	public void cacheIndexPageProduct() {
		productInfoDao.selectIndexPageProduct();
	}
	
	public List<Product> selectIndexPageProduct() {
		// TODO Auto-generated method stub
		return productInfoDao.selectIndexPageProduct();
	}

	
	public Integer selectProductCount(Product product) {
		// TODO Auto-generated method stub
		return this.productInfoDao.selectProductCount(product);
	}

	
	public Integer updateProductNumList(String orderId) {
		// TODO Auto-generated method stub
		List<Integer> productIdList = new ArrayList<Integer>();
		if (null == orderId || "".equals(orderId)) {
			return 0;
		}
		List<ShopOrderItemDto> orderItemList =  cartDao.selectShopOrderItem(orderId);
		if (orderItemList == null || orderItemList.size() <= 0) {
			return 0;
		}
		return productInfoDao.updateProductNumList(orderItemList);
	}

	@Override
	public ProductColor getProductColor(ProductColor productColor) {
		return productInfoDao.getProductColor(productColor);
	}

	@Override
	public ProductFittings getProductFittings(ProductFittings productFittings){
		return productInfoDao.getProductFittings(productFittings);
	}

	@Override
	public List<ProductType> selectProductType() {
		return productInfoDao.selectProductType();
	}

	@Override
	public List<Product> selectIndexChoicenessProduct(Integer productChoiceness) {
		return productInfoDao.selectIndexChoicenessProduct(productChoiceness);
	}


	@Override
	public List<ProductChildType> selectProductFittingsType(Integer typeId) {
		return productInfoDao.selectProductFittingsType(typeId);
	}
	@Override
	public Integer insertSearchInfo(SearchInfo searchInfo) {
		return productInfoDao.insertSearchInfo(searchInfo);
	}

	@Override
	public SearchInfo getSearchInfo(SearchInfo searchInfo) {
		return productInfoDao.getSearchInfo(searchInfo);
	}

	@Override
	public List<SearchInfo> selectSearchInfo(SearchInfo searchInfo) {
		return productInfoDao.selectSearchInfo(searchInfo);
	}

	@Override
	public Integer updateSearchInfo(SearchInfo searchInfo) {
		return productInfoDao.updateSearchInfo(searchInfo);
	}

	@Override
	public List<ProductColor> selectColorByPid(Integer productId) {
		return productInfoDao.selectColorByPid(productId);
	}

	@Override
	public Product selectProductByColorId(Integer colorId) {
		return productInfoDao.selectProductByColorId(colorId);
	}

}
