/**
  * @param ProductCategory $TargetProductCategory
  * @param $position
  * @return bool
  * @throws \Doctrine\DBAL\ConnectionException
  */
 public function moveRank(ProductCategory $TargetProductCategory, $position)
 {
     $repos = $this->_em->getRepository('\\Eccube\\Entity\\ProductCategory');
     $this->_em->getConnection()->beginTransaction();
     try {
         $oldRank = $TargetProductCategory->getRank();
         // 最大値取得
         $qb = $repos->createQueryBuilder('pc');
         $max = $qb->select($qb->expr()->max('pc.rank'))->where($qb->expr()->eq('pc.category_id', $TargetProductCategory->getCategoryId()))->getQuery()->getSingleScalarResult();
         $position = $max - ($position - 1);
         $position = max(1, $position);
         $TargetProductCategory->setRank($position);
         $status = true;
         if ($position != $oldRank) {
             // 他のItemのランクを調整する
             if ($position < $oldRank) {
                 // down
                 $this->_em->createQueryBuilder()->update('\\Eccube\\Entity\\ProductCategory', 'pc')->set('pc.rank', 'pc.rank + 1')->where('pc.rank <= :oldRank AND pc.rank >= :rank AND pc.category_id = :category_id AND pc.product_id != :product_id')->setParameter('oldRank', $oldRank)->setParameter('rank', $position)->setParameter('category_id', $TargetProductCategory->getCategoryId())->setParameter('product_id', $TargetProductCategory->getProductId())->getQuery()->execute();
             } else {
                 // up
                 $this->_em->createQueryBuilder()->update('\\Eccube\\Entity\\ProductCategory', 'pc')->set('pc.rank', 'pc.rank - 1')->where('pc.rank >= :oldRank AND pc.rank <= :rank AND pc.category_id = :category_id AND pc.product_id != :product_id')->setParameter('oldRank', $oldRank)->setParameter('rank', $position)->setParameter('category_id', $TargetProductCategory->getCategoryId())->setParameter('product_id', $TargetProductCategory->getProductId())->getQuery()->execute();
             }
             $this->_em->persist($TargetProductCategory);
             $this->_em->flush();
         }
         $this->_em->getConnection()->commit();
         return $status;
     } catch (\Exception $e) {
         $this->_em->getConnection()->rollback();
         $this->_em->close();
         $this->app->log($e);
     }
     return false;
 }