redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->initM($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->initM($redis, $mall); } } } //初始化会员数据 public function actionInitUser($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->initUser($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->initUser($redis, $mall); } } } public function initUser($redis, $mall) { $config_list = CensusMallPurchasingPower::getConfigList(); $mall_id = $mall['id']; $page_size = 1000; $new_customers = 0;//新客户数量 $regular_customers = 0;//老客户数量 $repurchase_people = 0;//复购人数 $repurchase_money = 0;//复购金额 $regular_customers_pay_money = 0;//老客户成交金额 $regular_customers_pay_peoples = 0;//老客户成交人数 $new_customers_pay_peoples = 0;//新客户成交人数 $new_customers_pay_money = 0;//新客户成交金额 $buy_power_arr = []; $mall_user_redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_USER, '', true) . ':' . $mall_id;; $i = 1; while ($user_list = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) { $st = time(); $user_ids = array_column($user_list, 'id'); $order_list = Order::lists([ 'where' => [ ['user_id' => $user_ids], ['store_id'=>0], ['mch_id'=>0], ], 'select' => 'user_id,sum(pay_money) as pay_money,count(*) counts,pay_status', 'group' => 'user_id,pay_status', ]); $user_arr = []; $have_pay_user_ids = []; foreach ($order_list as $val) { if (isset($user_arr[$val['user_id']])) { $user_arr[$val['user_id']]['add_orders'] += $val['counts']; $user_arr[$val['user_id']]['add_order_money'] += $val['pay_money']; } else { $user_arr[$val['user_id']]['add_orders'] = $val['counts']; $user_arr[$val['user_id']]['add_order_money'] = $val['pay_money']; } if ($val['pay_status'] == 1) { $have_pay_user_ids[] = $val['user_id']; $user_arr[$val['user_id']]['total_pay_money'] = $val['pay_money']; $user_arr[$val['user_id']]['add_pay_orders'] = $val['counts']; $redis->hset($mall_user_redis_key, 'total_pay_money:' . $val['user_id'], $val['pay_money']); if ($val['counts'] > 1) { $repurchase_money += $val['pay_money']; $regular_customers_pay_money += $val['pay_money']; $regular_customers++; $repurchase_people++; $regular_customers_pay_peoples++; } elseif ($val['counts'] == 1) { $new_customers_pay_money += $val['pay_money']; $regular_customers++; $new_customers_pay_peoples++; } $add_key = ''; foreach ($config_list as $key => $config) { if ($val['pay_money'] >= $config['min'] && $val['pay_money'] < $config['max']) { $add_key = $key; break; } } if ($add_key) { if (isset($buy_power_arr[$add_key])) { $buy_power_arr[$add_key]++; } else { $buy_power_arr[$add_key] = 1; } } } } $diff_user_ids = array_diff($user_ids, $have_pay_user_ids); $no_pay_counts = count($diff_user_ids); $new_customers += $no_pay_counts; if (isset($buy_power_arr[1])) { $buy_power_arr[1] += $no_pay_counts; } else { $buy_power_arr[1] = $no_pay_counts; } $sale_order_list = Order::lists([ 'where' => [ ['user_id' => $user_ids], ['store_id'=>0], ['mch_id'=>0], ['<>', 'finish_time', 0] ], 'select' => 'user_id,sum(pay_money) as pay_money,count(*) counts', 'group' => 'user_id', 'index' => 'user_id' ]); $pay_order_nums_list = OrderDetail::lists([ 'where' => [ ['user_id' => $user_ids], ['in', 'order_status', [1, 2, 3, 4, -5]] ], 'select' => 'user_id,sum(num) as num', 'group' => 'user_id', 'index' => 'user_id' ]); foreach ($user_list as $item) { $obj = CensusMallUser::findOne(['mall_id' => $item['mall_id'], 'user_id' => $item['id']]); if (empty($obj)) { $obj = new CensusMallUser(); $obj->loadDefaultValues(); $obj->mall_id = $item['mall_id']; $obj->user_id = $item['id']; } $obj->total_pay_money = $user_arr[$item['id']]['total_pay_money'] ?? 0; $obj->add_pay_orders = $user_arr[$item['id']]['add_pay_orders'] ?? 0; $obj->add_orders = $user_arr[$item['id']]['add_orders'] ?? 0; $obj->add_order_money = $user_arr[$item['id']]['add_order_money'] ?? 0; $obj->add_pay_nums = $pay_order_nums_list[$item['id']]['num'] ?? 0; $obj->add_sale_orders = $sale_order_list[$item['id']]['counts'] ?? 0; $obj->add_sale_money = $sale_order_list[$item['id']]['pay_money'] ?? 0; $obj->save(); } $t = time() - $st; $page = ($i - 1) * $page_size; var_dump($page . '耗时:' . $t); $i++; } $this->setBuyPower($redis, $mall_id, $buy_power_arr); $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]); if (empty($census_mall_obj)) { $census_mall_obj = new CensusMall(); $census_mall_obj->loadDefaultValues(); $census_mall_obj->mall_id = $mall['id']; } $census_mall_obj->new_customers = $new_customers ?? 0; $census_mall_obj->regular_customers = $regular_customers ?? 0; $census_mall_obj->repurchase_people = $repurchase_people ?? 0; $census_mall_obj->repurchase_money = $repurchase_money ?? 0; $census_mall_obj->new_customers_pay_money = $new_customers_pay_money ?? 0; $census_mall_obj->new_customers_pay_peoples = $new_customers_pay_peoples ?? 0; $census_mall_obj->regular_customers_pay_money = $regular_customers_pay_money ?? 0; $census_mall_obj->regular_customers_pay_peoples = $regular_customers_pay_peoples ?? 0; $census_mall_obj->save(); $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id; $set_data[] = 'new_customers'; $set_data[] = $new_customers; $set_data[] = 'regular_customers'; $set_data[] = $regular_customers; $set_data[] = 'repurchase_people'; $set_data[] = $repurchase_people; $set_data[] = 'repurchase_money'; $set_data[] = $repurchase_money; $set_data[] = 'new_customers_pay_money'; $set_data[] = $new_customers_pay_money; $set_data[] = 'new_customers_pay_peoples'; $set_data[] = $new_customers_pay_peoples; $set_data[] = 'regular_customers_pay_money'; $set_data[] = $regular_customers_pay_money; $set_data[] = 'regular_customers_pay_peoples'; $set_data[] = $regular_customers_pay_peoples; $redis->hmset($redis_key, ...$set_data); } public function initM($redis, $mall) { $this->initMall($redis, $mall); $this->updateCensusMallRegion($redis, $mall); $this->updateCensusMallGoods($redis, $mall); $this->updateCensusMallUserLevel($redis, $mall); $this->updateCensusMallUserSource($redis, $mall); $this->initAna($mall); $this->rfRm($mall); $date = date('Y-m-d', time()); $this->dateOrder($redis, $mall, $date); $this->dateUser($redis, $mall, $date); $this->hourOrder($redis, $mall); $this->month($redis, $mall); $this->paymentMonth($redis, $mall); $this->fixUserWallet($mall['id']); $this->uLevel($mall['id']); $this->fixCommissionDate($mall['id']); $this->capitalBalance($mall['id']); $this->capitalScore($mall['id']); $this->capitalCommission($mall['id']); $this->fixGoodsAdd($mall['id']); $this->fixOrdersAdd($mall['id']); $this->fixPayOrdersAdd($mall['id']); $this->fixPayOrdersNum($mall['id']); $this->fixAddPayPeopleNums($mall['id']); $this->fixOrderRefund($mall['id']); $date = date('Y-m-d', strtotime('-1 days')); $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time); $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time); $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time); } public function initMall($redis, $mall) { $t = time(); $mall_id = $mall['id']; $total_users = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count(); $total_orders_users = Order::find()->where(['mall_id' => $mall['id'], 'store_id'=>0,'mch_id'=>0,'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->select('user_id')->groupBy('user_id')->count(); $total_pay_people = Order::find()->where(['mall_id' => $mall['id'], 'store_id'=>0,'mch_id'=>0,'pay_status' => 1])->select('user_id')->groupBy('user_id')->count(); $order_list = Order::lists( [ 'where' => [ ['mall_id' => $mall_id], ['store_id' => 0], ['mch_id' => 0], ], 'select' => 'sum(pay_money) as pay_money,pay_status,count(*) counts', 'group' => 'pay_status' ] ); $order_arr = ['total_order_money' => 0, 'total_orders' => 0, 'total_pay_money' => 0, 'total_pay_orders' => 0]; foreach ($order_list as $val) { $order_arr['total_order_money'] += $val['pay_money']; $order_arr['total_orders'] += $val['counts']; if ($val['pay_status'] == 1) { $order_arr['total_pay_money'] += $val['pay_money']; $order_arr['total_pay_orders'] += $val['counts']; } } $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]); if (empty($census_mall_obj)) { $census_mall_obj = new CensusMall(); $census_mall_obj->loadDefaultValues(); $census_mall_obj->mall_id = $mall['id']; } $census_mall_obj->total_order_money = $order_arr['total_order_money']; $census_mall_obj->total_orders = $order_arr['total_orders']; $census_mall_obj->total_pay_money = $order_arr['total_pay_money']; $census_mall_obj->total_pay_orders = $order_arr['total_pay_orders']; $census_mall_obj->total_users = $total_users ?? 0; $census_mall_obj->number_of_orders = $total_orders_users ?? 0; $census_mall_obj->total_pay_people = $total_pay_people ?? 0; $census_mall_obj->total_visitors = $total_users; $census_mall_obj->total_views = $total_users; $census_mall_obj->save(); $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id; $set_data[] = 'total_pay_money'; $set_data[] = $order_arr['total_pay_money']; $set_data[] = 'total_orders'; $set_data[] = $order_arr['total_orders']; $set_data[] = 'total_users'; $set_data[] = $total_users; $set_data[] = 'total_pay_orders'; $set_data[] = $order_arr['total_pay_orders']; $set_data[] = 'total_pay_people'; $set_data[] = $total_pay_people; $set_data[] = 'number_of_orders'; $set_data[] = $total_orders_users; $set_data[] = 'total_order_money'; $set_data[] = $order_arr['total_order_money']; $set_data[] = 'total_visitors'; $set_data[] = $total_users; $set_data[] = 'total_views'; $set_data[] = $total_users; $redis->hmset($redis_key, ...$set_data); $date = date('Y-m-d', time()); $start_time = strtotime($date . ' 00:00:00'); $today_total_users = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->andWhere(['>=', 'created_at', $start_time]) ->count(); $today_order_list = Order::lists( [ 'where' => [ ['mall_id' => $mall_id], ['store_id' => 0], ['mch_id' => 0], ['>=', 'created_at', $start_time] ], 'select' => 'sum(pay_money) as pay_money,pay_status,count(*) counts', 'group' => 'pay_status' ] ); $today_order_arr = ['total_order_money' => 0, 'total_pay_money' => 0]; foreach ($today_order_list as $val) { $today_order_arr['total_order_money']+=$val['pay_money']; if($val['pay_status']==1) $today_order_arr['total_pay_money']+=$val['pay_money']; } $census_mall_single_day_obj = CensusMallSingleDay::findOne(['mall_id' => $mall['id'], 'date' => $date]); if (empty($census_mall_single_day_obj)) { $census_mall_single_day_obj = new CensusMallSingleDay(); $census_mall_single_day_obj->loadDefaultValues(); $census_mall_single_day_obj->mall_id = $mall['id']; $census_mall_single_day_obj->date = $date; } $census_mall_single_day_obj->total_users = $total_users ?? 0; $census_mall_single_day_obj->add_users = $today_total_users ?? 0; $census_mall_single_day_obj->total_pay_money = $today_order_arr['total_pay_money']; $census_mall_single_day_obj->total_order_money = $today_order_arr['total_order_money']; $census_mall_single_day_obj->save(); $date = date('Y-m-d', time()); $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date; $set_data = []; $set_data[] = 'total_order_money'; $set_data[] = $today_order_arr['total_order_money']; $set_data[] = 'total_pay_money'; $set_data[] = $today_order_arr['total_pay_money']; $set_data[] = 'add_users'; $set_data[] = $today_total_users; $set_data[] = 'total_users'; $set_data[] = $total_users; $redis->hmset($redis_key, ...$set_data); $redis->expire($redis_key, 24*3600); $t = time()-$t; var_dump('initMall执行耗时'.$t); } //复购,新客户,老客户 public function actionFugou($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fuGou($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fuGou($redis, $mall); } } } public function fuGou($redis, $mall) { $fu_gou_list = Order::lists(['where' => [ ['mall_id' => $mall['id']], ['pay_status' => 1], ['store_id' => 0], ['mch_id' => 0], ], 'select' => 'count(*) as count,user_id', 'group' => 'user_id' ]); $repurchase_people = 0; $repurchase_people_user_id_arr = []; $new_customers_pay_peoples = 0; $new_customers_pay_peoples_user_id_arr = []; foreach ($fu_gou_list as $item) { if ($item['count'] > 1) { $repurchase_people++; $repurchase_people_user_id_arr[] = $item['user_id']; } if ($item['count'] == 1) { $new_customers_pay_peoples++; $new_customers_pay_peoples_user_id_arr[] = $item['user_id']; } } $repurchase_money = Order::find()->where( [ 'mall_id' => $mall['id'], 'store_id'=>0, 'mch_id'=>0, 'user_id' => $repurchase_people_user_id_arr, 'pay_status' => 1 ])->sum('pay_money'); $new_customers_pay_money = Order::find()->where([ 'mall_id' => $mall['id'], 'store_id'=>0, 'mch_id'=>0, 'user_id' => $new_customers_pay_peoples_user_id_arr, 'pay_status' => 1 ])->sum('pay_money'); $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]); if (empty($census_mall_obj)) { $census_mall_obj = new CensusMall(); $census_mall_obj->loadDefaultValues(); $census_mall_obj->mall_id = $mall['id']; } $census_mall_obj->repurchase_people = $repurchase_people ?? 0; $census_mall_obj->regular_customers_pay_peoples = $repurchase_people ?? 0; $census_mall_obj->repurchase_money = $repurchase_money ?? 0; $census_mall_obj->regular_customers_pay_money = $repurchase_money ?? 0; $census_mall_obj->new_customers_pay_peoples = $new_customers_pay_peoples ?? 0; $census_mall_obj->new_customers_pay_money = $new_customers_pay_money ?? 0; $regular_customers = Order::find()->where([ 'mall_id' => $mall['id'], 'store_id'=>0, 'mch_id'=>0, 'pay_status' => 1])->select('user_id')->groupBy('user_id')->count(); $new_customers = $census_mall_obj->total_users - $regular_customers; if ($new_customers < 0) $new_customers = 0; $census_mall_obj->regular_customers = $regular_customers ?? 0; $census_mall_obj->new_customers = $new_customers ?? 0; $census_mall_obj->save(); $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id']; $set_data[] = 'repurchase_people'; $set_data[] = $repurchase_people; $set_data[] = 'regular_customers_pay_peoples'; $set_data[] = $repurchase_people; $set_data[] = 'repurchase_money'; $set_data[] = $repurchase_money; $set_data[] = 'regular_customers_pay_money'; $set_data[] = $repurchase_money; $set_data[] = 'new_customers_pay_peoples'; $set_data[] = $new_customers_pay_peoples; $set_data[] = 'new_customers_pay_money'; $set_data[] = $new_customers_pay_money; $set_data[] = 'regular_customers';//老客户 $set_data[] = $regular_customers;//老客户 $set_data[] = 'new_customers';//新客户 $set_data[] = $new_customers;//新客户 $redis->hmset($redis_key, ...$set_data); var_dump('fuGou执行结束'); } //地区 public function actionRegion($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->updateCensusMallRegion($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->updateCensusMallRegion($redis, $mall); } } } public function updateCensusMallRegion($redis, $mall) { $t = time(); $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_REGION, '', true) . ':' . $mall['id']; $region_order_list = Order::lists(['where' => [ ['mall_id' => $mall['id']], ['store_id'=>0], ['mch_id'=>0], ], 'select' => 'count(*) as count,province', 'group' => 'province' ] ); foreach ($region_order_list as $item) { if ($item['province']) { $obj = CensusMallRegion::findOne(['mall_id' => $mall['id'], 'province_id' => $item['province']]); if (empty($obj)) { $obj = new CensusMallRegion(); $obj->loadDefaultValues(); $obj->mall_id = $mall['id']; $obj->province_id = $item['province']; } $obj->total_orders = $item['count'] ?? 0; $obj->save(); $field = 'total_orders_province_id_' . $item['province']; $redis->hset($redis_key, $field, $obj->total_orders); } } $region_pay_pay_money_list = Order::lists(['where' => [ ['mall_id' => $mall['id']], ['pay_status' => 1], ['store_id'=>0], ['mch_id'=>0], ], 'select' => 'sum(pay_money) as pay_money,province', 'group' => 'province' ] ); foreach ($region_pay_pay_money_list as $item) { if ($item['province']) { $obj = CensusMallRegion::findOne(['mall_id' => $mall['id'], 'province_id' => $item['province']]); if (empty($obj)) { $obj = new CensusMallRegion(); $obj->loadDefaultValues(); $obj->mall_id = $mall['id']; $obj->province_id = $item['province']; } $obj->total_pay_money = $item['pay_money'] ?? 0; $obj->save(); $field = 'total_pay_money_province_id_' . $obj->province_id; $redis->hset($redis_key, $field, $obj->total_pay_money); } } $t = time()-$t; var_dump('updateCensusMallRegion耗时'.$t); } //商品 public function actionGoods($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->updateCensusMallGoods($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->updateCensusMallGoods($redis, $mall); } } } public function updateCensusMallGoods($redis, $mall) { $mall_id = $mall['id']; $t = time(); $i = 1; $page_size = 1000; $goods_listings = 0; $add_goods = 0; $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, '', true) . ':' . $mall['id']; $pay_money_redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_PAY_MONEY, '', true) . ':' . $mall['id']; $redis->del($redis_key); $redis->del($pay_money_redis_key); while ($goods_list = Goods::find()->where(['mall_id' => $mall_id,'mch_id'=>0, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->orderBy('id asc')->offset(($i - 1) * $page_size)->select('is_on_sale,sales_num,id')->limit($page_size)->all()) { $goods_ids = []; foreach ($goods_list as $goods) { $goods_ids[]=$goods['id']; $add_goods++; if (!empty($goods['is_on_sale'])) $goods_listings++; } //1待发货 2 已发货 3已收货 4已结束 $goods_price_arr = OrderDetail::lists([ 'where'=>[ ['goods_id' => $goods_ids], ['order_status' => [1, 2, 3, 4, -5]] ], 'select'=>'sum(goods_price) as goods_price,goods_id', 'group'=>'goods_id', 'index'=>'goods_id' ]); foreach ($goods_list as $goods) { $obj = CensusMallGoods::findOne(['mall_id' => $mall['id'], 'goods_id' => $goods['id']]); if (empty($obj)) { $obj = new CensusMallGoods(); $obj->loadDefaultValues(); $obj->mall_id = $mall['id']; $obj->goods_id = $goods['id']; } $value = 'goods_id:' . $goods['id']; if ($goods['sales_num'] > 0) $redis->zadd($redis_key, $goods['sales_num'], $value); $total_pay_money = $goods_price_arr[$goods['id']]['goods_price']??0; if ($total_pay_money > 0) $redis->zadd($pay_money_redis_key, $total_pay_money, $value); $obj->total_pay_money = $total_pay_money; $obj->total_sales_volume = $goods['sales_num']; $obj->save(); } $i++; } $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]); if (empty($census_mall_obj)) { $census_mall_obj = new CensusMall(); $census_mall_obj->loadDefaultValues(); $census_mall_obj->mall_id = $mall['id']; } $census_mall_obj->goods_listings = $goods_listings ?? 0; $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id']; $set_data[] = 'goods_listings'; $set_data[] = $goods_listings; $set_data[] = 'add_goods'; $set_data[] = $add_goods; $redis->hmset($redis_key, ...$set_data); $t = time()-$t; var_dump("商城{$mall_id}updateCensusMallGoods执行耗时".$t); } //会员等级分布 public function actionUserLevel($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->updateCensusMallUserLevel($redis, $mall); $this->updateCensusMallUserSource($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->updateCensusMallUserLevel($redis, $mall); $this->updateCensusMallUserSource($redis, $mall); } } } public function updateCensusMallUserLevel($redis, $mall) { $t = time(); $user_level = UserLevel::lists(['where' => [['mall_id' => $mall['id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]], 'select' => 'level']); $user_level_arr = array_column($user_level, 'level'); $user_level_arr[] = 0; $user_list = User::lists([ 'where' => [ ['mall_id' => $mall['id']], ['level' => $user_level_arr], ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]] ], 'select' => 'count(*) as count,level', 'group' => 'level' ]); $key = RedisKeyEnum::CENSUS_MALL_USER_LEVEL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id']; foreach ($user_list as $item) { $redis->hset($redis_key, 'level_' . $item['level'], $item['count']); $obj = CensusMallUserLevel::findOne(['mall_id' => $mall['id'], 'level' => $item['level']]); if (empty($obj)) { $obj = new CensusMallUserLevel(); $obj->loadDefaultValues(); $obj->mall_id = $mall['id']; $obj->level = $item['level']; } $obj->numbers = $item['count']; $obj->save(); } $t= time()-$t; var_dump("商城".$mall['id']."updateCensusMallUserLevel执行耗时".$t); } public function updateCensusMallUserSource($redis, $mall) { $t = time(); $user_list = User::lists([ 'where' => [ ['mall_id' => $mall['id']], ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]] ], 'select' => 'count(*) as count,platform', 'group' => 'platform' ]); $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, '', true) . ':' . $mall['id']; foreach ($user_list as $item) { if (empty($item['platform'])) $item['platform'] = 'manual'; $redis->hset($redis_key, $item['platform'], $item['count']); $obj = CensusMallUserSource::findOne(['mall_id' => $mall['id'], 'source' => $item['platform']]); if (empty($obj)) { $obj = new CensusMallUserSource(); $obj->loadDefaultValues(); $obj->mall_id = $mall['id']; $obj->source = $item['platform']; } $obj->numbers = $item['count']; $obj->save(); } $t = time()-$t; var_dump('商城'.$mall['id'].'updateCensusMallUserSource执行'.$t); } //当天交易情况 public function actionToday($mall_id = null, $date = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->dateOrder($redis, $mall, $date); $this->dateUser($redis, $mall, $date); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->dateOrder($redis, $mall, $date); $this->dateUser($redis, $mall, $date); } } } public function dateOrder($redis, $mall, $date) { $mall_id = $mall['id']; $i = 1; $page_size = 1000; $date_order_money = []; $date_pay_money = []; if (empty($date)) $date = date('Y-m-d', time()); $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); while ($order_list = Order::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]) ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) { foreach ($order_list as $item) { $created_at = $item['created_at']; $date = date('Y-m-d', $created_at); if (isset($date_arr[$date])) { $date_order_money[$date] += $item['pay_money']; } else { $date_order_money[$date] = $item['pay_money']; } if ($item['pay_status']) { $pay_time = $item['pay_time']; $date_pay = date('Y-m-d', $pay_time); if (isset($date_pay_money[$date_pay])) { $date_pay_money[$date_pay] += $item['pay_money']; } else { $date_pay_money[$date_pay] = $item['pay_money']; } } } $i++; } $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE; foreach ($date_order_money as $date => $money) { $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date; $redis->hset($redis_key, 'total_order_money', $money); $redis->expire($redis_key, 24 * 3600); $data = [ 'mall_id' => $mall_id, 'total_order_money' => $money, 'date' => $date, ]; CensusMallSingleDay::setData($data); } foreach ($date_pay_money as $date => $money) { $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date; $redis->hset($redis_key, 'total_order_money', $money); $redis->expire($redis_key, 24 * 3600); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $date; } $obj->total_pay_money = $money; $obj->save(); } var_dump('dateOrder执行结束'); } public function dateUserOld($redis, $mall, $date) { $date_arr = []; $i = 1; $page_size = 1000; $mall_id = $mall['id']; if (empty($date)) $date = date('Y-m-d', time()); $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); while ($user_list = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]) ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) { foreach ($user_list as $item) { $created_at = $item['created_at']; $date = date('Y-m-d', $created_at); if (isset($date_arr[$date])) { $date_arr[$date]++; } else { $date_arr[$date] = 1; } } $i++; } if ($date_arr) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE; $total_nums = 0; foreach ($date_arr as $date => $nums) { $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date; $total_nums += $nums; $redis->hset($redis_key, 'total_users', $total_nums); $redis->hset($redis_key, 'add_users', $nums); $redis->expire($redis_key, 24 * 3600); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $date; } $obj->add_users = $nums; $obj->total_users = $total_nums; $obj->save(); } } } //单日会员数据 public function dateUser($redis, $mall, $date) { $t = time(); $mall_id = $mall['id']; $query = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (!empty($date)) { $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); $query->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]); } $list = $query->select(new Expression("mall_id,count(*) as counts,FROM_UNIXTIME(created_at,'%Y-%m-%d') as date")) ->groupBy('date')->asArray()->all(); if ($list) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE; $total_nums = 0; foreach ($list as $item) { $nums = $item['counts']; $date = $item['date']; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date; $total_nums += $nums; $redis->hset($redis_key, 'total_users', $total_nums); $redis->hset($redis_key, 'add_users', $nums); $redis->expire($redis_key, 24 * 3600); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $date; } $obj->add_users = $nums; $obj->total_users = $total_nums; $res = $obj->save(); var_dump($res); } } $t = time()-$t; var_dump('dateUser执行结束'.$t); } //当天每小时的交易情况 public function actionHour($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->hourOrder($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->hourOrder($redis, $mall); } } } public function hourOrder($redis, $mall) { $t = time(); $mall_id = $mall['id']; $i = 1; $page_size = 1000; $hour_pay_money = []; $date = date('Y-m-d', time()); $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); while ($order_list = Order::find()->where(['mall_id' => $mall_id,'store_id'=>0,'mch_id'=>0, 'pay_status' => 1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]) ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) { foreach ($order_list as $item) { if ($item['pay_status']) { $pay_time = $item['pay_time']; $hour = date('H', $pay_time); if (isset($hour_pay_money[$hour])) { $hour_pay_money[$hour] += $item['pay_money']; } else { $hour_pay_money[$hour] = $item['pay_money']; } } } $i++; } $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR . ':' . $mall_id . ':' . $date; foreach ($hour_pay_money as $hour => $money) { $redis_key = RedisKeyEnum::suffix($key, '', true); $field = 'hour_' . $hour; $redis->hset($redis_key, $field, $money); $redis->expire($redis_key, 24 * 3600); $obj = CensusMallSingleHour::findOne(['mall_id' => $mall_id, 'hour' => $hour, 'date' => $date]); if (empty($obj)) { $obj = new CensusMallSingleHour(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->hour = $hour; $obj->date = $date; } $obj->total_pay_money = $money; $obj->save(); } $t = time()-$t; var_dump('hourOrder执行结束'.$t); } public function setBuyPower($redis, $mall_id, $buy_power_arr) { $set_data = []; foreach ($buy_power_arr as $add_key => $nums) { $redis_add_key = RedisKeyEnum::CENSUS_MALL_BUY_POWER . 'add_' . $add_key; $redis_add_key = RedisKeyEnum::suffix($redis_add_key, '', true); $redis->setbit($redis_add_key, $add_key, 1); $obj = CensusMallPurchasingPower::findOne(['mall_id' => $mall_id, 'purchasing_power' => $add_key]); if (empty($obj)) { $obj = new CensusMallPurchasingPower(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->purchasing_power = $add_key; } $obj->numbers = $nums; $obj->save(); $field = 'buy_power:' . $add_key; $set_data[] = $field; $set_data[] = $nums; } $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_BUY_POWER, '', true) . ':' . $mall_id; if ($set_data) { $redis->hmset($redis_key, ...$set_data); } } public function userVisitors($redis, $mall) { $counts = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count(); $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]); $census_mall_obj->total_visitors = $counts; $census_mall_obj->save(); $set_data[] = 'total_visitors'; $set_data[] = $counts; $set_data[] = 'total_views'; $set_data[] = $counts; $key = RedisKeyEnum::CENSUS_MALL; $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id']; $redis->hmset($redis_key, ...$set_data); var_dump('userVisitors执行结束'); } public function actionMonth($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->month($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->month($redis, $mall); } } } public function month($redis, $mall) { $t = time(); $mall_id = $mall['id']; //下单金额 $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money FROM qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 GROUP BY month; "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month']; $redis_key = RedisKeyEnum::suffix($key, '', true); $set_data = []; $set_data[] = 'total_order_money'; $set_data[] = $item['total_pay_money']; $redis->hmset($redis_key, ...$set_data); $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]); if (empty($obj)) { $obj = new CensusMallSingleMonth(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->month = $item['month']; } $obj->total_order_money = $item['total_pay_money']; $res = $obj->save(); } //支付金额 $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money FROM qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and pay_status=1 GROUP BY month; "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month']; $redis_key = RedisKeyEnum::suffix($key, '', true); $set_data = []; $set_data[] = 'total_pay_money'; $set_data[] = $item['total_pay_money']; $redis->hmset($redis_key, ...$set_data); $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]); if (empty($obj)) { $obj = new CensusMallSingleMonth(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->month = $item['month']; } $obj->total_pay_money = $item['total_pay_money']; $res = $obj->save(); } //抵扣金额 $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money ,sum((original_goods_price+shipping_money)) as original_goods_price FROM qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 GROUP BY month "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month']; $redis_key = RedisKeyEnum::suffix($key, '', true); $total_deduction_money = bcsub($item['original_goods_price'], $item['total_pay_money'], 2); if ($total_deduction_money < 0) $total_deduction_money = 0; $set_data = []; $set_data[] = 'total_deduction_money'; $set_data[] = $total_deduction_money; $redis->hmset($redis_key, ...$set_data); $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]); if (empty($obj)) { $obj = new CensusMallSingleMonth(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->month = $item['month']; } $obj->total_deduction_money = $total_deduction_money; $res = $obj->save(); } //退款金额 $sql = " SELECT FROM_UNIXTIME(refund_at,'%Y-%m') as month ,sum(reality_refund_price) as total_refund_money FROM qimall_order_refund where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and is_refund = 1 GROUP BY month "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month']; $redis_key = RedisKeyEnum::suffix($key, '', true); $set_data = []; $set_data[] = 'total_refund_money'; $set_data[] = $item['total_refund_money']; $redis->hmset($redis_key, ...$set_data); $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]); if (empty($obj)) { $obj = new CensusMallSingleMonth(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->month = $item['month']; } $obj->total_refund_money = $item['total_refund_money']; $res = $obj->save(); } //充值金额 $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(change_num) as total_recharge_money FROM qimall_balance_log where mall_id = {$mall_id} and change_type = 'add' and from_type = 'recharge' and status = 1 GROUP BY month "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month']; $redis_key = RedisKeyEnum::suffix($key, '', true); $set_data = []; $set_data[] = 'total_recharge_money'; $set_data[] = $item['total_recharge_money']; $redis->hmset($redis_key, ...$set_data); $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]); if (empty($obj)) { $obj = new CensusMallSingleMonth(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->month = $item['month']; } $obj->total_recharge_money = $item['total_recharge_money']; $res = $obj->save(); } $t = time()-$t; var_dump('month执行结束'.$t); } public function actionPaymentMonth($mall_id = null) { $redis = \Yii::$app->redis; if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->paymentMonth($redis, $mall); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->paymentMonth($redis, $mall); } } } public function paymentMonth($redis, $mall) { $t = time(); $mall_id = $mall['id']; $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money,payment_type FROM qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and pay_status=1 and payment_type>0 GROUP BY month,payment_type "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $key = RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month']; $redis_key = RedisKeyEnum::suffix($key, '', true); $set_data = []; $set_data[] = 'pay_type:' . $item['payment_type']; $set_data[] = $item['total_pay_money']; $redis->hmset($redis_key, ...$set_data); $obj = CensusMallPaymentSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month'], 'pay_type' => $item['payment_type']]); if (empty($obj)) { $obj = new CensusMallPaymentSingleMonth(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->month = $item['month']; $obj->pay_type = $item['payment_type']; } $obj->total_pay_money = $item['total_pay_money']; $res = $obj->save(); } $t = time()-$t; var_dump('paymentMonth执行结束'.$t); } public function actionFixUserWallet($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fixUserWallet($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fixUserWallet($mall['id']); } } } public function fixUserWallet($mall_id) { $t = time(); $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->select('sum(score) as can_use_score,sum(balance) as can_use_balance,sum(commission) as can_use_commission') ->asArray() ->one(); $obj = CensusMall::findOne(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($obj)) { $obj = new CensusMall(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->status = StatusEnum::ENABLED; } $obj->can_use_score = $result['can_use_score'] ?? 0; $obj->can_use_balance = $result['can_use_balance'] ?? 0; $obj->can_use_commission = $result['can_use_commission'] ?? 0; $res = $obj->save(); $t = time()-$t; var_dump('fixUserWallet执行结束'.$t); } //会员等级情况 public function actionULevel($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->uLevel($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->uLevel($mall['id']); } } } public function uLevel($mall_id) { $t = time(); $list = User::lists( [ 'where' => [ ['mall_id' => $mall_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]] ], 'select' => 'count(*) as counts,level', 'group' => 'level' ] ); foreach ($list as $item) { $date = date('Y-m-d', $t); $model = CensusMallUserLevelSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date, 'level' => $item['level']]); if (empty($model)) { $model = new CensusMallUserLevelSingleDay(); $model->loadDefaultValues(); $model->mall_id = $mall_id; $model->level = $item['level']; $model->date = $date; } $model->numbers = $item['counts']; $res = $model->save(); } $t = time()-$t; var_dump('uLevel执行结束'); } //商城资金统计 public function actionCapital($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->capitalBalance($mall['id']); $this->capitalScore($mall['id']); $this->capitalCommission($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->capitalBalance($mall['id']); $this->capitalScore($mall['id']); $this->capitalCommission($mall['id']); } } } public function capitalBalance($mall_id) { $type = 'balance'; $total_nums = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->sum('total_balance'); CapitalLog::$capitalType = $type; $use_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'change_type' => 'sub', 'status' => StatusEnum::ENABLED])->sum('change_num'); $surplus_nums = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->sum('balance'); CapitalLog::$capitalType = $type; $withdrawal_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'change_type' => 'sub', 'from_type' => 'withdrawal', 'status' => StatusEnum::ENABLED])->sum('change_num'); $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]); if (empty($model)) { $model = new CensusMallCapital(); $model->mall_id = $mall_id; $model->type = $type; } $model->total_nums = $total_nums ?? 0; $model->use_nums = $use_nums ?? 0; $model->surplus_nums = $surplus_nums ?? 0; $model->withdrawal_nums = $withdrawal_nums ?? 0; $res = $model->save(); var_dump('capitalBalance执行结束'); } public function capitalScore($mall_id) { $type = 'score'; $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->select('sum(total_score) as total_score,sum(total_use_score) as total_use_score,sum(score) as score') ->asArray() ->one(); $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]); if (empty($model)) { $model = new CensusMallCapital(); $model->mall_id = $mall_id; $model->type = $type; } $model->total_nums = $result['total_score'] ?? 0; $model->use_nums = $result['total_use_score'] ?? 0; $model->surplus_nums = $result['score'] ?? 0; $res = $model->save(); var_dump('capitalScore执行结束'); } public function capitalCommission($mall_id) { $type = 'commission'; $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]) ->select('sum(total_commission) as total_commission,sum(frozen_commission) as frozen_commission,sum(commission) as commission') ->asArray() ->one(); CapitalLog::$capitalType = $type . '_frozen'; $cancel_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::DELETE])->sum('change_num'); $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]); if (empty($model)) { $model = new CensusMallCapital(); $model->mall_id = $mall_id; $model->type = $type; } $model->total_nums = $result['total_commission'] ?? 0; $model->unsettled = $result['frozen_commission'] ?? 0; $model->surplus_nums = $result['commission'] ?? 0; $model->cancel_nums = $cancel_nums ?? 0; $res = $model->save(); var_dump('capitalCommission执行结束'); } //商城单日数据 public function actionMallSingleData($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fixGoodsAdd($mall['id']); $this->fixOrdersAdd($mall['id']); $this->fixPayOrdersAdd($mall['id']); $this->fixPayOrdersNum($mall['id']); $this->fixAddPayPeopleNums($mall['id']); $this->fixOrderRefund($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fixGoodsAdd($mall['id']); $this->fixOrdersAdd($mall['id']); $this->fixPayOrdersAdd($mall['id']); $this->fixPayOrdersNum($mall['id']); $this->fixAddPayPeopleNums($mall['id']); $this->fixOrderRefund($mall['id']); } } } public function actionFixGoods($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fixGoodsAdd($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fixGoodsAdd($mall['id']); } } } public function fixGoodsAdd($mall_id, $time = null) { $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM qimall_goods where mall_id = {$mall_id} and mch_id = 0 and status in (0,1) GROUP BY date "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); $time = strtotime($item['date'] . ' 00:00:02'); if (empty($obj)) { $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, $item['counts'], 0, 0, 0, 0, 0, 0, 0 ]; $values_arr[] = $info; } else { $res = CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_goods' => $item['counts']], ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } $counts = Goods::find()->where(['mall_id' => $mall_id, 'mch_id' => 0, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count(); $model = CensusMall::findOne(['mall_id' => $mall_id]); if (empty($model)) { $model = new CensusMall(); $model->loadDefaultValues(); $model->mall_id = $mall_id; } $model->add_goods = $counts; $res = $model->save(); var_dump('fixGoodsAdd执行结束'); } public function actionFixOrders($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fixGoodsAdd($mall['id']); $this->fixOrdersAdd($mall['id']); $this->fixPayOrdersAdd($mall['id']); $this->fixPayOrdersNum($mall['id']); $this->fixAddPayPeopleNums($mall['id']); $this->fixOrderRefund($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fixGoodsAdd($mall['id']); $this->fixOrdersAdd($mall['id']); $this->fixPayOrdersAdd($mall['id']); $this->fixPayOrdersNum($mall['id']); $this->fixAddPayPeopleNums($mall['id']); $this->fixOrderRefund($mall['id']); } } } public function fixOrdersAdd($mall_id) { $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 GROUP BY date "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $time = strtotime($item['date'] . ' 00:00:02'); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); if (empty($obj)) { $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, $item['counts'], 0, 0, 0, 0, 0, 0 ]; $values_arr[] = $info; } else { $time = strtotime($obj['date'] . ' 00:00:02'); CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_orders' => $item['counts']], ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } } public function fixPayOrdersAdd($mall_id) { $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,sum(pay_money) as pay_money, mall_id FROM qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status=1 GROUP BY date "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $time = strtotime($item['date'] . ' 00:00:02'); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $item['date']; $info = [ $mall_id, 0, 0, 0, 0, 0, 0, $item['pay_money'], $item['date'], 1, $time, $time, 0, 0, $item['counts'], 0, 0, 0, 0, 0 ]; $values_arr[] = $info; } else { $time = strtotime($obj['date'] . ' 00:00:02'); CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'total_pay_money' => $item['pay_money'], 'add_pay_orders' => $item['counts']], ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } var_dump('fixPayOrdersAdd执行结束'); } public function fixAddPayPeopleNums($mall_id) { $sql = " SELECT t.date,t.mall_id,count(t.user_id) as counts FROM ( SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,mall_id,user_id FROM qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status=1 GROUP BY date,user_id) t GROUP BY t.date "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $time = strtotime($item['date'] . ' 00:00:02'); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $item['date']; $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, $item['counts'], 0, 0, 0, 0 ]; $values_arr[] = $info; } else { $time = strtotime($obj['date'] . ' 00:00:02'); CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_pay_people_nums' => $item['counts']], ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } var_dump('fixAddPayPeopleNums执行结束'); } public function fixPayOrdersNum($mall_id) { $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,sum(num) as counts,mall_id FROM qimall_order_detail where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and order_status !=0 GROUP BY date "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $time = strtotime($item['date'] . ' 00:00:02'); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $item['date']; $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, $item['counts'], 0, 0, 0 ]; $values_arr[] = $info; } else { $time = strtotime($obj['date'] . ' 00:00:02'); CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_pay_nums' => $item['counts']], ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } var_dump('fixPayOrdersNum执行结束'); } public function actionFixOrderRefund($mall_id = null) { if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fixOrderRefund($mall['id']); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fixOrderRefund($mall['id']); } } } public function fixOrderRefund($mall_id) { $sql = " SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM qimall_order_refund where mall_id = $mall_id and mch_id = 0 and store_id=0 GROUP BY date "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); foreach ($list as $item) { $time = strtotime($item['date'] . ' 00:00:02'); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $item['date']; $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, $item['counts'], 0, 0 ]; $values_arr[] = $info; } else { $time = strtotime($obj['date'] . ' 00:00:02'); CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_refund_orders' => $item['counts']], ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } var_dump('fixOrderRefund执行结束'); } public function fixCommissionDate($mall_id) { $query = CommissionLog::find()->where(['mall_id' => $mall_id]); if (!empty($date)) { $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); $query->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]); } $list = $query->select(new Expression("mall_id, sum(change_num) as change_num,FROM_UNIXTIME(created_at,'%Y-%m-%d') as date,change_type")) ->groupBy('date,change_type')->asArray()->all(); foreach ($list as $item) { $time = strtotime($item['date'] . ' 00:00:02'); $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]); if (empty($obj)) { $obj = new CensusMallSingleDay(); $obj->loadDefaultValues(); $obj->mall_id = $mall_id; $obj->date = $item['date']; if ($item['change_type'] == 'add') { $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, 0, $item['change_num'], 0 ]; } else { $info = [ $mall_id, 0, 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, 0, 0, $item['change_num'] ]; } $values_arr[] = $info; } else { $time = strtotime($obj['date'] . ' 00:00:02'); $field = ['created_at' => $time, 'updated_at' => $time]; if ($item['change_type'] == 'add') { $field['settlement_commission'] = $item['change_num']; } else { $field['expenditure_commission'] = $item['change_num']; } CensusMallSingleDay::updateAll($field, ['id' => $obj['id']]); } } if (!empty($values_arr)) { \Yii::$app->db->createCommand()->batchInsert( CensusMallSingleDay::tableName(), $this->single_day_fields, $values_arr )->execute(); } var_dump($mall_id . 'fixCommissionDate执行结束'); } public function actionMallSingleHourData($mall_id = null) { // $date = date('Y-m-d', strtotime('-1 days')); $dates = []; for ($i=0;$i<=7;$i++){ $dates[] = date('Y-m-d',strtotime("-$i day")); } foreach ($dates as $date){ $start_time = strtotime($date . ' 00:00:00'); $end_time = strtotime($date . ' 23:59:59'); if (!empty($mall_id)) { $mall = Mall::findOne(['id' => $mall_id]); if ($mall) { $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time); $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time); $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time); } } else { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time); $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time); $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time); } } } } public function fixPayOrdersNumByHour($mall_id, $date, $start_time, $end_time) { $sql = " SELECT FROM_UNIXTIME(od.created_at,'%H') as hour, sum(od.num) as counts,od.mall_id FROM qimall_order_detail as od LEFT JOIN qimall_order as o on od.order_id = o.id where od.mall_id = {$mall_id} and od.mch_id = 0 and od.store_id=0 and o.pay_status =1 and od.created_at between {$start_time} and {$end_time} GROUP BY hour ; "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $rows = []; foreach ($list as $item) { $rows[$item['hour']] = $item; } for ($i = 0; $i < 24; $i++) { $temp = $i; if ($i <= 9) $temp = '0' . $i; $params = [ 'mall_id' => $mall_id, 'date' => $date, 'hour' => (string)$temp, 'add_pay_nums' => 0, ]; if (isset($rows[$temp])) { $params['add_pay_nums'] = $rows[$temp]['counts']; } $res = CensusMallSingleHour::addData($params); } var_dump('fixPayOrdersNumByHour执行结束'); } public function fixAddPayOrdersByHour($mall_id, $date, $start_time, $end_time) { $sql = " SELECT FROM_UNIXTIME(created_at,'%H') as hour, count(*) as counts,sum(pay_money) as total_pay_money,mall_id FROM qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1 and created_at between {$start_time} and {$end_time} GROUP BY hour "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $rows = []; foreach ($list as $item) { $rows[$item['hour']] = $item; } for ($i = 0; $i < 24; $i++) { $temp = $i; if ($i <= 9) $temp = '0' . $i; $params = [ 'mall_id' => $mall_id, 'date' => $date, 'hour' => (string)$temp, 'add_pay_orders' => 0, 'total_pay_money' => 0, ]; if (isset($rows[$temp])) { $params['add_pay_orders'] = $rows[$temp]['counts']; $params['total_pay_money'] = $rows[$temp]['total_pay_money']; } $res = CensusMallSingleHour::addData($params); } var_dump('fixAddPayOrdersByHour执行结束'); } public function fixAddPayPeopleNumsByHour($mall_id, $date, $start_time, $end_time) { $sql = " SELECT t.`hour`,t.mall_id,count(t.user_id) as counts FROM ( SELECT FROM_UNIXTIME(created_at,'%H') as hour, user_id,mall_id FROM qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1 and created_at between {$start_time} and {$end_time} GROUP BY hour,user_id )t GROUP BY t.`hour` "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $rows = []; foreach ($list as $item) { $rows[$item['hour']] = $item; } for ($i = 0; $i < 24; $i++) { $temp = $i; if ($i <= 9) $temp = '0' . $i; $params = [ 'mall_id' => $mall_id, 'date' => $date, 'hour' => (string)$temp, 'add_pay_people_nums' => 0, ]; if (isset($rows[$temp])) { $params['add_pay_people_nums'] = $rows[$temp]['counts']; } $res = CensusMallSingleHour::addData($params); } $sql = " SELECT FROM_UNIXTIME(created_at,'%H') as hour, user_id,mall_id FROM qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1 and created_at between {$start_time} and {$end_time} GROUP BY hour,user_id "; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $date = date('Y-m-d', $start_time); foreach ($list as $val){ $hour = $date.' '.$val['hour'].':00:00'; $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR .':'. $mall_id.':' .strtotime($hour); $redis_key = RedisKeyEnum::suffix($key, '', true); $redis = \Yii::$app->redis; $redis->setbit($redis_key, $val['user_id'], 1); } var_dump('fixAddPayPeopleNumsByHour执行结束'); } //初始化基础分析表 public function actionInitAna($mall_id = null) { $mall_list = Mall::getEnableMallList(true); if ($mall_id) { $mall = Mall::findOne(['id' => $mall_id]); $this->initAna($mall); } else { foreach ($mall_list as $mall) { $this->initAna($mall); } } } protected function initAna($mall) { $t = time(); $mall_id = $mall['id']; $sql = " SELECT t.mall_id, t.user_id,t.date,sum(t.pay_money) as total_pay_money,count(*) as buy_numbers from ( SELECT FROM_UNIXTIME(pay_time, '%Y-%m-%d') as date,user_id,mall_id, pay_money FROM qimall_order where mall_id = $mall_id and pay_status = 1 ) t GROUP BY t.user_id,t.date "; $list = \Yii::$app->getDb()->createCommand($sql)->queryAll(); foreach ($list as $item) { $model = CensusMallAnalysis::findOne(['mall_id' => $mall['id'], 'user_id' => $item['user_id'], 'date' => $item['date'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) { $model = new CensusMallAnalysis(); $model->date = $item['date']; $model->user_id = $item['user_id']; $model->mall_id = $mall['id']; $model->loadDefaultValues(); } $model->buy_numbers = $item['buy_numbers']; $model->total_pay_money = $item['total_pay_money']; $model->save(); $item['created_at'] = strtotime($item['date'] . ' 00:20:00'); $res = CensusMallAnalysis::updateAll(['created_at' => strtotime($item['date'] . ' 00:20:00')], ['mall_id' => $item['mall_id'], 'user_id' => $item['user_id'], 'date' => $item['date']] ); } $t = time()-$t; var_dump("商城{$mall_id}initAna执行".$t); } //初始化rf,rm表 public function actionInitRfm() { $mall_list = Mall::getEnableMallList(true); $time = strtotime(date('Y-m-d', time()) . ' 00:00:00'); $config_list = CensusMallAnalysis::getConfigList(); foreach ($mall_list as $mall) { $rf_arr = []; $rm_arr = []; foreach ($config_list as $key => $item) { list($rf, $rm) = $this->initRfRm($mall, $time, $item['min'], $item['max'], $key); if (!empty($rf)) $rf_arr[] = $rf; if (!empty($rm)) $rm_arr[] = $rm; } if (!empty($rf_arr)) { $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, '', true) . ':' . $mall['id']; $redis = \Yii::$app->redis; $redis->set($redis_key, json_encode($rf_arr)); foreach ($rf_arr as $item) { CensusMallRfAnalysis::setData($item); } } if (!empty($rm_arr)) { $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, '', true) . ':' . $mall['id']; $redis = \Yii::$app->redis; $redis->set($redis_key, json_encode($rm_arr)); foreach ($rm_arr as $item) { CensusMallRmAnalysis::setData($item); } } } } protected function rfRm($mall) { $t = time(); $time = strtotime(date('Y-m-d', time()) . ' 00:00:00'); $config_list = CensusMallAnalysis::getConfigList(); $rf_arr = []; $rm_arr = []; foreach ($config_list as $key => $item) { list($rf, $rm) = $this->initRfRm($mall, $time, $item['min'], $item['max'], $key); if (!empty($rf)) $rf_arr[] = $rf; if (!empty($rm)) $rm_arr[] = $rm; } if (!empty($rf_arr)) { $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, '', true) . ':' . $mall['id']; $redis = \Yii::$app->redis; $redis->set($redis_key, json_encode($rf_arr)); foreach ($rf_arr as $item) { CensusMallRfAnalysis::setData($item); } } if (!empty($rm_arr)) { $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, '', true) . ':' . $mall['id']; $redis = \Yii::$app->redis; $redis->set($redis_key, json_encode($rm_arr)); foreach ($rm_arr as $item) { CensusMallRmAnalysis::setData($item); } } $t = time()-$t; var_dump('商城'.$mall['id'].'rfRm执行结束'.$t); } protected function initRfRm($mall, $time, $start, $end, $purchase_interval = 'r_1_30') { $start_time = strtotime(date('Y-m-d', $time - $start * 24 * 3600) . ' 23:59:59'); if ($end) { $end_time = strtotime(date('Y-m-d', $time - $end * 24 * 3600) . ' 23:59:59'); } $where[] = ['mall_id' => $mall['id']]; $where[] = ['<', 'created_at', $start_time]; if (isset($end_time)) $where[] = ['>=', 'created_at', $end_time]; $list = CensusMallAnalysis::lists([ 'where' => $where, 'group' => 'user_id', 'select' => 'mall_id,user_id,sum(total_pay_money) as total_pay_money,sum(buy_numbers) as buy_numbers,' ]); $rf_row = ['purchase_interval' => $purchase_interval, 'mall_id' => $mall['id'], 'f_users1' => 0, 'f_users2' => 0, 'f_users3' => 0, 'f_users4' => 0, 'f_users5' => 0, 'f_total_pay_money1' => 0, 'f_total_pay_money2' => 0, 'f_total_pay_money3' => 0, 'f_total_pay_money4' => 0, 'f_total_pay_money5' => 0, ]; foreach ($list as $item) { switch ($item['buy_numbers']) { case 0: break; case 1: case 2: case 3: case 4: $rf_row['f_users' . $item['buy_numbers']]++; $rf_row['f_total_pay_money' . $item['buy_numbers']] += $item['total_pay_money']; $rf_row['f_total_pay_money' . $item['buy_numbers']] = bcmul($rf_row['f_total_pay_money' . $item['buy_numbers']] ,1,2); break; default: $rf_row['f_users5']++; $rf_row['f_total_pay_money5'] += $item['total_pay_money']; $rf_row['f_total_pay_money5'] = bcmul($rf_row['f_total_pay_money5'], 1,2); } } $rm_row = ['purchase_interval' => $purchase_interval, 'mall_id' => $mall['id'], 'm_users1' => 0, 'm_users2' => 0, 'm_users3' => 0, 'm_users4' => 0, 'm_users5' => 0, 'm_total_pay_money1' => 0, 'm_total_pay_money2' => 0, 'm_total_pay_money3' => 0, 'm_total_pay_money4' => 0, 'm_total_pay_money5' => 0, ]; foreach ($list as $item) { if ($item['total_pay_money'] >= 2000) { $rm_row['m_users5']++; $rm_row['m_total_pay_money5'] += $item['total_pay_money']; $rm_row['m_total_pay_money5'] = intval($rm_row['m_total_pay_money5'] * 100) / 100; } elseif ($item['total_pay_money'] >= 1000 && $item['total_pay_money'] < 2000) { $rm_row['m_users4']++; $rm_row['m_total_pay_money4'] += $item['total_pay_money']; $rm_row['m_total_pay_money4'] = intval($rm_row['m_total_pay_money4'] * 100) / 100; } elseif ($item['total_pay_money'] >= 500 && $item['total_pay_money'] < 1000) { $rm_row['m_users3']++; $rm_row['m_total_pay_money3'] += $item['total_pay_money']; $rm_row['m_total_pay_money3'] = intval($rm_row['m_total_pay_money3'] * 100) / 100; } elseif ($item['total_pay_money'] >= 200 && $item['total_pay_money'] < 500) { $rm_row['m_users2']++; $rm_row['m_total_pay_money2'] += $item['total_pay_money']; $rm_row['m_total_pay_money2'] = intval($rm_row['m_total_pay_money2'] * 100) / 100; } else { $rm_row['m_users1']++; $rm_row['m_total_pay_money1'] += $item['total_pay_money']; $rm_row['m_total_pay_money1'] = intval($rm_row['m_total_pay_money1'] * 100) / 100; } } return [$rf_row, $rm_row]; } /** * 清除小时交易额缓存 * @return void */ public function actionClearSingleHour() { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $mall_id = $mall['id']; $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR . ':' . $mall_id . ':'; $redis_key = RedisKeyEnum::suffix($key, '', true); $res = \Yii::$app->redis->keys($redis_key . '*'); if (empty($res)) { continue; } foreach ($res as $key) { $arr = explode(':', $key); $time = array_pop($arr); if (strpos($time, '-')) { continue; } if (time() - $time > 3600) { \Yii::$app->redis->del($key); } } } } /** * 清除单日数据缓存 * @return void */ public function actionClearSingleDate() { $mall_list = Mall::getEnableMallList(true); foreach ($mall_list as $mall) { $mall_id = $mall['id']; $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE . ':' . $mall_id . ':'; $redis_key = RedisKeyEnum::suffix($key, '', true); $res = \Yii::$app->redis->keys($redis_key . '*'); if (empty($res)) { continue; } foreach ($res as $key) { $arr = explode(':', $key); $time = array_pop($arr); if (strpos($time, '-')) { continue; } if (time() - $time > 24 * 3600) { \Yii::$app->redis->del($key); } } } } }