BalanceLogService.php 8.9 KB

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