| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace services\common;
- use common\components\Service;
- use common\models\goods\UserGoodsStatistics;
- use common\models\statistics\GoodsStatistics;
- use common\models\statistics\GlobalStatistics;
- use common\models\statistics\UserStatistics;
- use common\models\statistics\RegionStatistics;
- use Exception;
- class StatisticsService extends Service
- {
- public $modelClass = null;
- public $dimension = null;
- public function __get($name) {
- switch ($name){
- case 'goods':
- $this->modelClass = GoodsStatistics::class;
- break;
- case 'global':
- $this->modelClass = GlobalStatistics::class;
- break;
- case 'user':
- $this->modelClass = UserStatistics::class;
- break;
- case 'region':
- $this->modelClass = RegionStatistics::class;
- break;
- case 'alone':
- $this->modelClass = UserGoodsStatistics::class;
- break;
- }
- $this->dimension = $name;
- return $this;
- }
- /**
- * 获取统计
- *
- * 商品统计 Yii::$app->services->statistics->goods->get($mall_id)
- * 全局统计 Yii::$app->services->statistics->global->get($mall_id)
- * 订单统计 Yii::$app->services->statistics->user->get($mall_id)
- * @param int $mall_id
- * @author hpei
- * @return array
- */
- public function get(... $args) {
- switch ($this->dimension){
- case 'goods':
- $mall_id = $args[0];
- $stor = $args[1] ?? 'desc';
- $type = $args[2] ?? 'sales_num';
- $statisticsData = $this->modelClass::getData($mall_id, $stor, $type);
- break;
- case 'global':
- $mall_id = $args[0];
- $statisticsData = $this->modelClass::getData($mall_id);
- break;
- case 'user':
- $mall_id = $args[0];
- $statisticsData = $this->modelClass::getUserStatistics($mall_id);
- break;
- case 'region':
- $mall_id = $args[0];
- $statisticsData = $this->modelClass::getData($mall_id);
- }
- return $statisticsData;
- }
- /**
- * 设置统计数据
- *
- * 商品统计 Yii::$app->services->statistics->goods->set($mall_id, $goodsData)
- * 全局统计 Yii::$app->services->statistics->global->set($mall_id, $globalData)
- * 订单统计 Yii::$app->services->statistics->user->set($mall_id, $user_id, $orderData)
- * @param int $mall_id
- * @param array $data
- * @author hpei
- * @return bool
- */
- public function set(... $args) {
- try {
- switch($this->dimension) {
- case 'goods':
- $mall_id = $args[0];
- $goodsData = $args[1];
- $res = $this->modelClass::saveData($mall_id, $goodsData);
- break;
- case 'global':
- $mall_id = $args[0];
- $globalData = $args[1];
- $res = $this->modelClass::saveData($mall_id, $globalData);
- break;
- case 'user':
- $mall_id = $args[0];
- $user_id = $args[1];
- $orderData = $args[2];
- $res = $this->modelClass::setUserStatistics($mall_id, $user_id, $orderData);
- break;
- case 'region':
- $data = $args[0];
- $res = $this->modelClass::saveData($data);
- break;
- case 'alone':
- $data = $args[1];
- $res = $this->modelClass::saveData($data);
- break;
- }
- if ($res === false) throw new Exception($this->modelClass::getStaticError());
- return true;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage().'统计维度:'.$this->dimension.$e->getFile().':'.$e->getLine().PHP_EOL);
- return false;
- }
- }
- /**
- * 添加注册用户数据统计
- */
- public function increaseRegisterNum($mall_id) {
- $res = $this->modelClass::increaseRegisterNum($mall_id);
- if ($res === false) throw new Exception($this->modelClass::getStaticError());
- return true;
- }
- /**
- * 添加用户下单数量
- */
- public function createOrder($mall_id, $user_id, $num) {
- $res = $this->modelClass::setUserStatistics($mall_id, $user_id, ['order_num' => $num]);
- if ($res === false) throw new Exception($this->modelClass::getStaticError());
- return true;
- }
- /**
- * 发布商品
- */
- public function publishGoods($mall_id) {
- $res = $this->modelClass::publishGoods($mall_id);
- if ($res === false) throw new Exception($this->modelClass::getStaticError());
- return true;
- }
- }
|