<?php

namespace app\command\test\ln;

use app\common\repositories\user\UserRepository;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class ImportUser2 extends Command
{
    protected function configure()
    {
        $this->setName('chuxubao')->setDescription('导入幸福宝数据');
    }

    protected function execute(Input $input, Output $output)
    {
        try{
            $userRepository = app()->make(UserRepository::class);
            $list = Db::connect('chuxubao')->query("select uid,phone,spread_uid from rrx_user where is_del=0 and is_show=2");
            foreach ($list as $chuxubao_user) {
                $childSpreadUidList = Db::connect('chuxubao')->query("select getUserSpreadId({$chuxubao_user['uid']}) as uids");
                $count[0]['uids'] = trim($childSpreadUidList[0]['uids'], '$,');

                $resultArray = explode(",", $count[0]['uids']);

                sort($resultArray);

                foreach ($resultArray as $uid) {
                    if ($chuxubao_user['uid'] == $uid) {
                        continue;
                    }
                    $logs = [];
                    //查询用户信息
                    $sql = "select uid,phone,spread_uid,is_del from rrx_user where uid = {$uid}";
                    $child_user = Db::connect('chuxubao')->query($sql)[0] ?? [];
                    $logs['child_user'] = 'ok';
                    $logs['child_user_spread'] = 'ok';
                    if ($child_user['is_del'] == 1) {
                        $logs['child_user'] = 'del';
                    }
                    //查询父级信息
                    $sql2 = "select uid,phone,spread_uid,is_del from rrx_user where uid = {$child_user['spread_uid']}";
                    $father_user = Db::connect('chuxubao')->query($sql2)[0] ?? [];
                    if ($child_user['is_del'] == 1) {
                        $logs['child_user_spread'] = 'del';
                    }

                    $logs['发起人手机号'] = $chuxubao_user['phone'];
                    $logs['父级手机号'] = $father_user['phone'];
                    $logs['子级手机号'] = $child_user['phone'];

                    //父级用户信息
                    $yihucha_father_user = Db::name('user')->where('phone', $father_user['phone'])->where('is_del', 0)->find();
                    if (empty($yihucha_father_user)) {
                        $userRepository->registr($father_user['phone'], '123456', 'h5', 0);
                        $yihucha_father_user = Db::name('user')->where('phone', $father_user['phone'])->where('is_del', 0)->find();
                        $logs['t1'] = '父级手机号未注册用户-已注册';
                    } else {
                        $logs['t1'] = '父级手机号已注册用户';
                    }
                    //子级用户信息
                    $yihucha_child_user = Db::name('user')->where('phone', $child_user['phone'])->where('is_del', 0)->find();
                    //子级用户未注册用户，为子级用户注册用户
                    if (empty($yihucha_child_user['uid'])) {
                        $logs['step1'] = '子级手机号未注册用户';
                        $userRepository->registr($child_user['phone'], '123456', 'h5', 0);
                    } else {
                        $logs['step1'] = '子级手机号已注册用户';
                    }
                    //查询最新的子级用户信息
                    $yihucha_child_user = Db::name('user')->where('phone', $child_user['phone'])->where('is_del', 0)->find();
                    //子级用户信息为空，用父级uid覆盖
                    if (empty($yihucha_child_user['spread_uid'])) {
                        $logs['step2'] = "一壶茶邀请关系为空, 父级用户uid, {$yihucha_father_user['uid']}, 父级用户手机号, {{$yihucha_father_user['phone']}}, 使用幸福宝邀请关系覆盖";
                        Db::name('user')->where('uid', $yihucha_child_user['uid'])->update(['spread_uid' => $yihucha_father_user['uid']]);
                    } else {
                        $yihucha_child_spread_user = Db::name('user')->where('uid', $yihucha_child_user['spread_uid'])->where('is_del', 0)->find();
                        $logs['step2'] = "一壶茶邀请关系不为空, 父级用户uid, {$yihucha_child_user['spread_uid']}, 父级用户手机号, {$yihucha_child_spread_user['phone']}, 邀请关系不覆盖";
                    }
                    foreach ($logs as $key => $log) {
                        echo "{$key}, {$log},";
                    }
                    echo "\r\n";
                }
            }
        } catch (Exception $e) {
            var_dump($e->getFile() . '：' . $e->getLine() . '【' . $e->getMessage());
        }
    }
}