CensusMallUserSource.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\models\census;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\user\User;
  8. use Yii;
  9. use yii\db\Expression;
  10. /**
  11. * This is the model class for table "qimall_census_mall_user_source".
  12. *
  13. * @property int $id ID
  14. * @property int $mall_id 商城ID
  15. * @property string $source 来源
  16. * @property int $numbers 数量
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 上次更新数据时间
  20. */
  21. class CensusMallUserSource extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%census_mall_user_source}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'numbers', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['source'], 'string', 'max' => 30],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'source' => '来源',
  49. 'numbers' => '数量',
  50. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => '上次更新数据时间',
  53. ];
  54. }
  55. public static function setData($params)
  56. {
  57. try {
  58. if (empty($params['mall_id']) && empty($params['numbers'])) {
  59. return false;
  60. }
  61. $model = self::findOne(['mall_id' => $params['mall_id'],'source'=>$params['platform'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  62. if (empty($model)) {
  63. $model = new self();
  64. $model->mall_id = $params['mall_id'];
  65. $model->source = $params['platform'];
  66. $model->loadDefaultValues();
  67. }
  68. if (!empty($params['numbers'])) $model->numbers += $params['numbers'];
  69. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  70. return $model;
  71. } catch (\Exception $e) {
  72. self::$static_error = $e->getMessage();
  73. return false;
  74. }
  75. }
  76. public static function setCensusMallCache($mall_id, $data)
  77. {
  78. $redis = \Yii::$app->redis;
  79. $key = RedisKeyEnum::CENSUS_MALL_PLATFORM_USER;
  80. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  81. if (empty($data['platform'])) {
  82. $data['platform'] = 'manual';
  83. }
  84. $result = $redis->hget($redis_key, $data['platform']);
  85. if (!$result) {
  86. $result = 0;
  87. }
  88. $now_num = $result + $data['numbers'];
  89. $redis->hset($redis_key, $data['platform'], $now_num);
  90. $data['mall_id'] = $mall_id;
  91. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, self::class, 'setData');
  92. }
  93. public static function getMallUserSource($mall_id){
  94. $redis = \Yii::$app->redis;
  95. $key = RedisKeyEnum::CENSUS_MALL_PLATFORM_USER_OVERVIEW;
  96. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  97. // 因为大屏显示数据在两分钟内变动不太频繁。所以设置两分钟过期。防止同时多个请求页面导致数据库查询次数过多
  98. $userSource = $redis->get($redis_key);
  99. if (!empty($userSource)) {
  100. return json_decode($userSource, true);
  101. }
  102. $source_name_map = User::TYPE_NAME_LIST;
  103. /*备注:因为用户使用微信注册登录后。在后台注销授权。在使用小程序登录。用户来源会改变为小程序。所以需要实时读取数据*/
  104. // 获取当前全部用户来源。
  105. $userSource = User::find()
  106. ->select([ // 因为当用户来源为空时需要将其改为其它
  107. new Expression('IF(platform IS NULL OR platform = "", "other", platform) AS platform'),
  108. 'COUNT(platform) AS num',
  109. ])
  110. ->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  111. ->groupBy(new Expression('IF(platform IS NULL OR platform = "", "other", platform)'))
  112. ->orderBy('num DESC')
  113. ->asArray()
  114. ->all();
  115. foreach ($userSource as &$item) {
  116. $item['name'] = $source_name_map[$item['platform']] ?? '其它';
  117. }
  118. // 以为是平面图的原因。所以至少需要显示3个来源。当数量小于3时。从来源中取出其它来源来补充
  119. if (count($userSource) < 3) {
  120. $diffList = array_diff(array_keys($source_name_map), array_column($userSource, 'platform'));
  121. for ($i = 0; $i < 3 - count($userSource); $i++) {
  122. $userSource[] = [
  123. 'platform' => $diffList[$i],
  124. 'name' => $source_name_map[$diffList[$i]],
  125. 'num' => 0,
  126. ];
  127. }
  128. }
  129. $redis->set($redis_key, json_encode($userSource), 'EX', 120);
  130. return $userSource ?? [];
  131. }
  132. }