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.

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