BalanceLogService.php 9.6 KB

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