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.

256 lines
8.5 KiB

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