GiftController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Longyi\Gift\Http\Controllers\Admin;
  3. use Illuminate\Http\JsonResponse;
  4. use Illuminate\View\View;
  5. use Longyi\Gift\Models\GiftCards;
  6. use Longyi\Gift\Models\GiftCardUsageLog;
  7. use Webkul\Admin\Http\Controllers\Controller;
  8. use Longyi\Gift\DataGrids\GiftCards\GiftCardsDataGrid;
  9. use Longyi\Gift\Repositories\GiftCardsRepository;
  10. use Illuminate\Support\Str;
  11. use Webkul\Customer\Models\Customer;
  12. class GiftController extends Controller
  13. {
  14. /**
  15. * Create a new controller instance.
  16. *
  17. * @return void
  18. */
  19. public function __construct(
  20. protected GiftCardsRepository $giftCardsRepository
  21. ) {
  22. }
  23. /**
  24. * Display a listing of the resource.
  25. */
  26. public function index(): View|JsonResponse
  27. {
  28. if (request()->ajax()) {
  29. return datagrid(GiftCardsDataGrid::class)->process();
  30. }
  31. return view('gift::admin.index');
  32. }
  33. /**
  34. * Show the form for creating a new resource.
  35. */
  36. public function create(): View
  37. {
  38. return view('gift::admin.create');
  39. }
  40. /**
  41. * Store a newly created resource in storage.
  42. */
  43. public function store(): JsonResponse|\Illuminate\Http\RedirectResponse
  44. {
  45. $data = request()->validate([
  46. 'giftcard_amount' => 'required|numeric|min:0',
  47. 'customer_id' => 'required|integer',
  48. 'expirationdate' => 'required|date_format:Y-m-d',
  49. 'giftcard_status' => 'required|in:1,2',
  50. ]);
  51. $data['giftcard_number'] = $this->generateUniqueGiftCardNumber();
  52. $data['used_giftcard_amount'] = 0;
  53. $data['type'] = 1;
  54. $data['remaining_giftcard_amount'] = $data['giftcard_amount'] ?? 0;
  55. $this->giftCardsRepository->create($data);
  56. // 记录使用日志
  57. GiftCardUsageLog::create([
  58. 'giftcard_number' => $data['giftcard_number'],
  59. 'customer_id' => $data['customer_id'],
  60. 'used_amount' => 0,
  61. 'balance_before' => $data['giftcard_amount'],
  62. 'balance_after' => $data['giftcard_amount'],
  63. 'action_type' => GiftCardUsageLog::ACTION_ADD,
  64. 'status' => 'completed',
  65. 'notes' => "Administrator addition",
  66. ]);
  67. if (request()->expectsJson()) {
  68. return new JsonResponse([
  69. 'message' => '礼品卡创建成功',
  70. ]);
  71. }
  72. return redirect()->route('admin.gift.index');
  73. }
  74. /**
  75. * Validate customer email via AJAX and return customer_id
  76. */
  77. public function validateEmail(): JsonResponse
  78. {
  79. $email = request()->input('email');
  80. if (empty($email)) {
  81. return new JsonResponse([
  82. 'valid' => true,
  83. 'customer_id' => null,
  84. 'message' => '',
  85. ]);
  86. }
  87. $customer = Customer::where('email', $email)->first();
  88. if (!$customer) {
  89. return new JsonResponse([
  90. 'valid' => false,
  91. 'customer_id' => null,
  92. 'message' => '该邮箱对应的用户不存在',
  93. ], 422);
  94. }
  95. return new JsonResponse([
  96. 'valid' => true,
  97. 'customer_id' => $customer->id,
  98. 'message' => '邮箱验证通过',
  99. 'customer_name' => $customer->first_name . ' ' . $customer->last_name,
  100. ]);
  101. }
  102. /**
  103. * Generate a unique 16-digit gift card number
  104. *
  105. * @return string
  106. */
  107. private function generateUniqueGiftCardNumber()
  108. {
  109. do {
  110. // Generate a random string of characters
  111. $randomString = Str::upper(Str::random(16));
  112. // Format the string into groups of four separated by dashes
  113. $code = implode('-', str_split($randomString, 4));
  114. // Check if the code already exists in the database
  115. $exists = GiftCards::where('giftcard_number', $code)->exists();
  116. } while ($exists);
  117. return $code;
  118. }
  119. /**
  120. * Remove the specified resource from storage.
  121. */
  122. public function destroy(int $id): JsonResponse
  123. {
  124. try {
  125. $this->giftCardsRepository->delete($id);
  126. return new JsonResponse([
  127. 'message' => '礼品卡删除成功',
  128. ]);
  129. } catch (\Exception $e) {
  130. return new JsonResponse([
  131. 'message' => '礼品卡删除失败',
  132. ], 400);
  133. }
  134. }
  135. /**
  136. * Mass delete resources.
  137. */
  138. public function massDelete(): JsonResponse
  139. {
  140. $indices = request()->input('indices');
  141. if (empty($indices)) {
  142. return new JsonResponse([
  143. 'message' => '请选择要删除的礼品卡',
  144. ], 422);
  145. }
  146. try {
  147. foreach ($indices as $id) {
  148. $this->giftCardsRepository->delete($id);
  149. }
  150. return new JsonResponse([
  151. 'message' => '选中的礼品卡删除成功',
  152. ]);
  153. } catch (\Exception $e) {
  154. return new JsonResponse([
  155. 'message' => '批量删除失败',
  156. ], 400);
  157. }
  158. }
  159. }