BalanceLogService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Services;
  3. use App\Models\BalanceLog;
  4. // 余额额变动记录
  5. class BalanceLogService extends BaseService
  6. {
  7. public static $RW = ['充值','提现','人工充值','人工扣款','三方提现','三方充值','资产转移','比比返','投注','回水'];
  8. public static $MODEL= BalanceLog::class;
  9. /**
  10. * @description: 获取查询条件
  11. * @param {array} $search 查询内容
  12. * @return {array}
  13. */
  14. public static function getWhere(array $search = []): array
  15. {
  16. $where = [];
  17. if (isset($search['coin']) && !empty($search['coin'])) {
  18. $where[] = ['coin', '=', $search['coin']];
  19. }
  20. if (isset($search['net']) && !empty($search['net'])) {
  21. $where[] = ['net', '=', $search['net']];
  22. }
  23. if (isset($search['address']) && !empty($search['address'])) {
  24. $where[] = ['address', '=', $search['address']];
  25. }
  26. if (isset($search['id']) && !empty($search['id'])) {
  27. $where[] = ['id', '=', $search['id']];
  28. }
  29. if (isset($search['member_id']) && !empty($search['member_id'])) {
  30. $where[] = ['member_id', '=', $search['member_id']];
  31. }
  32. return $where;
  33. }
  34. /**
  35. * @description: 查询单条数据
  36. * @param array $search
  37. * @return \App\Models\Coin|null
  38. */
  39. public static function findOne(array $search): ?BalanceLog
  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. ->when(isset($search['change_types']) && !empty($search['change_types']), function ($query) use ($search) {
  62. return $query->whereIn('change_type', $search['change_types']);
  63. })
  64. ->orderBy('updated_at', 'desc')
  65. ->paginate($limit);
  66. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  67. }
  68. /**
  69. * @description: 生成资金变动日志
  70. * @param {*} $memberId
  71. * @param {*} $amount
  72. * @param {*} $before_balance
  73. * @param {*} $after_balance
  74. * @param {*} $change_type
  75. * @param {*} $related_id
  76. * @param {*} $remark
  77. * @return {*}
  78. */
  79. public static function addLog($memberId, $amount, $before_balance, $after_balance, $change_type, $related_id, $remark, $room_id = null)
  80. {
  81. $data = [];
  82. $data['member_id'] = $memberId;
  83. $data['amount'] = $amount;
  84. $data['before_balance'] = $before_balance;
  85. $data['after_balance'] = $after_balance;
  86. $data['change_type'] = $change_type;
  87. $data['related_id'] = $related_id;
  88. $data['remark'] = $remark;
  89. if ($room_id) $data['room_id'] = $room_id;
  90. return static::model()::create($data);
  91. }
  92. /**
  93. * @description: 获取今日流水
  94. * @param int $memberId
  95. * @param string|null $date
  96. * @return {*}
  97. */
  98. public static function getTodayFlowing($memberId, $date = null)
  99. {
  100. if (!$date) {
  101. $date = date('Y-m-d');
  102. }
  103. $startTime = date('Y-m-d H:i:s', strtotime($date . ' 00:00:00'));
  104. $endTime = date('Y-m-d H:i:s', strtotime($date . ' 23:59:59'));
  105. $flow = static::model()::where('member_id', $memberId)
  106. ->whereBetween('created_at', [$startTime, $endTime])
  107. ->where('change_type', '投注')
  108. ->sum('amount');
  109. $refund = static::model()::where('member_id', $memberId)
  110. ->whereBetween('created_at', [$startTime, $endTime])
  111. ->where('change_type', '返水')
  112. ->sum('amount');
  113. $profit = static::model()::where('member_id', $memberId)
  114. ->whereBetween('created_at', [$startTime, $endTime])
  115. ->where('change_type', '中奖') //嬴正数 输负数
  116. ->sum('amount');
  117. $walletInfo = WalletService::model()::where('member_id', $memberId)->first();
  118. $text = '';
  119. $text .= "当日流水: " . number_format((0-$flow), 2) . "\n";
  120. $text .= "返水额度: " . number_format($refund, 2) . "\n";
  121. $text .= "当日盈亏: " . number_format(($profit + $refund + $flow), 2) . "\n";
  122. $text .= "余额: " . number_format($walletInfo['available_balance'], 2) . "\n";
  123. return [
  124. 'chat_id' => $memberId,
  125. 'text' => $text
  126. ];
  127. }
  128. public static function getFlowingHistory($memberId, $messageId = null, $page = 1, $limit = 5)
  129. {
  130. $dateTime = date('Y-m-d 00:00:00');
  131. $list = [];
  132. for ($i = 0; $i < $limit; $i++) {
  133. $newIndex = ($page - 1) * $limit + $i;
  134. $date = date('Y-m-d', strtotime($dateTime . " -{$newIndex} day"));
  135. $startTime = date('Y-m-d H:i:s', strtotime($date . ' 00:00:00'));
  136. $endTime = date('Y-m-d H:i:s', strtotime($date . ' 23:59:59'));
  137. $flow = static::model()::where('member_id', $memberId)
  138. ->whereBetween('created_at', [$startTime, $endTime])
  139. ->where('change_type', '投注')
  140. ->sum('amount');
  141. $refund = static::model()::where('member_id', $memberId)
  142. ->whereBetween('created_at', [$startTime, $endTime])
  143. ->where('change_type', '返水')
  144. ->sum('amount');
  145. $profit = static::model()::where('member_id', $memberId)
  146. ->whereBetween('created_at', [$startTime, $endTime])
  147. ->where('change_type', '中奖') //嬴正数 输负数
  148. ->sum('amount');
  149. $list[] = [
  150. 'date' => $date,
  151. 'flow' => number_format($flow, 2),
  152. 'refund' => number_format($refund, 2),
  153. 'profit' => number_format(($profit + $refund + $flow), 2)
  154. ];
  155. }
  156. // $startTime = date('Y-m-d H:i:s',strtotime($date . ' 00:00:00'));
  157. // $endTime = date('Y-m-d H:i:s',strtotime($date . ' 23:59:59'));
  158. // $list = static::model()::where('member_id', $memberId)
  159. // ->whereBetween('created_at', [$startTime, $endTime])
  160. // ->orderBy('created_at', 'desc')
  161. // ->get();
  162. $text = lang("流水历史")." \n";
  163. foreach ($list as $item) {
  164. $text .= "---------------------\n";
  165. $text .= lang("日期").": {$item['date']} \n";
  166. $text .= lang("流水").": {$item['flow']} \n";
  167. $text .= lang("返水").": {$item['refund']} \n";
  168. $text .= lang("盈利").": {$item['profit']} \n";
  169. }
  170. $keyboard = [[
  171. ['text' => "👇".lang("下一页"), 'callback_data' => "FlowingHistoryPage@@" . ($page + 1)]
  172. ]];
  173. if ($page > 1) {
  174. array_unshift($keyboard[0], ['text' => "👆".lang("上一页"), 'callback_data' => "FlowingHistoryPage@@" . ($page - 1)]);
  175. }
  176. return [
  177. 'chat_id' => $memberId,
  178. 'text' => $text,
  179. 'message_id' => $messageId,
  180. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  181. ];
  182. }
  183. }