ChannelSaleStatistics.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\store\model;
  3. use app\common\library\helper;
  4. use app\common\model\ChannelSaleStatistics as ChannelSaleStatisticsModel;
  5. /**
  6. * 渠道销售统计模型
  7. * @package app\store\model
  8. */
  9. class ChannelSaleStatistics extends ChannelSaleStatisticsModel
  10. {
  11. public function getLst($param = [], $returnTotal = false, $offset = 0, $perPage = 15)
  12. {
  13. $model = $this->setQueryFilter($param);
  14. $query = $model->alias('st')
  15. ->field('shops.shop_id,shops.is_virtual as shop_type,shops.shop_name,sum(st.pay_amount) as pay_amount,sum(st.pay_count) as pay_count,
  16. sum(pay_goods_count) as pay_goods_count,sum(pay_user_count) as pay_user_count,
  17. sum(refund_amount) as refund_amount,sum(refund_order_count) as refund_order_count,sum(refund_goods_count) as refund_goods_count')
  18. ->leftJoin('shops', 'shops.shop_id=st.shop_id')
  19. ->where('is_virtual', 'in', [Shops::SHOP_TYPE_VIRTUAL, Shops::SHOP_TYPE_DIFF]) // 筛选 虚拟门店、异业合作
  20. ->whereOr('st.shop_id', '=', 0)
  21. ->group('st.shop_id')
  22. ->order(['shops.is_virtual' => 'asc', 'pay_amount' => 'desc']);
  23. if ($returnTotal) {
  24. return $query->count();
  25. } else {
  26. return $query->limit($offset, $perPage)
  27. ->select()->toArray();
  28. }
  29. }
  30. /**
  31. * 获取列表
  32. * @param $param
  33. * @return mixed
  34. */
  35. public function getList($param = [])
  36. {
  37. $other = $this->getOtherData($param);
  38. $otherCount = count($other);
  39. $oPerPage = $perPage = 15;
  40. $curPage = $param['page'] ?? 1;
  41. $offset = ($curPage - 1) * $perPage - $otherCount;
  42. $perPage = $perPage - $otherCount;
  43. if ($curPage == 1) { // 第一页只取13条数据
  44. // $perPage = $perPage - $otherCount;
  45. $offset = ($curPage - 1) * $perPage;
  46. }
  47. $total = $this->getLst($param, true) + $otherCount;
  48. $data = $this->getLst($param, false, $offset, $perPage);
  49. if ($curPage == 1) { // 第一页只取13条数据
  50. $lastPage = ceil(($total + $perPage - 1) / $oPerPage);
  51. } else {
  52. $lastPage = ceil(($total - $otherCount + $oPerPage - 1) / $oPerPage);
  53. }
  54. if ($curPage == 1 && $total <= $oPerPage) {
  55. $lastPage = 1;
  56. }
  57. $result = $curPage == 1 ? array_merge($other, $data) : $data;
  58. if (!empty($result)) {
  59. foreach ($result as &$item) {
  60. $text = Shops::SHOP_TYPE[$item['shop_type']] ?? '其他渠道';
  61. if (in_array($item['shop_type'], [Shops::SHOP_TYPE_VIRTUAL, Shops::SHOP_TYPE_DIFF])) {
  62. $text .= "({$item['shop_name']})";
  63. } else {
  64. $item['shop_id'] = 0;
  65. $item['shop_name'] = '';
  66. }
  67. $item['shop_type_text'] = $text;
  68. }
  69. }
  70. return [
  71. 'current_page' => (int)$curPage,
  72. 'data' => $result,
  73. 'last_page' => $lastPage,
  74. 'per_page' => $oPerPage,
  75. 'total' => $total,
  76. ];
  77. }
  78. public function getOtherData($param = [])
  79. {
  80. $model = $this->setQueryFilter($param);
  81. // 排序条件
  82. $data = $model->alias('st')
  83. ->field('st.shop_id,shops.is_virtual as shop_type,sum(st.pay_amount) as pay_amount,sum(st.pay_count) as pay_count,
  84. sum(pay_goods_count) as pay_goods_count,sum(pay_user_count) as pay_user_count,
  85. sum(refund_amount) as refund_amount,sum(refund_order_count) as refund_order_count,sum(refund_goods_count) as refund_goods_count')
  86. ->leftJoin('shops', 'shops.shop_id=st.shop_id')
  87. ->where('is_virtual', 'in', [Shops::SHOP_TYPE_JOIN, Shops::SHOP_TYPE_SELF]) // // 筛选 自营店、加盟店
  88. ->where('st.shop_id', '>', 0)
  89. ->group('shops.is_virtual')
  90. ->order(['shops.is_virtual' => 'asc'])
  91. ->select()->toArray();
  92. return $data;
  93. }
  94. /**
  95. * 检索查询条件
  96. * @param array $param
  97. * @return \think\db\BaseQuery
  98. */
  99. private function setQueryFilter(array $param)
  100. {
  101. // 实例化查询对象
  102. $query = $this->getNewQuery();
  103. // 查询参数
  104. $params = $this->setQueryDefaultValue($param, []);
  105. $filter = [];
  106. // 起止时间
  107. if (!empty($params['betweenTime'])) {
  108. $times = between_time($params['betweenTime']);
  109. $filter[] = ["st.start_time", '>=', $times['start_time']];
  110. $filter[] = ["st.end_time", '<=', $times['end_time']];
  111. }
  112. $query->where($filter);
  113. return $query;
  114. }
  115. /**
  116. * 导出记录
  117. */
  118. public function export(array $param)
  119. {
  120. $data['header'] = ['渠道类型', '支付金额(元)', '支付订单数', '支付商品件数', '支付买家数', '退货金额(元)', '退货订单数', '退商品件数'];
  121. $data['filename'] = '渠道销售数据导出';
  122. $data['data'] = [];
  123. $model = $this->setQueryFilter($param);
  124. $other = $this->getOtherData($param);
  125. $query = $model->alias('st')
  126. ->field('shops.shop_id,shops.is_virtual as shop_type,shops.shop_name,sum(st.pay_amount) as pay_amount,sum(st.pay_count) as pay_count,
  127. sum(pay_goods_count) as pay_goods_count,sum(pay_user_count) as pay_user_count,
  128. sum(refund_amount) as refund_amount,sum(refund_order_count) as refund_order_count,sum(refund_goods_count) as refund_goods_count')
  129. ->leftJoin('shops', 'shops.shop_id=st.shop_id')
  130. ->where('shops.is_virtual', 'in', [Shops::SHOP_TYPE_VIRTUAL, Shops::SHOP_TYPE_DIFF]) // 筛选 虚拟门店、异业合作
  131. ->whereOr('st.shop_id', '=', 0)
  132. ->group('st.shop_id')
  133. ->order(['shops.is_virtual' => 'asc', 'pay_amount' => 'desc']);
  134. $lst = $query->select()->toArray();
  135. $list = array_merge($other, $lst);
  136. foreach ($list as $arr) {
  137. $text = Shops::SHOP_TYPE[$arr['shop_type']] ?? '其他渠道';
  138. if (in_array($arr['shop_type'], [Shops::SHOP_TYPE_VIRTUAL, Shops::SHOP_TYPE_DIFF])) {
  139. $text .= "({$arr['shop_name']})";
  140. }
  141. $new_list['shop_type_text'] = $text;
  142. $new_list['pay_amount'] = helper::number2($arr['pay_amount']);
  143. $new_list['pay_count'] = $arr['pay_count'];
  144. $new_list['pay_goods_count'] = $arr['pay_goods_count'];
  145. $new_list['pay_user_count'] = $arr['pay_user_count'];
  146. $new_list['refund_amount'] = helper::number2($arr['refund_amount']);
  147. $new_list['refund_order_count'] = $arr['refund_order_count'];
  148. $new_list['refund_goods_count'] = $arr['refund_goods_count'];
  149. $data['data'][] = $new_list;
  150. }
  151. return $data;
  152. }
  153. }