CensusMallGoods.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace common\models\census;
  3. use common\enums\RedisKeyEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\common\Region;
  7. use common\models\goods\Goods;
  8. use common\models\mall\Mall;
  9. use Yii;
  10. /**
  11. * This is the model class for table "qimall_census_mall_goods".
  12. *
  13. * @property int $id ID
  14. * @property int $mall_id 商城ID
  15. * @property int $goods_id 商品id
  16. * @property int $total_views 浏览量
  17. * @property int $total_sales_volume 销量
  18. * @property float $total_pay_money 销售金额
  19. * @property int $add_card_numbers 加购数量
  20. * @property int $total_visitors 访客数
  21. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  22. * @property int $created_at 创建时间
  23. * @property int $updated_at 上次更新数据时间
  24. */
  25. class CensusMallGoods extends BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%census_mall_goods}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'goods_id', 'total_views', 'total_sales_volume', 'add_card_numbers', 'total_visitors', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['total_pay_money'], 'number'],
  42. ];
  43. }
  44. public function getGoods()
  45. {
  46. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  47. }
  48. public function getMall()
  49. {
  50. return $this->hasOne(Mall::class, ['id' => 'mall_id']);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => '商城ID',
  60. 'goods_id' => '商品id',
  61. 'total_views' => '浏览量',
  62. 'total_sales_volume' => '销量',
  63. 'total_pay_money' => '销售金额',
  64. 'add_card_numbers' => '加购数量',
  65. 'total_visitors' => '访客数',
  66. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  67. 'created_at' => '创建时间',
  68. 'updated_at' => '上次更新数据时间',
  69. ];
  70. }
  71. public static function setData($params)
  72. {
  73. try {
  74. if (empty($params['mall_id']) && empty($params['goods_id'])) {
  75. return false;
  76. }
  77. $model = self::findOne(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  78. if (empty($model)) {
  79. $model = new self();
  80. $model->mall_id = $params['mall_id'];
  81. $model->goods_id = $params['goods_id'];
  82. $model->loadDefaultValues();
  83. }
  84. if (!empty($params['views'])) $model->total_views += $params['views'];
  85. if (!empty($params['sales_volume'])) $model->total_sales_volume += $params['sales_volume'];
  86. if (!empty($params['pay_money'])) $model->total_pay_money += $params['pay_money'];
  87. if (!empty($params['add_card_numbers'])) $model->add_card_numbers += $params['add_card_numbers'];
  88. if (!empty($params['visitors'])) $model->total_visitors += $params['visitors'];
  89. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  90. return $model;
  91. } catch (\Exception $e) {
  92. self::$static_error = $e->getMessage();
  93. return false;
  94. }
  95. }
  96. public static function getMallGoodsList($mall_id)
  97. {
  98. $redis = \Yii::$app->redis;
  99. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, '', true) . ':' . $mall_id;
  100. $result = $redis->zrevrange($redis_key, 0, 10, 'WITHSCORES');
  101. $data = [];
  102. if ($result) {
  103. foreach ($result as $k => $v) {
  104. if ($k % 2 == 0) {
  105. $arr = explode(':', $v);
  106. $data[$k]['goods_id'] = array_pop($arr);
  107. }
  108. if ($k % 2 == 1) $data[$k - 1]['sales_num'] = $v;
  109. }
  110. }else{
  111. $list = CensusMallGoods::lists(['where'=>[['mall_id'=>$mall_id]],'order'=>'total_sales_volume desc']);
  112. foreach ($list as $item){
  113. $data[] = [
  114. 'goods_id'=>$item['goods_id'],
  115. 'sales_num'=>$item['total_sales_volume'],
  116. ];
  117. }
  118. }
  119. $goods_id_arr = array_column($data, 'goods_id');
  120. $goods_list = Goods::lists(['where' => [['id' => $goods_id_arr]], 'select' => 'id,goods_name,cover_pic,status', 'index' => 'id']);
  121. $list = [];
  122. foreach ($data as $item) {
  123. if($goods_list[$item['goods_id']]['status'] == StatusEnum::ENABLED){
  124. $list[] = [
  125. 'goods_id' => $item['goods_id'],
  126. 'sales_num' => $item['sales_num'],
  127. 'goods_name' => $goods_list[$item['goods_id']]['goods_name'] ?? '',
  128. 'cover_pic' => $goods_list[$item['goods_id']]['cover_pic'] ?? '',
  129. ];
  130. }
  131. }
  132. return $list;
  133. }
  134. }