UserBalanceLogService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\UserBalanceLog;
  5. class UserBalanceLogService extends BaseService
  6. {
  7. public static $MODEL = UserBalanceLog::class;
  8. /**
  9. * @description: 获取查询条件
  10. * @param {array} $search 查询内容
  11. * @return {array}
  12. */
  13. public static function getWhere(array $search = []): array
  14. {
  15. $where = [];
  16. if (isset($search['id']) && $search['id'] !== '') {
  17. $where[] = ['id', '=', $search['id']];
  18. }
  19. if (isset($search['user_id']) && $search['user_id'] !== '') {
  20. $where[] = ['user_id', '=', $search['user_id']];
  21. }
  22. if (isset($search['change_type']) && $search['change_type'] !== '') {
  23. $where[] = ['change_type', '=', $search['change_type']];
  24. }
  25. if (isset($search['type']) && $search['type'] !== '') {
  26. if ($search['type'] == self::YES) {
  27. $where[] = ['change_amount', '>', 0];
  28. } else {
  29. $where[] = ['change_amount', '<', 0];
  30. }
  31. }
  32. return $where;
  33. }
  34. /**
  35. * @description: 查询单条数据
  36. * @param array $search
  37. * @return
  38. */
  39. public static function findOne(array $search)
  40. {
  41. return static::model()::where(static::getWhere($search))->first();
  42. }
  43. /**
  44. * @description: 查询所有数据
  45. * @param array $search
  46. * @return \Illuminate\Database\Eloquent\Collection
  47. */
  48. public static function findAll(array $search = [])
  49. {
  50. return static::model()::where(static::getWhere($search))->get();
  51. }
  52. /**
  53. * @description: 分页查询
  54. * @param array $search
  55. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  56. */
  57. public static function paginate(array $search = [])
  58. {
  59. $limit = isset($search['limit']) ? $search['limit'] : 15;
  60. $paginator = static::model()::where(static::getWhere($search))
  61. ->orderBy('updated_at', 'desc')
  62. ->paginate($limit);
  63. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  64. }
  65. /**
  66. * @description:
  67. * @param {*} $params
  68. * @return {*}
  69. */
  70. public static function submit($params = [])
  71. {
  72. $result = false;
  73. $msg['code'] = self::NOT;
  74. $msg['msg'] = '';
  75. // 2. 判断是否是更新
  76. if (!empty($params['id'])) {
  77. // 更新
  78. $info = self::findOne(['id' => $params['id']]);
  79. if (!$info) {
  80. $msg['msg'] = '数据不存在!';
  81. } else {
  82. $result = $info->update($params);
  83. $id = $params['id'];
  84. }
  85. } else {
  86. // 创建
  87. $result = $info = self::model()::create($params);
  88. $id = $result->id;
  89. }
  90. if ($result) {
  91. $msg['code'] = self::YES;
  92. $msg['msg'] = '设置成功';
  93. } else {
  94. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  95. }
  96. return $msg;
  97. }
  98. /**
  99. * 添加日志
  100. *
  101. * @param int $userId
  102. * @param string|null $changeType 类型
  103. * @param string|null $beforeBalance 变动前余额
  104. * @param string|null $changeAmount 变动金额
  105. * @param string|null $remark 备注
  106. * @return array
  107. */
  108. public static function addLog($userId, $changeType = '', $beforeBalance = '', $changeAmount = '', $remark = '', $withdrawsId = null)
  109. {
  110. $afterBalance = bcadd($beforeBalance, $changeAmount, 4);
  111. $params = [
  112. 'user_id' => $userId,
  113. 'change_type' => $changeType,
  114. 'before_balance' => $beforeBalance,
  115. 'change_amount' => $changeAmount,
  116. 'after_balance' => $afterBalance,
  117. 'remark' => $remark,
  118. 'withdraws_id' => $withdrawsId
  119. ];
  120. return self::submit($params);
  121. }
  122. }