Пример #1
0
 public function newResourcePriceAction()
 {
     $resourceNum = (int) $this->get('resourceNum');
     $schoolId = (int) $this->get('schoolId');
     $resourcePrice = [];
     for ($i = 0; $i <= $resourceNum; $i++) {
         $priceNotFree = $this->get('priceType' . $i);
         //print_r('priceType'.$i."notFree:".$priceNotFree." ");
         $price = $this->get('price' . $i, 0);
         //print_r("price:".$price." ");
         $resourceId = (int) substr($priceNotFree, 1);
         $notFree = (int) substr($priceNotFree, 0, 1);
         array_push($resourcePrice, array("resourceId" => $resourceId, "notFree" => $notFree, "price" => $price));
     }
     //print_r($resourcePrice);
     $tradeModel = new TradeModel();
     $result = $tradeModel->newResourcePrice($resourcePrice, $schoolId);
     $this->redirect("getSchoolPrice?schoolId=" . $schoolId);
 }
Пример #2
0
 /**
  * @desc 生成出可以执行的交易,理论上在一次交易判断中同一个账号最多只能创建一个订单
  */
 private function generateTrade()
 {
     $available_accounts = $this->accounts;
     // 满足多市场组合算法
     $trades = array();
     while (count($available_accounts) > 0) {
         // 从账号组总弹出一个账号
         $account1 = array_shift($available_accounts);
         // 循环剩下的账号与其匹配
         foreach ($available_accounts as $account2) {
             foreach ($account1->actions as $action) {
                 // 第一个账号可以卖同时第二个市场也可以买
                 if ($action === 'sell' && in_array('buy', $account2->actions)) {
                     $orders = array(new OrderModel($account1, 'sell', $account1->price['buy'], $this->config->trans_unit), new OrderModel($account2, 'buy', $account2->price['sell'], $this->config->trans_unit));
                     $trade = new TradeModel($orders);
                     Logger::Log('Generate 1 trade: ' . $trade->__toString());
                     $trades[] = $trade;
                 }
                 // 第一个账号可以买同时第二个市场也可以卖
                 if ($action === 'buy' && in_array('sell', $account2->actions)) {
                     $orders = array(new OrderModel($account2, 'sell', $account2->price['buy'], $this->config->trans_unit), new OrderModel($account1, 'buy', $account1->price['sell'], $this->config->trans_unit));
                     $trade = new TradeModel($orders);
                     Logger::Log('Generate 1 trade: ' . $trade->__toString());
                     $trades[] = $trade;
                 }
             }
         }
     }
     // sort trade
     for ($i = 0; $i < count($trades); ++$i) {
         for ($j = 0; $j < $i; ++$j) {
             if ($trades[$j]->getSpread() < $trades[$j + 1]->getSpread()) {
                 $temp = $trades[$j];
                 $trades[$j] = $trades[$j + 1];
                 $trades[$j + 1] = $temp;
             }
         }
     }
     return $trades;
 }
Пример #3
0
 function accountAction()
 {
     $tradeModel = new TradeModel();
     $tradeModel->testAccount();
 }
