StoreStatisticsService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace addons\Store\common\services;
  3. use addons\Store\common\models\Store;
  4. use addons\Store\common\models\StoreCashierOrder;
  5. use addons\Store\common\models\StoreCommission;
  6. use addons\Store\common\models\StoreCommissionCheck;
  7. use common\enums\StatusEnum;
  8. use common\models\user\User;
  9. /**
  10. * 资产统计service
  11. */
  12. class StoreStatisticsService extends BaseService
  13. {
  14. public function __construct(int $mallId, int $storeId)
  15. {
  16. parent::__construct();
  17. $this->store_id = $storeId;
  18. $this->mall_id = $mallId;
  19. }
  20. /**
  21. * 提成明细(直接拿门店的明细修改)
  22. *
  23. * @param array $get
  24. * @param int $pageSize
  25. *
  26. * @return array
  27. */
  28. public function getIncomeLogList(array $get, int $pageSize): array
  29. {
  30. $where[] = ['mall_id' => $this->mall_id];
  31. $where[] = ['store_id' => $this->store_id];
  32. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  33. // 是否没有提交筛选条件 (用于决定总数直接从store表拿还是要重新查询统计)
  34. $whereIsEmpty = true;
  35. if (isset($get['keyword']) && $get['keyword'] !== '') {
  36. $whereIsEmpty = false;
  37. $user_list = User::lists([
  38. 'where' => [
  39. ['mall_id' => $this->mall_id],
  40. [
  41. 'or',
  42. ['like', 'username', trim($get['keyword'])],
  43. ['like', 'nickname', trim($get['keyword'])],
  44. ['like', 'mobile', trim($get['keyword'])]
  45. ]
  46. ],
  47. 'select' => 'id'
  48. ]);
  49. $where[] = ['user_id' => array_column($user_list, 'id')];
  50. }
  51. if (!empty($get['user_id'])) {
  52. $whereIsEmpty = false;
  53. $where[] = ['user_id' => $get['user_id']];
  54. }
  55. if (isset($get['order_no']) && $get['order_no'] !== '') {
  56. $whereIsEmpty = false;
  57. $where[] = ['order_no' => $get['order_no']];
  58. }
  59. if (isset($get['is_settlement']) && $get['is_settlement'] !== '') {
  60. $whereIsEmpty = false;
  61. $where[] = ['is_settlement' => $get['is_settlement']];
  62. }
  63. if (!empty($get['date'])) {
  64. $whereIsEmpty = false;
  65. $get['date'][1] .= ' 23:59:59';
  66. array_walk($get['date'], function (&$item) {
  67. $item = strtotime($item);
  68. });
  69. $where[] = ['between', 'created_at', $get['date'][0], $get['date'][1]];
  70. }
  71. $params = [
  72. 'where' => $where,
  73. 'order' => 'id desc',
  74. 'with' => ['user'],
  75. 'limit' => $get['limit'] ?? $pageSize,
  76. ];
  77. $page_data = StoreCommission::listPage($params);
  78. $total_num = $total_order_money = $total_settled_amount = $unsettled_amount = 0;
  79. if ($page_data) {
  80. $store = Store::findOne(['id' => $this->store_id]);
  81. // 如果所有搜索条件为空则显示这个,否则需要查询
  82. if ($whereIsEmpty) {
  83. if ($store) {
  84. $commissionTotal = $store;
  85. }
  86. } else {
  87. // 按搜索结果 sum
  88. $commissionTotalField = [
  89. 'COUNT(*) `total_num`',
  90. 'SUM(`pay_money`) `total_order_money`',
  91. 'SUM(IF(`is_settlement` = 1, `amount_received`, 0)) `total_settled_amount`',
  92. 'SUM(IF(`is_settlement` = 0, `amount_received`, 0)) `unsettled_amount`'
  93. ];
  94. $commissionTotal = StoreCommission::getOne($where, true, [], '', $commissionTotalField);
  95. }
  96. if (isset($commissionTotal)) {
  97. $total_num = $commissionTotal['total_num'];
  98. $total_order_money = $commissionTotal['total_order_money'];
  99. $total_settled_amount = $commissionTotal['total_settled_amount'];
  100. $unsettled_amount = $commissionTotal['unsettled_amount'];
  101. }
  102. if (!empty($page_data['list'])) {
  103. $user_id_arr = array_column($page_data['list'], 'user_id');
  104. $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'index' => 'id']);
  105. $order_no_arr = array_column($page_data['list'], 'order_no');
  106. foreach ($page_data['list'] as &$item) {
  107. if ($item['type'] == 2) $order_no_arr[] = $item['order_no'];
  108. }
  109. if (!empty($order_no_arr)) {
  110. $store_cashier_order_list = StoreCashierOrder::lists(['where' => [['order_no' => $order_no_arr]], 'select' => 'order_no,extra', 'index' => 'order_no']);
  111. }
  112. $ids = array_column($page_data['list'], 'id');
  113. $check_list = StoreCommissionCheck::lists([
  114. 'where' => [
  115. ['store_commission_id' => $ids],
  116. ['check_status' => [0, 1]]
  117. ],
  118. 'index' => 'store_commission_id'
  119. ]);
  120. foreach ($page_data['list'] as &$item) {
  121. $item['nickname'] = $item['mobile'] = $item['avatar_url'] = '';
  122. if (isset($user_list[$item['user_id']])) {
  123. $item['nickname'] = $user_list[$item['user_id']]['nickname'];
  124. $item['mobile'] = $user_list[$item['user_id']]['mobile'];
  125. $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
  126. }
  127. if ($item['type'] == 2) {
  128. if (isset($store_cashier_order_list[$item['order_no']])) {
  129. $addons_bonus = 0;
  130. $extra = json_decode($store_cashier_order_list[$item['order_no']]['extra'], true);
  131. if (!empty($extra)) {
  132. foreach ($extra as $k => $v) {
  133. if (in_array($k, ['agent_commission', 'distribution_commission', 'boss_commission', 'area_commission'])) $addons_bonus += $v;
  134. }
  135. }
  136. $item['addons_bonus'] = bcadd($addons_bonus, 0, 2);
  137. }
  138. }
  139. if (empty($store['payment_settlement_type'])) {
  140. $item['is_show'] = 0;
  141. } else {
  142. $item['is_show'] = 1;
  143. if (isset($check_list[$item['id']])) {
  144. $item['is_show'] = 0;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. $order_total_arr = [];
  151. $order_total_arr[] = ['type' => 'total_num', 'name' => '总数', 'value' => $total_num ?: 0, 'prompt' => ''];
  152. $order_total_arr[] = ['type' => 'total_order_money', 'name' => '订单总金额', 'value' => $total_order_money ?: 0, 'prompt' => ''];
  153. $order_total_arr[] = ['type' => 'total_settled_amount', 'name' => '提成已结算总金额', 'value' => $total_settled_amount ?: 0, 'prompt' => ''];
  154. $order_total_arr[] = ['type' => 'unsettled_amount', 'name' => '提成未结算总金额', 'value' => $unsettled_amount ?: 0, 'prompt' => ''];
  155. $page_data['order_total_arr'] = $order_total_arr;
  156. $page_data['store_name'] = isset($store) ? $store['store_name'] : '';
  157. return $page_data;
  158. }
  159. }