BackflowService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Backflow;
  4. use Carbon\Carbon;
  5. use Exception;
  6. class BackflowService extends BaseService
  7. {
  8. public static string $MODEL = Backflow::class;
  9. public static function findOne(array $search): ?Backflow
  10. {
  11. return self::$MODEL::where(static::getWhere($search))->first();
  12. }
  13. /**
  14. * 进行回水操作
  15. * @param $id
  16. * @return bool
  17. * @throws Exception
  18. */
  19. public static function grant($id): bool
  20. {
  21. return RewardGrantService::grant('backflow', (int) $id, (string) (request()->user->username ?? 'system'));
  22. }
  23. /**
  24. * 只记录当月充值/提现,回水金额由 rewards:sync-bases 按分类亏损和规则计算。
  25. * @param $memberId string 会员ID
  26. * @param $changeAmount float 充值或提现金额
  27. * @return Backflow
  28. */
  29. public static function updateOrCreate(string $memberId, float $changeAmount): Backflow
  30. {
  31. $date = Carbon::now('Asia/Shanghai')->format('Y-m');
  32. $field = $changeAmount > 0 ? 'recharge_amount' : 'withdrawal_amount';
  33. $backflow = static::$MODEL::firstOrCreate(['date' => $date, 'member_id' => $memberId]);
  34. if (in_array((int) $backflow->status, Backflow::OPEN_STATUSES, true)) $backflow->increment($field, $changeAmount);
  35. return $backflow->refresh();
  36. }
  37. /** 已发放 / 待发放 / 是否可发放(回水可在当月多次发放差额) */
  38. public static function grantFields(array $row): array
  39. {
  40. $amount = bcadd((string) ($row['amount'] ?? 0), '0', 2);
  41. $granted = bcadd((string) ($row['granted_amount'] ?? 0), '0', 2);
  42. $open = in_array((int) ($row['status'] ?? 0), Backflow::OPEN_STATUSES, true);
  43. $pending = $open && bccomp($amount, $granted, 2) > 0 ? bcsub($amount, $granted, 2) : '0.00';
  44. $month = Carbon::now('Asia/Shanghai')->format('Y-m');
  45. return [
  46. 'amount' => $amount,
  47. 'granted_amount' => $granted,
  48. 'pending_amount' => $pending,
  49. // 当月需有差额才能发;往月未结清的记录即使差额为 0 也可点发放以结清
  50. 'can_grant' => $open && (bccomp($pending, '0', 2) > 0 || (string) ($row['date'] ?? '') < $month),
  51. 'status_text' => [Backflow::STATUS_PENDING => '未发放', Backflow::STATUS_GRANTED => '已发放', Backflow::STATUS_PARTIAL => '部分发放'][(int) ($row['status'] ?? 0)] ?? '',
  52. ];
  53. }
  54. public static function getWhere(array $search = []): array
  55. {
  56. $where = [];
  57. if (isset($search['id']) && !empty($search['id'])) {
  58. $where[] = ['backflow.id', '=', $search['id']];
  59. }
  60. if (isset($search['member_id']) && !empty($search['member_id'])) {
  61. $where[] = ['backflow.member_id', '=', $search['member_id']];
  62. }
  63. if (isset($search['date']) && !empty($search['date'])) {
  64. $where[] = ['backflow.date', '=', $search['date']];
  65. }
  66. if (isset($search['status']) && $search['status'] != '') {
  67. $where[] = ['backflow.status', '=', $search['status']];
  68. }
  69. if (!empty($search['account'])) {
  70. $where[] = ['users.account', 'like', '%' . trim((string)$search['account']) . '%'];
  71. }
  72. if (!empty($search['first_name'])) {
  73. $where[] = ['users.first_name','like', '%'.$search['first_name'].'%'];
  74. }
  75. return $where;
  76. }
  77. public static function paginate(array $search = []): array
  78. {
  79. // 当月(上海时间)的记录也展示实时统计;后台可手动提前发放,自动发放仍等到下个月,见 RewardGrantService::grant
  80. $month = Carbon::now('Asia/Shanghai')->format('Y-m');
  81. $query = static::$MODEL::join('users', 'users.member_id', '=', 'backflow.member_id')
  82. ->where(static::getWhere($search))
  83. ->where('backflow.date', '<=', $month);
  84. $paginator = $query->select("backflow.*", "users.first_name", "users.account", "users.admin_note as user_admin_note")->orderByDesc('date')->orderBy('status')
  85. ->paginate($search['limit']);
  86. foreach ($paginator->items() as $item) {
  87. foreach (self::grantFields($item->toArray()) as $field => $value) $item[$field] = $value;
  88. }
  89. // $count = $query->count();
  90. // $query->with(['user'])->orderByDesc('date')->orderBy('status');
  91. // $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
  92. // return ['total' => $count, 'data' => $list];
  93. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  94. }
  95. }