BalanceLogService.php 9.3 KB

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