示例#1
0
 public static function &getInstance()
 {
     if (self::$_instance === null) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
示例#2
0
 /**
  * 购买房源
  * @param $userId
  * @param $propId
  * @param $source
  * @return array
  */
 public function addUserProp($userId, $propId, $source)
 {
     //$t1 = microtime(true);
     $result = array('status' => false, 'errcode' => '', 'msg' => '');
     if (empty($userId) || empty($propId) || empty($source)) {
         $result['errcode'] = Const_APIStatus::E_UNKNOWN;
         $result['msg'] = '参数不正确';
         return $result;
     }
     /*
      * 1、验证经纪人是否已经购买
      */
     $tmpUserPropData = $this->getUserPropByUserIdAndPropId($userId, $propId);
     if (!empty($tmpUserPropData)) {
         $result['errcode'] = Const_APIStatus::E_FYK_PROP_ACTION_HAVE_TO_BUY;
         $result['msg'] = '该房源已购买';
         return $result;
     }
     /*
      * 2、验证房源是否存在
      */
     $propData = $this->getPropInfo($propId, array(Const_Fyk::PROPSTATUS_SELLING, Const_Fyk::PROPSTATUS_NOSELL));
     if (empty($propData)) {
         $result['errcode'] = Const_APIStatus::E_FYK_PROP_INFO_IS_NULL;
         $result['msg'] = '该房源不存在';
         return $result;
     }
     // 判断是否是测试账户
     if (!empty($_SERVER['PG_TEST'])) {
         //PG
         $testCommIds = APF::get_instance()->get_config('fyk_pg_test_commIds');
         $tmpTestUserIds = APF::get_instance()->get_config('fyk_pg_test_userIds');
     } else {
         $testCommIds = APF::get_instance()->get_config('fyk_ga_test_commIds');
         $tmpTestUserIds = APF::get_instance()->get_config('fyk_ga_test_userIds');
     }
     $testUserIds = array_values($tmpTestUserIds);
     if (in_array($userId, $testUserIds) && !in_array($propData[$propId]['commId'], $testCommIds)) {
         $result['errcode'] = Const_APIStatus::E_UNKNOWN;
         $result['msg'] = '测试账户,不能购买其他房源';
         return $result;
     } else {
         if (!in_array($userId, $testUserIds) && in_array($propData[$propId]['commId'], $testCommIds)) {
             $result['errcode'] = Const_APIStatus::E_UNKNOWN;
             $result['msg'] = '测试房源,您不能购买';
             return $result;
         }
     }
     //$t2 =  microtime(true);
     //echo '1.本地耗时'.round($t2-$t1,3)."<br>";
     $cityId = empty($propData[$propId]['cityId']) ? 0 : $propData[$propId]['cityId'];
     $userCityId = Bll_Broker_User::getCityIdByUserId($userId);
     if (empty($cityId) || empty($userCityId) || $cityId != $userCityId) {
         $result['errcode'] = Const_APIStatus::E_UNKNOWN;
         $result['msg'] = '经纪人城市id信息异常';
         return $result;
     }
     /*
      * 3、验证余额是否足够
      */
     $userAccoutId = Bll_Fyk_Payment_User::getInstance()->getUserAccountId($userId);
     if (empty($userAccoutId)) {
         $result['errcode'] = Const_APIStatus::E_FYK_PROP_ACTION_ACCOUNT_IS_NULL;
         $result['msg'] = '支付账号不存在';
         return $result;
     }
     //$t3 =  microtime(true);
     //echo '2.本地耗时'.round($t3-$t2,3)."<br>";
     $balanceResult = Bll_Fyk_Payment_Amount::getInstance()->getBalance($userId, $userAccoutId);
     $balance = $balanceResult['status'] ? $balanceResult['balance'] : 0;
     //$t4 =  microtime(true);
     //echo '3.查询余额耗时'.round($t4-$t3,3)."<br>";
     // 获取购买价格
     $startDate = date("Ymd");
     $price = Bll_Fyk_Payment_Price::getInstance()->getPropUnitPrice($cityId, $startDate, Model_Fyk_CityPriceConfig::TYPE_BUY);
     if (empty($price)) {
         $result['errcode'] = Const_APIStatus::E_FYK_PROP_ACTION_PRICE_ANOMALIES;
         $result['msg'] = '支付价格异常';
         return $result;
     }
     if ($balance < $price) {
         $result['errcode'] = Const_APIStatus::E_FYK_PROP_ACTION_LACK_OF_BALANCE;
         $result['msg'] = '余额不足';
         return $result;
     }
     /*
      * 4、添加订单
      */
     $addOrderData = array('accountId' => $userAccoutId, 'userId' => $userId, 'propId' => $propId, 'price' => $price, 'status' => Model_Fyk_UserOrder::ORDER_STATUS_NODEAL, 'requestId' => 0, 'source' => $source, 'createTime' => time());
     $orderId = Bll_Fyk_Order_Manage::getInstance()->insertData($addOrderData);
     // $t5 = microtime(true);
     // echo '5.本地耗时'.round($t5-$t4,3)."<br>";
     /*
      * 5.扣费
      */
     $note = "购买房源扣款,房源id为{$propId}";
     $requestId = Bll_Fyk_Payment_Common::getInstance()->createRequsetId("order", $orderId);
     $payResult = Bll_Fyk_Payment_Amount::getInstance()->chargeMoney($userId, $price, $userAccountId = "", $note, $requestId);
     //$t6 = microtime(true);
     //echo '6.扣费耗时'.round($t6-$t5,3)."<br>";
     if ($payResult['status'] == false) {
         // 更新订单状态支付失败
         $updateOrderArr = array("status" => Model_Fyk_UserOrder::ORDER_STATUS_PAYFAIL, "requestId" => $requestId);
         Bll_Fyk_Order_Manage::getInstance()->updateData(array("id" => $orderId), $updateOrderArr);
         $result['errcode'] = Const_APIStatus::E_FYK_PROP_ACTION_PAY_FAILURE;
         $result['msg'] = '支付失败';
     } else {
         // 记录到账户流水表
         $addBalanceLogArr = array('userId' => $userId, 'accountId' => $userAccoutId, 'propId' => $propId, 'listId' => $orderId, 'price' => $price, 'realPrice' => $price, 'amount' => $payResult['balance'], 'requestId' => $requestId, 'type' => Const_Fyk::USER_BUY_PROP, 'createTime' => time());
         Bll_Fyk_Payment_Log::getInstance()->insertBalanceLog($addBalanceLogArr);
         // 更新订单状态支付成功
         $updateOrderArr = array("status" => Model_Fyk_UserOrder::ORDER_STATUS_PAYSUCCESS, "requestId" => $requestId);
         Bll_Fyk_Order_Manage::getInstance()->updateData(array("id" => $orderId), $updateOrderArr);
         // 插入我的房源列表
         $addUserPropArr = array('cityId' => $cityId, 'userId' => $userId, 'propId' => $propId, 'source' => $source, 'status' => !empty($propData[$propId]['status']) ? $propData[$propId]['status'] : Const_Fyk::PROPSTATUS_DELETE, 'comeFrom' => Model_Fyk_UserProps::COME_FROM_BUY, 'createTime' => time());
         Model_Fyk_UserProps::getInstance()->insertData($addUserPropArr);
         Bll_Fyk_UserUnReadData::getInstance()->updateUnReadNum($userId, Model_Fyk_UserUnread::TYTE_AMOUNT);
         $result['status'] = true;
     }
     // 记录到扣费详细日志
     //$t7 = microtime(true);
     //echo '7.耗时'.round($t7-$t6,3)."<br>";
     //echo '8.总耗时'.round($t7-$t1,3).'秒';exit;
     return $result;
 }