Esempio n. 1
0
 /**
  * 保存一条记录,注意:此处会对应增加或减少用户的云豆数!!
  * @param $userId
  * @param $bitcoin
  * @param $usageModeId
  * @param $type
  * @param $fromUserId
  * @throws Exception
  * @throws \Exception
  */
 public static function saveRecord($userId, $bitcoin, $usageModeId, $type, $fromUserId = null)
 {
     $record = new IncomeConsume();
     $record->userId = $userId;
     $record->bitcoin = $bitcoin;
     $record->usageModeId = $usageModeId;
     $record->type = $type;
     $record->fromUserId = $fromUserId;
     $record->createDate = DateFunctions::getCurrentDate();
     if (!$record->save()) {
         throw new Exception("IncomeConsume save error");
     }
     $user = Users::findOne($userId);
     if ($type == IncomeConsume::TYPE_INCOME) {
         $user->bitcoin += $bitcoin;
     } elseif ($type == IncomeConsume::TYPE_CONSUME) {
         if ($user->bitcoin < $bitcoin) {
             throw new Exception("IncomeConsume user bitcoin less than need error");
         } else {
             $user->bitcoin -= $bitcoin;
         }
     }
     if (!$user->save()) {
         throw new Exception("IncomeConsume user update error");
     }
 }