Survey.php 4.7 KB

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