12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace app\common\Service\User;
- use app\common\exception\BusinessException;
- use app\common\model\User as UserModel;
- class UserService
- {
- public static function getUserById($userId = 0)
- {
- return UserModel::where('id', $userId)->find();;
- }
- /**
- * @name 追加消费金额和总订单数
- * @param int|object $userId 会员ID
- * @param float $amount 变更金额
- * @param int $orderCount 订单数量增量,默认为1
- * @return boolean
- */
- public static function consume($userId = 0, $amount, $orderCount = 1)
- {
- // 判断金额
- if ($amount == 0) {
- return false;
- }
- $user = self::getUserById($userId);
- if (!$user) {
- throw new BusinessException('未找到用户');
- }
-
- // 更新会员消费金额和订单数量
- $user->setInc('total_consume', $amount);
- if ($orderCount > 0) {
- $user->setInc('order_count', $orderCount);
- }
- return true;
- }
- }
|