CouponController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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->where('grant_start_at', '<', Carbon::now())
  31. ->where('grant_end_at', '>', Carbon::now())->get();
  32. return $this->ok(CouponResource::collection($coupons));
  33. }
  34. /**
  35. * Show the form for creating a new resource.
  36. *
  37. * @return \Illuminate\Http\Response
  38. */
  39. public function create()
  40. {
  41. //
  42. }
  43. /**
  44. * store
  45. *
  46. * @param CouponRequest $couponRequest
  47. * @param Coupon $coupon
  48. * @return \Illuminate\Http\JsonResponse
  49. * @author Fx
  50. *
  51. */
  52. public function store(CouponRequest $couponRequest, Coupon $coupon)
  53. {
  54. //
  55. $inputs = $couponRequest->validated();
  56. $inputs['grant_start_at'] = Carbon::make($inputs['grant_start_at'])->format('Y-m-d H:i:s');
  57. $inputs['grant_end_at'] = Carbon::make($inputs['grant_end_at'])->format('Y-m-d H:i:s');
  58. $inputs['valid_start_time'] = Carbon::make($inputs['valid_start_time'])->format('Y-m-d H:i:s');
  59. $inputs['valid_end_time'] = Carbon::make($inputs['valid_end_time'])->format('Y-m-d H:i:s');
  60. $inputs['admin_id'] = Admin::user()->id;
  61. $inputs['merchant_id'] = AdminMerchant::putMerchantId();
  62. $coupon = $coupon->create($inputs);
  63. return $this->ok(CouponResource::make($coupon));
  64. }
  65. /**
  66. * Display the specified resource.
  67. *
  68. * @param int $id
  69. * @return \Illuminate\Http\Response
  70. */
  71. public function show($id)
  72. {
  73. //
  74. }
  75. /**
  76. * Show the form for editing the specified resource.
  77. *
  78. * @param int $id
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function edit(Coupon $coupon)
  82. {
  83. //
  84. return $this->ok(CouponResource::make($coupon));
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @param int $id
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function update(CouponRequest $couponRequest, Coupon $coupon)
  94. {
  95. //
  96. $inputs = $couponRequest->validated();
  97. $inputs['grant_start_at'] = Carbon::make($inputs['grant_start_at'])->format('Y-m-d H:i:s');
  98. $inputs['grant_end_at'] = Carbon::make($inputs['grant_end_at'])->format('Y-m-d H:i:s');
  99. $inputs['valid_start_time'] = Carbon::make($inputs['valid_start_time'])->format('Y-m-d H:i:s');
  100. $inputs['valid_end_time'] = Carbon::make($inputs['valid_end_time'])->format('Y-m-d H:i:s');
  101. $inputs['admin_id'] = Admin::user()->id;
  102. $coupon->update($inputs);
  103. return $this->ok(CouponResource::make($coupon));
  104. }
  105. /**
  106. * Remove the specified resource from storage.
  107. *
  108. * @param int $id
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function destroy(Coupon $coupon)
  112. {
  113. //
  114. $coupon->delete();
  115. return $this->noContent();
  116. }
  117. /**
  118. * 优惠券图片
  119. *
  120. * @return array ['path'=>'上传图片后返回的绝对路径']
  121. *
  122. * @author Fx
  123. * */
  124. public function uploadImg(Request $request)
  125. {
  126. if ($request->hasFile('icon')) {
  127. $file = $request->file('icon');
  128. $qiNiuUpload = new QiNiuUpload();
  129. $path = $qiNiuUpload->upload_image('/coupon/img', $file);
  130. return $this->ok(['path' => $path]);
  131. } else {
  132. return $this->error('文件不存在');
  133. }
  134. }
  135. public function giveCouponToUser(Request $request)
  136. {
  137. $mobile = $request->get('mobile') ?? '';
  138. $coupon_id = $request->get('coupon_id') ?? '';
  139. $user_id = $request->get('id', 0);
  140. if (empty($mobile) || !$user_id || empty($coupon_id)) return $this->error('参数错误');
  141. $users = User::query()->where('id', $user_id)
  142. ->where('mobile', $mobile)
  143. ->first();
  144. if (empty($users)) return $this->error('找不到该用户');
  145. $coupon = Coupon::query()
  146. ->where('grant_start_at', '<', Carbon::now())
  147. ->where('grant_end_at', '>', Carbon::now())
  148. ->where('status', Coupon::STATUS_OK)
  149. ->where(AdminMerchant::getMerchantWhere())
  150. ->find($coupon_id);
  151. if (empty($coupon)) return $this->error('找不到该优惠券或未到发券时间');
  152. $valid_end_time = $coupon->valid_end_time;
  153. switch ($coupon->valid_type) {
  154. case Coupon::VALID_TYPE_ABSOLUTELY:
  155. //绝对失效
  156. break;
  157. case Coupon::VALID_TYPE_RELATIVE:
  158. //相对失效
  159. $valid_end_time = Carbon::now()->addDays($coupon->valid_days)->toDateTimeString();
  160. break;
  161. case Coupon::VALID_TYPE_FOR_EVER:
  162. $valid_end_time = Carbon::now()->addYears(10)->toDateTimeString();
  163. //永久
  164. break;
  165. }
  166. $data = [
  167. 'coupon_id' => $coupon_id,
  168. 'type' => CouponsUserBag::TYPE_ADMIN_GIVE,
  169. 'user_id' => $users->id,
  170. 'coupons_data' => $coupon->toArray(),
  171. 'coupon_type' => $coupon->type,
  172. 'status' => CouponsUserBag::STATUS_OK,
  173. 'valid_type' => $coupon->valid_type,
  174. 'valid_start_time' => $coupon->valid_start_time,
  175. 'valid_end_time' => $valid_end_time,
  176. 'valid_days' => $coupon->valid_days,
  177. 'grant_start_at' => $coupon->grant_start_at,
  178. 'grant_end_at' => $coupon->grant_end_at,
  179. 'give_admin_id' => Admin::user()->id,
  180. 'order_type' => $coupon->order_type,
  181. 'merchant_id' => $users->merchant_id
  182. ];
  183. try {
  184. DB::beginTransaction();
  185. CouponsUserBag::create($data);
  186. $take_count = $coupon->take_count;
  187. $quota = $coupon->quota;
  188. if ($coupon->is_quota == Coupon::QUOTA_OK) {
  189. if ($quota <= $take_count) {
  190. DB::rollBack();
  191. return $this->error('赠送已达上限,无法继续赠送');
  192. }
  193. }
  194. $coupon->update(['take_count' => ++$take_count]);
  195. if ($coupon->type === Coupon::TYPE_DEPOSIT_FREE) {
  196. //免押金卡
  197. User::query()->where('user_id', $data['user_id'])->update(['is_coupon_deposit_free' => User::IS_COUPON_DEPOSIT_FREE_OK]);
  198. }
  199. DB::commit();
  200. return $this->ok();
  201. } catch (\Exception $e) {
  202. Log::error($e->getMessage());
  203. DB::rollBack();
  204. return $this->error('赠送错误,请联系管理员');
  205. }
  206. }
  207. }