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.

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