Trade7days.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\service\Basics as BasicsService;
  11. use app\store\model\Order as OrderModel;
  12. use app\common\library\helper;
  13. /**
  14. * 近7日走势
  15. * Class Trade7days
  16. * @package app\store\service\statistics\data
  17. */
  18. class Trade7days extends BasicsService
  19. {
  20. /* @var OrderModel $GoodsModel */
  21. private $OrderModel;
  22. /**
  23. * 构造方法
  24. */
  25. public function __construct()
  26. {
  27. /* 初始化模型 */
  28. $this->OrderModel = new OrderModel;
  29. }
  30. /**
  31. * 近7日走势
  32. * @return array
  33. * @throws \think\Exception
  34. */
  35. public function getTransactionTrend()
  36. {
  37. // 最近七天日期
  38. $lately7days = $this->getLately7days();
  39. return [
  40. 'date' => helper::jsonEncode($lately7days),
  41. 'order_total' => helper::jsonEncode($this->getOrderTotalByDate($lately7days)),
  42. 'order_total_price' => helper::jsonEncode($this->getOrderTotalPriceByDate($lately7days))
  43. ];
  44. }
  45. /**
  46. * 最近七天日期
  47. */
  48. private function getLately7days()
  49. {
  50. // 获取当前周几
  51. $date = [];
  52. for ($i = 0; $i < 7; $i++) {
  53. $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
  54. }
  55. return array_reverse($date);
  56. }
  57. /**
  58. * 获取订单总量 (指定日期)
  59. * @param $days
  60. * @return array
  61. * @throws \think\Exception
  62. */
  63. private function getOrderTotalByDate($days)
  64. {
  65. $data = [];
  66. foreach ($days as $day) {
  67. $data[] = $this->getOrderTotal($day);
  68. }
  69. return $data;
  70. }
  71. /**
  72. * 获取订单总量
  73. * @param null $day
  74. * @return string
  75. * @throws \think\Exception
  76. */
  77. private function getOrderTotal($day = null)
  78. {
  79. return number_format($this->OrderModel->getPayOrderTotal($day, $day));
  80. }
  81. /**
  82. * 获取某天的总销售额
  83. * @param null $day
  84. * @return string
  85. */
  86. private function getOrderTotalPrice($day = null)
  87. {
  88. return helper::number2($this->OrderModel->getOrderTotalPrice($day, $day));
  89. }
  90. /**
  91. * 获取订单总量 (指定日期)
  92. * @param $days
  93. * @return array
  94. */
  95. private function getOrderTotalPriceByDate($days)
  96. {
  97. $data = [];
  98. foreach ($days as $day) {
  99. $data[] = $this->getOrderTotalPrice($day);
  100. }
  101. return $data;
  102. }
  103. }