| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace addons\Wechat\services;
- use Yii;
- use addons\Wechat\common\models\FansStat;
- use common\components\Service;
- use common\helpers\EchantsHelper;
- /**
- * Class FansStatService
- * @package addons\Wechat\services
- * @author jianyan74 <751393839@qq.com>
- */
- class FansStatService extends Service
- {
- /**
- * 获取区间统计
- *
- * @return array|\yii\db\ActiveRecord|null
- */
- public function getBetweenCountStat($type)
- {
- $fields = [
- 'new_attention' => '新关注',
- 'cancel_attention' => '取消关注',
- 'cumulate_attention' => '累积关注',
- ];
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) use ($type) {
- $data = FansStat::find()
- ->select([
- 'sum(new_attention) as new_attention',
- 'sum(cancel_attention) as cancel_attention',
- 'sum(cumulate_attention) as cumulate_attention',
- "from_unixtime(created_at, '$formatting') as time"
- ])
- ->where(['between', 'created_at', $start_time, $end_time])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->groupBy(['time'])
- ->asArray()
- ->all();
- return $data;
- }, $fields, $time, $format);
- }
- /**
- * 关注计算
- */
- public function upFollowNum()
- {
- if (!($today = FansStat::find()->where(['date' => date('Y-m-d')])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
- $today = new FansStat();
- $today->date = date('Y-m-d');
- $today->created_at = strtotime($today->date);
- }
- $today->new_attention += 1;
- $today->save();
- }
- /**
- * 取消关注计算
- */
- public function upUnFollowNum()
- {
- if (!($today = FansStat::find()->where(['date' => date('Y-m-d')])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->one())) {
- $today = new FansStat();
- $today->date = date('Y-m-d');
- $today->created_at = strtotime($today->date);
- }
- $today->cancel_attention += 1;
- $today->save();
- }
- /**
- * @param $created_at
- * @return array|null|\yii\db\ActiveRecord
- */
- public function findByCreatedAt($created_at)
- {
- return FansStat::find()
- ->where(['created_at' => $created_at])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->asArray()
- ->one();
- }
- /**
- * @param $from_date
- * @param $to_date
- * @return array|\yii\db\ActiveRecord[]
- */
- public function findBetweenByCreatedAt($from_date, $to_date)
- {
- return FansStat::find()
- ->where(['between', 'created_at', $from_date, strtotime($to_date)])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->orderBy('created_at asc')
- ->asArray()
- ->all();
- }
- /**
- * @param $app
- * @return bool
- */
- public function getFansStat()
- {
- // 缓存设置
- $cacheKey = 'wechatFansStat:';
- if (Yii::$app->cache->get($cacheKey)) {
- return true;
- }
- $sevenDays = [
- date('Y-m-d', strtotime('-1 days')),
- date('Y-m-d', strtotime('-2 days')),
- date('Y-m-d', strtotime('-3 days')),
- date('Y-m-d', strtotime('-4 days')),
- date('Y-m-d', strtotime('-5 days')),
- date('Y-m-d', strtotime('-6 days')),
- date('Y-m-d', strtotime('-7 days')),
- ];
- $models = FansStat::find()
- ->where(['in', 'date', $sevenDays])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->all();
- $statUpdate = false;
- $weekStat = [];
- foreach ($models as $model) {
- $weekStat[$model['date']] = $model;
- }
- // 查询数据是否有
- foreach ($sevenDays as $sevenDay) {
- if (empty($weekStat[$sevenDay]) || $weekStat[$sevenDay]['cumulate_attention'] <= 0) {
- $statUpdate = true;
- break;
- }
- }
- if (empty($statUpdate)) {
- return true;
- }
- // 获取微信统计数据
- $stats = Yii::$app->wechat->app->data_cube;
- // 增减
- $userSummary = $stats->userSummary($sevenDays[6], $sevenDays[0]);
- // 累计用户
- $userCumulate = $stats->userCumulate($sevenDays[6], $sevenDays[0]);
- $list = [];
- if (!empty($userSummary['list'])) {
- foreach ($userSummary['list'] as $row) {
- $key = $row['ref_date'];
- $list[$key]['new_attention'] = $row['new_user'];
- $list[$key]['cancel_attention'] = $row['cancel_user'];
- }
- }
- if (!empty($userCumulate['list'])) {
- foreach ($userCumulate['list'] as $row) {
- $key = $row['ref_date'];
- $list[$key]['cumulate_attention'] = $row['cumulate_user'];
- }
- }
- // 更新到数据库
- foreach ($list as $key => $value) {
- $model = new FansStat();
- if (isset($weekStat[$key])) {
- $model = $weekStat[$key];
- }
- $model->attributes = $value;
- $model->date = $key;
- $model->created_at = strtotime($key);
- $model->save();
- }
- // 今日累计关注统计计算
- $cumulate_attention = Yii::$app->wechatService->fans->findFollowCount();
- if (!($today = FansStat::find()->where(['date' => date('Y-m-d')])->one())) {
- $today = new FansStat();
- $today->date = date('Y-m-d');
- $today->created_at = strtotime($today->date);
- }
- $today->cumulate_attention = $cumulate_attention;
- $today->save();
- Yii::$app->cache->set($cacheKey, true, 7200);
- return true;
- }
- }
|