Survey.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\service\statistics\data;
  10. use app\common\library\helper;
  11. use app\common\service\Basics as BasicsService;
  12. use app\store\model\User as UserModel;
  13. use app\store\model\Order as OrderModel;
  14. use app\store\model\Goods as GoodsModel;
  15. use app\store\model\recharge\Order as RechargeOrderModel;
  16. use app\common\enum\order\Status as OrderStatusEnum;
  17. use app\common\enum\order\PayStatus as PayStatusEnum;
  18. use app\common\enum\recharge\order\PayStatus as RechargePayStatusEnum;
  19. /**
  20. * 数据概况
  21. * Class Survey
  22. * @package app\store\service\statistics\data
  23. */
  24. class Survey extends BasicsService
  25. {
  26. /**
  27. * 获取数据概况
  28. * @param $startDate
  29. * @param $endDate
  30. * @return array
  31. * @throws \think\Exception
  32. */
  33. public function getSurveyData($startDate = null, $endDate = null)
  34. {
  35. return [
  36. // 用户数量
  37. 'user_total' => $this->getUserTotal($startDate, $endDate),
  38. // 消费人数
  39. 'consume_users' => $this->getConsumeUsers($startDate, $endDate),
  40. // 付款订单数
  41. 'order_total' => $this->getOrderTotal($startDate, $endDate),
  42. // 付款订单总额
  43. 'order_total_money' => $this->getOrderTotalMoney($startDate, $endDate),
  44. // 商品总量
  45. 'goods_total' => $this->getGoodsTotal($startDate, $endDate),
  46. // 用户充值总额
  47. 'recharge_total' => $this->getRechargeTotal($startDate, $endDate),
  48. ];
  49. }
  50. /**
  51. * 获取用户总量
  52. * @param null $startDate
  53. * @param null $endDate
  54. * @return string
  55. * @throws \think\Exception
  56. */
  57. private function getUserTotal($startDate = null, $endDate = null)
  58. {
  59. $model = new UserModel;
  60. if (!is_null($startDate) && !is_null($endDate)) {
  61. $model->where('create_time', '>=', strtotime($startDate))
  62. ->where('create_time', '<', strtotime($endDate) + 86400);
  63. }
  64. $value = $model->where('is_delete', '=', '0')->count();
  65. return number_format($value);
  66. }
  67. /**
  68. * 消费人数
  69. * @param null $startDate
  70. * @param null $endDate
  71. * @return string
  72. * @throws \think\Exception
  73. */
  74. public function getConsumeUsers($startDate = null, $endDate = null)
  75. {
  76. $model = new OrderModel;
  77. if (!is_null($startDate) && !is_null($endDate)) {
  78. $model->where('pay_time', '>=', strtotime($startDate))
  79. ->where('pay_time', '<', strtotime($endDate) + 86400);
  80. }
  81. $value = $model->field('user_id')
  82. ->where('pay_status', '=', PayStatusEnum::SUCCESS)
  83. ->where('order_status', '<>', OrderStatusEnum::CANCELLED)
  84. ->where('is_delete', '=', '0')
  85. ->group('user_id')
  86. ->count();
  87. return number_format($value);
  88. }
  89. /**
  90. * 获取订单总量
  91. * @param null $startDate
  92. * @param null $endDate
  93. * @return string
  94. * @throws \think\Exception
  95. */
  96. private function getOrderTotal($startDate = null, $endDate = null)
  97. {
  98. return number_format((new OrderModel)->getPayOrderTotal($startDate, $endDate));
  99. }
  100. /**
  101. * 付款订单总额
  102. * @param null $startDate
  103. * @param null $endDate
  104. * @return string
  105. */
  106. private function getOrderTotalMoney($startDate = null, $endDate = null)
  107. {
  108. return helper::number2((new OrderModel)->getOrderTotalPrice($startDate, $endDate));
  109. }
  110. /**
  111. * 获取商品总量
  112. * @param null $startDate
  113. * @param null $endDate
  114. * @return int|string
  115. * @throws \think\Exception
  116. */
  117. private function getGoodsTotal($startDate = null, $endDate = null)
  118. {
  119. $model = new GoodsModel;
  120. if (!is_null($startDate) && !is_null($endDate)) {
  121. $model->where('create_time', '>=', strtotime($startDate))
  122. ->where('create_time', '<', strtotime($endDate) + 86400);
  123. }
  124. $value = $model->where('is_delete', '=', 0)->count();
  125. return number_format($value);
  126. }
  127. /**
  128. * 用户充值总额
  129. * @param null $startDate
  130. * @param null $endDate
  131. * @return float|int
  132. */
  133. private function getRechargeTotal($startDate = null, $endDate = null)
  134. {
  135. $model = new RechargeOrderModel;
  136. if (!is_null($startDate) && !is_null($endDate)) {
  137. $model->where('pay_time', '>=', strtotime($startDate))
  138. ->where('pay_time', '<', strtotime($endDate) + 86400);
  139. }
  140. $value = $model->where('pay_status', '=', RechargePayStatusEnum::SUCCESS)
  141. ->sum('actual_money');
  142. return helper::number2($value);
  143. }
  144. }