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.

257 lines
8.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. ['receive_type',"=",$receive_type]
  32. ])->pluck('system_coupon_user_id');
  33. // $ids = array_merge($c_ids->toArray(), $cr_ids->toArray());
  34. //
  35. // //领过券的ID
  36. // $ids = collect($ids)->unique();
  37. $c_ids = $c_ids->toArray();
  38. $cr_ids = $cr_ids->toArray();
  39. // $ids = [];
  40. // foreach ($c_ids as $key => $val){
  41. // if(!in_array($val,$cr_ids)) {
  42. // $ids[] = $val;
  43. // }
  44. // }
  45. // print_r($ids);print_r($c_ids);print_r($cr_ids);
  46. $c = Db::table('ims_system_coupon_user as u')
  47. ->where([
  48. ['u.end_time','>',$nowTime],
  49. ['u.start_time','<=',$nowTime],
  50. ['type.receive_type','=',$receive_type],
  51. ['u.status','=',1],
  52. ])
  53. ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id')
  54. ->whereRaw('u.inventory_use <= u.inventory and u.inventory-u.inventory_use > type.one_receive_number')
  55. ->whereIn('u.id',$c_ids)
  56. ->whereNotIn('u.id',$cr_ids)
  57. ->select('u.*','type.one_receive_number')
  58. ->orderBy('u.weigh','desc')
  59. // ->orderByRaw('FIELD(u.id, '.implode(", " , $ids).')')
  60. ->limit(4)
  61. ->get();
  62. return $this->success(['not_reveive'=>$c]);
  63. }
  64. //统计用户
  65. public function userCouponAccount()
  66. {
  67. $userId = $this->request->input("user_id");
  68. $nowTime = time();
  69. $num = Db::table('ims_system_coupon_user_receive as receive')
  70. ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
  71. ->where([
  72. ['receive.user_id','=',$userId],
  73. ['receive.number_remain','>',0],
  74. ['u.usable_end_time','>',$nowTime],
  75. ])
  76. ->whereIn('receive.status',[0,1])
  77. ->sum('receive.number_remain');
  78. return $this->success(['total' => $num]);
  79. }
  80. /**
  81. * 用户领取优惠卷
  82. */
  83. public function userReceiveCoupon()
  84. {
  85. $userId = $this->request->input("user_id");
  86. $receiveType = $this->request->input("receive_type");
  87. $ids = $this->request->input("ids");
  88. $ids = explode(',', $ids);
  89. $now = time();
  90. $success = [];
  91. $fail = [];
  92. Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$now) {
  93. //读写锁,完全控制,性能底
  94. $cps = Coupon::whereIn('id', $ids)->lockForUpdate()->get();
  95. //写锁,可能在高并发下,读取到脏数据,写入可能有超发情况
  96. //$cps = Coupon::whereIn('id', $ids)->sharedLock()->get();
  97. foreach ($cps as $key => $cp) {
  98. $crt = CouponUserRecType::where(
  99. [
  100. 'system_coupon_user_id' => $cp->id,
  101. 'receive_type' => $receiveType
  102. ]
  103. )->first();
  104. $cr = new CouponRec;
  105. $cr->user_id = $userId;
  106. $cr->system_coupon_user_id = $cp->id;
  107. $cr->order_main_id = 0;
  108. $cr->receive_time = $now;
  109. $cr->number = $crt->one_receive_number;
  110. $cr->number_remain = $crt->one_receive_number;
  111. $cr->status = 0;
  112. $cr->update_time = $now;
  113. $cr->receive_type = $receiveType;
  114. //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
  115. if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){
  116. $fail[] = $cp;
  117. }else{
  118. $cp->inventory_use += $cr->number;//记录已领取的数量
  119. if ($cr->save()&&$cp->save()) {
  120. $success[] = $cp;
  121. } else {
  122. $fail[] = $cp;
  123. }
  124. }
  125. }
  126. });
  127. return $this->success([
  128. 'success' => $success,
  129. 'fail' => $fail,
  130. ]);
  131. }
  132. /**
  133. * 获取用户已经领取的优惠卷列表
  134. */
  135. public function getUserReceiveCouponList()
  136. {
  137. $userId = $this->request->input("user_id");
  138. $nowTime = time();
  139. $coupons = Db::table('ims_system_coupon_user_receive as receive')
  140. ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
  141. ->where([
  142. ['receive.user_id','=',$userId],
  143. ])
  144. ->whereIn('receive.status',[0,1])
  145. ->select('u.*','receive.number_remain')
  146. ->orderBy('u.weigh','desc')
  147. ->get();
  148. $not_expired = [];
  149. $expired = [];
  150. foreach ($coupons as $key => $coupon) {
  151. if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) {
  152. $expired[] = $coupon;
  153. } else {
  154. $not_expired[] = $coupon;
  155. }
  156. }
  157. $ret = ['not_expired' => $not_expired, 'expired' => $expired];
  158. return $this->success($ret);
  159. }
  160. /**
  161. * 获取用户当前订单可用的优惠券列表
  162. * 按分类(1订单 等优惠)分组返回
  163. */
  164. public function getUserAvailableCoupons()
  165. {
  166. // 获取参数
  167. # 订单金额
  168. $orderAmount = $this->request->input('order_amount', 0);
  169. # 用户id
  170. $userId = $this->request->input('user_id', 0);
  171. # 市场id
  172. $marketId = $this->request->input('market_id', 0);
  173. # 类型,1全平台 2线上 3线下,20200718默认全平台
  174. $type = $this->request->input('type', 1);
  175. # 店铺类型id
  176. $storetypeId = $this->request->input('storetype_id', 0);
  177. $storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
  178. // 获取用户优惠券
  179. $currentTime = time();
  180. $data = Db::table('ims_system_coupon_user_receive as receive')
  181. ->select([
  182. 'receive.id as receive_id',
  183. 'receive.user_id',
  184. 'receive.number_remain',
  185. 'coupon.id',
  186. 'coupon.title',
  187. 'coupon.full_amount',
  188. 'coupon.discounts',
  189. 'coupon.usable_start_time',
  190. 'coupon.usable_end_time',
  191. 'coupon.discount_type'
  192. ])
  193. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
  194. ->where(['receive.user_id' => $userId])
  195. ->whereIn('receive.status', [0,1])
  196. ->where('receive.number_remain', '>', 0)
  197. ->whereIn('coupon.type', [1,$type])
  198. ->where('coupon.full_amount', '<=', $orderAmount)
  199. ->where('coupon.usable_start_time', '<=', $currentTime)
  200. ->where('coupon.usable_end_time', '>=', $currentTime)
  201. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  202. ->where('coupon.market_id', 'in', [0, $marketId])
  203. ->whereIn('coupon.storetype_id', $storetypeIds)
  204. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  205. ->get();
  206. // 分离用户今天用过的优惠券种类
  207. $container = ApplicationContext::getContainer();
  208. $redis = $container->get(Redis::class);
  209. $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
  210. $available = [];
  211. $notAvailable = [];
  212. foreach ($data as $key => &$item) {
  213. if (in_array($item->id, $couponIds)) {
  214. $notAvailable[] = $item;
  215. } else {
  216. $available[] = $item;
  217. }
  218. }
  219. return $this->success([
  220. 'available' => $available,
  221. 'not_available' => $notAvailable
  222. ]);
  223. }
  224. }