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.

300 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
  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. $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_use <= u.inventory and 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. $crt = CouponUserRecType::where(
  120. [
  121. 'system_coupon_user_id' => $cp->id,
  122. 'receive_type' => $receiveType
  123. ]
  124. )->first();
  125. $cr = new CouponRec;
  126. $cr->user_id = $userId;
  127. $cr->system_coupon_user_id = $cp->id;
  128. $cr->order_main_id = 0;
  129. $cr->receive_time = $now;
  130. $cr->number = $crt->one_receive_number;
  131. $cr->number_remain = $crt->one_receive_number;
  132. $cr->status = 0;
  133. $cr->update_time = $now;
  134. $cr->receive_type = $receiveType;
  135. //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
  136. if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){
  137. $fail[] = $cp;
  138. }else{
  139. $cp->inventory_use += $cr->number;//记录已领取的数量
  140. if ($cr->save()&&$cp->save()) {
  141. $success[] = $cp;
  142. } else {
  143. $fail[] = $cp;
  144. }
  145. }
  146. }
  147. });
  148. return $this->success([
  149. 'success' => $success,
  150. 'fail' => $fail,
  151. ]);
  152. }
  153. /**
  154. * 获取用户已经领取的优惠卷列表
  155. */
  156. public function getUserReceiveCouponList()
  157. {
  158. $userId = $this->request->input("user_id");
  159. $not_expired = [];
  160. $expired = [];
  161. if ($this->empty($userId)) {
  162. return $this->success(['not_expired' => $not_expired, 'expired' => $expired]);
  163. }
  164. $nowTime = time();
  165. $coupons = Db::table('ims_system_coupon_user_receive as receive')
  166. ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id')
  167. ->where([
  168. ['receive.user_id','=',$userId],
  169. ])
  170. ->whereIn('receive.status',[0,1])
  171. ->select('u.*','receive.number_remain')
  172. ->orderBy('u.weigh','desc')
  173. ->get();
  174. foreach ($coupons as $key => $coupon) {
  175. if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) {
  176. $expired[] = $coupon;
  177. } else {
  178. $not_expired[] = $coupon;
  179. }
  180. }
  181. $ret = ['not_expired' => $not_expired, 'expired' => $expired];
  182. return $this->success($ret);
  183. }
  184. /**
  185. * 获取用户当前订单可用的优惠券列表
  186. * 按分类(1订单 等优惠)分组返回
  187. */
  188. public function getUserAvailableCoupons()
  189. {
  190. // 获取参数
  191. # 订单金额
  192. $orderAmount = $this->request->input('order_amount', 0);
  193. # 用户id
  194. $userId = $this->request->input('user_id', 0);
  195. # 市场id
  196. $marketId = $this->request->input('market_id', 0);
  197. # 类型,1全平台 2线上 3线下,20200718默认全平台
  198. $type = $this->request->input('type', 1);
  199. # 店铺类型id
  200. $storetypeId = $this->request->input('storetype_id', 0);
  201. $storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
  202. $available = [];
  203. $notAvailable = [];
  204. if ($this->empty($orderAmount) || $this->empty($userId)) {
  205. return $this->success([
  206. 'available' => $available,
  207. 'not_available' => array_values($notAvailable)
  208. ]);
  209. }
  210. // 获取用户优惠券
  211. $currentTime = time();
  212. $data = Db::table('ims_system_coupon_user_receive as receive')
  213. ->select([
  214. 'receive.id as receive_id',
  215. 'receive.user_id',
  216. 'receive.number_remain',
  217. 'coupon.id',
  218. 'coupon.title',
  219. 'coupon.full_amount',
  220. 'coupon.discounts',
  221. 'coupon.usable_start_time',
  222. 'coupon.usable_end_time',
  223. 'coupon.discount_type'
  224. ])
  225. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
  226. ->where(['receive.user_id' => $userId])
  227. ->whereIn('receive.status', [0,1])
  228. ->where('receive.number_remain', '>', 0)
  229. ->whereIn('coupon.type', [1,$type])
  230. ->where('coupon.full_amount', '<=', $orderAmount)
  231. ->where('coupon.usable_start_time', '<=', $currentTime)
  232. ->where('coupon.usable_end_time', '>=', $currentTime)
  233. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  234. ->where('coupon.market_id', 'in', [0, $marketId])
  235. ->whereIn('coupon.storetype_id', $storetypeIds)
  236. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  237. ->get();
  238. // 分离用户今天用过的优惠券种类
  239. $container = ApplicationContext::getContainer();
  240. $redis = $container->get(Redis::class);
  241. $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
  242. foreach ($data as $key => &$item) {
  243. if (in_array($item->id, $couponIds)) {
  244. $notAvailable[$item->id] = $item;
  245. } else {
  246. $available[] = $item;
  247. }
  248. }
  249. return $this->success([
  250. 'available' => $available,
  251. 'not_available' => array_values($notAvailable)
  252. ]);
  253. }
  254. }