ProductStatistic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\statistic;
  12. use app\controller\admin\AuthController;
  13. use app\services\product\category\StoreProductCategoryServices;
  14. use app\services\statistic\ProductStatisticServices;
  15. use think\facade\App;
  16. /**
  17. * Class ProductStatistic
  18. * @package app\controller\admin\v1\statistic
  19. */
  20. class ProductStatistic extends AuthController
  21. {
  22. /**
  23. * ProductStatistic constructor.
  24. * @param App $app
  25. * @param ProductStatisticServices $services
  26. */
  27. public function __construct(App $app, ProductStatisticServices $services)
  28. {
  29. parent::__construct($app);
  30. $this->services = $services;
  31. }
  32. /**
  33. * 商品基础
  34. * @return mixed
  35. */
  36. public function getBasic()
  37. {
  38. $where = $this->request->getMore([
  39. ['data', '', '', 'time']
  40. ]);
  41. $where['time'] = $this->getDay($where['time']);
  42. return $this->success($this->services->getBasic($where));
  43. }
  44. /**
  45. * 商品趋势
  46. * @return mixed
  47. */
  48. public function getTrend()
  49. {
  50. $where = $this->request->getMore([
  51. ['data', '', '', 'time']
  52. ]);
  53. $where['time'] = $this->getDay($where['time']);
  54. return $this->success($this->services->getTrend($where));
  55. }
  56. /**
  57. * 商品排行
  58. * @return mixed
  59. */
  60. public function getProductRanking(StoreProductCategoryServices $storeProductCategoryServices)
  61. {
  62. $where = $this->request->getMore([
  63. ['cate_id', ''],
  64. ['data', '', '', 'time'],
  65. ['sort', '']
  66. ]);
  67. $cateId = $where['cate_id'];
  68. if ($cateId) {
  69. $cateId = is_string($cateId) ? [$cateId] : $cateId;
  70. $cateId = array_merge($cateId, $storeProductCategoryServices->getColumn(['pid' => $cateId], 'id'));
  71. $cateId = array_unique(array_diff($cateId, [0]));
  72. }
  73. $where['cate_id'] = $cateId;
  74. $where['time'] = $this->getDay($where['time']);
  75. return $this->success($this->services->getProductRanking($where));
  76. }
  77. /**
  78. * 导出
  79. * @return mixed
  80. */
  81. public function getExcel()
  82. {
  83. $where = $this->request->getMore([
  84. ['data', '', '', 'time']
  85. ]);
  86. $where['time'] = $this->getDay($where['time']);
  87. return $this->success($this->services->getTrend($where, true));
  88. }
  89. /**
  90. * 格式化时间
  91. * @param $time
  92. * @return string
  93. */
  94. public function getDay($time)
  95. {
  96. if (strstr($time, '-') !== false) {
  97. [$startTime, $endTime] = explode('-', $time);
  98. if (!$startTime && !$endTime) {
  99. return date("Y/m/d", strtotime("-30 days", time())) . '-' . date("Y/m/d", time());
  100. } else {
  101. return $startTime . '-' . $endTime;
  102. }
  103. } else {
  104. return date("Y/m/d", strtotime("-30 days", time())) . '-' . date("Y/m/d", time());
  105. }
  106. }
  107. }