MemberCoupon.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\coupon\model;
  11. use app\model\BaseModel;
  12. /**
  13. * 优惠券
  14. */
  15. class MemberCoupon extends BaseModel
  16. {
  17. /**
  18. * 获取会员已领取优惠券优惠券
  19. * @param array $member_id
  20. */
  21. public function getMemberCouponList($member_id, $state, $site_id = 0, $order = "fetch_time desc")
  22. {
  23. $condition = array (
  24. [ "member_id", "=", $member_id ],
  25. [ "state", "=", $state ],
  26. );
  27. if ($site_id > 0) {
  28. $condition[] = [ "site_id", "=", $site_id ];
  29. }
  30. $list = model("promotion_coupon")->getList($condition, "*", $order, '', '', '', 0);
  31. return $this->success($list);
  32. }
  33. /**
  34. * 使用优惠券
  35. * @param $coupon_id
  36. */
  37. public function useMemberCoupon($coupon_id, $member_id, $order_id = 0)
  38. {
  39. //优惠券处理方案
  40. $result = model('promotion_coupon')->update([ 'use_order_id' => $order_id, 'state' => 2, 'use_time' => time() ], [ [ 'coupon_id', '=', $coupon_id ], [ "member_id", "=", $member_id ], [ 'state', '=', 1 ] ]);
  41. if ($result === false) {
  42. return $this->error();
  43. }
  44. return $this->success();
  45. }
  46. /**
  47. * 获取会员已领取优惠券优惠券数量
  48. * @param $member_id
  49. * @param $state
  50. * @param int $site_id
  51. * @return array
  52. */
  53. public function getMemberCouponNum($member_id, $state, $site_id = 0)
  54. {
  55. $condition = array (
  56. [ "member_id", "=", $member_id ],
  57. [ "state", "=", $state ],
  58. );
  59. if ($site_id > 0) {
  60. $condition[] = [ "site_id", "=", $site_id ];
  61. }
  62. $num = model("promotion_coupon")->getCount($condition);
  63. return $this->success($num);
  64. }
  65. /**
  66. * 会员是否可领取该优惠券
  67. */
  68. public function receivedNum($coupon_type_id, $member_id)
  69. {
  70. $received_num = model('promotion_coupon')->getCount([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'member_id', '=', $member_id ] ]);
  71. return $this->success($received_num);
  72. }
  73. /**
  74. * 获取编码
  75. */
  76. public function getCode()
  77. {
  78. return random_keys(8);
  79. }
  80. /**
  81. * 会员批量发送优惠券
  82. */
  83. public function sendCoupon($coupon_type_ids, $site_id, $member_id, $get_type = 4, $is_stock = 0, $is_limit = 1, $related_id = 0)
  84. {
  85. //已选优惠券提交数组
  86. if (!empty($coupon_type_ids)) {
  87. $res = 0;
  88. foreach ($coupon_type_ids as $coupon_type_id) {
  89. $coupon_type_info = model('promotion_coupon_type')->getInfo([ 'coupon_type_id' => $coupon_type_id, 'site_id' => $site_id, 'status' => 1 ]);
  90. if (!empty($coupon_type_info)) {
  91. $data = [
  92. 'coupon_type_id' => $coupon_type_id,
  93. 'site_id' => $site_id,
  94. 'coupon_code' => $this->getCode(),
  95. 'member_id' => $member_id,
  96. 'money' => $coupon_type_info[ 'money' ],
  97. 'state' => 1,
  98. 'get_type' => $get_type,
  99. 'goods_type' => $coupon_type_info[ 'goods_type' ],
  100. 'fetch_time' => time(),
  101. 'coupon_name' => $coupon_type_info[ 'coupon_name' ],
  102. 'at_least' => $coupon_type_info[ 'at_least' ],
  103. 'type' => $coupon_type_info[ 'type' ],
  104. 'discount' => $coupon_type_info[ 'discount' ],
  105. 'discount_limit' => $coupon_type_info[ 'discount_limit' ],
  106. 'goods_ids' => $coupon_type_info[ 'goods_ids' ],
  107. 'related_id' => $related_id
  108. ];
  109. if ($coupon_type_info[ 'validity_type' ] == 0) {
  110. $data[ 'end_time' ] = $coupon_type_info[ 'end_time' ];
  111. } elseif ($coupon_type_info[ 'validity_type' ] == 1) {
  112. $data[ 'end_time' ] = ( time() + $coupon_type_info[ 'fixed_term' ] * 86400 );
  113. }
  114. $res = model('promotion_coupon')->add($data);
  115. if ($is_stock == 0) {
  116. model('promotion_coupon_type')->setInc([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count');
  117. }
  118. }
  119. }
  120. if ($res) {
  121. return $this->success($res);
  122. } else {
  123. return $this->error();
  124. }
  125. } else {
  126. return $this->error();
  127. }
  128. }
  129. /**
  130. * 回收优惠券
  131. * @param array $coupon_list
  132. */
  133. public function recoveryCoupon(array $coupon_list, $site_id)
  134. {
  135. $coupon = [];
  136. foreach ($coupon_list as $coupon_item) {
  137. if (isset($coupon[ $coupon_item['coupon_type_id'] ])) {
  138. array_push($coupon[ $coupon_item['coupon_type_id'] ], $coupon_item['coupon_id']);
  139. } else {
  140. $coupon[ $coupon_item['coupon_type_id'] ] = [ $coupon_item['coupon_id'] ];
  141. }
  142. }
  143. if (!count($coupon)) return $this->error();
  144. model('promotion_coupon')->startTrans();
  145. try {
  146. foreach ($coupon as $coupon_type_id => $coupon_ids) {
  147. $num = model('promotion_coupon')->delete([ ['coupon_id', 'in', $coupon_ids], ['site_id', '=', $site_id], ['state', '=', 1] ]);
  148. if ($num) model('promotion_coupon_type')->setDec([ ['coupon_type_id', '=', $coupon_type_id ] ], 'lead_count', $num);
  149. }
  150. model('promotion_coupon')->commit();
  151. return $this->success();
  152. } catch (\Exception $e) {
  153. model('promotion_coupon')->rollback();
  154. return $this->error('', '回收失败');
  155. }
  156. }
  157. }