123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Filters\CouponFilter;
- use App\Http\Requests\CouponRequest;
- use App\Http\Resources\CouponResource;
- use App\Models\AdminMerchant;
- use App\Models\Coupon;
- use App\Models\CouponsUserBag;
- use App\Models\User;
- use App\Utils\Admin;
- use App\Utils\QiNiuUpload;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class CouponController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @param CouponFilter $filter
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function index(CouponFilter $filter, Request $request)
- {
- //
- $coupons = Coupon::query()->filter($filter)->where(AdminMerchant::getMerchantWhere())->orderByDesc('id');
- $coupons = empty($request->get('all')) ? $coupons->paginate() : $coupons->get();
- return $this->ok(CouponResource::collection($coupons));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * store
- *
- * @param CouponRequest $couponRequest
- * @param Coupon $coupon
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function store(CouponRequest $couponRequest, Coupon $coupon)
- {
- //
- $inputs = $couponRequest->validated();
- $inputs['grant_start_at'] = Carbon::make($inputs['grant_start_at'])->format('Y-m-d H:i:s');
- $inputs['grant_end_at'] = Carbon::make($inputs['grant_end_at'])->format('Y-m-d H:i:s');
- $inputs['valid_start_time'] = Carbon::make($inputs['valid_start_time'])->format('Y-m-d H:i:s');
- $inputs['valid_end_time'] = Carbon::make($inputs['valid_end_time'])->format('Y-m-d H:i:s');
- $inputs['admin_id'] = Admin::user()->id;
- $inputs['merchant_id'] = AdminMerchant::putMerchantId();
- $coupon = $coupon->create($inputs);
- return $this->ok(CouponResource::make($coupon));
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(Coupon $coupon)
- {
- //
- return $this->ok(CouponResource::make($coupon));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(CouponRequest $couponRequest, Coupon $coupon)
- {
- //
- $inputs = $couponRequest->validated();
- $inputs['grant_start_at'] = Carbon::make($inputs['grant_start_at'])->format('Y-m-d H:i:s');
- $inputs['grant_end_at'] = Carbon::make($inputs['grant_end_at'])->format('Y-m-d H:i:s');
- $inputs['valid_start_time'] = Carbon::make($inputs['valid_start_time'])->format('Y-m-d H:i:s');
- $inputs['valid_end_time'] = Carbon::make($inputs['valid_end_time'])->format('Y-m-d H:i:s');
- $inputs['admin_id'] = Admin::user()->id;
- $coupon->update($inputs);
- return $this->ok(CouponResource::make($coupon));
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy(Coupon $coupon)
- {
- //
- $coupon->delete();
- return $this->noContent();
- }
- /**
- * 优惠券图片
- *
- * @return array ['path'=>'上传图片后返回的绝对路径']
- *
- * @author Fx
- * */
- public function uploadImg(Request $request)
- {
- if($request->hasFile('icon')){
- $file = $request->file('icon');
- $qiNiuUpload = new QiNiuUpload();
- $path = $qiNiuUpload->upload_image('/coupon/img', $file);
- return $this->ok(['path' => $path]);
- }else{
- return $this->error('文件不存在');
- }
- }
- public function giveCouponToUser(Request $request)
- {
- $mobile = $request->get('mobile') ?? '';
- $coupon_id = $request->get('coupon_id') ?? '';
- if (empty($mobile) || empty($coupon_id)) return $this->error('参数错误');
- $users = User::query()
- ->where('mobile', $mobile)
- ->first();
- if (empty($users)) return $this->error('找不到该用户,请检查手机号是否正确');
- $coupon = Coupon::query()
- ->where('grant_start_at','<',Carbon::now())
- ->where('grant_end_at','>',Carbon::now())
- ->where('status',Coupon::STATUS_OK)
- ->where(AdminMerchant::getMerchantWhere())
- ->find($coupon_id);
- if (empty($coupon)) return $this->error('找不到该优惠券或未到发券时间');
- $data = [
- 'coupon_id' => $coupon_id,
- 'type' => CouponsUserBag::TYPE_ADMIN_GIVE,
- 'user_id' => $users->id,
- 'coupons_data' => $coupon->toArray(),
- 'status' => CouponsUserBag::STATUS_OK,
- 'valid_type' => $coupon->valid_type,
- 'valid_start_time' => $coupon->valid_start_time,
- 'valid_end_time' => $coupon->valid_end_time,
- 'valid_days' => $coupon->valid_days,
- 'grant_start_at' => $coupon->grant_start_at,
- 'grant_end_at' => $coupon->grant_end_at,
- 'give_admin_id' => Admin::user()->id,
- 'order_type' => $coupon->order_type,
- 'merchant_id' => AdminMerchant::putMerchantId()
- ];
- try {
- DB::beginTransaction();
- CouponsUserBag::create($data);
- $take_count = $coupon->take_count;
- $quota = $coupon->quota;
- if($coupon->is_quota ==Coupon::QUOTA_OK){
- if($quota <= $take_count){
- DB::rollBack();
- return $this->error('赠送已达上限,无法继续赠送');
- }
- }
- $coupon->update(['take_count' => ++$take_count]);
- DB::commit();
- return $this->ok();
- } catch (\Exception $e) {
- Log::error($e->getMessage());
- DB::rollBack();
- return $this->error('赠送错误,请联系管理员');
- }
- }
- }
|