StatisticsService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace services\common;
  3. use common\components\Service;
  4. use common\models\goods\UserGoodsStatistics;
  5. use common\models\statistics\GoodsStatistics;
  6. use common\models\statistics\GlobalStatistics;
  7. use common\models\statistics\UserStatistics;
  8. use common\models\statistics\RegionStatistics;
  9. use Exception;
  10. class StatisticsService extends Service
  11. {
  12. public $modelClass = null;
  13. public $dimension = null;
  14. public function __get($name) {
  15. switch ($name){
  16. case 'goods':
  17. $this->modelClass = GoodsStatistics::class;
  18. break;
  19. case 'global':
  20. $this->modelClass = GlobalStatistics::class;
  21. break;
  22. case 'user':
  23. $this->modelClass = UserStatistics::class;
  24. break;
  25. case 'region':
  26. $this->modelClass = RegionStatistics::class;
  27. break;
  28. case 'alone':
  29. $this->modelClass = UserGoodsStatistics::class;
  30. break;
  31. }
  32. $this->dimension = $name;
  33. return $this;
  34. }
  35. /**
  36. * 获取统计
  37. *
  38. * 商品统计 Yii::$app->services->statistics->goods->get($mall_id)
  39. * 全局统计 Yii::$app->services->statistics->global->get($mall_id)
  40. * 订单统计 Yii::$app->services->statistics->user->get($mall_id)
  41. * @param int $mall_id
  42. * @author hpei
  43. * @return array
  44. */
  45. public function get(... $args) {
  46. switch ($this->dimension){
  47. case 'goods':
  48. $mall_id = $args[0];
  49. $stor = $args[1] ?? 'desc';
  50. $type = $args[2] ?? 'sales_num';
  51. $statisticsData = $this->modelClass::getData($mall_id, $stor, $type);
  52. break;
  53. case 'global':
  54. $mall_id = $args[0];
  55. $statisticsData = $this->modelClass::getData($mall_id);
  56. break;
  57. case 'user':
  58. $mall_id = $args[0];
  59. $statisticsData = $this->modelClass::getUserStatistics($mall_id);
  60. break;
  61. case 'region':
  62. $mall_id = $args[0];
  63. $statisticsData = $this->modelClass::getData($mall_id);
  64. }
  65. return $statisticsData;
  66. }
  67. /**
  68. * 设置统计数据
  69. *
  70. * 商品统计 Yii::$app->services->statistics->goods->set($mall_id, $goodsData)
  71. * 全局统计 Yii::$app->services->statistics->global->set($mall_id, $globalData)
  72. * 订单统计 Yii::$app->services->statistics->user->set($mall_id, $user_id, $orderData)
  73. * @param int $mall_id
  74. * @param array $data
  75. * @author hpei
  76. * @return bool
  77. */
  78. public function set(... $args) {
  79. try {
  80. switch($this->dimension) {
  81. case 'goods':
  82. $mall_id = $args[0];
  83. $goodsData = $args[1];
  84. $res = $this->modelClass::saveData($mall_id, $goodsData);
  85. break;
  86. case 'global':
  87. $mall_id = $args[0];
  88. $globalData = $args[1];
  89. $res = $this->modelClass::saveData($mall_id, $globalData);
  90. break;
  91. case 'user':
  92. $mall_id = $args[0];
  93. $user_id = $args[1];
  94. $orderData = $args[2];
  95. $res = $this->modelClass::setUserStatistics($mall_id, $user_id, $orderData);
  96. break;
  97. case 'region':
  98. $data = $args[0];
  99. $res = $this->modelClass::saveData($data);
  100. break;
  101. case 'alone':
  102. $data = $args[1];
  103. $res = $this->modelClass::saveData($data);
  104. break;
  105. }
  106. if ($res === false) throw new Exception($this->modelClass::getStaticError());
  107. return true;
  108. } catch (\Exception $e) {
  109. throw new Exception($e->getMessage().'统计维度:'.$this->dimension.$e->getFile().':'.$e->getLine().PHP_EOL);
  110. return false;
  111. }
  112. }
  113. /**
  114. * 添加注册用户数据统计
  115. */
  116. public function increaseRegisterNum($mall_id) {
  117. $res = $this->modelClass::increaseRegisterNum($mall_id);
  118. if ($res === false) throw new Exception($this->modelClass::getStaticError());
  119. return true;
  120. }
  121. /**
  122. * 添加用户下单数量
  123. */
  124. public function createOrder($mall_id, $user_id, $num) {
  125. $res = $this->modelClass::setUserStatistics($mall_id, $user_id, ['order_num' => $num]);
  126. if ($res === false) throw new Exception($this->modelClass::getStaticError());
  127. return true;
  128. }
  129. /**
  130. * 发布商品
  131. */
  132. public function publishGoods($mall_id) {
  133. $res = $this->modelClass::publishGoods($mall_id);
  134. if ($res === false) throw new Exception($this->modelClass::getStaticError());
  135. return true;
  136. }
  137. }