| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <?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 common\models\order\Order;
- /**
- * This is the model class for table "qimall_census_mall".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $total_users 总会员数
- * @property int $total_orders 总订单量
- * @property int $total_pay_orders 支付订单量
- * @property int $total_pay_people 支付人数
- * @property int $number_of_orders 下单人数
- * @property int $repurchase_people 复购人数
- * @property int $share_people 分享人数
- * @property int $total_visitors 总访客数
- * @property int $total_views 总浏览量
- * @property int $new_customers 新客户,未支付过
- * @property int $regular_customers 老客户,已支付过
- * @property int $goods_listings 商品上架量
- * @property int $goods_total_visitors 商品访客数
- * @property int $goods_total_views 商品浏览量
- * @property int $new_customers_pay_peoples 新客户成交人数
- * @property int $regular_customers_pay_peoples 老客户成交人数
- * @property float $total_order_money 下单金额
- * @property float $total_pay_money 支付金额
- * @property float $repurchase_money 复购金额
- * @property float $new_customers_pay_money 新客户成交金额
- * @property float $regular_customers_pay_money 老客户成交金额
- * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
- * @property int|null $total_follow_num 关注
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- * @property float $can_use_commission 可使用佣金
- * @property float $can_use_balance 可使用余额
- * @property float $can_use_score 可使用积分
- */
- class CensusMall extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%census_mall}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_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_follow_num', 'status', 'created_at', 'updated_at','add_pay_nums','add_goods'], 'integer'],
- [['total_order_money', 'total_pay_money', 'repurchase_money', 'new_customers_pay_money', 'regular_customers_pay_money',
- 'can_use_commission','can_use_balance','can_use_score'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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 [];
- }
- }
- }
- }
|