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.

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