| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace Longyi\Gift\Http\Controllers\Admin;
- use Illuminate\Http\JsonResponse;
- use Illuminate\View\View;
- use Longyi\Gift\Models\GiftCards;
- use Longyi\Gift\Models\GiftCardUsageLog;
- use Webkul\Admin\Http\Controllers\Controller;
- use Longyi\Gift\DataGrids\GiftCards\GiftCardsDataGrid;
- use Longyi\Gift\Repositories\GiftCardsRepository;
- use Illuminate\Support\Str;
- use Webkul\Customer\Models\Customer;
- class GiftController extends Controller
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct(
- protected GiftCardsRepository $giftCardsRepository
- ) {
- }
- /**
- * Display a listing of the resource.
- */
- public function index(): View|JsonResponse
- {
- if (request()->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);
- }
- }
- }
|