Coupon.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\api\model\plus\coupon;
  3. use app\common\model\plus\coupon\Coupon as CouponModel;
  4. /**
  5. * 优惠券模型
  6. */
  7. class Coupon extends CouponModel
  8. {
  9. /**
  10. * 隐藏字段
  11. */
  12. protected $hidden = [
  13. 'receive_num',
  14. 'is_delete',
  15. 'create_time',
  16. 'update_time',
  17. ];
  18. /**
  19. * 获取用户优惠券总数量(可用)
  20. */
  21. public function getCount($user_id)
  22. {
  23. return $this->where('user_id', '=', $user_id)
  24. ->where('is_use', '=', 0)
  25. ->where('is_expire', '=', 0)
  26. ->count();
  27. }
  28. /**
  29. * 获取用户优惠券ID集
  30. */
  31. public function getUserCouponIds($user_id)
  32. {
  33. return $this->where('user_id', '=', $user_id)->column('coupon_id');
  34. }
  35. /**
  36. * 领取优惠券
  37. */
  38. public function receive($user, $coupon_id)
  39. {
  40. // 获取优惠券信息
  41. $coupon = Coupon::detail($coupon_id);
  42. // 验证优惠券是否可领取
  43. if (!$this->checkReceive()) {
  44. return false;
  45. }
  46. // 添加领取记录
  47. return $this->add($user, $coupon);
  48. }
  49. /**
  50. * 添加领取记录
  51. */
  52. private function add($user, $coupon)
  53. {
  54. // 计算有效期
  55. if ($coupon['expire_type'] == 10) {
  56. $start_time = time();
  57. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  58. } else {
  59. $start_time = $coupon['start_time']['value'];
  60. $end_time = $coupon['end_time']['value'];
  61. }
  62. // 整理领取记录
  63. $data = [
  64. 'coupon_id' => $coupon['coupon_id'],
  65. 'name' => $coupon['name'],
  66. 'color' => $coupon['color']['value'],
  67. 'coupon_type' => $coupon['coupon_type']['value'],
  68. 'reduce_price' => $coupon['reduce_price'],
  69. 'discount' => $coupon->getData('discount'),
  70. 'min_price' => $coupon['min_price'],
  71. 'expire_type' => $coupon['expire_type'],
  72. 'expire_day' => $coupon['expire_day'],
  73. 'start_time' => $start_time,
  74. 'end_time' => $end_time,
  75. 'apply_range' => $coupon['apply_range'],
  76. 'user_id' => $user['user_id'],
  77. 'app_id' => self::$app_id
  78. ];
  79. return $this->transaction(function () use ($data, $coupon) {
  80. // 添加领取记录
  81. $status = $this->save($data);
  82. if ($status) {
  83. // 更新优惠券领取数量
  84. $coupon->setIncReceiveNum();
  85. }
  86. return $status;
  87. });
  88. }
  89. /**
  90. * 获取优惠券列表
  91. */
  92. public function getList($user = false, $limit = null, $only_receive = false)
  93. {
  94. $model = $this;
  95. // 构造查询条件
  96. $model = $model->where('is_delete', '=', 0);
  97. // 只显示可领取(未过期,未发完)的优惠券
  98. if ($only_receive) {
  99. $model = $model->where(' IF ( `total_num` > - 1, `receive_num` < `total_num`, 1 = 1 )')
  100. ->where('IF ( `expire_type` = 20, (`end_time` + 86400) >= ' . time() . ', 1 = 1 )');
  101. }
  102. // 优惠券列表
  103. $couponList = $model->order(['sort' => 'asc', 'create_time' => 'desc'])->limit($limit)->select();
  104. // 获取用户已领取的优惠券
  105. if ($user !== false) {
  106. $CouponModel = new UserCoupon();
  107. $userCouponIds = $CouponModel->getUserCouponIds($user['user_id']);
  108. foreach ($couponList as $key => $item) {
  109. $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userCouponIds);
  110. }
  111. }
  112. return $couponList;
  113. }
  114. /**
  115. * 待领取优惠券
  116. */
  117. public function getWaitList($user = false)
  118. {
  119. $model = $this;
  120. // 构造查询条件
  121. $model = $model->where('is_delete', '=', 0);
  122. /*$model = $model->where(' IF ( `total_num` > - 1, `receive_num` <= `total_num`, 1 = 1 )')
  123. ->where('IF ( `expire_type` = 20, `end_time` >= ' . time() . ', 1 = 1 )');*/
  124. $model = $model//->where('total_num', '<>', -1)
  125. //->where('receive_num', '<', 'total_num')
  126. ->whereRaw('(total_num = -1) OR (receive_num < total_num)')
  127. ->whereRaw('(end_time = 0) OR (end_time > unix_timestamp())');
  128. $CouponModel = new UserCoupon();
  129. $userCouponIds = $CouponModel->getUserCouponIds($user['user_id']);
  130. $model = $model->where('coupon_id', 'not in', implode(',', $userCouponIds));
  131. // 是否显示在领券中心
  132. $model = $model->where('show_center', '=', 1);
  133. // 用户可领取优惠券列表
  134. return $model->order(['sort' => 'asc', 'create_time' => 'desc'])->select();
  135. }
  136. /**
  137. * 验证优惠券是否可领取
  138. */
  139. public function checkReceive()
  140. {
  141. if ($this['total_num'] != -1 && $this['receive_num'] >= $this['total_num']) {
  142. $this->error = '优惠券已发完';
  143. return false;
  144. }
  145. if ($this['expire_type'] == 20 && ($this->getData('end_time') + 86400) < time()) {
  146. $this->error = '优惠券已过期';
  147. return false;
  148. }
  149. return true;
  150. }
  151. /**
  152. * 累计已领取数量
  153. */
  154. public function setIncReceiveNum()
  155. {
  156. return $this->where('coupon_id','=',$this['coupon_id'])->inc('receive_num')->update();
  157. }
  158. public function getWhereData($coupon_arr)
  159. {
  160. return $this->where('coupon_id', 'in', $coupon_arr)->select();
  161. }
  162. /**
  163. * 查询指定优惠券
  164. */
  165. public function getCoupon($value)
  166. {
  167. return $this->where('coupon_id', 'in', $value)->select();
  168. }
  169. /**
  170. * 查询单个优惠券信息
  171. * @param $value
  172. */
  173. public function getCouponInfo($value)
  174. {
  175. return $this->where('coupon_id', '=', $value)->find();
  176. }
  177. }