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.

268 lines
8.9 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
  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. class CouponController extends BaseController
  17. {
  18. /**
  19. * 获取用户可领取优惠卷接口
  20. */
  21. public function getSystemCouponUserList(){
  22. $user_id = $this->request->input('user_id');
  23. $receive_type = $this->request->input('receive_type');
  24. $c_ids = CouponUserRecType::where('receive_type',$receive_type)->pluck('system_coupon_user_id');
  25. $nowTime = time();
  26. $cr_ids = CouponRec::where('user_id',$user_id)->pluck('system_coupon_user_id');
  27. $ids = array_merge($c_ids->toArray(),$cr_ids->toArray());
  28. $ids = collect($ids)->unique();
  29. $c = Coupon::where('start_time','<=',$nowTime)
  30. ->where([
  31. ['end_time','>',$nowTime],
  32. ['status','=',1],
  33. ])
  34. ->whereRaw('inventory_use < inventory')
  35. ->whereIn('id',$ids)
  36. ->orderBy('weigh','desc')
  37. ->limit(4)
  38. ->get();
  39. return $this->success(['not_reveive'=>$c]);
  40. }
  41. public function userCouponAccount()
  42. {
  43. $user_id = $this->request->input('user_id');
  44. $nowTime = time();
  45. $userCouponCount = DB::table('ims_system_coupon_user_receive')
  46. ->leftJoin('ims_system_coupon_user', 'ims_system_coupon_user_receive.system_coupon_user_id', '=', 'ims_system_coupon_user.id')
  47. ->count();
  48. // $userCouponCount = CouponRec::with('coupon')->where('user_id',$user_id)->where('usable_start_time','<=',$nowTime)
  49. // ->where('usable_end_time','>',$nowTime)->count();
  50. return $this->success(['total'=>$userCouponCount]);
  51. }
  52. /**
  53. * 用户领取优惠卷
  54. */
  55. public function userReceiveCoupon()
  56. {
  57. $userId = $this->request->input("user_id");
  58. $receiveType = $this->request->input("receive_type");
  59. $ids = $this->request->input("ids");
  60. $test = $this->request->input("test",0);
  61. $ids = is_array($ids)?implode(',',$ids):$ids;
  62. $cps = Coupon::whereIn('id', $ids)->get();
  63. $now = time();
  64. $success = [];
  65. $fail = [];
  66. foreach ($cps as $key => $cp) {
  67. $crt = CouponUserRecType::where('system_coupon_user_id',$cp->id)->first();
  68. //TODO 会有超发情况
  69. $cr = new CouponRec;
  70. $cr->user_id = $userId;
  71. $cr->system_coupon_user_id = $cp->id;
  72. $cr->order_main_id = 0;
  73. $cr->receive_time = $now;
  74. $cr->number = $crt->one_receive_number;
  75. $cr->nnumber_remain = $crt->one_receive_number;
  76. $cr->status = 0;
  77. $cr->update_time = $now;
  78. $cr->receive_type = $receiveType;
  79. if($test){
  80. $fail[] = $cp;
  81. }else{
  82. if($cr->save()){
  83. $success[] = $cp;
  84. }else{
  85. $fail[] = $cp;
  86. }
  87. }
  88. }
  89. return $this->success([
  90. 'success'=>$success,
  91. 'fail'=>$fail,
  92. ]);
  93. }
  94. /**
  95. * 获取用户已经领取的优惠卷列表
  96. */
  97. public function getUserReceiveCouponList()
  98. {
  99. $userId = $this->request->input("user_id");
  100. $nowTime = time();
  101. $couponIds = CouponRec::where('user_id',$userId)
  102. ->whereIn('status',[0,1])
  103. ->orderBy('receive_time','desc')
  104. ->get()
  105. ->pluck('system_coupon_user_id');
  106. $not_expired = [];
  107. $expired = [];
  108. $couponIds = $couponIds->toArray();
  109. $coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get();
  110. foreach ($coupons as $key => $coupon) {
  111. if($coupon->usable_end_time < $nowTime){
  112. $expired[] = $coupon;
  113. }else{
  114. $not_expired[] = $coupon;
  115. }
  116. }
  117. $ret = ['not_expired'=>$not_expired,'expired'=>$expired];
  118. return $this->success($ret);
  119. }
  120. /**
  121. * 获取用户当前订单可用的优惠券列表
  122. * 按分类(1订单 等优惠)分组返回
  123. */
  124. public function getUserAvailableCoupons()
  125. {
  126. // 获取参数
  127. # 订单金额
  128. $orderAmount = $this->request->input('order_amount',0);
  129. # 用户id
  130. $userId = $this->request->input('user_id',0);
  131. # 市场id
  132. $marketId = $this->request->input('market_id',0);
  133. # 类型,1全平台 2线上 3线下,20200718默认全平台
  134. $type = $this->request->input('type',1);
  135. # 店铺类型id
  136. $storetypeId = $this->request->input('storetype_id',0);
  137. // 获取用户优惠券
  138. $currentTime = time();
  139. $data = Db::table('ims_system_coupon_user_receive as receive')
  140. ->select([
  141. 'receive.id as receive_id',
  142. 'receive.user_id',
  143. 'receive.number_remain',
  144. 'coupon.id',
  145. 'coupon.title',
  146. 'coupon.full_amount',
  147. 'coupon.discounts',
  148. 'coupon.usable_start_time',
  149. 'coupon.usable_end_time',
  150. 'coupon.discount_type'
  151. ])
  152. ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
  153. ->where(['receive.user_id' => $userId])
  154. ->where(['receive.user_id' => $userId])
  155. ->whereIn('coupon.type', [1,$type])
  156. ->whereIn('receive.status', [0,1])
  157. ->where('receive.number_remain', '>', 0)
  158. ->where('coupon.full_amount', '<=', $orderAmount)
  159. ->where('coupon.usable_start_time', '<=', $currentTime)
  160. ->where('coupon.usable_end_time', '>=', $currentTime)
  161. ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  162. ->where('coupon.market_id', 'in', [0,$marketId])
  163. ->where(function ($query) use ($storetypeId) {
  164. $query->whereOr(
  165. [
  166. [
  167. ['coupon.type', 'in', [1,2]],
  168. ['coupon.storetype_id', '=', 0]
  169. ],
  170. [
  171. ['coupon.type', 'in', [1,3]],
  172. ['coupon.storetype_id', '=', $storetypeId]
  173. ]
  174. ]
  175. );
  176. })
  177. ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
  178. ->get();
  179. //var_dump($d);
  180. return $this->success($data);
  181. // $coupons = CouponRec::alias('receive')
  182. // ->field([
  183. // 'receive.id 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. // ->withAttr('usable_start_time', function ($value){
  195. // return $value ? date('Y-m-d H:i:s', $value) : '';
  196. // })
  197. // ->withAttr('usable_end_time', function ($value){
  198. // return $value ? date('Y-m-d H:i:s', $value) : '';
  199. // })
  200. // ->join(Coupon::getTable() . ' coupon ', 'coupon.id=receive.system_coupon_user_id')
  201. // ->where(['receive.user_id' => $userId])
  202. // ->whereIn('coupon.type', [1,$type])
  203. // ->whereIn('receive.status', [0,1])
  204. // ->where('receive.number_remain', '>', 0)
  205. // ->where('coupon.full_amount', '<=', $orderAmount)
  206. // ->where('coupon.usable_start_time', '<=', $currentTime)
  207. // ->where('coupon.usable_end_time', '>=', $currentTime)
  208. // ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'))
  209. // ->where('coupon.market_id', 'in', [0,$marketId])
  210. // ->where(function ($query) use ($storetypeId) {
  211. // $query->whereOr(
  212. // [
  213. // [
  214. // ['coupon.type', 'in', [1,2]],
  215. // ['coupon.storetype_id', '=', 0]
  216. // ],
  217. // [
  218. // ['coupon.type', 'in', [1,3]],
  219. // ['coupon.storetype_id', '=', $storetypeId]
  220. // ]
  221. // ]
  222. // );
  223. // })
  224. // ->order('coupon.discounts DESC, coupon.full_amount DESC')
  225. // ->select()
  226. // ->toArray();
  227. // var_dump($coupons);
  228. }
  229. }