| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace common\models\census;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\Region;
- use common\models\goods\Goods;
- use common\models\mall\Mall;
- use Yii;
- /**
- * This is the model class for table "qimall_census_mall_goods".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $goods_id 商品id
- * @property int $total_views 浏览量
- * @property int $total_sales_volume 销量
- * @property float $total_pay_money 销售金额
- * @property int $add_card_numbers 加购数量
- * @property int $total_visitors 访客数
- * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- */
- class CensusMallGoods extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%census_mall_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'total_views', 'total_sales_volume', 'add_card_numbers', 'total_visitors', 'status', 'created_at', 'updated_at'], 'integer'],
- [['total_pay_money'], 'number'],
- ];
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public function getMall()
- {
- return $this->hasOne(Mall::class, ['id' => 'mall_id']);
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'goods_id' => '商品id',
- 'total_views' => '浏览量',
- 'total_sales_volume' => '销量',
- 'total_pay_money' => '销售金额',
- 'add_card_numbers' => '加购数量',
- 'total_visitors' => '访客数',
- 'status' => '状态[-1:删除;0:禁用;1启用;]',
- 'created_at' => '创建时间',
- 'updated_at' => '上次更新数据时间',
- ];
- }
- public static function setData($params)
- {
- try {
- if (empty($params['mall_id']) && empty($params['goods_id'])) {
- return false;
- }
- $model = self::findOne(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->goods_id = $params['goods_id'];
- $model->loadDefaultValues();
- }
- if (!empty($params['views'])) $model->total_views += $params['views'];
- if (!empty($params['sales_volume'])) $model->total_sales_volume += $params['sales_volume'];
- if (!empty($params['pay_money'])) $model->total_pay_money += $params['pay_money'];
- if (!empty($params['add_card_numbers'])) $model->add_card_numbers += $params['add_card_numbers'];
- if (!empty($params['visitors'])) $model->total_visitors += $params['visitors'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getMallGoodsList($mall_id)
- {
- $redis = \Yii::$app->redis;
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, '', true) . ':' . $mall_id;
- $result = $redis->zrevrange($redis_key, 0, 10, 'WITHSCORES');
- $data = [];
- if ($result) {
- foreach ($result as $k => $v) {
- if ($k % 2 == 0) {
- $arr = explode(':', $v);
- $data[$k]['goods_id'] = array_pop($arr);
- }
- if ($k % 2 == 1) $data[$k - 1]['sales_num'] = $v;
- }
- }else{
- $list = CensusMallGoods::lists(['where'=>[['mall_id'=>$mall_id]],'order'=>'total_sales_volume desc']);
- foreach ($list as $item){
- $data[] = [
- 'goods_id'=>$item['goods_id'],
- 'sales_num'=>$item['total_sales_volume'],
- ];
- }
- }
- $goods_id_arr = array_column($data, 'goods_id');
- $goods_list = Goods::lists(['where' => [['id' => $goods_id_arr]], 'select' => 'id,goods_name,cover_pic,status', 'index' => 'id']);
- $list = [];
- foreach ($data as $item) {
- if($goods_list[$item['goods_id']]['status'] == StatusEnum::ENABLED){
- $list[] = [
- 'goods_id' => $item['goods_id'],
- 'sales_num' => $item['sales_num'],
- 'goods_name' => $goods_list[$item['goods_id']]['goods_name'] ?? '',
- 'cover_pic' => $goods_list[$item['goods_id']]['cover_pic'] ?? '',
- ];
- }
- }
- return $list;
- }
- }
|