FixUserDataController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace console\controllers;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\census\CensusMall;
  7. use common\models\census\CensusMallGoods;
  8. use common\models\census\CensusMallPurchasingPower;
  9. use common\models\census\CensusMallRegion;
  10. use common\models\census\CensusMallSingleDay;
  11. use common\models\census\CensusMallSingleHour;
  12. use common\models\census\CensusMallUser;
  13. use common\models\census\CensusMallUserLevel;
  14. use common\models\census\CensusMallUserSource;
  15. use common\models\goods\Goods;
  16. use common\models\mall\Mall;
  17. use common\models\order\Order;
  18. use common\models\order\OrderDetail;
  19. use common\models\user\User;
  20. use common\models\user\UserLevel;
  21. use common\models\user\UserPartitionRelationship;
  22. use common\models\user\UserTeamStatistics;
  23. use yii\console\Controller;
  24. class FixUserDataController extends Controller
  25. {
  26. public function actionIndex($mall_id = null)
  27. {
  28. $redis = \Yii::$app->redis;
  29. if (!empty($mall_id)) {
  30. $mall = Mall::findOne(['id' => $mall_id]);
  31. if ($mall) {
  32. $this->fixUserData($mall);
  33. }
  34. } else {
  35. $mall_list = Mall::getEnableMallList(true);
  36. foreach ($mall_list as $mall) {
  37. $this->fixUserData($mall);
  38. }
  39. }
  40. }
  41. //修复UserTeamStatistics表,直推会员,间推会员,所有下级会员数
  42. public function fixUserData($mall)
  43. {
  44. $i = 1;
  45. $page_size = 1000;
  46. while ($user_list = User::find()->where(['mall_id' => $mall['id']])
  47. ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  48. foreach ($user_list as $item) {
  49. $user_direct_total = User::find()->where(['parent_id' => $item['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
  50. $user_indirect_total = User::find()->where(['second_parent_id' => $item['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
  51. $user_team_total = UserPartitionRelationship::getChildCounts($item['id']);
  52. $model = UserTeamStatistics::findOne(['user_id' => $item['id']]);
  53. if (empty($model)) {
  54. $model = new UserTeamStatistics();
  55. $model->loadDefaultValues();
  56. $model->mall_id = $mall['id'];
  57. $model->user_id = $item['id'];
  58. }
  59. $model->user_direct_total = $user_direct_total ?? 0;
  60. $model->user_indirect_total = $user_indirect_total ?? 0;
  61. $model->user_team_total = $user_team_total ?? 0;
  62. $res = $model->save();
  63. var_dump($res);
  64. }
  65. $i++;
  66. }
  67. }
  68. public function actionFixTree($mall_id = null)
  69. {
  70. $i = 1;
  71. $page_size = 1000;
  72. $where = [];
  73. $mall_id && $where['mall_id'] = $mall_id;
  74. $where['status'] = StatusEnum::ENABLED;
  75. while ($list = User::find()
  76. ->where($where)
  77. ->offset(($i - 1) * $page_size)
  78. ->orderBy('id asc')->limit($page_size)->all()) {
  79. foreach ($list as $user) {
  80. $tree_arr = [$user['base64_code']];
  81. $pid_arr = [$user['id']];
  82. if (!empty($user['parent_id'])) {
  83. $parent_id = $user['parent_id'];
  84. while (true) {
  85. $parent = User::findOne(['id' => $parent_id]);
  86. $tree_arr[] = $parent['base64_code'];
  87. $pid_arr[] = $parent['id'];
  88. if (empty($parent['parent_id'])) break;
  89. $parent_id = $parent['parent_id'];
  90. }
  91. }
  92. $tree_arr = array_reverse($tree_arr);
  93. $pid_arr = array_reverse($pid_arr);
  94. $tree = join(',', $tree_arr);
  95. $deep = count($tree_arr) - 1;
  96. array_pop($pid_arr);
  97. $pid1 = array_pop($pid_arr);
  98. $pid2 = array_pop($pid_arr);
  99. $pid3 = array_pop($pid_arr);
  100. $user_re = UserPartitionRelationship::findOne(['user_id' => $user['id']]);
  101. if ($user_re['tree'] != $tree || $user_re['deep'] != $deep) {
  102. $user_re->tree = $tree;
  103. $user_re->deep = $deep;
  104. $res = $user_re->save();
  105. var_dump($res);
  106. }
  107. if (!empty($pid1)) $user->parent_id = $pid1;
  108. if (!empty($pid2)) $user->second_parent_id = $pid2;
  109. if (!empty($pid3)) $user->third_parent_id = $pid3;
  110. $user->save();
  111. var_dump('user_id:' . $user['id'] . ':tree:' . $tree . ':deep:' . $deep);
  112. }
  113. $i++;
  114. }
  115. }
  116. public function actionFDeep($mall_id = null)
  117. {
  118. if (!empty($mall_id)) {
  119. $mall = Mall::findOne(['id' => $mall_id]);
  120. if ($mall) {
  121. $this->fixDeep($mall);
  122. }
  123. } else {
  124. $mall_list = Mall::getEnableMallList(true);
  125. foreach ($mall_list as $mall) {
  126. $this->fixDeep($mall);
  127. }
  128. }
  129. }
  130. public function fixDeep($mall)
  131. {
  132. $i = 1;
  133. $page_size = 1000;
  134. $where = [
  135. 'and',
  136. ['mall_id' => $mall['id']],
  137. ];
  138. while ($user_list = User::find()->where($where)
  139. ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  140. foreach ($user_list as $item) {
  141. $user_id = $item['id'];
  142. list($tree, $deep, $all_tree, $all_tree_arr) = UserPartitionRelationship::getTree($user_id);
  143. $counts = count($all_tree_arr);
  144. $new_deep = $counts - 1;
  145. if ($new_deep != $deep) {
  146. $res = UserPartitionRelationship::updateAll(['deep' => $new_deep], ['user_id' => $user_id]);
  147. var_dump($res);
  148. }
  149. }
  150. $i++;
  151. }
  152. }
  153. public function actionFbCnum()
  154. {
  155. $i = 1;
  156. $page_size = 1000;
  157. $where = ['status' => ['status' => StatusEnum::ENABLED]];
  158. while ($list = User::find()
  159. ->where($where)
  160. ->offset(($i - 1) * $page_size)
  161. ->limit($page_size)->all()) {
  162. foreach ($list as $user){
  163. $user_id = $user['id'];
  164. $balance_list = \common\models\common\BalanceLog::find()->where(['user_id' => $user_id, 'status' => 1])->orderBy('id asc')->all();
  165. foreach ($balance_list as $k => $item) {
  166. if (isset($balance_list[$k + 1])) {
  167. if ($k == 0) $item['current_num'] = 0;
  168. if($item['change_type']=='add'){
  169. $balance_list[$k + 1]->current_num = $item['change_num'] + $item['current_num'];
  170. }else{
  171. $balance_list[$k + 1]->current_num = $item['current_num'] - $item['change_num'] ;
  172. }
  173. $balance_list[$k + 1]->save();
  174. }
  175. }
  176. var_dump($user_id);
  177. }
  178. $i++;
  179. }
  180. }
  181. }