<?php

namespace OfficeBrain\Bundle\RewardReferralBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * RewardReferralConfigurationRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class RewardReferralConfigurationRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;
    protected $userId;

    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    public function prepareProjectSettingData()
    {
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
    }
    
    public function setData($rewardMaster)
    {
        $this->_em->persist($rewardMaster);
        return $rewardMaster;
    }
    
    /**
     * Commit entity
     */
    public function commit()
    {
        $this->_em->flush();
        return;
    }
    
    public function findConfigurationFromInstance($countryId=null)
    {
        $query=$this->createQueryBuilder('rr')
              ->select('rr')
              ->where('rr.instanceId  = :instanceId')
              ->setParameter(':instanceId', $this->instanceId)
              ->andWhere('rr.instanceType  = :instanceType')
              ->setParameter(':instanceType', $this->instanceType);
        if ($countryId != null) {
            $query->andWhere('rr.countryId  = :countryId')
              ->setParameter(':countryId', $countryId);
        }
        $result=$query->getQuery()
              //->getSQL();
              ->getOneOrNullResult();
        return $result;
    }
    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        $query=$this->createQueryBuilder('s')
                        ->select('s')
                        ->Where('s.instanceId  = :instanceId')
                        ->setParameter(':instanceId', $this->instanceId)
                        ->andWhere('s.id  = :id')
                        ->setParameter(':id', $id)
                        ->getQuery();
        $result =$query->getSingleResult();
        return $result;
    }
}
