Trade7days.php 2.5 KB

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