Hongbao.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Hongbao as MemberHongbao;
  14. use app\model\system\Promotion as PromotionModel;
  15. /**
  16. * 红包控制器
  17. * Class Hongbao
  18. * @package addon\shop\siteapi\controller
  19. */
  20. class Hongbao extends BaseStoreApi
  21. {
  22. /**
  23. * 红包
  24. */
  25. public function lists()
  26. {
  27. $hongbao_model = new MemberHongbao();
  28. $site_id = $this->site_id;
  29. $from_type = isset($this->params[ 'from_type' ]) ? $this->params[ 'from_type' ] : '';
  30. $from_type_id = isset($this->params[ 'from_type_id' ]) ? $this->params[ 'from_type_id' ] : 0;
  31. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  32. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  33. $start_time = isset($this->params[ 'start_time' ]) ? $this->params[ 'start_time' ] : '';
  34. $end_time = isset($this->params[ 'end_time' ]) ? $this->params[ 'end_time' ] : '';
  35. $from_type_name = isset($this->params[ 'from_type_name' ]) ? $this->params[ 'from_type_name' ] : '';
  36. $name = isset($this->params[ 'name' ]) ? $this->params[ 'name' ] : '';
  37. $hongbao_type = isset($this->params[ 'hongbao_type' ]) ? $this->params[ 'hongbao_type' ] : '';
  38. $condition = [
  39. [ 'a.site_id', "=", $site_id ],
  40. ];
  41. if (!empty($from_type)) {
  42. $condition[] = [ "a.from_type", "=", $from_type ];
  43. }
  44. if (!empty($from_type_id)) {
  45. $condition[] = [ "a.from_type_id", "=", $from_type_id ];
  46. }
  47. if (!empty($from_type_name)) {
  48. $condition[] = [ "a.from_type_name", 'like', '%' . $from_type_name . '%' ];
  49. }
  50. if (!empty($hongbao_type)) {
  51. $condition[] = [ "a.hongbao_type", '=', $hongbao_type ];
  52. }
  53. if ($name !== '') {
  54. $condition[] = [ "a.name", 'like', '%' . $name . '%' ];
  55. }
  56. if (!empty($start_time) && empty($end_time)) {
  57. $condition[] = [ 'a.create_time', '>=', date_to_time($start_time) ];
  58. } elseif (empty($start_time) && !empty($end_time)) {
  59. $condition[] = [ "a.create_time", "<=", date_to_time($end_time) ];
  60. } elseif (!empty($start_time) && !empty($end_time)) {
  61. $condition[] = [ 'a.create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
  62. }
  63. $join = [
  64. [ 'goods g', "g.goods_id = a.from_type_id and a.from_type = 'goods' ", 'left' ],
  65. [ 'promotion p', "p.promotion_id = a.from_type_id and a.from_type = 'promotion' ", 'left' ],
  66. ];
  67. $order = 'a.hongbao_id desc';
  68. $field = "a.*,IF(a.from_type = 'goods',(g.goods_image),(p.image)) as images,IF(a.from_type = 'goods',(g.start_time),(p.start_time)) as start_time,IF(a.from_type = 'goods',(g.end_time),(p.end_time)) as end_time,IF(a.from_type = 'goods',(g.promotion_type),(p.promotion_type)) as promotion_type";
  69. $list = $hongbao_model->getHongbaoPageList($condition, $page, $page_size, $order, $field, $alias = 'a', $join);
  70. $promotion_model = new PromotionModel();
  71. $promotion_type_list = $promotion_model->getPromotionType('all');
  72. if (!empty($list[ 'data' ][ 'list' ])) {
  73. foreach ($list[ 'data' ][ 'list' ] as $k => $v) {
  74. $list[ 'data' ][ 'list' ][ $k ][ 'promotion_type_name' ] = $promotion_type_list[ $v[ 'promotion_type' ] ][ 'title' ];
  75. $list[ 'data' ][ 'list' ][ $k ][ 'promotion_type_icon' ] = $promotion_type_list[ $v[ 'promotion_type' ] ][ 'icon' ];
  76. }
  77. }
  78. return $this->response($list);
  79. }
  80. /**
  81. * 账户红包数据
  82. */
  83. public function hongbaoAccount()
  84. {
  85. $hongbao_model = new MemberHongbao();
  86. $site_id = $this->site_id;
  87. //累计发放红包数,累计发放金额
  88. $total_info = $hongbao_model->getMemberHongbaoInfo([ [ 'hongbao_type', '=', 'site' ], [ 'site_id', '=', $site_id ] ], 'sum(money) as total_money, count(member_hongbao_id) as total_count')[ 'data' ];
  89. //累计使用
  90. $use_info = $hongbao_model->getMemberHongbaoInfo([ [ 'hongbao_type', '=', 'site' ], [ 'site_id', '=', $site_id ], [ 'state', '=', 2 ] ], 'sum(money) as use_money')[ 'data' ];
  91. //剩余
  92. $surplus_info = $hongbao_model->getMemberHongbaoInfo([ [ 'hongbao_type', '=', 'site' ], [ 'site_id', '=', $site_id ], [ 'state', '=', 1 ] ], 'sum(money) as surplus_money')[ 'data' ];
  93. $data = [];
  94. $data[ 'total_money' ] = $total_info[ 'total_money' ] ?? 0;
  95. $data[ 'total_count' ] = $total_info[ 'total_count' ] ?? 0;
  96. $data[ 'use_money' ] = $use_info[ 'use_money' ] ?? 0;
  97. $data[ 'surplus_money' ] = $surplus_info[ 'surplus_money' ] ?? 0;
  98. return $this->response($this->success($data));
  99. }
  100. /**
  101. * 红包领取记录
  102. */
  103. public function receivelists()
  104. {
  105. $hongbao_model = new MemberHongbao();
  106. $site_id = $this->site_id;
  107. $from_type = isset($this->params[ 'from_type' ]) ? $this->params[ 'from_type' ] : '';
  108. $from_type_id = isset($this->params[ 'from_type_id' ]) ? $this->params[ 'from_type_id' ] : 0;
  109. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  110. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  111. $start_time = isset($this->params[ 'start_time' ]) ? $this->params[ 'start_time' ] : '';
  112. $end_time = isset($this->params[ 'end_time' ]) ? $this->params[ 'end_time' ] : '';
  113. $from_type_name = isset($this->params[ 'from_type_name' ]) ? $this->params[ 'from_type_name' ] : '';
  114. $parent_id = isset($this->params[ 'parent_id' ]) ? $this->params[ 'parent_id' ] : '';
  115. $nick_name = isset($this->params[ 'nickname' ]) ? $this->params[ 'nickname' ] : '';
  116. $hongbao_type = isset($this->params[ 'hongbao_type' ]) ? $this->params[ 'hongbao_type' ] : '';
  117. $state = isset($this->params[ 'state' ]) ? $this->params[ 'state' ] : '';
  118. $condition = [
  119. [ 'site_id', "=", $site_id ],
  120. ];
  121. if (!empty($from_type)) {
  122. $condition[] = [ "from_type", "=", $from_type ];
  123. if (!empty($from_type_id)) {
  124. $condition[] = [ "from_type_id", "=", $from_type_id ];
  125. }
  126. }
  127. if (!empty($from_type_name)) {
  128. $condition[] = [ "from_type_name", 'like', '%' . $from_type_name . '%' ];
  129. }
  130. if (!empty($parent_id)) {
  131. $condition[] = [ "hongbao_id", "=", $parent_id ];
  132. }
  133. if (!empty($nick_name)) {
  134. $condition[] = [ "member_name", 'like', '%' . $nick_name . '%' ];
  135. }
  136. if (!empty($hongbao_type)) {
  137. $condition[] = [ "hongbao_type", '=', $hongbao_type ];
  138. }
  139. if ($state !== '') {
  140. $condition[] = [ "state", '=', $state ];
  141. }
  142. if (!empty($start_time) && empty($end_time)) {
  143. $condition[] = [ 'fetch_time', '>=', date_to_time($start_time) ];
  144. } elseif (empty($start_time) && !empty($end_time)) {
  145. $condition[] = [ "fetch_time", "<=", date_to_time($end_time) ];
  146. } elseif (!empty($start_time) && !empty($end_time)) {
  147. $condition[] = [ 'fetch_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
  148. }
  149. $list = $hongbao_model->getMemberHongbaoPageList($condition, $page, $page_size);
  150. return $this->response($list);
  151. }
  152. /**
  153. * 红包详情
  154. */
  155. public function detail()
  156. {
  157. $id = isset($this->params[ 'id' ]) ? $this->params[ 'id' ] : 0;
  158. $site_id = $this->site_id;
  159. $hongbao_model = new MemberHongbao();
  160. $info = $hongbao_model->getHongbaoDetail($id, $site_id);
  161. if (empty($info)) return $this->response($this->error([], "未获取到红包详情!"));
  162. return $this->response($info);
  163. }
  164. }