BalanceLogService.php 9.3 KB

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