You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
8.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://doc.hyperf.io
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Controller;
  12. use App\Model\CouponUserRecType;
  13. use App\Model\Coupon;
  14. use App\Model\CouponRec;
  15. use Hyperf\DbConnection\Db;
  16. use Hyperf\Redis\Redis;
  17. use Hyperf\Utils\ApplicationContext;
  18. class CouponController extends BaseController
  19. {
  20. /**
  21. * 获取用户可领取优惠卷接口
  22. */
  23. public function getSystemCouponUserList()
  24. {
  25. $user_id = $this->request->input('user_id');
  26. $receive_type = $this->request->input('receive_type');
  27. $c_ids = CouponUserRecType::where('receive_type', $receive_type)->pluck('system_coupon_user_id');
  28. $nowTime = time();
  29. $cr_ids = CouponRec::where(
  30. 'user_id',$user_id
  31. )->pluck('system_coupon_user_id');
  32. $ids = array_merge($c_ids->toArray(), $cr_ids->toArray());
  33. //领过券的ID
  34. $ids = collect($ids)->unique();
  35. $c = Db::table('ims_system_coupon_user as u')
  36. ->where([
  37. ['u.end_time','>',$nowTime],
  38. ['u.start_time','<=',$nowTime],
  39. ['type.receive_type','=',$receive_type],
  40. ['u.status','=',1],
  41. ])
  42. ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id')
  43. ->whereRaw('inventory_use <= inventory')
  44. ->whereIn('u.id',$ids)
  45. ->select('u.*','type.one_receive_number')
  46. ->orderBy('u.weigh','desc')
  47. ->orderByRaw('FIELD(u.id, '.implode(", " , $ids).')')
  48. ->limit(4)
  49. ->get();
  50. return $this->success(['not_reveive'=>$c]);
  51. }
  52. //统计用户
  53. public function userCouponAccount()
  54. {
  55. $userId = $this->request->input("user_id");
  56. $nowTime = time();
  57. $num = Db::table('ims_system_coupon_user_receive as receive')
  58. ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
  59. ->where([
  60. ['receive.user_id','=',$userId],
  61. ['receive.number_remain','>',0],
  62. ['u.usable_end_time','>',$nowTime],
  63. ])
  64. ->whereIn('receive.status',[0,1])
  65. ->sum('receive.number_remain');
  66. return $this->success(['total' => $num]);
  67. }
  68. /**
  69. * 用户领取优惠卷
  70. */
  71. public function userReceiveCoupon()
  72. {
  73. $userId = $this->request->input("user_id");
  74. $receiveType = $this->request->input("receive_type");
  75. $ids = $this->request->input("ids");
  76. $test = $this->request->input("test", 0);
  77. $ids = explode(',', $ids);
  78. $cps = Coupon::whereIn('id', $ids)->get();
  79. $now = time();
  80. $success = [];
  81. $fail = [];
  82. foreach ($cps as $key => $cp) {
  83. $crt = CouponUserRecType::where(
  84. [
  85. 'system_coupon_user_id' => $cp->id,
  86. 'receive_type' => $receiveType
  87. ]
  88. )->first();
  89. $cr = new CouponRec;
  90. $cr->user_id = $userId;
  91. $cr->system_coupon_user_id = $cp->id;
  92. $cr->order_main_id = 0;
  93. $cr->receive_time = $now;
  94. $cr->number = $crt->one_receive_number;
  95. $cr->number_remain = $crt->one_receive_number;
  96. $cr->status = 0;
  97. $cr->update_time = $now;
  98. $cr->receive_type = $receiveType;
  99. if ($test && ($cp->id % 2)) {
  100. $fail[] = $cp;
  101. } else {
  102. //TODO 会有超发情况
  103. //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
  104. if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){
  105. $fail[] = $cp;
  106. }else{
  107. $cp->inventory_use += $cr->number;//记录已领取的数量
  108. if ($cr->save()&&$cp->save()) {
  109. $success[] = $cp;
  110. } else {
  111. $fail[] = $cp;
  112. }
  113. }
  114. }
  115. }
  116. return $this->success([
  117. 'success' => $success,
  118. 'fail' => $fail,
  119. ]);
  120. }
  121. /**
  122. * 获取用户已经领取的优惠卷列表
  123. */
  124. public function getUserReceiveCouponList()
  125. {
  126. $userId = $this->request->input("user_id");
  127. $nowTime = time();
  128. $coupons = Db::table('ims_system_coupon_user_receive as receive')
  129. ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
  130. ->where([
  131. ['receive.user_id','=',$userId],
  132. ])
  133. ->whereIn('receive.status',[0,1])
  134. ->select('u.*','receive.number_remain')
  135. ->orderBy('u.weigh','desc')
  136. ->get();
  137. $not_expired = [];
  138. $expired = [];
  139. foreach ($coupons as $key => $coupon) {
  140. if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) {
  141. $expired[] = $coupon;
  142. } else {
  143. $not_expired[] = $coupon;
  144. }
  145. }
  146. $ret = ['not_expired' => $not_expired, 'expired' => $expired];
  147. return $this->success($ret);
  148. }
  149. /**
  150. * 获取用户当前订单可用的优惠券列表
  151. * 按分类(1订单 等优惠)分组返回
  152. */
  153. public function getUserAvailableCoupons()
  154. {
  155. // 获取参数
  156. # 订单金额
  157. $orderAmount = $this->request->input('order_amount', 0);
  158. # 用户id
  159. $userId = $this->request->input('user_id', 0);
  160. # 市场id
  161. $marketId = $this->request->input('market_id', 0);
  162. # 类型,1全平台 2线上 3线下,20200718默认全平台
  163. $type = $this->request->input('type', 1);
  164. # 店铺类型id
  165. $storetypeId = $this->request->input('storetype_id', 0);
  166. $storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
  167. // 获取用户优惠券
  168. $currentTime = time();
  169. $data = Db::table('ims_system_coupon_user_receive as receive')
  170. ->select([
  171. 'receive.id as receive_id',
  172. 'receive.user_id',
  173. 'receive.number_remain',
  174. 'coupon.id',
  175. 'coupon.title',
  176. 'coupon.full_amount',
  177. 'coupon.discounts',
  178. 'coupon.usable_start_time',
  179. 'coupon.usable_end_time',
  180. 'coupon.discount_type'
  181. ])
  182. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
  183. ->where(['receive.user_id' => $userId])
  184. ->whereIn('receive.status', [0,1])
  185. ->where('receive.number_remain', '>', 0)
  186. ->whereIn('coupon.type', [1,$type])
  187. ->where('coupon.full_amount', '<=', $orderAmount)
  188. ->where('coupon.usable_start_time', '<=', $currentTime)
  189. ->where('coupon.usable_end_time', '>=', $currentTime)
  190. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  191. ->where('coupon.market_id', 'in', [0, $marketId])
  192. ->whereIn('coupon.storetype_id', $storetypeIds)
  193. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  194. ->get();
  195. // 剔除用户今天用过的优惠券种类
  196. $container = ApplicationContext::getContainer();
  197. $redis = $container->get(Redis::class);
  198. $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
  199. $available = $data->reject(function ($item) use ($couponIds) {
  200. return in_array($item->id, $couponIds);
  201. });
  202. $notAvailable = $data->reject(function ($item) use ($couponIds) {
  203. return !in_array($item->id, $couponIds);
  204. });
  205. return $this->success(['available' => $available, 'not_available' => $notAvailable]);
  206. }
  207. }