ajax()) { return datagrid(GiftCardsDataGrid::class)->process(); } return view('gift::admin.index'); } /** * Show the form for creating a new resource. */ public function create(): View { return view('gift::admin.create'); } /** * Store a newly created resource in storage. */ public function store(): JsonResponse|\Illuminate\Http\RedirectResponse { $data = request()->validate([ 'giftcard_amount' => 'required|numeric|min:0', 'customer_id' => 'required|integer', 'expirationdate' => 'required|date_format:Y-m-d', 'giftcard_status' => 'required|in:1,2', ]); $data['giftcard_number'] = $this->generateUniqueGiftCardNumber(); $data['used_giftcard_amount'] = 0; $data['type'] = 1; $data['remaining_giftcard_amount'] = $data['giftcard_amount'] ?? 0; $this->giftCardsRepository->create($data); // 记录使用日志 GiftCardUsageLog::create([ 'giftcard_number' => $data['giftcard_number'], 'customer_id' => $data['customer_id'], 'used_amount' => 0, 'balance_before' => $data['giftcard_amount'], 'balance_after' => $data['giftcard_amount'], 'action_type' => GiftCardUsageLog::ACTION_ADD, 'status' => 'completed', 'notes' => "Administrator addition", ]); if (request()->expectsJson()) { return new JsonResponse([ 'message' => '礼品卡创建成功', ]); } return redirect()->route('admin.gift.index'); } /** * Validate customer email via AJAX and return customer_id */ public function validateEmail(): JsonResponse { $email = request()->input('email'); if (empty($email)) { return new JsonResponse([ 'valid' => true, 'customer_id' => null, 'message' => '', ]); } $customer = Customer::where('email', $email)->first(); if (!$customer) { return new JsonResponse([ 'valid' => false, 'customer_id' => null, 'message' => '该邮箱对应的用户不存在', ], 422); } return new JsonResponse([ 'valid' => true, 'customer_id' => $customer->id, 'message' => '邮箱验证通过', 'customer_name' => $customer->first_name . ' ' . $customer->last_name, ]); } /** * Generate a unique 16-digit gift card number * * @return string */ private function generateUniqueGiftCardNumber() { do { // Generate a random string of characters $randomString = Str::upper(Str::random(16)); // Format the string into groups of four separated by dashes $code = implode('-', str_split($randomString, 4)); // Check if the code already exists in the database $exists = GiftCards::where('giftcard_number', $code)->exists(); } while ($exists); return $code; } /** * Remove the specified resource from storage. */ public function destroy(int $id): JsonResponse { try { $this->giftCardsRepository->delete($id); return new JsonResponse([ 'message' => '礼品卡删除成功', ]); } catch (\Exception $e) { return new JsonResponse([ 'message' => '礼品卡删除失败', ], 400); } } /** * Mass delete resources. */ public function massDelete(): JsonResponse { $indices = request()->input('indices'); if (empty($indices)) { return new JsonResponse([ 'message' => '请选择要删除的礼品卡', ], 422); } try { foreach ($indices as $id) { $this->giftCardsRepository->delete($id); } return new JsonResponse([ 'message' => '选中的礼品卡删除成功', ]); } catch (\Exception $e) { return new JsonResponse([ 'message' => '批量删除失败', ], 400); } } }