Coupon.php 6.6 KB

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