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.

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