'ID', 'mall_id' => '商城ID', 'total_users' => '总会员数', 'total_orders' => '总订单量', 'total_pay_orders' => '支付订单量', 'total_pay_people' => '支付人数', 'number_of_orders' => '下单人数', 'repurchase_people' => '复购人数', 'share_people' => '分享人数', 'total_visitors' => '总访客数', 'total_views' => '总浏览量', 'new_customers' => '新客户,未支付过', 'regular_customers' => '老客户,已支付过', 'goods_listings' => '商品上架量', 'goods_total_visitors' => '商品访客数', 'goods_total_views' => '商品浏览量', 'new_customers_pay_peoples' => '新客户成交人数', 'regular_customers_pay_peoples' => '老客户成交人数', 'total_order_money' => '下单金额', 'total_pay_money' => '支付金额', 'repurchase_money' => '复购金额', 'new_customers_pay_money' => '新客户成交金额', 'regular_customers_pay_money' => '老客户成交金额', 'total_follow_num' => '关注', 'status' => '状态[-1:删除;0:禁用;1启用;]', 'created_at' => '创建时间', 'updated_at' => '上次更新数据时间', ]; } public static function setData($params) { try { if (empty($params['mall_id'])) { return false; } $model = self::findOne(['mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) { $model = new self(); $model->mall_id = $params['mall_id']; $model->loadDefaultValues(); } if (!empty($params['users'])) $model->total_users += $params['users']; if (!empty($params['create_counts'])) $model->total_orders += $params['create_counts']; if (!empty($params['number_of_orders'])) $model->number_of_orders += $params['number_of_orders']; if (!empty($params['order_money'])) $model->total_order_money += $params['order_money']; if (!empty($params['pay_counts'])) $model->total_pay_orders += $params['pay_counts']; if (!empty($params['pay_people'])) $model->total_pay_people += $params['pay_people']; if (!empty($params['pay_money'])) $model->total_pay_money += $params['pay_money']; if (!empty($params['repurchase_people'])) $model->repurchase_people += $params['repurchase_people']; if (!empty($params['repurchase_money'])) $model->repurchase_money += $params['repurchase_money']; if (!empty($params['regular_customers_pay_money'])) $model->regular_customers_pay_money += $params['regular_customers_pay_money']; if (!empty($params['regular_customers_pay_peoples'])) $model->regular_customers_pay_peoples += $params['regular_customers_pay_peoples']; if (!empty($params['new_customers'])) $model->new_customers += $params['new_customers']; if (!empty($params['share_people'])) $model->share_people += $params['share_people']; if (!empty($params['regular_customers'])) $model->regular_customers += $params['regular_customers']; if (!empty($params['new_customers_pay_peoples'])) $model->new_customers_pay_peoples += $params['new_customers_pay_peoples']; if (!empty($params['new_customers_pay_money'])) $model->new_customers_pay_money += $params['new_customers_pay_money']; if (!empty($params['views'])) $model->total_views += $params['views']; if (!empty($params['goods_listings'])) $model->goods_listings += $params['goods_listings']; if (!empty($params['goods_total_visitors'])) $model->goods_total_visitors += $params['goods_total_visitors']; if (!empty($params['goods_total_views'])) $model->goods_total_views += $params['goods_total_views']; if (!empty($params['visitors'])) $model->total_visitors = $params['visitors']; if (!empty($params['total_follow_num'])) $model->total_follow_num += $params['total_follow_num']; if (!empty($params['add_pay_nums'])) $model->add_pay_nums += $params['add_pay_nums']; if (!empty($params['add_goods'])) $model->add_goods += $params['add_goods']; 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, $type = 'views', $fields = []) { $redis = \Yii::$app->redis; $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id; switch ($type) { case 'views': self::getViews($redis, $redis_key, $data); break; case 'share': $result = $redis->hget($redis_key, $fields); $data['share_people'] = $data['share_people'] ?? 1; !empty($result) ? $result += $data['share_people'] : $result = $data['share_people']; $redis->hset($redis_key, $fields, $result); break; case 'goods_listings': $result = $redis->hget($redis_key, $fields); $data['goods_listings'] = $data['goods_listings'] ?? 1; !empty($result) ? $result += $data['goods_listings'] : $result = $data['goods_listings'] ? $data['goods_listings'] : 0; $redis->hset($redis_key, $fields, $result); break; case 'visitors': $counts = User::find()->andWhere(['mall_id'=>$mall_id])->andWhere(['>','last_login_at',0])->count(); $data['visitors'] = $counts ?? 0; $redis->hset($redis_key, $fields, $data['visitors'] ); break; case 'pay': self::getPayOrder($redis, $redis_key, $data); break; case 'create_order': self::getCreateOrder($redis, $redis_key, $data); break; case 'user_register': self::getUserRegister($redis, $redis_key, $data); break; case 'follow': $result = $redis->hget($redis_key, $fields); $data['total_follow_num'] = $data['total_follow_num'] ?? 1; !empty($result) ? $result += $data['total_follow_num'] : $result = $data['total_follow_num']; $redis->hset($redis_key, $fields, $result); break; case 'add_goods': $result = $redis->hget($redis_key, $fields); $data['add_goods'] = $data['add_goods'] ?? 1; !empty($result) ? $result += $data['add_goods'] : $result = $data['add_goods'] ? $data['add_goods'] : 0; $redis->hset($redis_key, $fields, $result); break; default: return; } $data['mall_id'] = $mall_id; \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK_CENSUS, RabbitMqEnum::CENSUS_QUEUE, $data, self::class, 'setData'); } //添加订单 public static function getCreateOrder($redis, $redis_key, $data) { $fields = ['total_orders', 'total_order_money']; if(!Order::userIsCreated($data['user_id'])){ $fields[] = 'number_of_orders'; } $arr = array_flip($fields); $result = $redis->hmget($redis_key, ...$fields); if(isset($arr['number_of_orders'])){ isset($result[$arr['number_of_orders']]) ? $result[$arr['number_of_orders']] += $data['number_of_orders'] : $result[$arr['number_of_orders']] = $data['number_of_orders']; $set_data[] = 'number_of_orders'; $set_data[] = $result[$arr['number_of_orders']]; } isset($result[$arr['total_orders']]) ? $result[$arr['total_orders']] += $data['create_counts'] : $result[$arr['total_orders']] = $data['create_counts']; isset($result[$arr['total_order_money']]) ? $result[$arr['total_order_money']] += $data['order_money'] : $result[$arr['total_order_money']] = $data['order_money']; $set_data[] = 'total_orders'; $set_data[] = $result[$arr['total_orders']]; $set_data[] = 'total_order_money'; $set_data[] = $result[$arr['total_order_money']]; $redis->hmset($redis_key, ...$set_data ); } //支付 public static function getPayOrder($redis, $redis_key, $data) { $fields = ['total_pay_orders', 'total_pay_money']; if (!Order::userIsPay($data['user_id'])) { $fields[] = 'total_pay_people'; } if (isset($data['repurchase_people'])) $fields[] = 'repurchase_people'; if (isset($data['repurchase_money'])) $fields[] = 'repurchase_money'; if (isset($data['new_customers_pay_money'])) $fields[] = 'new_customers_pay_money'; if (isset($data['new_customers_pay_peoples'])) $fields[] = 'new_customers_pay_peoples'; if (isset($data['regular_customers_pay_money'])) $fields[] = 'regular_customers_pay_money'; if (isset($data['regular_customers_pay_peoples'])) $fields[] = 'regular_customers_pay_peoples'; if (isset($data['regular_customers'])) { $fields[] = 'regular_customers';//老客户 $fields[] = 'new_customers';//新客户 } $arr = array_flip($fields); $result = $redis->hmget($redis_key, ...$fields); !empty($result[$arr['total_pay_orders']]) ? $result[$arr['total_pay_orders']] += $data['pay_counts'] : $result[$arr['total_pay_orders']] = $data['pay_counts']; !empty($result[$arr['total_pay_money']]) ? $result[$arr['total_pay_money']] += $data['pay_money'] : $result[$arr['total_pay_money']] = $data['pay_money']; $set_data = [ 'total_pay_orders', $result[$arr['total_pay_orders']], 'total_pay_money', $result[$arr['total_pay_money']] ]; if(isset($arr['total_pay_people'])){ !empty($result[$arr['total_pay_people']]) ? $result[$arr['total_pay_people']] += $data['pay_people'] : $result[$arr['total_pay_people']] = $data['pay_people']; $set_data[] = 'total_pay_people'; $set_data[] = $result[$arr['total_pay_people']]; } //复购人数 if (isset($arr['repurchase_people'])) { !empty($result[$arr['repurchase_people']]) ? $result[$arr['repurchase_people']] += $data['repurchase_people'] : $result[$arr['repurchase_people']] = $data['repurchase_people']; $set_data[] = 'repurchase_people'; $set_data[] = $result[$arr['repurchase_people']]; } //复购金额 if (isset($arr['repurchase_money'])) { !empty($result[$arr['repurchase_money']]) ? $result[$arr['repurchase_money']] += $data['repurchase_money'] : $result[$arr['repurchase_money']] = $data['repurchase_money']; $set_data[] = 'repurchase_money'; $set_data[] = $result[$arr['repurchase_money']]; } //添加老客户 if (isset($arr['regular_customers'])) { !empty($result[$arr['regular_customers']]) ? $result[$arr['regular_customers']] += $data['regular_customers'] : $result[$arr['regular_customers']] = $data['regular_customers']; if (!empty($result[$arr['new_customers']])) { $result[$arr['new_customers']]--; } else { $result[$arr['new_customers']] = 0; } $set_data[] = 'regular_customers'; $set_data[] = $result[$arr['regular_customers']]; $set_data[] = 'new_customers'; $set_data[] = $result[$arr['new_customers']]; } //新客户成交金额 if (isset($arr['new_customers_pay_money'])) { !empty($result[$arr['new_customers_pay_money']]) ? $result[$arr['new_customers_pay_money']] += $data['new_customers_pay_money'] : $result[$arr['new_customers_pay_money']] = $data['new_customers_pay_money']; $set_data[] = 'new_customers_pay_money'; $set_data[] = $result[$arr['new_customers_pay_money']]; } //新客户成交人数 if (isset($arr['new_customers_pay_peoples'])) { !empty($result[$arr['new_customers_pay_peoples']]) ? $result[$arr['new_customers_pay_peoples']] += $data['new_customers_pay_peoples'] : $result[$arr['new_customers_pay_peoples']] = $data['new_customers_pay_peoples']; $set_data[] = 'new_customers_pay_peoples'; $set_data[] = $result[$arr['new_customers_pay_peoples']]; } //老客户成交金额 if (isset($arr['regular_customers_pay_money'])) { !empty($result[$arr['regular_customers_pay_money']]) ? $result[$arr['regular_customers_pay_money']] += $data['regular_customers_pay_money'] : $result[$arr['regular_customers_pay_money']] = $data['new_customers_pay_money']; $set_data[] = 'regular_customers_pay_money'; $set_data[] = $result[$arr['regular_customers_pay_money']]; } //老客户成交人数 if (isset($arr['regular_customers_pay_peoples'])) { !empty($result[$arr['regular_customers_pay_peoples']]) ? $result[$arr['regular_customers_pay_peoples']] += $data['regular_customers_pay_peoples'] : $result[$arr['regular_customers_pay_peoples']] = $data['regular_customers_pay_peoples']; $set_data[] = 'regular_customers_pay_peoples'; $set_data[] = $result[$arr['regular_customers_pay_peoples']]; } $redis->hmset($redis_key, ...$set_data); } //注册 public static function getUserRegister($redis, $redis_key, $data) { $fields = ['total_users', 'new_customers']; $arr = array_flip($fields); $result = $redis->hmget($redis_key, ...$fields); !empty($result[$arr['total_users']]) ? $result[$arr['total_users']] += $data['users'] : $result[$arr['total_users']] = $data['users']; !empty($result[$arr['new_customers']]) ? $result[$arr['new_customers']] += $data['new_customers'] : $result[$arr['new_customers']] = $data['new_customers']; $redis->hmset($redis_key, ...[ 'total_users', $result[$arr['total_users']], 'new_customers', $result[$arr['new_customers']], ] ); } public static function getViews($redis, $redis_key, $data) { $fields = ['total_views']; if (!empty($data['goods_id'])) { $fields[] = 'goods_total_visitors'; } if (!empty($data['goods_total_views'])) { $fields[] = 'goods_total_views'; } $arr = array_flip($fields); $result = $redis->hmget($redis_key, ...$fields); !empty($result[$arr['total_views']]) ? $result[$arr['total_views']] += $data['views'] : $result[$arr['total_views']] = $data['views']; $set_data = [ 'total_views', $result[$arr['total_views']], ]; if (isset($arr['goods_total_visitors'])&&!empty($data['user_id'])) { $key = RedisKeyEnum::CENSUS_MALL.'_is_goods_total_visitors:'; $is_goods_total_visitors_redis_key = RedisKeyEnum::suffix($key, '', true); if($redis->setbit($is_goods_total_visitors_redis_key,$data['user_id'],1)==0){ !empty($result[$arr['goods_total_visitors']]) ? $result[$arr['goods_total_visitors']] += $data['visitors'] : $result[$arr['goods_total_visitors']] = $data['visitors']; $set_data[] = 'goods_total_visitors'; $set_data[] = $result[$arr['goods_total_visitors']]; } } if (isset($arr['goods_total_views'])) { !empty($result[$arr['goods_total_views']]) ? $result[$arr['goods_total_views']] += $data['visitors'] : $result[$arr['goods_total_views']] = $data['visitors']; $set_data[] = 'goods_total_views'; $set_data[] = $result[$arr['goods_total_views']]; } $redis->hmset($redis_key, ...$set_data); CensusMall::setData($data); } public static function getMallData($mall_id,$fields=null) { $redis = \Yii::$app->redis; $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id; if($fields){ if(is_string($fields)){ $result = $redis->hget($redis_key,$fields); return [$fields=>$result??0]; }else{ $result = $redis->hmget($redis_key,...$fields); $data = []; foreach ($fields as $k=>$v){ $data[$v]=$result[$k]??0; } return $data; } }else{ $result = $redis->hgetall($redis_key); if($result){ foreach ($result as $k=>$v){ if($k%2==0){ $data[$k]['name'] = $v; } if($k%2==1)$data[$k-1]['num'] = $v; } return array_column($data,'num','name'); }else{ return []; } } } }