<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;

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

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set project settings
     *
     * @param 1: $projectSetting
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to get project settings
     *
     * @param : None
     *
     * @return: project setting array
     *
     * @throws Exception: Null
     */
    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function prepareProjectSettingData()
    {
        $this->projectId = $this->projectSetting['project_id'];
        $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'];
        $this->userId = $this->projectSetting['user_id'];
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Persist Object
     *
     * @param : null
     *
     * @return Object
     *
     * @throws : null
     **/
    public function setData($formObj)
    {
        $this->_em->persist($formObj);
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Flush Object
     *
     * @param : null
     *
     * @throws : null
     **/
    public function flush($entity = NULL)
    {
        $this->_em->flush();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find single result  based on ID
     *
     * @param $id
     *
     * @return : Third Party Service Key Object
     *
     * @throws Exception: Null
     */
    public function findCount($str, $countryId, $userId)
    //public function findCount($str, $countryId)
    {
        return $this->createQueryBuilder('ml')
        ->select('COUNT(ml.id)')
        ->where('ml.thirdPartyServiceKeyId IN (:ids) ')
        ->andwhere('ml.countryId = :countryId')
        ->andWhere('ml.instanceId = :instanceId')
        ->andWhere('ml.createdUid = :createdUid')
        ->setParameter('ids', $str)
        ->setParameter('countryId', $countryId)
        ->setParameter('instanceId', $this->instanceId)
        ->setParameter('createdUid', $userId)
        ->getQuery()
        ->getSingleScalarResult();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find single result  based on ID and Country
     *
     * @param $id
     * @param 2:
     *
     * @return : Third Party Service Key Object
     *
     * @throws Exception: Null
     */
    public function findByCountry($str, $countryId)
    {
        return $this->createQueryBuilder('ml')
        ->select('ml.id,ml.value')
        ->where('ml.thirdPartyServiceKeyId IN (:ids)')
        ->andwhere('ml.countryId = :countryId')
        ->andWhere('ml.instanceId = :instanceId')
        ->setParameter('ids', $str)
        ->setParameter('countryId', $countryId)
        ->setParameter('instanceId', $this->instanceId)
        ->getQuery()//->getSQL();
        ->getResult();
        //echo $str,'---',$this->instanceId,'---',$countryId,'---', $res; die;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find single result  based on ID
     *
     * @param $id
     *
     * @return : Third Party Service Key Object
     *
     * @throws Exception: Null
     */
    public function findByTpskIdAndCountry($id, $countryId)
    {
        $res = $this->createQueryBuilder('tpsm')
        ->select('tpsm')
        ->where('tpsm.thirdPartyServiceKeyId = :id')
        ->andWhere('tpsm.countryId = :cId')
        ->setParameter('id', $id)
        ->setParameter('cId', $countryId)
        ->getQuery()
        ->getResult();

        return $res[0];
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find Id based on Form
     *
     * @param : 1 $tpSettingKeyId
     * @param : 2 $countryId
     *
     * @return : Result
     *
     * @throws Exception: Null
     */
    public function findMethod($tpSettingKeyId, $countryId)
    {
        return $this->createQueryBuilder('tpsm')
        ->select(['tpsm'])
        ->where('tpsk.thirdPartySettingId = :formId')
        ->andWhere('tpsm.countryId= :countryId')
        ->setParameter(':formId', $formId)
        ->setParameter(':countryId', $countryId)
        ->getQuery()
        ->getResult();
    }

    /**
     * @author Employee Id: 4494
     *
     * Description : Find single result  based on ID with countrt Id and User ID
     *
     * @param $id
     *
     * @return : Third Party Service Key Object
     *
     * @throws Exception: Null
     */
    public function findByTpskIdAndCountryAndUserId($id, $countryId, $userId)
    {
    	return $this->createQueryBuilder('tpsm')
			    	->select('tpsm')
			    	->where('tpsm.thirdPartyServiceKeyId = :id')
			    	->andWhere('tpsm.countryId = :countryId')
			    	->andWhere('tpsm.createdUid = :createdUid')
			    	->setParameter(':id', $id)
			    	->setParameter(':countryId', $countryId)
			    	->setParameter(':createdUid', $userId)
			    	->getQuery()
			    	->getOneOrNullResult();
    }
}
