Survey.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\service\statistics\data;
  13. use app\common\library\helper;
  14. use app\common\service\BaseService;
  15. use app\store\model\User as UserModel;
  16. use app\store\model\Order as OrderModel;
  17. use app\store\model\Goods as GoodsModel;
  18. use app\store\model\recharge\Order as RechargeOrderModel;
  19. use app\common\enum\order\PayStatus as PayStatusEnum;
  20. use app\common\enum\recharge\order\PayStatus as RechargePayStatusEnum;
  21. /**
  22. * 数据概况
  23. * Class Survey
  24. * @package app\store\service\statistics\data
  25. */
  26. class Survey extends BaseService
  27. {
  28. /**
  29. * 获取数据概况
  30. * @param null $startDate
  31. * @param null $endDate
  32. * @return array
  33. */
  34. public function getSurveyData($startDate = null, $endDate = null): array
  35. {
  36. return [
  37. // 用户数量
  38. 'userTotal' => $this->getUserTotal($startDate, $endDate),
  39. // 消费人数
  40. 'consumeUsers' => $this->getConsumeUsers($startDate, $endDate),
  41. // 支付订单数
  42. 'orderTotal' => $this->getOrderTotal($startDate, $endDate),
  43. // 销售额(元)
  44. 'orderTotalPrice' => $this->getOrderTotalPrice($startDate, $endDate),
  45. // 商品总量
  46. 'goodsTotal' => $this->getGoodsTotal($startDate, $endDate),
  47. // 用户充值总额
  48. 'rechargeTotalMoney' => $this->getRechargeTotalMoney($startDate, $endDate),
  49. ];
  50. }
  51. /**
  52. * 获取用户总量
  53. * @param null $startDate
  54. * @param null $endDate
  55. * @return string
  56. */
  57. private function getUserTotal($startDate = null, $endDate = null): string
  58. {
  59. // 检索查询条件
  60. $filter = [];
  61. if (!is_null($startDate) && !is_null($endDate)) {
  62. $filter[] = ['create_time', '>=', strtotime($startDate)];
  63. $filter[] = ['create_time', '<', strtotime($endDate) + 86400];
  64. }
  65. // 查询总记录
  66. $value = (new UserModel)->where($filter)
  67. ->where('is_delete', '=', '0')
  68. ->count();
  69. return number_format($value);
  70. }
  71. /**
  72. * 消费人数
  73. * @param null $startDate
  74. * @param null $endDate
  75. * @return string
  76. */
  77. public function getConsumeUsers($startDate = null, $endDate = null): string
  78. {
  79. // 检索查询条件
  80. $filter = [];
  81. if (!is_null($startDate) && !is_null($endDate)) {
  82. $filter[] = ['pay_time', '>=', strtotime($startDate)];
  83. $filter[] = ['pay_time', '<', strtotime($endDate) + 86400];
  84. }
  85. // 查询总记录
  86. $value = (new OrderModel)->field('user_id')
  87. ->where($filter)
  88. ->where('pay_status', '=', PayStatusEnum::SUCCESS)
  89. // ->where('order_status', '<>', OrderStatusEnum::CANCELLED)
  90. ->where('is_delete', '=', '0')
  91. ->group('user_id')
  92. ->count();
  93. return number_format($value);
  94. }
  95. /**
  96. * 获取订单总量
  97. * @param null $startDate
  98. * @param null $endDate
  99. * @return string
  100. */
  101. private function getOrderTotal($startDate = null, $endDate = null): string
  102. {
  103. return number_format((new OrderModel)->getPayOrderTotal($startDate, $endDate));
  104. }
  105. /**
  106. * 付款订单总额
  107. * @param null $startDate
  108. * @param null $endDate
  109. * @return string
  110. */
  111. private function getOrderTotalPrice($startDate = null, $endDate = null): string
  112. {
  113. return helper::number2((new OrderModel)->getOrderTotalPrice($startDate, $endDate));
  114. }
  115. /**
  116. * 获取商品总量
  117. * @param null $startDate
  118. * @param null $endDate
  119. * @return string
  120. */
  121. private function getGoodsTotal($startDate = null, $endDate = null): string
  122. {
  123. // 检索查询条件
  124. $filter = [];
  125. if (!is_null($startDate) && !is_null($endDate)) {
  126. $filter[] = ['create_time', '>=', strtotime($startDate)];
  127. $filter[] = ['create_time', '<', strtotime($endDate) + 86400];
  128. }
  129. // 查询总记录
  130. $value = (new GoodsModel)->where($filter)
  131. ->where('is_delete', '=', 0)
  132. ->count();
  133. return number_format($value);
  134. }
  135. /**
  136. * 用户充值总额
  137. * @param null $startDate
  138. * @param null $endDate
  139. * @return string
  140. */
  141. private function getRechargeTotalMoney($startDate = null, $endDate = null): string
  142. {
  143. // 检索查询条件
  144. $filter = [];
  145. if (!is_null($startDate) && !is_null($endDate)) {
  146. $filter[] = ['pay_time', '>=', strtotime($startDate)];
  147. $filter[] = ['pay_time', '<', strtotime($endDate) + 86400];
  148. }
  149. // 查询总记录
  150. $value = (new RechargeOrderModel)->where($filter)
  151. ->where('pay_status', '=', RechargePayStatusEnum::SUCCESS)
  152. ->sum('actual_money');
  153. return helper::number2($value);
  154. }
  155. }