BalanceLogService.php 8.4 KB

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