public function uploadMaterial($material) { if (!ArrayToolkit::requireds($material, array('courseId', 'fileId'))) { throw $this->createServiceException('参数缺失,上传失败!'); } $course = $this->getCourseService()->getCourse($material['courseId']); if (empty($course)) { throw $this->createServiceException('课程不存在,上传资料失败!'); } $fields = array('courseId' => $material['courseId'], 'lessonId' => empty($material['lessonId']) ? 0 : $material['lessonId'], 'description' => empty($material['description']) ? '' : $material['description'], 'userId' => $this->getCurrentUser()->id, 'createdTime' => time()); if (empty($material['fileId'])) { if (empty($material['link'])) { throw $this->createServiceException('资料链接地址不能为空,添加资料失败!'); } $fields['fileId'] = 0; $fields['link'] = $material['link']; $fields['title'] = empty($material['description']) ? $material['link'] : $material['description']; } else { $fields['fileId'] = (int) $material['fileId']; $file = $this->getUploadFileService()->getFile($material['fileId']); if (empty($file)) { throw $this->createServiceException('文件不存在,上传资料失败!'); } $fields['link'] = ''; $fields['title'] = $file['filename']; $fields['fileSize'] = $file['size']; } $material = $this->getMaterialDao()->addMaterial($fields); $this->getCourseService()->increaseLessonMaterialCount($fields['lessonId']); return $material; }
public function createOrder($info) { $user = $this->getCurrentUser(); if (!$user->isLogin()) { throw $this->createServiceException('用户未登录,不能创建订单'); } if (!ArrayToolkit::requireds($info, array('targetId'))) { throw $this->createServiceException('订单数据缺失,创建资料交换失败。'); } // 获得锁 $user = $this->getUserService()->getUser($user['id'], true); $exchange = $this->getExchangeService()->getExchange($info['targetId']); if (empty($exchange)) { throw $this->createServiceException('资料不存在,操作失败。'); } $order = array(); $order['title'] = "兑换资料《{$exchange['title']}》"; $order['sn'] = $this->generateOrderSn($order); $order['userId'] = $info['userId']; $order['targetType'] = $info['targetType']; $order['targetId'] = $info['targetId']; $order['payment'] = $info['payment']; $order['totalPoints'] = $info["totalPoints"]; $order['status'] = 'created'; $order['createdTime'] = time(); if (!empty($info['note'])) { $order['data'] = array('note' => $info['note']); } $order = $this->getExchangeOrdersDao()->addOrder($order); if (empty($order)) { throw $this->createServiceException('创建资料兑换订单失败!'); } // $this->_createLog($order['id'], 'created', '创建订单'); return $order; }
public function createOrder($order) { if (!ArrayToolkit::requireds($order, array('userId', 'title', 'amount', 'targetType', 'targetId', 'payment'))) { throw $this->createServiceException('创建订单失败:缺少参数。'); } $order = ArrayToolkit::parts($order, array('userId', 'title', 'amount', 'targetType', 'targetId', 'payment', 'note', 'snPrefix', 'data', 'couponCode', 'coinAmount', 'coinRate', 'priceType', 'totalPrice', 'coupon', 'couponDiscount', 'discountId', 'discount')); $orderUser = $this->getUserService()->getUser($order['userId']); if (empty($orderUser)) { throw $this->createServiceException("订单用户(#{$order['userId']})不存在,不能创建订单。"); } if (!in_array($order['payment'], array('none', 'alipay', 'alipaydouble', 'tenpay', 'coin'))) { throw $this->createServiceException('创建订单失败:payment取值不正确。'); } $order['sn'] = $this->generateOrderSn($order); unset($order['snPrefix']); if (!empty($order['couponCode'])) { $couponInfo = $this->getCouponService()->checkCouponUseable($order['couponCode'], $order['targetType'], $order['targetId'], $order['amount']); if ($couponInfo['useable'] != 'yes') { throw $this->createServiceException("优惠码不可用"); } } unset($order['couponCode']); $order['amount'] = number_format($order['amount'], 2, '.', ''); if (intval($order['amount'] * 100) == 0) { $order['payment'] = 'none'; } $order['status'] = 'created'; $order['createdTime'] = time(); $order = $this->getOrderDao()->addOrder($order); $this->_createLog($order['id'], 'created', '创建订单'); return $order; }
protected function filterTestpaperFields($fields, $mode = 'create') { $filtedFields = array(); $filtedFields['updatedUserId'] = $this->getCurrentUser()->id; $filtedFields['updatedTime'] = time(); if ($mode == 'create') { if (!ArrayToolkit::requireds($fields, array('name', 'pattern', 'target'))) { throw $this->createServiceException('缺少必要字段!'); } $filtedFields['name'] = $fields['name']; $filtedFields['target'] = $fields['target']; $filtedFields['pattern'] = $fields['pattern']; $filtedFields['description'] = empty($fields['description']) ? '' : $fields['description']; $filtedFields['limitedTime'] = empty($fields['limitedTime']) ? 0 : (int) $fields['limitedTime']; $filtedFields['metas'] = empty($fields['metas']) ? array() : $fields['metas']; $filtedFields['status'] = 'draft'; $filtedFields['createdUserId'] = $this->getCurrentUser()->id; $filtedFields['createdTime'] = time(); } else { if (array_key_exists('name', $fields)) { $filtedFields['name'] = empty($fields['name']) ? '' : $fields['name']; } if (array_key_exists('description', $fields)) { $filtedFields['description'] = empty($fields['description']) ? '' : $fields['description']; } if (array_key_exists('limitedTime', $fields)) { $filtedFields['limitedTime'] = empty($fields['limitedTime']) ? 0 : (int) $fields['limitedTime']; } if (array_key_exists('passedScore', $fields)) { $filtedFields['passedScore'] = empty($fields['passedScore']) ? 0 : (double) $fields['passedScore']; } } return $filtedFields; }
/** *类似这样的,提交数据保存到数据的流程是: * * 1. 检查参数是否正确,不正确就抛出异常 * 2. 过滤数据 * 3. 插入到数据库 * 4. 更新其他相关的缓存字段 */ public function saveNote(array $note) { if (!ArrayToolkit::requireds($note, array('lessonId', 'courseId', 'content'))) { throw $this->createServiceException('缺少必要的字段,保存笔记失败'); } list($course, $member) = $this->getCourseService()->tryTakeCourse($note['courseId']); $user = $this->getCurrentUser(); if (!$this->getCourseService()->getCourseLesson($note['courseId'], $note['lessonId'])) { throw $this->createServiceException('课时不存在,保存笔记失败'); } $note = ArrayToolkit::filter($note, array('courseId' => 0, 'lessonId' => 0, 'content' => '', 'status' => 0)); $note['content'] = $this->purifyHtml($note['content']) ?: ''; $note['length'] = $this->calculateContnentLength($note['content']); $existNote = $this->getUserLessonNote($user['id'], $note['lessonId']); if (!$existNote) { $note['userId'] = $user['id']; $note['createdTime'] = time(); $note['updatedTime'] = time(); $note = $this->getNoteDao()->addNote($note); $this->getDispatcher()->dispatch('course.note.create', new ServiceEvent($note)); } else { $note['updatedTime'] = time(); $note = $this->getNoteDao()->updateNote($existNote['id'], $note); $this->getDispatcher()->dispatch('course.note.update', new ServiceEvent($note, array('preStatus' => $existNote['status']))); } $this->getCourseService()->setMemberNoteNumber($note['courseId'], $note['userId'], $this->getNoteDao()->getNoteCountByUserIdAndCourseId($note['userId'], $note['courseId'])); return $note; }
public function createOrder($info) { $connection = ServiceKernel::instance()->getConnection(); try { $connection->beginTransaction(); $user = $this->getCurrentUser(); if (!$user->isLogin()) { throw $this->createServiceException('用户未登录,不能创建订单'); } if (!ArrayToolkit::requireds($info, array('targetId', 'payment'))) { throw $this->createServiceException('订单数据缺失,创建课程订单失败。'); } // 获得锁 $user = $this->getUserService()->getUser($user['id'], true); if ($this->getClassroomService()->isClassroomStudent($info['targetId'], $user['id'])) { throw $this->createServiceException('已经是班级学员,创建订单失败。'); } $classroom = $this->getClassroomService()->getClassroom($info['targetId']); if (empty($classroom)) { throw $this->createServiceException('班级不存在,创建课程订单失败。'); } if ($classroom['private'] == 1) { throw $this->createServiceException('该班级是封闭班级,学员不能自行加入!'); } $order = array(); $order['userId'] = $user['id']; $order['title'] = "购买班级《{$classroom['title']}》"; $order['targetType'] = 'classroom'; $order['targetId'] = $classroom['id']; $order['payment'] = $info['payment']; $order['amount'] = empty($info['amount']) ? 0 : $info['amount']; $order['priceType'] = $info['priceType']; $order['totalPrice'] = $info["totalPrice"]; $order['coinRate'] = $info['coinRate']; $order['coinAmount'] = $info['coinAmount']; $order['snPrefix'] = 'CR'; if (!empty($info['note'])) { $order['data'] = array('note' => $info['note']); } if (!empty($info['coupon'])) { $order['coupon'] = $info['coupon']; $order['couponDiscount'] = $info['couponDiscount']; } $order = $this->getOrderService()->createOrder($order); if (empty($order)) { throw $this->createServiceException('创建订单失败!'); } // 免费班级,就直接将订单置为已购买 if (intval($order['amount'] * 100) == 0 && intval($order['coinAmount'] * 100) == 0 && empty($order['coupon'])) { list($success, $order) = $this->getOrderService()->payOrder(array('sn' => $order['sn'], 'status' => 'success', 'amount' => $order['amount'], 'paidTime' => time())); $info = array('orderId' => $order['id'], 'remark' => empty($order['data']['note']) ? '' : $order['data']['note']); $this->getClassroomService()->becomeStudent($order['targetId'], $order['userId'], $info); } $connection->commit(); return $order; } catch (\Exception $e) { $connection->rollBack(); throw $e; } }
public function becomeStudentAndCreateOrder($userId, $courseId, $data) { if (!ArrayToolkit::requireds($data, array("price", "remark"))) { throw $this->createServiceException("参数不对!"); } $user = $this->getUserService()->getUser($userId); if (empty($user)) { throw $this->createNotFoundException("用户{$user['nickname']}不存在"); } $course = $this->getCourseService()->getCourse($courseId); if (empty($course)) { throw $this->createNotFoundException("课程{$course['title']}不存在"); } if ($this->getCourseService()->isCourseStudent($course['id'], $user['id'])) { throw $this->createNotFoundException("用户已经是学员,不能添加!"); } $orderTitle = "购买课程《{$course['title']}》"; if (isset($data["isAdminAdded"]) && $data["isAdminAdded"] == 1) { $orderTitle = $orderTitle . "(管理员添加)"; } $order = $this->getOrderService()->createOrder(array('userId' => $user['id'], 'title' => $orderTitle, 'targetType' => 'course', 'targetId' => $course['id'], 'amount' => $data['price'], 'totalPrice' => $data['price'], 'payment' => 'none', 'snPrefix' => 'C')); $this->getOrderService()->payOrder(array('sn' => $order['sn'], 'status' => 'success', 'amount' => $order['amount'], 'paidTime' => time())); $info = array('orderId' => $order['id'], 'note' => $data['remark']); $this->getCourseService()->becomeStudent($order['targetId'], $order['userId'], $info); $member = $this->getCourseService()->getCourseMember($course['id'], $user['id']); if (isset($data["isAdminAdded"]) && $data["isAdminAdded"] == 1) { $this->getNotificationService()->notify($member['userId'], 'student-create', array('courseId' => $course['id'], 'courseTitle' => $course['title'])); } $this->getLogService()->info('course', 'add_student', "课程《{$course['title']}》(#{$course['id']}),添加学员{$user['nickname']}(#{$user['id']}),备注:{$data['remark']}"); return array($course, $member, $order); }
public function addFile($targetType, $targetId, array $fileInfo = array(), UploadedFile $originalFile = null) { if (!ArrayToolkit::requireds($fileInfo, array('filename', 'key', 'size'))) { throw $this->createServiceException('参数缺失,添加用户文件失败!'); } $uploadFile = array(); $uploadFile['targetId'] = $targetId; $uploadFile['targetType'] = $targetType; $uploadFile['hashId'] = $fileInfo['key']; $uploadFile['filename'] = $fileInfo['filename']; $uploadFile['ext'] = pathinfo($uploadFile['filename'], PATHINFO_EXTENSION); $uploadFile['size'] = (int) $fileInfo['size']; $uploadFile['etag'] = empty($fileInfo['etag']) ? '' : $fileInfo['etag']; $uploadFile['length'] = empty($fileInfo['length']) ? 0 : intval($fileInfo['length']); $uploadFile['metas'] = $this->encodeMetas(empty($fileInfo['metas']) ? array() : $fileInfo['metas']); $uploadFile['metas2'] = $this->encodeMetas(empty($fileInfo['metas2']) ? array() : $fileInfo['metas2']); if (empty($fileInfo['convertHash'])) { $uploadFile['convertHash'] = "ch-{$uploadFile['hashId']}"; $uploadFile['convertStatus'] = 'none'; $uploadFile['convertParams'] = ''; } else { $uploadFile['convertHash'] = "{$fileInfo['convertHash']}"; $uploadFile['convertStatus'] = 'waiting'; $uploadFile['convertParams'] = $fileInfo['convertParams']; } $uploadFile['type'] = FileToolkit::getFileTypeByMimeType($fileInfo['mimeType']); $uploadFile['canDownload'] = empty($uploadFile['canDownload']) ? 0 : 1; $uploadFile['storage'] = 'cloud'; $uploadFile['createdUserId'] = $this->getCurrentUser()->id; $uploadFile['updatedUserId'] = $uploadFile['createdUserId']; $uploadFile['updatedTime'] = $uploadFile['createdTime'] = time(); return $uploadFile; }
public function addCard($card) { if (!ArrayToolkit::requireds($card, array('cardType', 'cardId', 'deadline', 'userId'))) { throw $this->createServiceException('缺少必要字段,新创建卡失败!'); } $card['createdTime'] = time(); return $this->getCardDao()->addCard($card); }
public function shouldPayAmount($targetId, $priceType, $cashRate, $coinEnabled, $orderData) { if (!ArrayToolkit::requireds($orderData, array('buyType', 'targetId', 'unitType', 'duration'))) { throw new Exception('订单数据缺失,创建会员订单失败。'); } if (!in_array($orderData['buyType'], array('new', 'renew', 'upgrade'))) { throw new Exception('购买类型不正确,创建会员订单失败。'); } if (!(array_key_exists("buyType", $orderData) && $orderData["buyType"] == "upgrade")) { $orderData['duration'] = intval($orderData['duration']); if (empty($orderData['duration'])) { throw new Exception('会员开通时长不正确,创建会员订单失败。'); } if (!in_array($orderData['unitType'], array('month', 'year'))) { throw new Exception('付费方式不正确,创建会员订单失败。'); } } $level = $this->getLevelService()->getLevel($orderData['targetId']); if (empty($level)) { throw new Exception('会员等级不存在,创建会员订单失败。'); } if (empty($level['enabled'])) { throw new Exception('会员等级已关闭,创建会员订单失败。'); } $currentUser = $this->getLevelService()->getCurrentUser(); if (array_key_exists("buyType", $orderData) && $orderData["buyType"] == "upgrade") { $totalPrice = $this->getVipService()->calUpgradeMemberAmount($currentUser->id, $level['id']); } else { $unitPrice = $level[$orderData['unitType'] . 'Price']; if ($priceType == "Coin") { $unitPrice = NumberToolkit::roundUp($unitPrice * $cashRate); } $totalPrice = $unitPrice * $orderData['duration']; } $amount = $totalPrice; //优惠码优惠价格 $couponApp = $this->getAppService()->findInstallApp("Coupon"); $couponSetting = $this->getSettingService()->get("coupon"); if (!empty($couponApp) && isset($couponSetting["enabled"]) && $couponSetting["enabled"] == 1 && $orderData["couponCode"] && trim($orderData["couponCode"]) != "") { $couponResult = $this->afterCouponPay($orderData["couponCode"], 'vip', $targetId, $totalPrice, $priceType, $cashRate); if (isset($couponResult["useable"]) && $couponResult["useable"] == "yes" && isset($couponResult["afterAmount"])) { $amount = $couponResult["afterAmount"]; } } //虚拟币优惠价格 if (array_key_exists("coinPayAmount", $orderData)) { $amount = $this->afterCoinPay($coinEnabled, $priceType, $cashRate, $amount, $orderData['coinPayAmount'], $orderData["payPassword"]); } if ($priceType == "Coin") { $amount = $amount / $cashRate; } if ($amount <= 0) { $amount = 0; } $totalPrice = NumberToolkit::roundUp($totalPrice); $amount = NumberToolkit::roundUp($amount); return array($amount, $totalPrice, empty($couponResult) ? null : $couponResult); }
public function createAttribute($attribute) { if (!ArrayToolkit::requireds($attribute, array('productId', 'attrId', 'attrValue'))) { throw $this->createServiceException("缺少必要参数,,添加属性失败"); } $this->_filterAttributeFields($attribute); $attribute = $this->getProductAttributeDao()->addAttribute($attribute); // $this->getLogService()->info('attribute', 'create', "添加栏目 {$attribute['name']}(#{$attribute['id']})", $attribute); return $attribute; }
public function createProduct($fields) { if (!ArrayToolkit::requireds($fields, array('name'))) { throw $this->createServiceException('缺少必要字段,创建课程失败!'); } $fields = ArrayToolkit::parts($fields, array('name', 'price', 'categoryId', 'userId')); $fields['productDetail'] = !empty($fields['productDetail']) ? $this->purifyHtml($fields['productDetail']) : ''; $fields['userId'] = $this->getCurrentUser()->id; $fields['createdTime'] = time(); $product = $this->getCourseDao()->addProduct(ProductSerialize::serialize($fields)); return $product; }
public function createExchange($exchange) { if (!ArrayToolkit::requireds($exchange, array('title', 'points', 'about'))) { throw $this->createServiceException('缺少必要字段,添加交换物品失败!'); } $exchange['about'] = !empty($exchange['about']) ? $this->purifyHtml($exchange['about']) : ''; $exchange['createdTime'] = time(); $exchange['userId'] = $this->getCurrentUser()->id; $exchange = $this->getExchangeDao()->addExchange($exchange); $exchange = $this->getExchange($exchange['id']); return $exchange; }
public function createGift($gift) { if (!ArrayToolkit::requireds($gift, array('giftName'))) { throw $this->createServiceException('缺少必要字段,添加礼品失败!'); } $gift = ArrayToolkit::parts($gift, array('giftName', 'points', 'detail')); $gift['detail'] = !empty($gift['detail']) ? $this->purifyHtml($gift['detail']) : ''; $gift['createdTime'] = time(); $gift = $this->getGiftDao()->addGift($gift); $gift = $this->getGift($gift['id']); // $this->getLogService()->info('gift', 'create', "添加礼品《{$gift['giftName']}》(#{$gift['id']})"); return $gift; }
public function createBlock($block) { if (!ArrayToolkit::requireds($block, array('code', 'title'))) { throw $this->createServiceException("创建编辑区失败,缺少必要的字段"); } $user = $this->getCurrentUser(); $block['userId'] = $user['id']; $block['tips'] = empty($block['tips']) ? '' : $block['tips']; $block['createdTime'] = time(); $block['updateTime'] = time(); $createdBlock = $this->getBlockDao()->addBlock($block); $blockHistoryInfo = array('blockId' => $createdBlock['id'], 'content' => $createdBlock['content'], 'userId' => $createdBlock['userId'], 'createdTime' => time()); $this->getBlockHistoryDao()->addBlockHistory($blockHistoryInfo); return $createdBlock; }
public function createOrder($info) { $user = $this->getCurrentUser(); if (!$user->isLogin()) { throw $this->createServiceException('用户未登录,不能创建订单'); } if (!ArrayToolkit::requireds($info, array('courseId', 'payment'))) { throw $this->createServiceException('订单数据缺失,创建课程订单失败。'); } $course = $this->getCourseService()->getCourse($info['courseId']); if (empty($course)) { throw $this->createServiceException('课程不存在,创建课程订单失败。'); } $order = array(); $order['userId'] = $user->id; $order['title'] = "购买课程《{$course['title']}》"; $order['targetType'] = 'course'; $order['targetId'] = $course['id']; $order['payment'] = $info['payment']; $order['amount'] = $course['price']; if ($order['amount'] > 0) { //如果是限时打折,判断是否在限免期,如果是,则Amout为0 if ($course['freeStartTime'] < time() && $course['freeEndTime'] > time()) { $order['amount'] = 0; } } $order['snPrefix'] = 'C'; if (!empty($info['coupon'])) { $order['couponCode'] = $info['coupon']; } if (!empty($info['note'])) { $order['data'] = array('note' => $info['note']); } $order = $this->getOrderService()->createOrder($order); if (empty($order)) { throw $this->createServiceException('创建课程订单失败!'); } // 免费课程,就直接将订单置为已购买 if (intval($order['amount'] * 100) == 0) { list($success, $order) = $this->getOrderService()->payOrder(array('sn' => $order['sn'], 'status' => 'success', 'amount' => $order['amount'], 'paidTime' => time())); $info = array('orderId' => $order['id'], 'remark' => empty($order['data']['note']) ? '' : $order['data']['note']); $this->getCourseService()->becomeStudent($order['targetId'], $order['userId'], $info); } return $order; }
public function post(Application $app, Request $request) { $fields = $request->request->all(); if (!ArrayToolkit::requireds($fields, array('email', 'nickname', 'password'))) { return array('message' => '缺少必填字段'); } $ip = $request->getClientIp(); $fields['createdIp'] = $ip; $authSettings = $this->getServiceKernel()->createService('System.SettingService')->get('auth', array()); if (isset($authSettings['register_protective'])) { $type = $authSettings['register_protective']; switch ($type) { case 'middle': $condition = array('startTime' => time() - 24 * 3600, 'createdIp' => $ip); $registerCount = $this->getUserService()->searchUserCount($condition); if ($registerCount > 30) { goto failure; } goto register; break; case 'high': $condition = array('startTime' => time() - 24 * 3600, 'createdIp' => $ip); $registerCount = $this->getUserService()->searchUserCount($condition); if ($registerCount > 10) { goto failure; } $registerCount = $this->getUserService()->searchUserCount(array('startTime' => time() - 3600, 'createdIp' => $ip)); if ($registerCount >= 1) { goto failure; } goto register; break; default: goto register; break; } } register: $user = $this->getUserService()->register($fields); $user['profile'] = $this->getUserService()->getUserProfile($user['id']); return $this->callFilter('User', $user); failure: return array('message' => '已经超出用户注册次数限制,用户注册失败'); }
public function addBlacklist($blacklist) { if (!ArrayToolkit::requireds($blacklist, array('userId', 'blackId'))) { throw $this->createServiceException('缺少必要字段,添加黑名单失败!'); } if (!$this->canTakeBlacklist($blacklist['userId'])) { throw $this->createAccessDeniedException('您没有权限'); } $blackUser = $this->getUserService()->getUser($blacklist['blackId']); if (empty($blackUser)) { throw $this->createNotFoundException('被拉黑用户不存在'); } $black = $this->getBlacklistByUserIdAndBlackId($blacklist['userId'], $blackUser['id']); if (!empty($black)) { throw $this->createServiceException('不能重复添加黑名单!'); } $blacklist['createdTime'] = time(); return $this->getBlacklistDao()->addBlacklist($blacklist); }
public function registerApp($app) { if (!ArrayToolkit::requireds($app, array('code', 'name', 'version'))) { throw $this->createServiceException('参数缺失,注册APP失败!'); } $app = ArrayToolkit::parts($app, array('code', 'name', 'description', 'version', 'type')); $app['fromVersion'] = $app['version']; $app['description'] = empty($app['description']) ? '' : $app['description']; $app['icon'] = empty($app['icon']) ? '' : $app['icon']; $app['developerId'] = 0; $app['developerName'] = empty($app['author']) ? '未知' : $app['author']; $app['installedTime'] = time(); $app['updatedTime'] = time(); $exist = $this->getAppDao()->getAppByCode($app['code']); if ($exist) { return $this->getAppDao()->updateApp($exist['id'], $app); } return $this->getAppDao()->addApp($app); }
public function inflow($inflow) { if (!ArrayToolkit::requireds($inflow, array('userId', 'amount', 'name', 'orderSn', 'note'))) { throw $this->createServiceException('参数缺失'); } if (!is_numeric($inflow["amount"]) || $inflow["amount"] <= 0) { throw $this->createServiceException('金额必须为数字,并且不能小于0'); } $account = $this->getPointsAccountService()->getAccountByUserId($inflow["userId"]); if (empty($account)) { $account = $this->getPointsAccountService()->createAccount($inflow["userId"]); $account = $this->getPointsAccountService()->getAccountByUserId($inflow["userId"], true); } $inflow["type"] = "inflow"; $inflow["sn"] = $this->makeSn(); $inflow["createdTime"] = time(); $inflow["points"] = $account["points"] + $inflow["amount"]; $inflow = $this->getFlowDao()->addFlow($inflow); $this->getPointsAccountService()->wavePointsField($account["id"], $inflow["amount"]); return $inflow; }
public function uploadMaterial($material) { $argument = $material; if (!ArrayToolkit::requireds($material, array('courseId', 'fileId'))) { throw $this->createServiceException('参数缺失,上传失败!'); } $course = $this->getCourseService()->getCourse($material['courseId']); if (empty($course)) { throw $this->createServiceException('课程不存在,上传资料失败!'); } $fields = array('courseId' => $material['courseId'], 'lessonId' => empty($material['lessonId']) ? 0 : $material['lessonId'], 'description' => empty($material['description']) ? '' : $material['description'], 'userId' => $this->getCurrentUser()->id, 'createdTime' => time()); if (empty($material['fileId'])) { if (empty($material['link'])) { throw $this->createServiceException('资料链接地址不能为空,添加资料失败!'); } $fields['fileId'] = 0; $fields['link'] = $material['link']; $fields['title'] = empty($material['description']) ? $material['link'] : $material['description']; } else { $fields['fileId'] = (int) $material['fileId']; $file = $this->getUploadFileService()->getFile($material['fileId']); if (empty($file)) { throw $this->createServiceException('文件不存在,上传资料失败!'); } $fields['link'] = ''; $fields['title'] = $file['filename']; $fields['fileSize'] = $file['size']; } if (array_key_exists('copyId', $material)) { $fields['copyId'] = $material['copyId']; } $material = $this->getMaterialDao()->addMaterial($fields); // Increase the linked file usage count, if there's a linked file used by this material. if (!empty($material['fileId'])) { $this->getUploadFileService()->waveUploadFile($material['fileId'], 'usedCount', 1); } $this->getCourseService()->increaseLessonMaterialCount($fields['lessonId']); $this->dispatchEvent("material.create", array('argument' => $argument, 'material' => $material)); return $material; }
public function saveReview($fields) { if (!ArrayToolkit::requireds($fields, array('courseId', 'userId', 'rating'))) { throw $this->createServiceException('参数不正确,评价失败!'); } list($course, $member) = $this->getCourseService()->tryTakeCourse($fields['courseId']); $userId = $this->getCurrentUser()->id; if (empty($course)) { throw $this->createServiceException("课程(#{$fields['courseId']})不存在,评价失败!"); } $user = $this->getUserService()->getUser($fields['userId']); if (empty($user)) { return $this->createServiceException("用户(#{$fields['userId']})不存在,评价失败!"); } $review = $this->getReviewDao()->getReviewByUserIdAndCourseId($user['id'], $course['id']); if (empty($review)) { $review = $this->getReviewDao()->addReview(array('userId' => $fields['userId'], 'courseId' => $fields['courseId'], 'rating' => $fields['rating'], 'private' => $course['status'] == 'published' ? 0 : 1, 'content' => empty($fields['content']) ? '' : $fields['content'], 'createdTime' => time())); } else { $review = $this->getReviewDao()->updateReview($review['id'], array('rating' => $fields['rating'], 'content' => empty($fields['content']) ? '' : $fields['content'])); } $this->calculateCourseRating($course['id']); return $review; }
public function saveReview($fields) { if (!ArrayToolkit::requireds($fields, array('classroomId', 'userId', 'rating'))) { throw $this->createServiceException('参数不正确,评价失败!'); } $classroom = $this->getClassroomDao()->getClassroom($fields['classroomId']); $userId = $this->getCurrentUser()->id; if (empty($classroom)) { throw $this->createServiceException("班级(#{$fields['classroomId']})不存在,评价失败!"); } $user = $this->getUserService()->getUser($fields['userId']); if (empty($user)) { return $this->createServiceException("用户(#{$fields['userId']})不存在,评价失败!"); } $review = $this->getClassroomReviewDao()->getReviewByUserIdAndClassroomId($user['id'], $classroom['id']); if (empty($review)) { $review = $this->getClassroomReviewDao()->addReview(array('userId' => $fields['userId'], 'classroomId' => $fields['classroomId'], 'rating' => $fields['rating'], 'content' => empty($fields['content']) ? '' : $fields['content'], 'title' => empty($fields['title']) ? '' : $fields['title'], 'createdTime' => time())); } else { $review = $this->getClassroomReviewDao()->updateReview($review['id'], array('rating' => $fields['rating'], 'title' => empty($fields['title']) ? '' : $fields['title'], 'content' => empty($fields['content']) ? '' : $fields['content'])); } $this->calculateClassroomRating($classroom['id']); return $review; }
public function createOrder($info) { $user = $this->getCurrentUser(); if (!$user->isLogin()) { throw $this->createServiceException('用户未登录,不能创建订单'); } if (!ArrayToolkit::requireds($info, array('targetId', 'payment'))) { throw $this->createServiceException('订单数据缺失,创建礼品兑换失败。'); } // 获得锁 $user = $this->getUserService()->getUser($user['id'], true); $gift = $this->getGiftService()->getGift($info['targetId']); if (empty($gift)) { throw $this->createServiceException('礼品不存在,操作失败。'); } $order = array(); $order['title'] = "兑换礼品《{$gift['giftName']}》"; $order['sn'] = $this->generateOrderSn($order); $order['userId'] = $user['id']; $order['targetType'] = 'gift'; $order['targetId'] = $info['targetId']; $order['payment'] = $info['payment']; $order['amount'] = empty($info['amount']) ? 0 : $info['amount']; $order['totalPoints'] = $info["totalPrice"]; $order['status'] = 'created'; $order['createdTime'] = time(); if (!empty($info['note'])) { $order['data'] = array('note' => $info['note']); } $order = $this->getGiftOrdersDao()->addOrder($order); if (empty($order)) { throw $this->createServiceException('创建礼品兑换订单失败!'); } // $this->_createLog($order['id'], 'created', '创建订单'); return $order; }
public function createLesson($lesson) { $argument = $lesson; $lesson = ArrayToolkit::filter($lesson, array('courseId' => 0, 'chapterId' => 0, 'free' => 0, 'title' => '', 'summary' => '', 'tags' => array(), 'type' => 'text', 'content' => '', 'media' => array(), 'mediaId' => 0, 'length' => 0, 'startTime' => 0, 'giveCredit' => 0, 'requireCredit' => 0, 'liveProvider' => 'none', 'copyId' => 0, 'testMode' => 'normal', 'testStartTime' => 0, 'suggestHours' => '0.0')); if (!ArrayToolkit::requireds($lesson, array('courseId', 'title', 'type'))) { throw $this->createServiceException('参数缺失,创建课时失败!'); } if (empty($lesson['courseId'])) { throw $this->createServiceException('添加课时失败,课程ID为空。'); } $course = $this->getCourse($lesson['courseId'], true); if (empty($course)) { throw $this->createServiceException('添加课时失败,课程不存在。'); } if (!in_array($lesson['type'], array('text', 'audio', 'video', 'testpaper', 'live', 'ppt', 'document', 'flash'))) { throw $this->createServiceException('课时类型不正确,添加失败!'); } $this->fillLessonMediaFields($lesson); //课程内容的过滤 @todo // if(isset($lesson['content'])){ // $lesson['content'] = $this->purifyHtml($lesson['content']); // } if (isset($fields['title'])) { $fields['title'] = $this->purifyHtml($fields['title']); } // 课程处于发布状态时,新增课时,课时默认的状态为“未发布" $lesson['status'] = $course['status'] == 'published' ? 'unpublished' : 'published'; $lesson['free'] = empty($lesson['free']) ? 0 : 1; $lesson['number'] = $this->getNextLessonNumber($lesson['courseId']); $lesson['seq'] = $this->getNextCourseItemSeq($lesson['courseId']); $lesson['userId'] = $this->getCurrentUser()->id; $lesson['createdTime'] = time(); $lastChapter = $this->getChapterDao()->getLastChapterByCourseId($lesson['courseId']); $lesson['chapterId'] = empty($lastChapter) ? 0 : $lastChapter['id']; if ($lesson['type'] == 'live') { $lesson['endTime'] = $lesson['startTime'] + $lesson['length'] * 60; $lesson['suggestHours'] = $lesson['length'] / 60; } if (array_key_exists('copyId', $lesson)) { $lesson['copyId'] = $lesson['copyId']; } $lesson = $this->getLessonDao()->addLesson(LessonSerialize::serialize($lesson)); // Increase the linked file usage count, if there's a linked file used by this lesson. if (!empty($lesson['mediaId'])) { $this->getUploadFileService()->waveUploadFile($lesson['mediaId'], 'usedCount', 1); } $this->updateCourseCounter($course['id'], array('lessonNum' => $this->getLessonDao()->getLessonCountByCourseId($course['id']), 'giveCredit' => $this->getLessonDao()->sumLessonGiveCreditByCourseId($course['id']))); $this->getLogService()->info('course', 'add_lesson', "添加课时《{$lesson['title']}》({$lesson['id']})", $lesson); $this->dispatchEvent("course.lesson.create", array('argument' => $argument, 'lesson' => $lesson)); return $lesson; }
public function createPost($post) { $requiredKeys = array('courseId', 'threadId', 'content'); if (!ArrayToolkit::requireds($post, $requiredKeys)) { throw $this->createServiceException('参数缺失'); } $event = $this->dispatchEvent('course.thread.post.before_create', $post); if ($event->isPropagationStopped()) { throw $this->createServiceException('发帖次数过多,请稍候尝试。'); } $thread = $this->getThread($post['courseId'], $post['threadId']); if (empty($thread)) { throw $this->createServiceException(sprintf('课程(ID: %s)话题(ID: %s)不存在。', $post['courseId'], $post['threadId'])); } $post['content'] = $this->sensitiveFilter($post['content'], 'course-thread-post-create'); list($course, $member) = $this->getCourseService()->tryTakeCourse($post['courseId']); $post['userId'] = $this->getCurrentUser()->id; $post['isElite'] = $this->getCourseService()->isCourseTeacher($post['courseId'], $post['userId']) ? 1 : 0; $post['createdTime'] = time(); //创建post过滤html $post['content'] = $this->purifyHtml($post['content']); $post = $this->getThreadPostDao()->addPost($post); // 高并发的时候, 这样更新postNum是有问题的,这里暂时不考虑这个问题。 $threadFields = array('postNum' => $thread['postNum'] + 1, 'latestPostUserId' => $post['userId'], 'latestPostTime' => $post['createdTime']); $this->getThreadDao()->updateThread($thread['id'], $threadFields); $this->dispatchEvent('course.thread.post.create', $post); return $post; }
public function createOrder($info) { $connection = ServiceKernel::instance()->getConnection(); try { $connection->beginTransaction(); $user = $this->getCurrentUser(); if (!$user->isLogin()) { throw $this->createServiceException('用户未登录,不能创建订单'); } if (!ArrayToolkit::requireds($info, array('targetIds', 'payment'))) { throw $this->createServiceException('订单数据缺失,创建课程订单失败。'); } // 获得锁 $user = $this->getUserService()->getUser($user['id'], true); /* if ($this->getCourseService()->isCourseStudent($info['targetId'], $user['id'])) { throw $this->createServiceException('已经是课程学员,操作失败。'); } */ $courses = $this->getCourseService()->findCoursesByIds($info['targetIds']); if (empty($courses)) { throw $this->createServiceException('课程不存在,操作失败。'); } $order = array(); $flag = 1; foreach ($courses as $key => $course) { if ($flag == 1) { $order['title'] = "购买《{$course['title']}》"; } else { $order['title'] .= "+《{$course['title']}》"; } $flag++; } $order['userId'] = $user['id']; $order['targetType'] = 'course'; // $order['targetIds'] = $info['targetIds']; $order['targetIds'] = '|' . implode('|', $info['targetIds']) . '|'; // $order['targetId'] = $course['id']; /* if(!empty($course['discountId'])){ $order['discountId'] = $course['discountId']; $order['discount'] = $course['discount']; } */ $order['payment'] = $info['payment']; $order['amount'] = empty($info['amount']) ? 0 : $info['amount']; $order['priceType'] = $info['priceType']; $order['totalPrice'] = $info["totalPrice"]; $order['coinRate'] = $info['coinRate']; $order['coinAmount'] = $info['coinAmount']; $courseSetting = $this->getSettingService()->get('course', array()); if (array_key_exists("coursesPrice", $courseSetting)) { $notShowPrice = $courseSetting['coursesPrice']; } else { $notShowPrice = 0; } if ($notShowPrice == 1) { $order['amount'] = 0; $order['totalPrice'] = 0; } $order['snPrefix'] = 'C'; if (!empty($info['coupon'])) { $order['coupon'] = $info['coupon']; $order['couponDiscount'] = $info['couponDiscount']; } if (!empty($info['note'])) { $order['data'] = array('note' => $info['note']); } $order = $this->getOrderService()->createOrder($order); //重要代码 if (empty($order)) { throw $this->createServiceException('创建订单失败!'); } if (intval($order['amount'] * 100) == 0 && intval($order['coinAmount'] * 100) == 0 && empty($order['coupon'])) { list($success, $order) = $this->getOrderService()->payOrder(array('sn' => $order['sn'], 'status' => 'success', 'amount' => $order['amount'], 'paidTime' => time())); $infoMember = array('orderId' => $order['id'], 'remark' => empty($order['data']['note']) ? '' : $order['data']['note']); foreach ($info['targetIds'] as $targetid) { $this->getCourseService()->becomeStudent($targetid, $order['userId'], $infoMember); } } $connection->commit(); return $order; } catch (\Exception $e) { $connection->rollBack(); throw $e; } }
public function createCategory(array $category) { $category = ArrayToolkit::parts($category, array('name', 'code', 'weight', 'parentId', 'publishArticle', 'seoTitle', 'seoKeyword', 'seoDesc', 'published')); if (!ArrayToolkit::requireds($category, array('name', 'code', 'weight', 'parentId'))) { throw $this->createServiceException("缺少必要参数,,添加栏目失败"); } $this->_filterCategoryFields($category); $category['createdTime'] = time(); $category = $this->getCategoryDao()->addCategory($category); $this->getLogService()->info('category', 'create', "添加栏目 {$category['name']}(#{$category['id']})", $category); return $category; }
public function createCategory(array $category) { $category = ArrayToolkit::parts($category, array('description', 'name', 'code', 'weight', 'groupId', 'parentId', 'icon')); if (!ArrayToolkit::requireds($category, array('name', 'code', 'weight', 'groupId', 'parentId'))) { throw $this->createServiceException("缺少必要参数,,添加分类失败"); } $this->filterCategoryFields($category); $category = $this->getCategoryDao()->addCategory($category); $this->getLogService()->info('category', 'create', "添加分类 {$category['name']}(#{$category['id']})", $category); return $category; }
public function updateCategory($id, $fields) { if (!ArrayToolkit::requireds($fields, array('name'))) { throw $this->createServiceException("缺少必要参数,更新类别失败"); } $category['name'] = $fields['name']; $category['updatedTime'] = time(); return $this->getCategoryDao()->updateCategory($id, $category); }