| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace App\Services;
- use App\Services\BaseService;
- use App\Models\User;
- use Illuminate\Support\Facades\DB;
- use App\Services\OrderService;
- class UserService extends BaseService
- {
- const STATUS_YES = 1; // 状态正常
- const STATUS_NOT = 0; // 状态禁用
- const LOGIN_IP_LIMIT = 2; // 同一IP允许登录用户数量
- public static $MODEL= User::class;
-
- /**
- * @description: 获取查询条件
- * @param {array} $search 查询内容
- * @return {array}
- */
- public static function getWhere(array $search = []): array
- {
- $where = [];
- if (isset($search['id']) && $search['id'] !== '') {
- $where[] = ['id', '=', $search['id']];
- }
- // if (isset($search['username']) && $search['username'] !== '') {
- // $where[] = ['username', '=', $search['username']];
- // }
- if (isset($search['username']) && $search['username'] !== '') {
- $where[] = ['username', 'like', '%' . $search['username'] . '%'];
- }
- if (isset($search['nickname']) && $search['nickname'] !== '') {
- $where[] = ['nickname', 'like', '%' . $search['nickname'] . '%'];
- }
- if (isset($search['status']) && $search['status'] !== '') {
- $where[] = ['status', '=', intval($search['status'])];
- }
- if (isset($search['flag']) && $search['flag'] !== '') {
- $where[] = ['flag', '=', intval($search['flag'])];
- }
- if (isset($search['mobile']) && $search['mobile'] !== '') {
- $where[] = ['mobile', '=', $search['mobile']];
- }
- if (isset($search['membership_level_code']) && $search['membership_level_code'] !== '') {
- $where[] = ['membership_level_code', '=', $search['membership_level_code']];
- }
- if (isset($search['last_login_ip']) && $search['last_login_ip'] !== '') {
- $where[] = ['last_login_ip', '=', $search['last_login_ip']];
- }
- if (isset($search['invite_code']) && $search['invite_code'] !== '') {
- $where[] = ['invite_code', '=', $search['invite_code']];
- }
- if (isset($search['inviter_id_1']) && $search['inviter_id_1'] !== '') {
- $where[] = ['inviter_id_1', '=', $search['inviter_id_1']];
- }
- if (isset($search['inviter_id_2']) && $search['inviter_id_2'] !== '') {
- $where[] = ['inviter_id_2', '=', $search['inviter_id_2']];
- }
- if (isset($search['inviter_id_3']) && $search['inviter_id_3'] !== '') {
- $where[] = ['inviter_id_3', '=', $search['inviter_id_3']];
- }
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return
- */
- public static function findOne(array $search)
- {
- return static::model()::with(
- [
- 'inviterLevel1' => fn($query) => $query->select('id', 'username', 'nickname'),
- 'inviterLevel2' => fn($query) => $query->select('id', 'username', 'nickname'),
- 'inviterLevel3' => fn($query) => $query->select('id', 'username', 'nickname'),
- ]
- )->where(static::getWhere($search))->first();
- }
- /**
- * @description: 查询所有数据
- * @param array $search
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function findAll(array $search = [])
- {
- return static::model()::where(static::getWhere($search))->get();
- }
-
- /**
- * @description: 分页查询
- * @param array $search
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public static function paginate(array $search = [])
- {
- $limit = isset($search['limit']) ? $search['limit'] : 15;
- $paginator = static::model()::with(
- [
- 'inviterLevel1' => fn($query) => $query->select('id', 'username', 'nickname'),
- 'inviterLevel2' => fn($query) => $query->select('id', 'username', 'nickname'),
- 'inviterLevel3' => fn($query) => $query->select('id', 'username', 'nickname'),
- ]
- )->where(static::getWhere($search))
- ->orderBy('id', 'desc')
- ->paginate($limit);
- $data = $paginator->items();
- foreach($data as $k=> $v){
- $data[$k]['total_order_count'] = OrderService::model()::where(OrderService::getWhere(['user_id' => $v->id]))->count();
- $data[$k]['total_order_profit'] = OrderService::model()::where(OrderService::getWhere(['user_id' => $v->id ,'status' => OrderService::STATUS_YES]))->sum('commission');
- $data[$k]['today_order_count'] = OrderService::model()::where(OrderService::getWhere(['user_id' => $v->id ,'status' => OrderService::STATUS_YES ,'order_date' => date('Y-m-d')]))->count();
- time() - $v['last_active_time'] >180 ? $data[$k]['is_online'] = 0 : $data[$k]['is_online'] = 1;
- }
- return ['total' => $paginator->total(), 'data' => $data];
- }
- /**
- * @description:
- * @param {*} $params
- * @return {*}
- */
- public static function submit($params = [])
- {
- $result = false;
- $msg['code'] = self::NOT;
- $msg['msg'] = '';
-
- if(isset($params['password'])){
- if(empty($params['password'])){
- unset($params['password']);
- }
- }
- if(isset($params['transaction_password'])){
- if(empty($params['transaction_password'])){
- unset($params['transaction_password']);
- }
- }
- // 2. 判断是否是更新
- if (!empty($params['id'])) {
- // 更新
- $info = self::findOne(['id'=>$params['id']] );
- if (!$info) {
- $msg['msg'] = '数据不存在!';
- }else{
- $result = $info->update($params);
- $id = $params['id'];
- }
- } else {
- // 创建
- $result = $info = self::model()::create($params);
- $id = $result->id;
- }
-
-
- if($result){
- $msg['code'] = self::YES;
- $msg['msg'] = '设置成功';
- }else{
- $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
- }
- return $msg;
- }
- /**
- * @description: 检查IP登录数量
- * @param string $ip
- * @return int
- */
- public static function checkIpLimit($ip)
- {
- // $count = static::model()::where(self::getWhere(['last_login_ip' => $ip]))->count();
- // if($count > self::LOGIN_IP_LIMIT){
- // static::model()::where(self::getWhere(['last_login_ip' => $ip]))->update(['status' => self::STATUS_NOT]);
- // return false;
- // }
- return true;
- }
- /**
- * @description: 更新登录iP
- * @param {*} $id
- * @param {*} $ip
- * @return {*}
- */
- public static function updateLoginIp($id,$ip)
- {
- static::model()::where('id',$id)->update(['last_login_ip' => $ip,'last_login_at' => date('Y-m-d H:i:s')]);
- }
- /**
- * @description: 调整用户余额
- * @param int $userId 用户ID
- * @param float $amount 调整金额,正数为增加,负数为减少
- * @param string $changeType 变动类型
- * @param string $remark 备注
- * @return bool
- */
- public static function adjustBalance($userId, $amount, $changeType, $remark = '')
- {
- $result = false;
- $msg['code'] = self::NOT;
- $msg['msg'] = '';
- $user = static::model()::where(self::getWhere(['id' => $userId]))->lockForUpdate()->first();
- if (!$user) {
- $msg['msg'] = '用户不存在';
- return $msg;
- }
- $beforeBalance = $user->balance;
- $afterBalance = $beforeBalance + $amount;
- if($afterBalance < 0){
- $msg['msg'] = '用户余额不足,无法扣款';
- return $msg;
- }
-
- DB::beginTransaction();
- try {
- // 更新用户余额
- $user->balance = $afterBalance;
- $user->save();
- // 记录余额变动日志
- $log = UserBalanceLogService::addLog(
- $userId,
- $changeType,
- $beforeBalance,
- $amount,
- $remark
- );
- if($log['code'] == self::NOT){
- DB::rollBack();
- return $log;
- }
- DB::commit();
- $msg['code'] = self::YES;
- $msg['msg'] = '用户余额调整成功';
- return $msg;
- } catch (\Exception $e) {
- DB::rollBack();
-
- $msg['msg'] = $e->getMessage();
- return $msg;
- }
- }
- }
|