UserService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Services\OrderService;
  7. class UserService extends BaseService
  8. {
  9. const STATUS_YES = 1; // 状态正常
  10. const STATUS_NOT = 0; // 状态禁用
  11. const LOGIN_IP_LIMIT = 2; // 同一IP允许登录用户数量
  12. public static $MODEL= User::class;
  13. /**
  14. * @description: 获取查询条件
  15. * @param {array} $search 查询内容
  16. * @return {array}
  17. */
  18. public static function getWhere(array $search = []): array
  19. {
  20. $where = [];
  21. if (isset($search['id']) && $search['id'] !== '') {
  22. $where[] = ['id', '=', $search['id']];
  23. }
  24. // if (isset($search['username']) && $search['username'] !== '') {
  25. // $where[] = ['username', '=', $search['username']];
  26. // }
  27. if (isset($search['username']) && $search['username'] !== '') {
  28. $where[] = ['username', 'like', '%' . $search['username'] . '%'];
  29. }
  30. if (isset($search['nickname']) && $search['nickname'] !== '') {
  31. $where[] = ['nickname', 'like', '%' . $search['nickname'] . '%'];
  32. }
  33. if (isset($search['status']) && $search['status'] !== '') {
  34. $where[] = ['status', '=', intval($search['status'])];
  35. }
  36. if (isset($search['flag']) && $search['flag'] !== '') {
  37. $where[] = ['flag', '=', intval($search['flag'])];
  38. }
  39. if (isset($search['mobile']) && $search['mobile'] !== '') {
  40. $where[] = ['mobile', '=', $search['mobile']];
  41. }
  42. if (isset($search['membership_level_code']) && $search['membership_level_code'] !== '') {
  43. $where[] = ['membership_level_code', '=', $search['membership_level_code']];
  44. }
  45. if (isset($search['last_login_ip']) && $search['last_login_ip'] !== '') {
  46. $where[] = ['last_login_ip', '=', $search['last_login_ip']];
  47. }
  48. if (isset($search['invite_code']) && $search['invite_code'] !== '') {
  49. $where[] = ['invite_code', '=', $search['invite_code']];
  50. }
  51. if (isset($search['inviter_id_1']) && $search['inviter_id_1'] !== '') {
  52. $where[] = ['inviter_id_1', '=', $search['inviter_id_1']];
  53. }
  54. if (isset($search['inviter_id_2']) && $search['inviter_id_2'] !== '') {
  55. $where[] = ['inviter_id_2', '=', $search['inviter_id_2']];
  56. }
  57. if (isset($search['inviter_id_3']) && $search['inviter_id_3'] !== '') {
  58. $where[] = ['inviter_id_3', '=', $search['inviter_id_3']];
  59. }
  60. return $where;
  61. }
  62. /**
  63. * @description: 查询单条数据
  64. * @param array $search
  65. * @return
  66. */
  67. public static function findOne(array $search)
  68. {
  69. return static::model()::with(
  70. [
  71. 'inviterLevel1' => fn($query) => $query->select('id', 'username', 'nickname'),
  72. 'inviterLevel2' => fn($query) => $query->select('id', 'username', 'nickname'),
  73. 'inviterLevel3' => fn($query) => $query->select('id', 'username', 'nickname'),
  74. ]
  75. )->where(static::getWhere($search))->first();
  76. }
  77. /**
  78. * @description: 查询所有数据
  79. * @param array $search
  80. * @return \Illuminate\Database\Eloquent\Collection
  81. */
  82. public static function findAll(array $search = [])
  83. {
  84. return static::model()::where(static::getWhere($search))->get();
  85. }
  86. /**
  87. * @description: 分页查询
  88. * @param array $search
  89. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  90. */
  91. public static function paginate(array $search = [])
  92. {
  93. $limit = isset($search['limit']) ? $search['limit'] : 15;
  94. $paginator = static::model()::with(
  95. [
  96. 'inviterLevel1' => fn($query) => $query->select('id', 'username', 'nickname'),
  97. 'inviterLevel2' => fn($query) => $query->select('id', 'username', 'nickname'),
  98. 'inviterLevel3' => fn($query) => $query->select('id', 'username', 'nickname'),
  99. ]
  100. )->where(static::getWhere($search))
  101. ->orderBy('id', 'desc')
  102. ->paginate($limit);
  103. $data = $paginator->items();
  104. foreach($data as $k=> $v){
  105. $data[$k]['total_order_count'] = OrderService::model()::where(OrderService::getWhere(['user_id' => $v->id]))->count();
  106. $data[$k]['total_order_profit'] = OrderService::model()::where(OrderService::getWhere(['user_id' => $v->id ,'status' => OrderService::STATUS_YES]))->sum('commission');
  107. $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();
  108. time() - $v['last_active_time'] >180 ? $data[$k]['is_online'] = 0 : $data[$k]['is_online'] = 1;
  109. }
  110. return ['total' => $paginator->total(), 'data' => $data];
  111. }
  112. /**
  113. * @description:
  114. * @param {*} $params
  115. * @return {*}
  116. */
  117. public static function submit($params = [])
  118. {
  119. $result = false;
  120. $msg['code'] = self::NOT;
  121. $msg['msg'] = '';
  122. if(isset($params['password'])){
  123. if(empty($params['password'])){
  124. unset($params['password']);
  125. }
  126. }
  127. if(isset($params['transaction_password'])){
  128. if(empty($params['transaction_password'])){
  129. unset($params['transaction_password']);
  130. }
  131. }
  132. // 2. 判断是否是更新
  133. if (!empty($params['id'])) {
  134. // 更新
  135. $info = self::findOne(['id'=>$params['id']] );
  136. if (!$info) {
  137. $msg['msg'] = '数据不存在!';
  138. }else{
  139. $result = $info->update($params);
  140. $id = $params['id'];
  141. }
  142. } else {
  143. // 创建
  144. $result = $info = self::model()::create($params);
  145. $id = $result->id;
  146. }
  147. if($result){
  148. $msg['code'] = self::YES;
  149. $msg['msg'] = '设置成功';
  150. }else{
  151. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  152. }
  153. return $msg;
  154. }
  155. /**
  156. * @description: 检查IP登录数量
  157. * @param string $ip
  158. * @return int
  159. */
  160. public static function checkIpLimit($ip)
  161. {
  162. // $count = static::model()::where(self::getWhere(['last_login_ip' => $ip]))->count();
  163. // if($count > self::LOGIN_IP_LIMIT){
  164. // static::model()::where(self::getWhere(['last_login_ip' => $ip]))->update(['status' => self::STATUS_NOT]);
  165. // return false;
  166. // }
  167. return true;
  168. }
  169. /**
  170. * @description: 更新登录iP
  171. * @param {*} $id
  172. * @param {*} $ip
  173. * @return {*}
  174. */
  175. public static function updateLoginIp($id,$ip)
  176. {
  177. static::model()::where('id',$id)->update(['last_login_ip' => $ip,'last_login_at' => date('Y-m-d H:i:s')]);
  178. }
  179. /**
  180. * @description: 调整用户余额
  181. * @param int $userId 用户ID
  182. * @param float $amount 调整金额,正数为增加,负数为减少
  183. * @param string $changeType 变动类型
  184. * @param string $remark 备注
  185. * @return bool
  186. */
  187. public static function adjustBalance($userId, $amount, $changeType, $remark = '')
  188. {
  189. $result = false;
  190. $msg['code'] = self::NOT;
  191. $msg['msg'] = '';
  192. $user = static::model()::where(self::getWhere(['id' => $userId]))->lockForUpdate()->first();
  193. if (!$user) {
  194. $msg['msg'] = '用户不存在';
  195. return $msg;
  196. }
  197. $beforeBalance = $user->balance;
  198. $afterBalance = $beforeBalance + $amount;
  199. if($afterBalance < 0){
  200. $msg['msg'] = '用户余额不足,无法扣款';
  201. return $msg;
  202. }
  203. DB::beginTransaction();
  204. try {
  205. // 更新用户余额
  206. $user->balance = $afterBalance;
  207. $user->save();
  208. // 记录余额变动日志
  209. $log = UserBalanceLogService::addLog(
  210. $userId,
  211. $changeType,
  212. $beforeBalance,
  213. $amount,
  214. $remark
  215. );
  216. if($log['code'] == self::NOT){
  217. DB::rollBack();
  218. return $log;
  219. }
  220. DB::commit();
  221. $msg['code'] = self::YES;
  222. $msg['msg'] = '用户余额调整成功';
  223. return $msg;
  224. } catch (\Exception $e) {
  225. DB::rollBack();
  226. $msg['msg'] = $e->getMessage();
  227. return $msg;
  228. }
  229. }
  230. }