BargainActivityLists.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\lists\bargain;
  20. use app\adminapi\lists\BaseAdminDataLists;
  21. use app\common\enum\BargainEnum;
  22. use app\common\enum\OrderEnum;
  23. use app\common\enum\YesNoEnum;
  24. use app\common\lists\ListsExtendInterface;
  25. use app\common\lists\ListsSearchInterface;
  26. use app\common\model\BargainActivity;
  27. use app\common\model\BargainGoods;
  28. use app\common\model\BargainInitiate;
  29. use app\common\model\Order;
  30. /**
  31. * 砍价活动列表
  32. * Class BargainActivityLists
  33. * @package app\adminapi\lists\bargain
  34. */
  35. class BargainActivityLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExtendInterface
  36. {
  37. /**
  38. * @notes 统计信息
  39. * @return mixed|void
  40. * @author Tab
  41. * @date 2021/8/27 18:19
  42. */
  43. public function extend()
  44. {
  45. $all = BargainActivity::count();
  46. $wait = BargainActivity::where('status', BargainEnum::ACTIVITY_STATUS_WAIT)->count();
  47. $ing = BargainActivity::where('status', BargainEnum::ACTIVITY_STATUS_ING)->count();
  48. $end = BargainActivity::where('status', BargainEnum::ACTIVITY_STATUS_END)->count();
  49. return [
  50. 'all' => $all,
  51. 'wait' => $wait,
  52. 'ing' => $ing,
  53. 'end' => $end
  54. ];
  55. }
  56. /**
  57. * @notes 设置搜索
  58. * @return \string[][]
  59. * @author Tab
  60. * @date 2021/8/27 18:19
  61. */
  62. public function setSearch(): array
  63. {
  64. return [
  65. '=' => ['status']
  66. ];
  67. }
  68. /**
  69. * @notes 附加搜索
  70. * @author Tab
  71. * @date 2021/9/24 11:52
  72. */
  73. public function attachSearch()
  74. {
  75. // 活动信息
  76. if (isset($this->params['activity_info']) && !empty($this->params['activity_info'])) {
  77. $this->searchWhere[] = ['sn|name', 'like', '%'. $this->params['activity_info'] .'%'];
  78. }
  79. }
  80. /**
  81. * @notes 列表
  82. * @return array
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. * @author Tab
  87. * @date 2021/8/27 18:26
  88. */
  89. public function lists() : array
  90. {
  91. $this->attachSearch();
  92. $field = [
  93. 'id',
  94. 'sn',
  95. 'name',
  96. 'start_time' => 'start_time_desc',
  97. 'end_time' => 'end_time_desc',
  98. 'visited',
  99. 'status',
  100. 'status' => 'status_desc',
  101. 'create_time',
  102. ];
  103. $lists = BargainActivity::field($field)
  104. ->where($this->searchWhere)
  105. ->order('id','desc')
  106. ->withSearch(['activity_time', 'goods_info'], $this->params)
  107. ->limit($this->limitOffset, $this->limitLength)
  108. ->select()
  109. ->toArray();
  110. foreach ($lists as &$item) {
  111. $item['goods_count'] = $this->goodsCount($item);
  112. $orderData= $this->orderData($item);
  113. $item['total_amount'] = $orderData['total_amount'];
  114. $item['total_num'] = $orderData['total_num'];
  115. $item['order_count'] = $orderData['order_count'];
  116. }
  117. return $lists;
  118. }
  119. /**
  120. * @notes 记录数
  121. * @return int
  122. * @author Tab
  123. * @date 2021/8/27 18:25
  124. */
  125. public function count() : int
  126. {
  127. $this->attachSearch();
  128. $count = BargainActivity::where($this->searchWhere)
  129. ->withSearch(['activity_time', 'goods_name', 'goods_code'], $this->params)
  130. ->count();
  131. return $count;
  132. }
  133. /**
  134. * @notes 活动商品数量
  135. * @param $item
  136. * @return int
  137. * @author Tab
  138. * @date 2021/9/24 9:10
  139. */
  140. public function goodsCount($item)
  141. {
  142. $goodsIds = BargainGoods::distinct(true)
  143. ->field('goods_id')
  144. ->where('activity_id', $item['id'])
  145. ->select()
  146. ->toArray();
  147. return count($goodsIds);
  148. }
  149. /**
  150. * @notes 订单数据
  151. * @param $item
  152. * @author Tab
  153. * @date 2021/9/24 9:22
  154. */
  155. public function orderData($item)
  156. {
  157. // 活动对应的订单id
  158. $orderIds = BargainInitiate::where('activity_id', $item['id'])->whereNotNull('order_id')->column('order_id');
  159. // 成交订单数
  160. $orderCount = Order::where([
  161. ['order_type', '=' ,OrderEnum::BARGAIN_ORDER],
  162. ['pay_status', '=', YesNoEnum::YES],
  163. ['id', 'in', $orderIds]
  164. ])->count();
  165. // 销量
  166. $totalNum = Order::where([
  167. ['order_type', '=' ,OrderEnum::BARGAIN_ORDER],
  168. ['pay_status', '=', YesNoEnum::YES],
  169. ['id', 'in', $orderIds]
  170. ])->sum('total_num');
  171. // 销售额
  172. $totalAmount = Order::where([
  173. ['order_type', '=' ,OrderEnum::BARGAIN_ORDER],
  174. ['pay_status', '=', YesNoEnum::YES],
  175. ['id', 'in', $orderIds]
  176. ])->sum('order_amount');
  177. return [
  178. 'total_amount' => $totalAmount,
  179. 'total_num' => $totalNum,
  180. 'order_count' => $orderCount,
  181. ];
  182. }
  183. }