BalanceLogService.php 7.6 KB

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