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.

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