StatController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use common\helpers\ArrayHelper;
  6. use common\helpers\ResultHelper;
  7. use addons\Wechat\common\models\RuleStat;
  8. use addons\Wechat\common\models\RuleKeywordStat;
  9. /**
  10. * 数据统计
  11. *
  12. * Class StatController
  13. * @package addons\Wechat\merchant\controllers
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class StatController extends BaseController
  17. {
  18. /**
  19. * 默认关注数据
  20. *
  21. * @var array
  22. */
  23. public $attention = [
  24. 'new_attention' => 0,
  25. 'cancel_attention' => 0,
  26. 'increase_attention' => 0,
  27. 'cumulate_attention' => 0,
  28. ];
  29. /**
  30. * 粉丝关注统计
  31. *
  32. * @return string
  33. */
  34. public function actionFansFollow()
  35. {
  36. // 更新微信统计数据
  37. Yii::$app->wechatService->fansStat->getFansStat();
  38. $request = Yii::$app->request;
  39. $from_date = $request->get('from_date', date('Y-m-d', strtotime("-6 day")));
  40. $to_date = $request->get('to_date', date('Y-m-d'));
  41. $models = Yii::$app->wechatService->fansStat->findBetweenByCreatedAt($from_date, $to_date);
  42. $stat = ArrayHelper::arrayKey($models, 'date');
  43. $fansStat = [];
  44. for ($i = strtotime($from_date); $i <= strtotime($to_date); $i += 86400) {
  45. $day = date('Y-m-d', $i);
  46. if (isset($stat[$day])) {
  47. $fansStat['new_attention'][] = $stat[$day]['new_attention'];
  48. $fansStat['cancel_attention'][] = $stat[$day]['cancel_attention'];
  49. $fansStat['increase_attention'][] = $stat[$day]['new_attention'] - $stat[$day]['cancel_attention'];
  50. $fansStat['cumulate_attention'][] = $stat[$day]['cumulate_attention'];
  51. } else {
  52. $fansStat['new_attention'][] = 0;
  53. $fansStat['cancel_attention'][] = 0;
  54. $fansStat['increase_attention'][] = 0;
  55. $fansStat['cumulate_attention'][] = 0;
  56. }
  57. $fansStat['chartTime'][] = $day;
  58. }
  59. // 昨日关注
  60. $yesterday = $this->attention;
  61. if ($yesterdayModel = Yii::$app->wechatService->fansStat->findByCreatedAt(strtotime(date('Y-m-d')) - 60 * 60 * 24)) {
  62. $yesterday = ArrayHelper::merge($this->attention, $yesterdayModel);
  63. $yesterday['increase_attention'] = $yesterday['new_attention'] - $yesterday['cancel_attention'];
  64. }
  65. // 今日关注
  66. $today = $this->attention;
  67. if ($todayModel = Yii::$app->wechatService->fansStat->findByCreatedAt(strtotime(date('Y-m-d')))) {
  68. $today = ArrayHelper::merge($this->attention, $todayModel);
  69. $today['increase_attention'] = $today['new_attention'] - $today['cancel_attention'];
  70. }
  71. return $this->render('fans-follow', [
  72. 'models' => $models,
  73. 'yesterday' => $yesterday,
  74. 'today' => $today,
  75. 'fansStat' => $fansStat,
  76. 'from_date' => $from_date,
  77. 'to_date' => $to_date,
  78. 'countFollow' => Yii::$app->wechatService->fans->findFollowCount()
  79. ]);
  80. }
  81. /**
  82. * 回复规则统计
  83. *
  84. * @return string
  85. */
  86. public function actionRule()
  87. {
  88. $request = Yii::$app->request;
  89. $from_date = $request->get('from_date', date('Y-m-d', strtotime("-60 day")));
  90. $to_date = $request->get('to_date', date('Y-m-d', strtotime("+1 day")));
  91. $data = RuleStat::find()
  92. ->select(['rule_id', 'sum(hit) as hit', 'max(updated_at) as updated_at'])
  93. ->groupBy(['rule_id'])
  94. ->where(['between', 'created_at', strtotime($from_date), strtotime($to_date)])
  95. ->andWhere(['merchant_id' => $this->getMerchantId()]);
  96. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  97. $models = $data->offset($pages->offset)
  98. ->with('rule')
  99. ->orderBy('updated_at desc')
  100. ->limit($pages->limit)
  101. ->all();
  102. return $this->render('rule', [
  103. 'models' => $models,
  104. 'pages' => $pages,
  105. 'from_date' => $from_date,
  106. 'to_date' => $to_date,
  107. ]);
  108. }
  109. /**
  110. * 回复规则统计
  111. *
  112. * @return string
  113. */
  114. public function actionRuleKeyword()
  115. {
  116. $request = Yii::$app->request;
  117. $from_date = $request->get('from_date', date('Y-m-d', strtotime("-60 day")));
  118. $to_date = $request->get('to_date', date('Y-m-d', strtotime("+1 day")));
  119. $data = RuleKeywordStat::find()
  120. ->select([
  121. 'keyword_id',
  122. 'sum(hit) as hit',
  123. 'max(rule_id) as rule_id',
  124. 'max(updated_at) as updated_at'
  125. ])
  126. ->groupBy(['keyword_id'])
  127. ->where(['between', 'created_at', strtotime($from_date), strtotime($to_date)])
  128. ->andWhere(['merchant_id' => $this->getMerchantId()]);
  129. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  130. $models = $data->offset($pages->offset)
  131. ->with(['rule', 'ruleKeyword'])
  132. ->orderBy('updated_at desc')
  133. ->limit($pages->limit)
  134. ->all();
  135. return $this->render('rule-keyword', [
  136. 'models' => $models,
  137. 'pages' => $pages,
  138. 'from_date' => $from_date,
  139. 'to_date' => $to_date,
  140. ]);
  141. }
  142. /**
  143. * @param $type
  144. * @return array|mixed
  145. */
  146. public function actionFansStat($type)
  147. {
  148. $data = Yii::$app->wechatService->fansStat->getBetweenCountStat($type);
  149. return ResultHelper::json(200, '获取成功', $data);
  150. }
  151. }
粤ICP备19079148号