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.

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