FansStatService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace addons\Wechat\services;
  3. use Yii;
  4. use addons\Wechat\common\models\FansStat;
  5. use common\components\Service;
  6. use common\helpers\EchantsHelper;
  7. /**
  8. * Class FansStatService
  9. * @package addons\Wechat\services
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class FansStatService extends Service
  13. {
  14. /**
  15. * 获取区间统计
  16. *
  17. * @return array|\yii\db\ActiveRecord|null
  18. */
  19. public function getBetweenCountStat($type)
  20. {
  21. $fields = [
  22. 'new_attention' => '新关注',
  23. 'cancel_attention' => '取消关注',
  24. 'cumulate_attention' => '累积关注',
  25. ];
  26. // 获取时间和格式化
  27. list($time, $format) = EchantsHelper::getFormatTime($type);
  28. // 获取数据
  29. return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) use ($type) {
  30. $data = FansStat::find()
  31. ->select([
  32. 'sum(new_attention) as new_attention',
  33. 'sum(cancel_attention) as cancel_attention',
  34. 'sum(cumulate_attention) as cumulate_attention',
  35. "from_unixtime(created_at, '$formatting') as time"
  36. ])
  37. ->where(['between', 'created_at', $start_time, $end_time])
  38. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  39. ->groupBy(['time'])
  40. ->asArray()
  41. ->all();
  42. return $data;
  43. }, $fields, $time, $format);
  44. }
  45. /**
  46. * 关注计算
  47. */
  48. public function upFollowNum()
  49. {
  50. if (!($today = FansStat::find()->where(['date' => date('Y-m-d')])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
  51. $today = new FansStat();
  52. $today->date = date('Y-m-d');
  53. $today->created_at = strtotime($today->date);
  54. }
  55. $today->new_attention += 1;
  56. $today->save();
  57. }
  58. /**
  59. * 取消关注计算
  60. */
  61. public function upUnFollowNum()
  62. {
  63. if (!($today = FansStat::find()->where(['date' => date('Y-m-d')])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
  64. $today = new FansStat();
  65. $today->date = date('Y-m-d');
  66. $today->created_at = strtotime($today->date);
  67. }
  68. $today->cancel_attention += 1;
  69. $today->save();
  70. }
  71. /**
  72. * @param $created_at
  73. * @return array|null|\yii\db\ActiveRecord
  74. */
  75. public function findByCreatedAt($created_at)
  76. {
  77. return FansStat::find()
  78. ->where(['created_at' => $created_at])
  79. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  80. ->asArray()
  81. ->one();
  82. }
  83. /**
  84. * @param $from_date
  85. * @param $to_date
  86. * @return array|\yii\db\ActiveRecord[]
  87. */
  88. public function findBetweenByCreatedAt($from_date, $to_date)
  89. {
  90. return FansStat::find()
  91. ->where(['between', 'created_at', $from_date, strtotime($to_date)])
  92. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  93. ->orderBy('created_at asc')
  94. ->asArray()
  95. ->all();
  96. }
  97. /**
  98. * @param $app
  99. * @return bool
  100. */
  101. public function getFansStat()
  102. {
  103. // 缓存设置
  104. $cacheKey = 'wechatFansStat:';
  105. if (Yii::$app->cache->get($cacheKey)) {
  106. return true;
  107. }
  108. $sevenDays = [
  109. date('Y-m-d', strtotime('-1 days')),
  110. date('Y-m-d', strtotime('-2 days')),
  111. date('Y-m-d', strtotime('-3 days')),
  112. date('Y-m-d', strtotime('-4 days')),
  113. date('Y-m-d', strtotime('-5 days')),
  114. date('Y-m-d', strtotime('-6 days')),
  115. date('Y-m-d', strtotime('-7 days')),
  116. ];
  117. $models = FansStat::find()
  118. ->where(['in', 'date', $sevenDays])
  119. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  120. ->all();
  121. $statUpdate = false;
  122. $weekStat = [];
  123. foreach ($models as $model) {
  124. $weekStat[$model['date']] = $model;
  125. }
  126. // 查询数据是否有
  127. foreach ($sevenDays as $sevenDay) {
  128. if (empty($weekStat[$sevenDay]) || $weekStat[$sevenDay]['cumulate_attention'] <= 0) {
  129. $statUpdate = true;
  130. break;
  131. }
  132. }
  133. if (empty($statUpdate)) {
  134. return true;
  135. }
  136. // 获取微信统计数据
  137. $stats = Yii::$app->wechat->app->data_cube;
  138. // 增减
  139. $userSummary = $stats->userSummary($sevenDays[6], $sevenDays[0]);
  140. // 累计用户
  141. $userCumulate = $stats->userCumulate($sevenDays[6], $sevenDays[0]);
  142. $list = [];
  143. if (!empty($userSummary['list'])) {
  144. foreach ($userSummary['list'] as $row) {
  145. $key = $row['ref_date'];
  146. $list[$key]['new_attention'] = $row['new_user'];
  147. $list[$key]['cancel_attention'] = $row['cancel_user'];
  148. }
  149. }
  150. if (!empty($userCumulate['list'])) {
  151. foreach ($userCumulate['list'] as $row) {
  152. $key = $row['ref_date'];
  153. $list[$key]['cumulate_attention'] = $row['cumulate_user'];
  154. }
  155. }
  156. // 更新到数据库
  157. foreach ($list as $key => $value) {
  158. $model = new FansStat();
  159. if (isset($weekStat[$key])) {
  160. $model = $weekStat[$key];
  161. }
  162. $model->attributes = $value;
  163. $model->date = $key;
  164. $model->created_at = strtotime($key);
  165. $model->save();
  166. }
  167. // 今日累计关注统计计算
  168. $cumulate_attention = Yii::$app->wechatService->fans->findFollowCount();
  169. if (!($today = FansStat::find()->where(['date' => date('Y-m-d')])->one())) {
  170. $today = new FansStat();
  171. $today->date = date('Y-m-d');
  172. $today->created_at = strtotime($today->date);
  173. }
  174. $today->cumulate_attention = $cumulate_attention;
  175. $today->save();
  176. Yii::$app->cache->set($cacheKey, true, 7200);
  177. return true;
  178. }
  179. }
粤ICP备19079148号