| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace console\controllers;
- use common\enums\RabbitMqEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\census\CensusMall;
- use common\models\census\CensusMallGoods;
- use common\models\census\CensusMallPurchasingPower;
- use common\models\census\CensusMallRegion;
- use common\models\census\CensusMallSingleDay;
- use common\models\census\CensusMallSingleHour;
- use common\models\census\CensusMallUser;
- use common\models\census\CensusMallUserLevel;
- use common\models\census\CensusMallUserSource;
- use common\models\goods\Goods;
- use common\models\mall\Mall;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use common\models\user\UserPartitionRelationship;
- use common\models\user\UserTeamStatistics;
- use yii\console\Controller;
- class FixUserDataController extends Controller
- {
- public function actionIndex($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixUserData($mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixUserData($mall);
- }
- }
- }
- //修复UserTeamStatistics表,直推会员,间推会员,所有下级会员数
- public function fixUserData($mall)
- {
- $i = 1;
- $page_size = 1000;
- while ($user_list = User::find()->where(['mall_id' => $mall['id']])
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($user_list as $item) {
- $user_direct_total = User::find()->where(['parent_id' => $item['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
- $user_indirect_total = User::find()->where(['second_parent_id' => $item['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
- $user_team_total = UserPartitionRelationship::getChildCounts($item['id']);
- $model = UserTeamStatistics::findOne(['user_id' => $item['id']]);
- if (empty($model)) {
- $model = new UserTeamStatistics();
- $model->loadDefaultValues();
- $model->mall_id = $mall['id'];
- $model->user_id = $item['id'];
- }
- $model->user_direct_total = $user_direct_total ?? 0;
- $model->user_indirect_total = $user_indirect_total ?? 0;
- $model->user_team_total = $user_team_total ?? 0;
- $res = $model->save();
- var_dump($res);
- }
- $i++;
- }
- }
- public function actionFixTree($mall_id = null)
- {
- $i = 1;
- $page_size = 1000;
- $where = [];
- $mall_id && $where['mall_id'] = $mall_id;
- $where['status'] = StatusEnum::ENABLED;
- while ($list = User::find()
- ->where($where)
- ->offset(($i - 1) * $page_size)
- ->orderBy('id asc')->limit($page_size)->all()) {
- foreach ($list as $user) {
- $tree_arr = [$user['base64_code']];
- $pid_arr = [$user['id']];
- if (!empty($user['parent_id'])) {
- $parent_id = $user['parent_id'];
- while (true) {
- $parent = User::findOne(['id' => $parent_id]);
- $tree_arr[] = $parent['base64_code'];
- $pid_arr[] = $parent['id'];
- if (empty($parent['parent_id'])) break;
- $parent_id = $parent['parent_id'];
- }
- }
- $tree_arr = array_reverse($tree_arr);
- $pid_arr = array_reverse($pid_arr);
- $tree = join(',', $tree_arr);
- $deep = count($tree_arr) - 1;
- array_pop($pid_arr);
- $pid1 = array_pop($pid_arr);
- $pid2 = array_pop($pid_arr);
- $pid3 = array_pop($pid_arr);
- $user_re = UserPartitionRelationship::findOne(['user_id' => $user['id']]);
- if ($user_re['tree'] != $tree || $user_re['deep'] != $deep) {
- $user_re->tree = $tree;
- $user_re->deep = $deep;
- $res = $user_re->save();
- var_dump($res);
- }
- if (!empty($pid1)) $user->parent_id = $pid1;
- if (!empty($pid2)) $user->second_parent_id = $pid2;
- if (!empty($pid3)) $user->third_parent_id = $pid3;
- $user->save();
- var_dump('user_id:' . $user['id'] . ':tree:' . $tree . ':deep:' . $deep);
- }
- $i++;
- }
- }
- public function actionFDeep($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixDeep($mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixDeep($mall);
- }
- }
- }
- public function fixDeep($mall)
- {
- $i = 1;
- $page_size = 1000;
- $where = [
- 'and',
- ['mall_id' => $mall['id']],
- ];
- while ($user_list = User::find()->where($where)
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($user_list as $item) {
- $user_id = $item['id'];
- list($tree, $deep, $all_tree, $all_tree_arr) = UserPartitionRelationship::getTree($user_id);
- $counts = count($all_tree_arr);
- $new_deep = $counts - 1;
- if ($new_deep != $deep) {
- $res = UserPartitionRelationship::updateAll(['deep' => $new_deep], ['user_id' => $user_id]);
- var_dump($res);
- }
- }
- $i++;
- }
- }
- public function actionFbCnum()
- {
- $i = 1;
- $page_size = 1000;
- $where = ['status' => ['status' => StatusEnum::ENABLED]];
- while ($list = User::find()
- ->where($where)
- ->offset(($i - 1) * $page_size)
- ->limit($page_size)->all()) {
- foreach ($list as $user){
- $user_id = $user['id'];
- $balance_list = \common\models\common\BalanceLog::find()->where(['user_id' => $user_id, 'status' => 1])->orderBy('id asc')->all();
- foreach ($balance_list as $k => $item) {
- if (isset($balance_list[$k + 1])) {
- if ($k == 0) $item['current_num'] = 0;
- if($item['change_type']=='add'){
- $balance_list[$k + 1]->current_num = $item['change_num'] + $item['current_num'];
- }else{
- $balance_list[$k + 1]->current_num = $item['current_num'] - $item['change_num'] ;
- }
- $balance_list[$k + 1]->save();
- }
- }
- var_dump($user_id);
- }
- $i++;
- }
- }
- }
|