ImportUser2.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\command\test\ln;
  3. use app\common\repositories\user\UserRepository;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\facade\Db;
  8. class ImportUser2 extends Command
  9. {
  10. protected function configure()
  11. {
  12. $this->setName('chuxubao')->setDescription('导入幸福宝数据');
  13. }
  14. protected function execute(Input $input, Output $output)
  15. {
  16. try{
  17. $userRepository = app()->make(UserRepository::class);
  18. $list = Db::connect('chuxubao')->query("select uid,phone,spread_uid from rrx_user where is_del=0 and is_show=2");
  19. foreach ($list as $chuxubao_user) {
  20. $childSpreadUidList = Db::connect('chuxubao')->query("select getUserSpreadId({$chuxubao_user['uid']}) as uids");
  21. $count[0]['uids'] = trim($childSpreadUidList[0]['uids'], '$,');
  22. $resultArray = explode(",", $count[0]['uids']);
  23. sort($resultArray);
  24. foreach ($resultArray as $uid) {
  25. if ($chuxubao_user['uid'] == $uid) {
  26. continue;
  27. }
  28. $logs = [];
  29. //查询用户信息
  30. $sql = "select uid,phone,spread_uid,is_del from rrx_user where uid = {$uid}";
  31. $child_user = Db::connect('chuxubao')->query($sql)[0] ?? [];
  32. $logs['child_user'] = 'ok';
  33. $logs['child_user_spread'] = 'ok';
  34. if ($child_user['is_del'] == 1) {
  35. $logs['child_user'] = 'del';
  36. }
  37. //查询父级信息
  38. $sql2 = "select uid,phone,spread_uid,is_del from rrx_user where uid = {$child_user['spread_uid']}";
  39. $father_user = Db::connect('chuxubao')->query($sql2)[0] ?? [];
  40. if ($child_user['is_del'] == 1) {
  41. $logs['child_user_spread'] = 'del';
  42. }
  43. $logs['发起人手机号'] = $chuxubao_user['phone'];
  44. $logs['父级手机号'] = $father_user['phone'];
  45. $logs['子级手机号'] = $child_user['phone'];
  46. //父级用户信息
  47. $yihucha_father_user = Db::name('user')->where('phone', $father_user['phone'])->where('is_del', 0)->find();
  48. if (empty($yihucha_father_user)) {
  49. $userRepository->registr($father_user['phone'], '123456', 'h5', 0);
  50. $yihucha_father_user = Db::name('user')->where('phone', $father_user['phone'])->where('is_del', 0)->find();
  51. $logs['t1'] = '父级手机号未注册用户-已注册';
  52. } else {
  53. $logs['t1'] = '父级手机号已注册用户';
  54. }
  55. //子级用户信息
  56. $yihucha_child_user = Db::name('user')->where('phone', $child_user['phone'])->where('is_del', 0)->find();
  57. //子级用户未注册用户,为子级用户注册用户
  58. if (empty($yihucha_child_user['uid'])) {
  59. $logs['step1'] = '子级手机号未注册用户';
  60. $userRepository->registr($child_user['phone'], '123456', 'h5', 0);
  61. } else {
  62. $logs['step1'] = '子级手机号已注册用户';
  63. }
  64. //查询最新的子级用户信息
  65. $yihucha_child_user = Db::name('user')->where('phone', $child_user['phone'])->where('is_del', 0)->find();
  66. //子级用户信息为空,用父级uid覆盖
  67. if (empty($yihucha_child_user['spread_uid'])) {
  68. $logs['step2'] = "一壶茶邀请关系为空, 父级用户uid, {$yihucha_father_user['uid']}, 父级用户手机号, {{$yihucha_father_user['phone']}}, 使用幸福宝邀请关系覆盖";
  69. Db::name('user')->where('uid', $yihucha_child_user['uid'])->update(['spread_uid' => $yihucha_father_user['uid']]);
  70. } else {
  71. $yihucha_child_spread_user = Db::name('user')->where('uid', $yihucha_child_user['spread_uid'])->where('is_del', 0)->find();
  72. $logs['step2'] = "一壶茶邀请关系不为空, 父级用户uid, {$yihucha_child_user['spread_uid']}, 父级用户手机号, {$yihucha_child_spread_user['phone']}, 邀请关系不覆盖";
  73. }
  74. foreach ($logs as $key => $log) {
  75. echo "{$key}, {$log},";
  76. }
  77. echo "\r\n";
  78. }
  79. }
  80. } catch (Exception $e) {
  81. var_dump($e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage());
  82. }
  83. }
  84. }