BalanceLogService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Services;
  3. use App\Models\BalanceLog;
  4. // 余额额变动记录
  5. class BalanceLogService extends BaseService
  6. {
  7. public static $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. return $where;
  56. }
  57. /**
  58. * @description: 查询单条数据
  59. * @param array $search
  60. * @return \App\Models\Coin|null
  61. */
  62. public static function findOne(array $search): ?BalanceLog
  63. {
  64. return static::model()::where(static::getWhere($search))->first();
  65. }
  66. /**
  67. * @description: 查询所有数据
  68. * @param array $search
  69. * @return \Illuminate\Database\Eloquent\Collection
  70. */
  71. public static function findAll(array $search = [])
  72. {
  73. return static::model()::where(static::getWhere($search))->get();
  74. }
  75. /**
  76. * @description: 分页查询
  77. * @param array $search
  78. * @return array
  79. */
  80. public static function paginate(array $search = []): array
  81. {
  82. $limit = isset($search['limit']) ? $search['limit'] : 15;
  83. $paginator = static::model()::where(static::getWhere($search))
  84. ->when(isset($search['change_types']) && !empty($search['change_types']), function ($query) use ($search) {
  85. return $query->whereIn('change_type', $search['change_types']);
  86. })
  87. ->orderBy('updated_at', 'desc')
  88. ->paginate($limit);
  89. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  90. }
  91. /**
  92. * @description: 生成资金变动日志
  93. * @param {*} $memberId
  94. * @param {*} $amount
  95. * @param {*} $before_balance
  96. * @param {*} $after_balance
  97. * @param {*} $change_type
  98. * @param {*} $related_id
  99. * @param {*} $remark
  100. * @return {*}
  101. */
  102. public static function addLog($memberId, $amount, $before_balance, $after_balance, $change_type, $related_id, $remark, $room_id = null)
  103. {
  104. $data = [];
  105. $data['member_id'] = $memberId;
  106. $data['amount'] = $amount;
  107. $data['before_balance'] = $before_balance;
  108. $data['after_balance'] = $after_balance;
  109. $data['change_type'] = $change_type;
  110. $data['related_id'] = $related_id;
  111. $data['remark'] = $remark;
  112. if ($room_id) $data['room_id'] = $room_id;
  113. return static::model()::create($data);
  114. }
  115. /**
  116. * @description: 获取今日流水
  117. * @param int $memberId
  118. * @param string|null $date
  119. * @return {*}
  120. */
  121. public static function getTodayFlowing($memberId, $date = null)
  122. {
  123. if (!$date) {
  124. $date = date('Y-m-d');
  125. }
  126. $startTime = date('Y-m-d H:i:s', strtotime($date . ' 00:00:00'));
  127. $endTime = date('Y-m-d H:i:s', strtotime($date . ' 23:59:59'));
  128. $flow = static::model()::where('member_id', $memberId)
  129. ->whereBetween('created_at', [$startTime, $endTime])
  130. ->where('change_type', '投注')
  131. ->sum('amount');
  132. $refund = static::model()::where('member_id', $memberId)
  133. ->whereBetween('created_at', [$startTime, $endTime])
  134. ->where('change_type', '返水')
  135. ->sum('amount');
  136. $profit = static::model()::where('member_id', $memberId)
  137. ->whereBetween('created_at', [$startTime, $endTime])
  138. ->where('change_type', '中奖') //嬴正数 输负数
  139. ->sum('amount');
  140. $walletInfo = WalletService::model()::where('member_id', $memberId)->first();
  141. $text = '';
  142. $text .= "当日流水: " . number_format((0 - $flow), 2) . "\n";
  143. $text .= "返水额度: " . number_format($refund, 2) . "\n";
  144. $text .= "当日盈亏: " . number_format(($profit + $refund + $flow), 2) . "\n";
  145. $text .= "余额: " . number_format($walletInfo['available_balance'], 2) . "\n";
  146. return [
  147. 'chat_id' => $memberId,
  148. 'text' => $text
  149. ];
  150. }
  151. public static function getFlowingHistory($memberId, $messageId = null, $page = 1, $limit = 5)
  152. {
  153. $dateTime = date('Y-m-d 00:00:00');
  154. $list = [];
  155. for ($i = 0; $i < $limit; $i++) {
  156. $newIndex = ($page - 1) * $limit + $i;
  157. $date = date('Y-m-d', strtotime($dateTime . " -{$newIndex} day"));
  158. $startTime = date('Y-m-d H:i:s', strtotime($date . ' 00:00:00'));
  159. $endTime = date('Y-m-d H:i:s', strtotime($date . ' 23:59:59'));
  160. $flow = static::model()::where('member_id', $memberId)
  161. ->whereBetween('created_at', [$startTime, $endTime])
  162. ->where('change_type', '投注')
  163. ->sum('amount');
  164. $refund = static::model()::where('member_id', $memberId)
  165. ->whereBetween('created_at', [$startTime, $endTime])
  166. ->where('change_type', '返水')
  167. ->sum('amount');
  168. $profit = static::model()::where('member_id', $memberId)
  169. ->whereBetween('created_at', [$startTime, $endTime])
  170. ->where('change_type', '中奖') //嬴正数 输负数
  171. ->sum('amount');
  172. $list[] = [
  173. 'date' => $date,
  174. 'flow' => number_format($flow, 2),
  175. 'refund' => number_format($refund, 2),
  176. 'profit' => number_format(($profit + $refund + $flow), 2)
  177. ];
  178. }
  179. // $startTime = date('Y-m-d H:i:s',strtotime($date . ' 00:00:00'));
  180. // $endTime = date('Y-m-d H:i:s',strtotime($date . ' 23:59:59'));
  181. // $list = static::model()::where('member_id', $memberId)
  182. // ->whereBetween('created_at', [$startTime, $endTime])
  183. // ->orderBy('created_at', 'desc')
  184. // ->get();
  185. $text = lang("流水历史") . " \n";
  186. foreach ($list as $item) {
  187. $text .= "---------------------\n";
  188. $text .= lang("日期") . ": {$item['date']} \n";
  189. $text .= lang("流水") . ": {$item['flow']} \n";
  190. $text .= lang("返水") . ": {$item['refund']} \n";
  191. $text .= lang("盈利") . ": {$item['profit']} \n";
  192. }
  193. $keyboard = [[
  194. ['text' => "👇" . lang("下一页"), 'callback_data' => "FlowingHistoryPage@@" . ($page + 1)]
  195. ]];
  196. if ($page > 1) {
  197. array_unshift($keyboard[0], ['text' => "👆" . lang("上一页"), 'callback_data' => "FlowingHistoryPage@@" . ($page - 1)]);
  198. }
  199. return [
  200. 'chat_id' => $memberId,
  201. 'text' => $text,
  202. 'message_id' => $messageId,
  203. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  204. ];
  205. }
  206. }