protected function buildCardKey($cardKeyData)
 {
     $cardKey = new CardKey();
     $cardKey->setCardKey($cardKeyData['cardKey']);
     $cardKey->setCardKeyClassId($cardKeyData['cardKeyClassId']);
     $cardKey->setStatus($cardKeyData['status']);
     $cardKey->setCreatedTime($cardKeyData['createdTime']);
     return $cardKey;
 }
 public function userUseCardKeyAjaxAction(Request $request, Application $app)
 {
     $userCardKey = $request->query->get('cardKey');
     $userId = $app['session']->get('userId');
     $cardKey = new CardKey();
     $cardKey->setCardKey($userCardKey);
     $cardKeyInfo = $app['repository.cardKey']->findById($cardKey);
     if (!$cardKeyInfo) {
         $result = array('result' => 0, 'msg' => '卡密错误,请重新输入');
         return JsonResponse::create($result);
     }
     $cardKeyStatus = $cardKeyInfo->getStatus();
     if ($cardKeyStatus == -1) {
         $result = array('result' => -7, 'msg' => '卡密已经被删除');
         return JsonResponse::create($result);
     }
     if ($cardKeyStatus == 3) {
         $result = array('result' => -8, 'msg' => '卡密已经被兑换');
         return JsonResponse::create($result);
     }
     $cardKeyClass = new CardKeyClass();
     $cardKeyClass->setId($cardKeyInfo->getCardKeyClassId());
     $cardKeyClassInfo = $app['repository.cardKeyClass']->findById($cardKeyClass);
     if (!$cardKeyClassInfo) {
         $result = array('result' => -1, 'msg' => '卡密类型bug,请联系管理员');
         return JsonResponse::create($result);
     }
     $cardKeyClassType = $cardKeyClassInfo->getIsVipOrInt();
     $cardKeyClassNum = $cardKeyClassInfo->getNumber();
     $nowTime = time();
     if ($cardKeyClassType == 1) {
         $user = new User();
         $user->setId($userId);
         $userInfo = $app['repository.user']->findById($user);
         if ($userInfo->getVipEndTime() < $nowTime) {
             $user->setVipStartTime($nowTime);
             $user->setVipEndTime($nowTime + $cardKeyClassNum * 24 * 60 * 60);
         } else {
             $user->setVipStartTime($userInfo->getVipStartTime());
             $user->setVipEndTime($userInfo->getVipEndTime() + $cardKeyClassNum * 24 * 60 * 60);
         }
         $resultChangeVipTime = $app['repository.user']->changeVipTime($user);
         if ($resultChangeVipTime) {
             $cardKey = new CardKey();
             $cardKey->setStatus(3);
             $cardKey->setCardKey($userCardKey);
             $resultEditStatus = $app['repository.cardKey']->editStatus($cardKey);
             if ($resultEditStatus) {
                 $user = new User();
                 $user->setId($userId);
                 $user->setIsVip(1);
                 if ($userInfo->getIsVip() == 0) {
                     $resultChangeIsVip = $app['repository.user']->changeIsVip($user);
                 } else {
                     $resultChangeIsVip = 1;
                 }
                 if ($resultChangeIsVip) {
                     $result = array('result' => 1, 'msg' => '兑换成功');
                     return JsonResponse::create($result);
                 } else {
                     $result = array('result' => -6, 'msg' => '数据库错误,请联系管理员');
                     return JsonResponse::create($result);
                 }
             } else {
                 $result = array('result' => -3, 'msg' => '数据库错误,请联系管理员');
                 return JsonResponse::create($result);
             }
         } else {
             $result = array('result' => -2, 'msg' => '数据库错误,请联系管理员');
             return JsonResponse::create($result);
         }
     } else {
         if ($cardKeyClassType == 2) {
             $user = new User();
             $user->setId($userId);
             $userInfo = $app['repository.user']->findById($user);
             $user->setIntegration($userInfo->getIntegration() + $cardKeyClassNum);
             $user->setId($userId);
             $resultChangeIntegration = $app['repository.user']->changeIntegration($user);
             if ($resultChangeIntegration) {
                 $cardKey = new CardKey();
                 $cardKey->setStatus(3);
                 $cardKey->setCardKey($userCardKey);
                 $resultEditStatus = $app['repository.cardKey']->editStatus($cardKey);
                 if ($resultEditStatus) {
                     $result = array('result' => 1, 'msg' => '兑换成功');
                     return JsonResponse::create($result);
                 } else {
                     $result = array('result' => -5, 'msg' => '数据库错误,请联系管理员');
                     return JsonResponse::create($result);
                 }
             } else {
                 $result = array('result' => -4, 'msg' => '数据库错误,请联系管理员');
                 return JsonResponse::create($result);
             }
         }
     }
 }