| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace addons\Wechat\merchant\controllers;
- use Yii;
- use yii\data\Pagination;
- use common\helpers\ArrayHelper;
- use common\helpers\ResultHelper;
- use addons\Wechat\common\models\RuleStat;
- use addons\Wechat\common\models\RuleKeywordStat;
- /**
- * 数据统计
- *
- * Class StatController
- * @package addons\Wechat\merchant\controllers
- * @author jianyan74 <751393839@qq.com>
- */
- class StatController extends BaseController
- {
- /**
- * 默认关注数据
- *
- * @var array
- */
- public $attention = [
- 'new_attention' => 0,
- 'cancel_attention' => 0,
- 'increase_attention' => 0,
- 'cumulate_attention' => 0,
- ];
- /**
- * 粉丝关注统计
- *
- * @return string
- */
- public function actionFansFollow()
- {
- // 更新微信统计数据
- Yii::$app->wechatService->fansStat->getFansStat();
- $request = Yii::$app->request;
- $from_date = $request->get('from_date', date('Y-m-d', strtotime("-6 day")));
- $to_date = $request->get('to_date', date('Y-m-d'));
- $models = Yii::$app->wechatService->fansStat->findBetweenByCreatedAt($from_date, $to_date);
- $stat = ArrayHelper::arrayKey($models, 'date');
- $fansStat = [];
- for ($i = strtotime($from_date); $i <= strtotime($to_date); $i += 86400) {
- $day = date('Y-m-d', $i);
- if (isset($stat[$day])) {
- $fansStat['new_attention'][] = $stat[$day]['new_attention'];
- $fansStat['cancel_attention'][] = $stat[$day]['cancel_attention'];
- $fansStat['increase_attention'][] = $stat[$day]['new_attention'] - $stat[$day]['cancel_attention'];
- $fansStat['cumulate_attention'][] = $stat[$day]['cumulate_attention'];
- } else {
- $fansStat['new_attention'][] = 0;
- $fansStat['cancel_attention'][] = 0;
- $fansStat['increase_attention'][] = 0;
- $fansStat['cumulate_attention'][] = 0;
- }
- $fansStat['chartTime'][] = $day;
- }
- // 昨日关注
- $yesterday = $this->attention;
- if ($yesterdayModel = Yii::$app->wechatService->fansStat->findByCreatedAt(strtotime(date('Y-m-d')) - 60 * 60 * 24)) {
- $yesterday = ArrayHelper::merge($this->attention, $yesterdayModel);
- $yesterday['increase_attention'] = $yesterday['new_attention'] - $yesterday['cancel_attention'];
- }
- // 今日关注
- $today = $this->attention;
- if ($todayModel = Yii::$app->wechatService->fansStat->findByCreatedAt(strtotime(date('Y-m-d')))) {
- $today = ArrayHelper::merge($this->attention, $todayModel);
- $today['increase_attention'] = $today['new_attention'] - $today['cancel_attention'];
- }
- return $this->render('fans-follow', [
- 'models' => $models,
- 'yesterday' => $yesterday,
- 'today' => $today,
- 'fansStat' => $fansStat,
- 'from_date' => $from_date,
- 'to_date' => $to_date,
- 'countFollow' => Yii::$app->wechatService->fans->findFollowCount()
- ]);
- }
- /**
- * 回复规则统计
- *
- * @return string
- */
- public function actionRule()
- {
- $request = Yii::$app->request;
- $from_date = $request->get('from_date', date('Y-m-d', strtotime("-60 day")));
- $to_date = $request->get('to_date', date('Y-m-d', strtotime("+1 day")));
- $data = RuleStat::find()
- ->select(['rule_id', 'sum(hit) as hit', 'max(updated_at) as updated_at'])
- ->groupBy(['rule_id'])
- ->where(['between', 'created_at', strtotime($from_date), strtotime($to_date)])
- ->andWhere(['merchant_id' => $this->getMerchantId()]);
- $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
- $models = $data->offset($pages->offset)
- ->with('rule')
- ->orderBy('updated_at desc')
- ->limit($pages->limit)
- ->all();
- return $this->render('rule', [
- 'models' => $models,
- 'pages' => $pages,
- 'from_date' => $from_date,
- 'to_date' => $to_date,
- ]);
- }
- /**
- * 回复规则统计
- *
- * @return string
- */
- public function actionRuleKeyword()
- {
- $request = Yii::$app->request;
- $from_date = $request->get('from_date', date('Y-m-d', strtotime("-60 day")));
- $to_date = $request->get('to_date', date('Y-m-d', strtotime("+1 day")));
- $data = RuleKeywordStat::find()
- ->select([
- 'keyword_id',
- 'sum(hit) as hit',
- 'max(rule_id) as rule_id',
- 'max(updated_at) as updated_at'
- ])
- ->groupBy(['keyword_id'])
- ->where(['between', 'created_at', strtotime($from_date), strtotime($to_date)])
- ->andWhere(['merchant_id' => $this->getMerchantId()]);
- $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
- $models = $data->offset($pages->offset)
- ->with(['rule', 'ruleKeyword'])
- ->orderBy('updated_at desc')
- ->limit($pages->limit)
- ->all();
- return $this->render('rule-keyword', [
- 'models' => $models,
- 'pages' => $pages,
- 'from_date' => $from_date,
- 'to_date' => $to_date,
- ]);
- }
- /**
- * @param $type
- * @return array|mixed
- */
- public function actionFansStat($type)
- {
- $data = Yii::$app->wechatService->fansStat->getBetweenCountStat($type);
- return ResultHelper::json(200, '获取成功', $data);
- }
- }
|