| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace common\models\census;
- use common\enums\RabbitMqEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\user\User;
- use Yii;
- use yii\db\Expression;
- /**
- * This is the model class for table "qimall_census_mall_user_source".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property string $source 来源
- * @property int $numbers 数量
- * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- */
- class CensusMallUserSource extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%census_mall_user_source}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'numbers', 'status', 'created_at', 'updated_at'], 'integer'],
- [['source'], 'string', 'max' => 30],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'source' => '来源',
- 'numbers' => '数量',
- 'status' => '状态[-1:删除;0:禁用;1启用;]',
- 'created_at' => '创建时间',
- 'updated_at' => '上次更新数据时间',
- ];
- }
- public static function setData($params)
- {
- try {
- if (empty($params['mall_id']) && empty($params['numbers'])) {
- return false;
- }
- $model = self::findOne(['mall_id' => $params['mall_id'],'source'=>$params['platform'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->source = $params['platform'];
- $model->loadDefaultValues();
- }
- if (!empty($params['numbers'])) $model->numbers += $params['numbers'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function setCensusMallCache($mall_id, $data)
- {
- $redis = \Yii::$app->redis;
- $key = RedisKeyEnum::CENSUS_MALL_PLATFORM_USER;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
- if (empty($data['platform'])) {
- $data['platform'] = 'manual';
- }
- $result = $redis->hget($redis_key, $data['platform']);
- if (!$result) {
- $result = 0;
- }
- $now_num = $result + $data['numbers'];
- $redis->hset($redis_key, $data['platform'], $now_num);
- $data['mall_id'] = $mall_id;
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, self::class, 'setData');
- }
- public static function getMallUserSource($mall_id){
- $redis = \Yii::$app->redis;
- $key = RedisKeyEnum::CENSUS_MALL_PLATFORM_USER_OVERVIEW;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
- // 因为大屏显示数据在两分钟内变动不太频繁。所以设置两分钟过期。防止同时多个请求页面导致数据库查询次数过多
- $userSource = $redis->get($redis_key);
- if (!empty($userSource)) {
- return json_decode($userSource, true);
- }
- $source_name_map = User::TYPE_NAME_LIST;
- /*备注:因为用户使用微信注册登录后。在后台注销授权。在使用小程序登录。用户来源会改变为小程序。所以需要实时读取数据*/
- // 获取当前全部用户来源。
- $userSource = User::find()
- ->select([ // 因为当用户来源为空时需要将其改为其它
- new Expression('IF(platform IS NULL OR platform = "", "other", platform) AS platform'),
- 'COUNT(platform) AS num',
- ])
- ->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->groupBy(new Expression('IF(platform IS NULL OR platform = "", "other", platform)'))
- ->orderBy('num DESC')
- ->asArray()
- ->all();
- foreach ($userSource as &$item) {
- $item['name'] = $source_name_map[$item['platform']] ?? '其它';
- }
- // 以为是平面图的原因。所以至少需要显示3个来源。当数量小于3时。从来源中取出其它来源来补充
- if (count($userSource) < 3) {
- $diffList = array_diff(array_keys($source_name_map), array_column($userSource, 'platform'));
- for ($i = 0; $i < 3 - count($userSource); $i++) {
- $userSource[] = [
- 'platform' => $diffList[$i],
- 'name' => $source_name_map[$diffList[$i]],
- 'num' => 0,
- ];
- }
- }
- $redis->set($redis_key, json_encode($userSource), 'EX', 120);
- return $userSource ?? [];
- }
- }
|