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.

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