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.

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