BalanceLogService.php 9.0 KB

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