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. //回水相关
  117. if (in_array($change_type, static::$computeBackFlowChangeTypes)) {
  118. BackflowService::updateOrCreate($memberId, $amount);
  119. }
  120. $data = [];
  121. $data['member_id'] = $memberId;
  122. $data['amount'] = $amount;
  123. $data['before_balance'] = $before_balance;
  124. $data['after_balance'] = $after_balance;
  125. $data['change_type'] = $change_type;
  126. $data['related_id'] = $related_id;
  127. $data['remark'] = $remark;
  128. if ($room_id) $data['room_id'] = $room_id;
  129. return static::$MODEL::create($data);
  130. }
  131. /**
  132. * @description: 获取今日流水
  133. * @param int $memberId
  134. * @param string|null $date
  135. * @return {*}
  136. */
  137. public static function getTodayFlowing($memberId, $date = null)
  138. {
  139. if (!$date) {
  140. $date = date('Y-m-d');
  141. }
  142. $startTime = date('Y-m-d H:i:s', strtotime($date . ' 00:00:00'));
  143. $endTime = date('Y-m-d H:i:s', strtotime($date . ' 23:59:59'));
  144. $flow = static::model()::where('member_id', $memberId)
  145. ->whereBetween('created_at', [$startTime, $endTime])
  146. ->where('change_type', '投注')
  147. ->sum('amount');
  148. $refund = static::model()::where('member_id', $memberId)
  149. ->whereBetween('created_at', [$startTime, $endTime])
  150. ->where('change_type', '返水')
  151. ->sum('amount');
  152. $profit = static::model()::where('member_id', $memberId)
  153. ->whereBetween('created_at', [$startTime, $endTime])
  154. ->where('change_type', '中奖') //嬴正数 输负数
  155. ->sum('amount');
  156. $walletInfo = WalletService::model()::where('member_id', $memberId)->first();
  157. $text = '';
  158. $text .= "当日流水: " . number_format((0 - $flow), 2) . "\n";
  159. $text .= "返水额度: " . number_format($refund, 2) . "\n";
  160. $text .= "当日盈亏: " . number_format(($profit + $refund + $flow), 2) . "\n";
  161. $text .= "余额: " . number_format($walletInfo['available_balance'], 2) . "\n";
  162. return [
  163. 'chat_id' => $memberId,
  164. 'text' => $text
  165. ];
  166. }
  167. public static function getFlowingHistory($memberId, $messageId = null, $page = 1, $limit = 5)
  168. {
  169. $dateTime = date('Y-m-d 00:00:00');
  170. $list = [];
  171. for ($i = 0; $i < $limit; $i++) {
  172. $newIndex = ($page - 1) * $limit + $i;
  173. $date = date('Y-m-d', strtotime($dateTime . " -{$newIndex} day"));
  174. $startTime = date('Y-m-d H:i:s', strtotime($date . ' 00:00:00'));
  175. $endTime = date('Y-m-d H:i:s', strtotime($date . ' 23:59:59'));
  176. $flow = static::model()::where('member_id', $memberId)
  177. ->whereBetween('created_at', [$startTime, $endTime])
  178. ->where('change_type', '投注')
  179. ->sum('amount');
  180. $refund = static::model()::where('member_id', $memberId)
  181. ->whereBetween('created_at', [$startTime, $endTime])
  182. ->where('change_type', '返水')
  183. ->sum('amount');
  184. $profit = static::model()::where('member_id', $memberId)
  185. ->whereBetween('created_at', [$startTime, $endTime])
  186. ->where('change_type', '中奖') //嬴正数 输负数
  187. ->sum('amount');
  188. $list[] = [
  189. 'date' => $date,
  190. 'flow' => number_format($flow, 2),
  191. 'refund' => number_format($refund, 2),
  192. 'profit' => number_format(($profit + $refund + $flow), 2)
  193. ];
  194. }
  195. // $startTime = date('Y-m-d H:i:s',strtotime($date . ' 00:00:00'));
  196. // $endTime = date('Y-m-d H:i:s',strtotime($date . ' 23:59:59'));
  197. // $list = static::model()::where('member_id', $memberId)
  198. // ->whereBetween('created_at', [$startTime, $endTime])
  199. // ->orderBy('created_at', 'desc')
  200. // ->get();
  201. $text = lang("流水历史") . " \n";
  202. foreach ($list as $item) {
  203. $text .= "---------------------\n";
  204. $text .= lang("日期") . ": {$item['date']} \n";
  205. $text .= lang("流水") . ": {$item['flow']} \n";
  206. $text .= lang("返水") . ": {$item['refund']} \n";
  207. $text .= lang("盈利") . ": {$item['profit']} \n";
  208. }
  209. $keyboard = [[
  210. ['text' => "👇" . lang("下一页"), 'callback_data' => "FlowingHistoryPage@@" . ($page + 1)]
  211. ]];
  212. if ($page > 1) {
  213. array_unshift($keyboard[0], ['text' => "👆" . lang("上一页"), 'callback_data' => "FlowingHistoryPage@@" . ($page - 1)]);
  214. }
  215. return [
  216. 'chat_id' => $memberId,
  217. 'text' => $text,
  218. 'message_id' => $messageId,
  219. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  220. ];
  221. }
  222. }