<?php

namespace VGM\WOBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * EquipmentRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EquipmentRepository extends EntityRepository
{
    public function getAll()
    {
        $qb = $this->createQueryBuilder('e')
        ->addOrderBy('e.code');

        $query = $qb->getQuery();

        return $query->getResult();
    }

    public function getbyId($id)
    {
        if($this->countEqByID($id) == 1){
            $qb = $this->createQueryBuilder('eq')
                ->where('eq.id = :id')
                ->setParameter('id', $id);

            $query = $qb->getQuery();

            return $query->getSingleResult();
        }
        return null;
    }

    public function getbyASDBId($id)
    {
        if($this->countEqByASDBID($id) == 1){
            $qb = $this->createQueryBuilder('eq')
                ->where('eq.asdb_id = :id')
                ->setParameter('id', $id);

            $query = $qb->getQuery();

            return $query->getSingleResult();
        }
        return null;
    }

    public function getCount()
    {
        $qb = $this->createQueryBuilder('e')
            ->select('count(e.id)');

        $query = $qb->getQuery();

        return $query->getSingleScalarResult();
    }

    public function countEqByASDBID($id)
    {
        $qb = $this->createQueryBuilder('eq')
            ->select('count(eq.id)')
            ->where('eq.asdb_id = :id')
            ->setParameter('id', $id);

        $query = $qb->getQuery();

        return $query->getSingleScalarResult();
    }

    public function countEqByID($id)
    {
        $qb = $this->createQueryBuilder('eq')
            ->select('count(eq.id)')
            ->where('eq.id = :id')
            ->setParameter('id', $id);

        $query = $qb->getQuery();

        return $query->getSingleScalarResult();
    }
}
