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.

210 lines
7.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
  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.title','u.discounts','u.full_amount','u.discount_type','u.introduce','u.usable_end_time','receive.number_remain')
  132. ->orderBy('u.weigh','desc')
  133. ->get();
  134. foreach ($coupons as $key => $coupon) {
  135. //拼接满减文字提示
  136. $coupon->full_amount_text = '满' . $coupon->full_amount . "可用";
  137. //判断是折扣优惠券还是满减优惠券
  138. if($coupon->discount_type == 1){
  139. $coupon->discounts_text = '¥'.$coupon->discounts;
  140. }elseif($coupon->discount_type == 2){
  141. $coupon->discounts_text = floatval($coupon->discounts)."";
  142. }
  143. //失效时间格式转换
  144. $usable_end_time = date('Y-m-d H:i:s',$coupon->usable_end_time);
  145. $coupon->usable_end_time_text = '有效期至:'.$usable_end_time;
  146. if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) {
  147. $expired[] = $coupon;
  148. } else {
  149. $not_expired[] = $coupon;
  150. }
  151. }
  152. $ret = ['not_expired' => $not_expired, 'expired' => $expired];
  153. return $this->success($ret);
  154. }
  155. /**
  156. * 获取用户当前订单可用的优惠券列表
  157. * 按分类(1订单 等优惠)分组返回
  158. */
  159. public function getUserAvailableCoupons()
  160. {
  161. // 获取参数
  162. # 订单金额
  163. $orderAmount = $this->request->input('order_amount', 0);
  164. # 用户id
  165. $userId = $this->request->input('user_id', 0);
  166. # 市场id
  167. $marketId = $this->request->input('market_id', 0);
  168. # 类型,1全平台 2线上 3线下,20200718默认全平台
  169. $type = $this->request->input('type', 1);
  170. # 店铺类型id
  171. $storetypeId = $this->request->input('storetype_id', 0);
  172. # 购物车商品id
  173. $carIds = $this->request->input('car_ids', 0);
  174. $res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds);
  175. return $this->success($res);
  176. }
  177. }