BalanceLogService.php 8.5 KB

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