Coupon.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\store\shopapi\controller;
  13. use app\model\member\MemberCoupon;
  14. use app\model\system\Promotion as PromotionModel;
  15. class Coupon extends BaseStoreApi
  16. {
  17. /**
  18. * 列表
  19. */
  20. public function lists()
  21. {
  22. $from_type = $this->params[ 'from_type' ] ?? '';
  23. $page = $this->params[ 'page' ] ?? 1;
  24. $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
  25. $from_type_id = $this->params[ 'from_type_id' ] ?? 0;
  26. $from_type_name = $this->params[ 'from_type_name' ] ?? '';
  27. $name = $this->params[ 'name' ] ?? '';
  28. $start_time = $this->params[ 'fetch_time_start' ] ?? '';
  29. $end_time = $this->params[ 'fetch_time_end' ] ?? '';
  30. $type = $this->params[ 'type' ] ?? '';
  31. $use_type = $this->params[ 'use_type' ] ?? '';
  32. $coupon_model = new MemberCoupon();
  33. $condition = [
  34. [ 'site_id', "=", $this->site_id ],
  35. ];
  36. if (!empty($from_type)) {
  37. $condition[] = [ "from_type", "=", $from_type ];
  38. if (!empty($from_type_id)) {
  39. $condition[] = [ "from_type_id", "=", $from_type_id ];
  40. }
  41. }
  42. if (!empty($from_type_name)) {
  43. $condition[] = [ "from_type_name", 'like', '%' . $from_type_name . '%' ];
  44. }
  45. if (!empty($type)) {
  46. $condition[] = [ "type", '=', $type ];
  47. }
  48. if (!empty($use_type)) {
  49. $condition[] = [ "use_type", '=', $use_type ];
  50. }
  51. if ($name !== '') {
  52. $condition[] = [ "name", 'like', '%' . $name . '%' ];
  53. }
  54. if (!empty($start_time) && empty($end_time)) {
  55. $condition[] = [ 'create_time', '>=', date_to_time($start_time) ];
  56. } elseif (empty($start_time) && !empty($end_time)) {
  57. $condition[] = [ "create_time", "<=", date_to_time($end_time) ];
  58. } elseif (!empty($start_time) && !empty($end_time)) {
  59. $condition[] = [ 'create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
  60. }
  61. $list = $coupon_model->getCouponPageList($condition, $page, $page_size, 'id desc');
  62. return $this->response($list);
  63. }
  64. /**
  65. * 顶部活动列表
  66. */
  67. public function couponList()
  68. {
  69. $promotion_model = new PromotionModel();
  70. $promotion_type_list = $promotion_model->getPromotionType('member');
  71. $coupon_list = [];
  72. foreach ($promotion_type_list as $k => $v) {
  73. if ($v[ 'name' ] == 'sitecoupon') {
  74. $coupon_list [ $v[ 'name' ] ] = $v;
  75. }
  76. }
  77. return $this->response($this->success($coupon_list));
  78. }
  79. /**
  80. * 账户优惠券数据
  81. */
  82. public function couponAccount()
  83. {
  84. $coupon_model = new MemberCoupon();
  85. $site_id = $this->site_id;
  86. //累计发放优惠券数
  87. $total_info = $coupon_model->getMemberCouponInfo([ [ 'site_id', '=', $site_id ] ], 'count(coupon_id) as count_coupon')[ 'data' ];
  88. //累计使用
  89. $use_info = $coupon_model->getMemberCouponInfo([ [ 'site_id', '=', $site_id ], [ 'state', '=', 2 ] ], 'count(coupon_id) as count_coupon')[ 'data' ];
  90. //剩余
  91. $surplus_info = $coupon_model->getMemberCouponInfo([ [ 'site_id', '=', $site_id ], [ 'state', '=', 1 ] ], 'count(coupon_id) as count_coupon')[ 'data' ];
  92. $data = [];
  93. $data[ 'coupon_count' ] = $total_info[ 'count_coupon' ] ?? 0;
  94. $data[ 'use_coupon' ] = $use_info[ 'count_coupon' ] ?? 0;
  95. $data[ 'surplus_coupon' ] = $surplus_info[ 'count_coupon' ] ?? 0;
  96. return $this->response($this->success($data));
  97. }
  98. /**
  99. * 会员优惠券
  100. */
  101. public function receivelists()
  102. {
  103. $site_id = $this->site_id;
  104. $from_type = $this->params[ 'from_type' ] ?? '';
  105. $page = $this->params[ 'page' ] ?? 1;
  106. $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
  107. $from_type_id = $this->params[ 'from_type_id' ] ?? 0;
  108. $from_type_name = $this->params[ 'from_type_name' ] ?? '';
  109. $coupon_code = $this->params[ 'coupon_code' ] ?? '';
  110. $start_time = $this->params[ 'fetch_time_start' ] ?? '';
  111. $end_time = $this->params[ 'fetch_time_end' ] ?? '';
  112. $type = $this->params[ 'type' ] ?? '';
  113. $parent_id = $this->params[ 'parent_id' ] ?? '';
  114. $nick_name = $this->params[ 'nickname' ] ?? '';
  115. $state = $this->params[ 'state' ] ?? '';
  116. $coupon_model = new MemberCoupon();
  117. $condition = [
  118. [ 'site_id', "=", $site_id ],
  119. ];
  120. if (!empty($from_type)) {
  121. $condition[] = [ "from_type", "=", $from_type ];
  122. if (!empty($from_type_id)) {
  123. $condition[] = [ "from_type_id", "=", $from_type_id ];
  124. }
  125. }
  126. if (!empty($from_type_name)) {
  127. $condition[] = [ "from_type_name", 'like', '%' . $from_type_name . '%' ];
  128. }
  129. if (!empty($parent_id)) {
  130. $condition[] = [ "parent_id", "=", $parent_id ];
  131. }
  132. if (!empty($coupon_code)) {
  133. $condition[] = [ "coupon_code", 'like', '%' . $coupon_code . '%' ];
  134. }
  135. if (!empty($nick_name)) {
  136. $condition[] = [ "member_name", 'like', '%' . $nick_name . '%' ];
  137. }
  138. if (!empty($type)) {
  139. $condition[] = [ "type", '=', $type ];
  140. }
  141. if ($state !== '') {
  142. $condition[] = [ "state", '=', $state ];
  143. }
  144. if (!empty($start_time) && empty($end_time)) {
  145. $condition[] = [ 'fetch_time', '>=', date_to_time($start_time) ];
  146. } elseif (empty($start_time) && !empty($end_time)) {
  147. $condition[] = [ "fetch_time", "<=", date_to_time($end_time) ];
  148. } elseif (!empty($start_time) && !empty($end_time)) {
  149. $condition[] = [ 'fetch_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
  150. }
  151. $list = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
  152. return $this->response($list);
  153. }
  154. /**
  155. * 优惠券详情
  156. */
  157. public function detail()
  158. {
  159. $id = $this->params[ 'id' ] ?? 0;
  160. $site_id = $this->site_id;
  161. $coupon_model = new MemberCoupon();
  162. $info = $coupon_model->getCouponDetail($id, $site_id);
  163. return $this->response($info);
  164. }
  165. }