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.

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