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.

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