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.

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