CouponController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\CouponFilter;
  4. use App\Http\Requests\CouponRequest;
  5. use App\Http\Resources\CouponResource;
  6. use App\Models\AdminMerchant;
  7. use App\Models\Coupon;
  8. use App\Models\CouponsUserBag;
  9. use App\Models\User;
  10. use App\Utils\Admin;
  11. use App\Utils\QiNiuUpload;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. class CouponController extends Controller
  18. {
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @param CouponFilter $filter
  23. * @param Request $request
  24. * @return \Illuminate\Http\JsonResponse
  25. */
  26. public function index(CouponFilter $filter, Request $request)
  27. {
  28. //
  29. $coupons = Coupon::query()->filter($filter)->where(AdminMerchant::getMerchantWhere())->orderByDesc('id');
  30. $coupons = empty($request->get('all')) ? $coupons->paginate() : $coupons->get();
  31. return $this->ok(CouponResource::collection($coupons));
  32. }
  33. /**
  34. * Show the form for creating a new resource.
  35. *
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function create()
  39. {
  40. //
  41. }
  42. /**
  43. * store
  44. *
  45. * @param CouponRequest $couponRequest
  46. * @param Coupon $coupon
  47. * @return \Illuminate\Http\JsonResponse
  48. * @author Fx
  49. *
  50. */
  51. public function store(CouponRequest $couponRequest, Coupon $coupon)
  52. {
  53. //
  54. $inputs = $couponRequest->validated();
  55. $inputs['grant_start_at'] = Carbon::make($inputs['grant_start_at'])->format('Y-m-d H:i:s');
  56. $inputs['grant_end_at'] = Carbon::make($inputs['grant_end_at'])->format('Y-m-d H:i:s');
  57. $inputs['valid_start_time'] = Carbon::make($inputs['valid_start_time'])->format('Y-m-d H:i:s');
  58. $inputs['valid_end_time'] = Carbon::make($inputs['valid_end_time'])->format('Y-m-d H:i:s');
  59. $inputs['admin_id'] = Admin::user()->id;
  60. $inputs['merchant_id'] = AdminMerchant::putMerchantId();
  61. $coupon = $coupon->create($inputs);
  62. return $this->ok(CouponResource::make($coupon));
  63. }
  64. /**
  65. * Display the specified resource.
  66. *
  67. * @param int $id
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function show($id)
  71. {
  72. //
  73. }
  74. /**
  75. * Show the form for editing the specified resource.
  76. *
  77. * @param int $id
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function edit(Coupon $coupon)
  81. {
  82. //
  83. return $this->ok(CouponResource::make($coupon));
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param \Illuminate\Http\Request $request
  89. * @param int $id
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function update(CouponRequest $couponRequest, Coupon $coupon)
  93. {
  94. //
  95. $inputs = $couponRequest->validated();
  96. $inputs['grant_start_at'] = Carbon::make($inputs['grant_start_at'])->format('Y-m-d H:i:s');
  97. $inputs['grant_end_at'] = Carbon::make($inputs['grant_end_at'])->format('Y-m-d H:i:s');
  98. $inputs['valid_start_time'] = Carbon::make($inputs['valid_start_time'])->format('Y-m-d H:i:s');
  99. $inputs['valid_end_time'] = Carbon::make($inputs['valid_end_time'])->format('Y-m-d H:i:s');
  100. $inputs['admin_id'] = Admin::user()->id;
  101. $coupon->update($inputs);
  102. return $this->ok(CouponResource::make($coupon));
  103. }
  104. /**
  105. * Remove the specified resource from storage.
  106. *
  107. * @param int $id
  108. * @return \Illuminate\Http\Response
  109. */
  110. public function destroy(Coupon $coupon)
  111. {
  112. //
  113. $coupon->delete();
  114. return $this->noContent();
  115. }
  116. /**
  117. * 优惠券图片
  118. *
  119. * @return array ['path'=>'上传图片后返回的绝对路径']
  120. *
  121. * @author Fx
  122. * */
  123. public function uploadImg(Request $request)
  124. {
  125. if($request->hasFile('icon')){
  126. $file = $request->file('icon');
  127. $qiNiuUpload = new QiNiuUpload();
  128. $path = $qiNiuUpload->upload_image('/coupon/img', $file);
  129. return $this->ok(['path' => $path]);
  130. }else{
  131. return $this->error('文件不存在');
  132. }
  133. }
  134. public function giveCouponToUser(Request $request)
  135. {
  136. $mobile = $request->get('mobile') ?? '';
  137. $coupon_id = $request->get('coupon_id') ?? '';
  138. if (empty($mobile) || empty($coupon_id)) return $this->error('参数错误');
  139. $users = User::query()
  140. ->where('mobile', $mobile)
  141. ->first();
  142. if (empty($users)) return $this->error('找不到该用户,请检查手机号是否正确');
  143. $coupon = Coupon::query()
  144. ->where('grant_start_at','<',Carbon::now())
  145. ->where('grant_end_at','>',Carbon::now())
  146. ->where('status',Coupon::STATUS_OK)
  147. ->where(AdminMerchant::getMerchantWhere())
  148. ->find($coupon_id);
  149. if (empty($coupon)) return $this->error('找不到该优惠券或未到发券时间');
  150. $data = [
  151. 'coupon_id' => $coupon_id,
  152. 'type' => CouponsUserBag::TYPE_ADMIN_GIVE,
  153. 'user_id' => $users->id,
  154. 'coupons_data' => $coupon->toArray(),
  155. 'status' => CouponsUserBag::STATUS_OK,
  156. 'valid_type' => $coupon->valid_type,
  157. 'valid_start_time' => $coupon->valid_start_time,
  158. 'valid_end_time' => $coupon->valid_end_time,
  159. 'valid_days' => $coupon->valid_days,
  160. 'grant_start_at' => $coupon->grant_start_at,
  161. 'grant_end_at' => $coupon->grant_end_at,
  162. 'give_admin_id' => Admin::user()->id,
  163. 'order_type' => $coupon->order_type,
  164. 'merchant_id' => AdminMerchant::putMerchantId()
  165. ];
  166. try {
  167. DB::beginTransaction();
  168. CouponsUserBag::create($data);
  169. $take_count = $coupon->take_count;
  170. $quota = $coupon->quota;
  171. if($coupon->is_quota ==Coupon::QUOTA_OK){
  172. if($quota <= $take_count){
  173. DB::rollBack();
  174. return $this->error('赠送已达上限,无法继续赠送');
  175. }
  176. }
  177. $coupon->update(['take_count' => ++$take_count]);
  178. DB::commit();
  179. return $this->ok();
  180. } catch (\Exception $e) {
  181. Log::error($e->getMessage());
  182. DB::rollBack();
  183. return $this->error('赠送错误,请联系管理员');
  184. }
  185. }
  186. }