BalanceLogService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\BalanceLog;
  5. // 余额额变动记录
  6. class BalanceLogService extends BaseService
  7. {
  8. /**
  9. * @description: 模型
  10. * @return {string}
  11. */
  12. public static function model(): string
  13. {
  14. return BalanceLog::class;
  15. }
  16. /**
  17. * @description: 枚举
  18. * @return {*}
  19. */
  20. public static function enum(): string
  21. {
  22. return '';
  23. }
  24. /**
  25. * @description: 获取查询条件
  26. * @param {array} $search 查询内容
  27. * @return {array}
  28. */
  29. public static function getWhere(array $search = []): array
  30. {
  31. $where = [];
  32. if (isset($search['coin']) && !empty($search['coin'])) {
  33. $where[] = ['coin', '=', $search['coin']];
  34. }
  35. if (isset($search['net']) && !empty($search['net'])) {
  36. $where[] = ['net', '=', $search['net']];
  37. }
  38. if (isset($search['address']) && !empty($search['address'])) {
  39. $where[] = ['address', '=', $search['address']];
  40. }
  41. if (isset($search['id']) && !empty($search['id'])) {
  42. $where[] = ['id', '=', $search['id']];
  43. }
  44. if (isset($search['member_id']) && !empty($search['member_id'])) {
  45. $where[] = ['member_id', '=', $search['member_id']];
  46. }
  47. return $where;
  48. }
  49. /**
  50. * @description: 查询单条数据
  51. * @param array $search
  52. * @return \App\Models\Coin|null
  53. */
  54. public static function findOne(array $search): ?BalanceLog
  55. {
  56. return self::model()::where(self::getWhere($search))->first();
  57. }
  58. /**
  59. * @description: 查询所有数据
  60. * @param array $search
  61. * @return \Illuminate\Database\Eloquent\Collection
  62. */
  63. public static function findAll(array $search = [])
  64. {
  65. return self::model()::where(self::getWhere($search))->get();
  66. }
  67. /**
  68. * @description: 分页查询
  69. * @param array $search
  70. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  71. */
  72. public static function paginate(array $search = [])
  73. {
  74. $limit = isset($search['limit']) ? $search['limit'] : 15;
  75. $paginator = self::model()::where(self::getWhere($search))
  76. ->orderBy('updated_at', 'desc')
  77. ->paginate($limit);
  78. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  79. }
  80. /**
  81. * @description: 生成资金变动日志
  82. * @param {*} $memberId
  83. * @param {*} $amount
  84. * @param {*} $before_balance
  85. * @param {*} $after_balance
  86. * @param {*} $change_type
  87. * @param {*} $related_id
  88. * @param {*} $remark
  89. * @return {*}
  90. */
  91. public static function addLog($memberId, $amount, $before_balance, $after_balance, $change_type, $related_id, $remark, $room_id = null)
  92. {
  93. $data = [];
  94. $data['member_id'] = $memberId;
  95. $data['amount'] = $amount;
  96. $data['before_balance'] = $before_balance;
  97. $data['after_balance'] = $after_balance;
  98. $data['change_type'] = $change_type;
  99. $data['related_id'] = $related_id;
  100. $data['remark'] = $remark;
  101. if ($room_id) $data['room_id'] = $room_id;
  102. return self::model()::create($data);
  103. }
  104. }