Пример #4
0
    parse_str($body, $data);
    //die(var_dump($data));
    $buyer = $data['buyer'];
    $tokers = explode("-", $buyer);
    $buyer = $tokers[1];
    $seller = $data['seller'];
    $tokers = explode("-", $seller);
    $seller = $tokers[1];
    $enteredBy = $data['enteredBy'];
    $tokers = explode("-", $enteredBy);
    $enteredBy = $tokers[1];
    $description = $data['description'];
    $categoryId = $data['category'];
    $amount = $data['amount'];
    $dateAdded = $data['dateAdded'];
    $trade = new TradeModel();
    $trade->setDateAdded($dateAdded);
    $trade->setDescription($description);
    $trade->setAmount($amount);
    $trade->setCategoryId($categoryId);
    $trade->setSellerUid($seller);
    $trade->setBuyerUid($buyer);
    $trade->setEnteredByUid($enteredBy);
    //die(print_r($trade));
    $cra = new CesRestAPI();
    print $cra->insertTradeSQL($trade);
})->setName('trades put');
//Recommend all
$app->get('/recommend/all', function () {
    $cra = new CesRestAPI();
    print $cra->getAllRecommendsJSON();
Пример #5
0
 function getOrderAction()
 {
     $userId = (int) $this->get('userId', 0);
     $mobile = $this->get('mobile', "");
     $status = (int) $this->get('status', -1);
     $payType = (int) $this->get('payType', 0);
     $previousId = (int) $this->get('previousId', 0);
     $pageSize = (int) $this->get('pageSize', 20);
     $date = $this->get('date', "");
     $page = (int) $this->get('page', 1);
     $startTime = 0;
     $endTime = 0;
     if ($date) {
         $startTime = strtotime(substr($date, 0, 10));
         $endTime = strtotime(substr($date, 13, 10));
     }
     /*        print_r($date);
             print_r(substr($date,0,10)." - ".substr($date,13,10));
             print_r($startTime." - ".$endTime);*/
     $accountModel = new AccountModel();
     $order = $accountModel->getOrder($userId, $mobile, $startTime, $endTime, $status, $payType, $previousId, $page, $pageSize);
     $orderList = $order['orderList'];
     $userModel = new UserModel();
     $schoolModel = new SchoolModel();
     $tradeModel = new TradeModel();
     //对orderList进行处理
     foreach ($orderList as $k => $value) {
         $orderList[$k]['mobile'] = $userModel->getUserName($value['userId'])['mobile'];
         switch ($value['payType']) {
             case Common_Config::UDO_PAYTYPE_COIN:
                 $orderList[$k]['payType'] = "U币购买";
                 break;
             case Common_Config::UDO_PAYTYPE_CREDIT:
                 $orderList[$k]['payType'] = "学分购买";
                 break;
             case Common_Config::UDO_PAYTYPE_RECHARGE:
                 $orderList[$k]['payType'] = "U币充值";
                 break;
         }
         switch ($value['status']) {
             case Common_Config::ORDER_SUCCESS:
                 $orderList[$k]['status'] = "支付成功";
                 break;
             case Common_Config::ORDER_NOT_PAY:
                 $orderList[$k]['status'] = "尚未支付";
                 break;
             case Common_Config::ORDER_FAIL:
                 $orderList[$k]['status'] = "订单支付失败";
                 break;
             case Common_Config::ORDER_CLOSED:
                 $orderList[$k]['status'] = "订单关闭";
                 break;
         }
         $orderList[$k]['createTime'] = date('Y-m-d H:i:s', $value['createTime']);
         //为order中的每一项赋予序号,便于在模板中的foreach给class赋值
         $orderList[$k]['no'] = $k;
         //处理resource
         //resource取出来是序列化的字符串,首先先反序列化
         if ($value['resource']) {
             $courseCount = 0;
             //$value['resource'] = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $value['resource']);
             //print_r($value['resource']);
             $orderList[$k]['resource'] = json_decode($value['resource'], true);
             $schoolInfo = [];
             //print_r($orderList[$k]['resource']);
             foreach ($orderList[$k]['resource'] as $v => $val) {
                 if ($val['resourceId'] == 1) {
                     continue;
                 }
                 $orderList[$k]['resource'][$v]['resourceName'] = $schoolModel->getSingleCourse($val['resourceId'])['name'];
                 $courseCount++;
                 if (!$schoolInfo) {
                     $schoolInfo = $schoolModel->getSchoolByCourse($val['resourceId']);
                 }
             }
             $orderList[$k]['courseCount'] = $courseCount;
             $orderList[$k]['schoolName'] = $schoolInfo['customer_name'];
             $orderList[$k]['schoolTitle'] = $schoolInfo['customer_title'];
         }
     }
     /*
      * 计算总页数,确定页码显示.页码显示需要的参数:
      */
     $pageNumber = ceil($order['orderCount'] / $pageSize);
     //如果总页数比10小或当前页比6小,那么分页变量是1到分页总数
     if ($pageNumber <= 10) {
         $pagination = range(1, $pageNumber);
     } elseif ($page <= 6 && $pageNumber > 10) {
         $pagination = range(1, 10);
     } else {
         $pagination = range($page - 5, $page + 4);
     }
     /*
      * 初始化筛选参数
      */
     $initFilter = array("date" => $date, "status" => $status, "payType" => $payType, "mobile" => $mobile, "userId" => $userId, "page" => $page, "pageNumber" => $pageNumber, "orderCount" => $order['orderCount']);
     $this->assign('orderList', $orderList);
     $this->assign('status', $tradeModel->getOrderStatus());
     $this->assign('payType', $tradeModel->getOrderPaytype());
     $this->assign('init', $initFilter);
     $this->assign('pagination', $pagination);
 }
Пример #6
0
 function newOrder($ssotoken, $uid, $schoolId, $courseCount = 0, $payType, $resource = [], $coinId = 0, $amount, $platform = 0, $couponId = 0, $couponAmt = 0)
 {
     $tblOrder = new DB_Udo_Order();
     $tblSchoolPrice = new DB_Udo_SchoolPrice();
     $tblResource = new DB_Sso_Resource();
     $tblCoinInfo = new DB_Udo_CoinInfo();
     $tblBought = new DB_Udo_UserBought();
     $tradeModel = new TradeModel();
     $correct = 0;
     $schoolPrice = $tblSchoolPrice->scalar("priceType,price", "where resourceId = {$schoolId}");
     $balance = $this->getSsoBalance($ssotoken);
     $userModel = new UserModel();
     $mobile = $userModel->getUserName($uid)['mobile'];
     //生成订单前,首先判断资源定价信息是否有误
     if ($payType == Common_Config::UDO_PAYTYPE_COIN || $payType == Common_Config::UDO_PAYTYPE_CREDIT) {
         //如果是频道类型,首先获取频道的定价类型和定价
         //根据用户提交过来的参数并不知道是否全部购买了课程
         //$resource 获取所有非免费课程
         $charge = $tblResource->fetchAll("id", "where entrance_id = {$schoolId} and type = 6 and price_type <> 3 and enabled = 1");
         $totalPrice = 0;
         foreach ($resource as $k => $val) {
             if ($val['resourceType'] == 1) {
                 continue;
             }
             $resourcePrice = $tblResource->scalar("id,type,entrance_id,price_type,cur_price", "where id = {$val['resourceId']}");
             $totalPrice += $resourcePrice['cur_price'];
             //print_r($resourcePrice['price_type']);
             if ($payType == Common_Config::UDO_PAYTYPE_COIN && $resourcePrice['price_type'] == Common_Config::UDO_PRICETYPE_COIN || $payType == Common_Config::UDO_PAYTYPE_CREDIT && $resourcePrice['price_type'] == Common_Config::UDO_PRICETYPE_CREDIT) {
                 $correct = 1;
             } else {
                 return Common_Error::ERROR_COURSE_PRICETYPE;
             }
             //再判断课程用户是否已经购买
             $bought = $tblBought->scalar("id", "where userId ={$uid} and resourceId= {$val['resourceId']} and schoolId = {$schoolId}");
             //print_r($bought);
             if ($bought) {
                 return Common_Error::ERROR_COURSE_BOUGHT;
             }
         }
         //再判断定价值是否正确
         //如果传过来的课程数量就是所有需付费的课程,那么总价是频道定价
         if (count($charge) == $courseCount) {
             $totalPrice = $schoolPrice['price'];
         }
         if ($amount != $totalPrice) {
             return Common_Error::ERROR_COURSE_PRICE;
         }
         //总价核验后,再核验账户余额
         if ($totalPrice > ($payType == Common_Config::UDO_PAYTYPE_COIN ? $balance['amt'] : $balance['score'])) {
             return Common_Error::ERROR_SHORT_BALANCE;
         }
         //在数据核验准确后,生成订单
         $newOrder = [];
         $retry = 0;
         //如果生成失败会再循环尝试三次
         while ($retry <= 3 && !$newOrder) {
             $newOrder = $tblOrder->insert(array("userId" => $uid, "mobile" => $mobile, "payType" => $payType, "resource" => json_encode($resource), "amount" => $amount, "createTime" => time(), "status" => Common_Config::ORDER_NOT_PAY));
             $retry++;
             //如果第三次仍失败,返回订单创建失败
             if ($retry == 3) {
                 return Common_Error::ERROR_ORDER_FAIL;
             }
         }
         return $newOrder;
     } elseif ($payType == Common_Config::UDO_PAYTYPE_RECHARGE) {
         $coinInfo = $tblCoinInfo->scalar("amt", "where id = {$coinId}");
         if ($amount != $coinInfo['amt']) {
             return Common_Error::ERROR_COIN_INFO;
         }
         //在数据核验准确后,生成订单
         $newOrder = [];
         $retry = 0;
         $coinMoney = $tradeModel->getCoinMoney($coinId);
         //如果生成失败会再循环尝试三次
         while ($retry <= 3 && !$newOrder) {
             $newOrder = $tblOrder->insert(array("userId" => $uid, "mobile" => $mobile, "payType" => $payType, "coinId" => $coinId, "money" => $coinMoney['price'], "amount" => $amount, "platform" => $platform, "createTime" => time(), "status" => Common_Config::ORDER_NOT_PAY));
             $retry++;
             //如果第三次仍失败,返回订单创建失败
             if ($retry == 3) {
                 return Common_Error::ERROR_ORDER_FAIL;
             }
         }
         return $newOrder;
     }
 }