<?php

namespace OfficeBrain\Bundle\ProductBundle\Entity;

/**
 * ProductCategoryMappingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProductCategoryMappingRepository extends \Doctrine\ORM\EntityRepository
{
	/**
	 * Description : Is For add category Mapping Data
	 * 
	 * @param unknown $categoryId
	 * @param unknown $prodyctId
	 * @param unknown $countryId
	 * @return \OfficeBrain\Bundle\ProductBundle\Entity\ProductCategoryMapping
	 */
	public function add($categoryId,$prodyctId,$countryId){
		$product_category_mapping = new ProductCategoryMapping();
		$product_category_mapping->setCategoryId($categoryId);
		$product_category_mapping->setProductId($prodyctId);
		$product_category_mapping->setCountryId($countryId);
		
		$this->_em->persist($product_category_mapping);
		$this->_em->flush();
		
		return $product_category_mapping;
	}
	/**
	 * Description : Delete Category From Product Id and Country Id
	 * 
	 * @param unknown $productId
	 * @param unknown $countryId
	 */
	public function delete($productId,$countryId = ""){
		$queryBuilder = $this->getEntityManager()->createQueryBuilder();
		$queryBuilder->delete('OfficeBrainProductBundle:ProductCategoryMapping','pcm')
		->where('pcm.productId=:productId')
		->setParameter('productId',$productId);
		if($countryId != ""){
			$queryBuilder->andWhere('pcm.countryId=:countryId')
				->setParameter('countryId',$countryId);
		}
		return $queryBuilder->getQuery()->execute();
	}
	
	/** 
	 * Description : Method For Get Product Category Mapping Data from country and Product Id
	 * @param unknown $productId
	 * @param unknown $countryId
	 */
	public function getProductCategoryMapping($categoryId,$countryId = "") {
		$query = $this->_em->createQueryBuilder()
		->from('OfficeBrainProductBundle:ProductCategoryMapping','pcm')
		->select("pcm.categoryId,pcm.productId,pcm.countryId")
		->where('pcm.categoryId = :category_id')
		->setParameter('category_id', $categoryId);
		if($countryId != ""){
			$query->andWhere('pcm.countryId = :country_id')
			->setParameter('country_id', $countryId);
		}
		
		return $query->getQuery()->getResult();
	}
	
	/**
	 * 
	 * @param unknown $categoryId
	 * @param unknown $productId
	 * @param unknown $countryId
	 */
	public function insertProductCategoryMapping($categoryId, $productId, $countryId){
		
		$productCategoryMappingObj = $this->findOneBy(array('categoryId' => $categoryId, 'productId' => $productId, 'countryId' => $countryId));
		
		$productCategoryMappingObj = ($productCategoryMappingObj) ? $productCategoryMappingObj : new ProductCategoryMapping();
		$productCategoryMappingObj->setCategoryId($categoryId);
		$productCategoryMappingObj->setProductId($productId);
		$productCategoryMappingObj->setCountryId($countryId);
	
		$this->_em->persist($productCategoryMappingObj);
		$this->_em->flush();
	
		return $productCategoryMappingObj;
	}
	
	/**
	 * Description : Select Category From Product Id and Country Id
	 *
	 * @param unknown $productId
	 * @param unknown $countryId
	 */
	public function getCategoryByProductId($productId,$countryId = "" )
	{
		$queryBuilder = $this->getEntityManager()->createQueryBuilder();
		$queryBuilder->select('pcm')
		->from('OfficeBrainProductBundle:ProductCategoryMapping','pcm')
		->where('pcm.productId=:productId')
		->setParameter('productId',$productId);
		if($countryId != "")
		{
			$queryBuilder->andWhere('pcm.countryId=:countryId')->setParameter('countryId',$countryId);
		}
		return $queryBuilder->getQuery()->getArrayResult();
	}
	
	public function setProductCategoryMapping($productCategoryMappingArray,$countryId){
		$this->removeProductCategoryMapping($countryId);
		$em = $this->getEntityManager();
		$batchSize = 20;
		$i = 0;
		foreach ($productCategoryMappingArray as $key => $item) {
			@list($categoryId,$productId) = explode('#',$item);
			$productCategoryMappingObj = new ProductCategoryMapping();
			$productCategoryMappingObj->setCategoryId($categoryId);
			$productCategoryMappingObj->setProductId($productId);
			$productCategoryMappingObj->setCountryId($countryId);
			$em->persist($productCategoryMappingObj);
			$i++;
			if (($i % $batchSize) === 0) {
				$em->flush();
				$em->clear();
			}
		}
		$em->flush();
		$em->clear();
		return true;
	}
	
	private function removeProductCategoryMapping($countryId){
		$queryBuilder = $this->getEntityManager()->createQueryBuilder();
		$queryBuilder->delete('OfficeBrainProductBundle:ProductCategoryMapping','pcm');
		$queryBuilder->andWhere('pcm.countryId=:countryId')
		->setParameter('countryId',$countryId);
		return $queryBuilder->getQuery()->execute();
	}
}
