<?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 ImportUser extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('import_user')->setDescription('导入幸福宝一壶茶会员');
    }

    protected function execute(Input $input, Output $output)
    {
        $logs = [];
        $list = Db::connect('chuxubao')->query("SELECT rrx_user.uid as uid, account as phone, nickname, puid as spread_uid, (SELECT account from rrx_user where uid = puid limit 1) as spread_phone from rrx_user INNER JOIN rrx_enterprise_user on rrx_user.uid = rrx_enterprise_user.uid where rrx_user.is_del = 0");

        $userRepository = app()->make(UserRepository::class);
        //先走一遍注册
        foreach ($list as $reg_user) {
            $logs[$reg_user['phone']]['需要处理的信息'] = $reg_user;
            $temp_user = Db::name('user')->where('phone', $reg_user['phone'])->find();
            $temp1 = "存在";
            if (!$temp_user) {
                $temp1 = "不存在";
                $userRepository->registr($reg_user['phone'], null, 'h5', 0);
                echo "{$reg_user['phone']}注册成功\r\n";
            }
            $user = Db::name('user')->where('phone', $reg_user['phone'])->find();
            $logs[$reg_user['phone']]['注册用户'] = "导入手机号 {$reg_user['phone']}, {$temp1}, 数据库存在的ID {$temp_user['uid']}, 最新用户ID {$user['uid']}, 历史user_group {$user['user_group']}";
            if ($user['user_group'] != 4) {
                Db::name('user')->where('uid', $user['uid'])->update(['user_group' => 4]);
            }
            if ($user['is_show'] != 2) {
                Db::name('user')->where('uid', $user['uid'])->update(['is_show' => 2]);
            }
        }
        // 再走一遍绑定关系
        foreach ($list as $reg_user) {
            $user = Db::name('user')->where('phone', $reg_user['phone'])->find();
            $spread_user = Db::name('user')->where('phone', $reg_user['spread_phone'])->find();
            $logs[$reg_user['phone']]['绑定邀请关系'] = [
                'uid' => $user['uid'],
                'spread_uid' => $user['spread_uid'],
                'phone' => $user['phone'],
                'spread_user_uid' => $spread_user['uid'],
                'spread_user_phone' => $spread_user['phone'],
            ];
            if ($spread_user && empty($user['spread_uid'])) {
                Db::name('user')->where('uid', $user['uid'])->update(['spread_uid' => $spread_user['uid']]);
                $logs[$reg_user['phone']]['绑定邀请关系_没有邀请人绑定最新的'] = [
                    'user' => $user,
                    'spread_user_uid' => $spread_user['uid'],
                ];
            }
            $user = Db::name('user')->where('phone', $reg_user['phone'])->find();
            $mer_count=Db::name('merchant')->where('uid',$user['uid'])->count();
            if(!$mer_count){
//                $code_insert['uid']=$v['uid'];
//                $code_insert['code']=time().'_'.$this->generateRandomCode(20);
//                $code_insert['ctime']=time();
//                $code_insert['type']=2;
//                $re=Db::name('activation_code')->insertGetId($code_insert);
                $mer_insert['uid']=$user['uid'];
                $mer_insert['spread_uid']=$user['spread_uid'];
                $mer_id=Db::name('merchant')->insertGetId($mer_insert);
                $merchant_admin_insert['mer_id']=$mer_id;
                $merchant_admin_insert['uid']=  $user['uid'];
                $merchant_admin_insert['account']=  $user['account'];
                $merchant_admin_insert['real_name']=  $user['account'];
                $merchant_admin_insert['phone']=  $user['account'];
                $merchant_admin_insert['level']=  0;
                try {
                    $re=Db::name('merchant_admin')->insertGetId($merchant_admin_insert);
                }catch (\Exception $exception) {
                    var_dump($exception->getMessage());
                }

                $merchant_intention['mer_id']=$mer_id;
                $merchant_intention['uid']=  $user['uid'];
                $merchant_intention['phone']=   $user['account'];
                try {
                    Db::name('merchant_intention')->insertGetId($merchant_intention);
                }catch (\Exception $exception) {
                    var_dump($exception->getMessage());
                }
                $logs[$reg_user['phone']]['创建商户'] = [
                    'mer_insert' => $mer_insert,
                    'merchant_admin_insert' => $merchant_admin_insert,
                    'merchant_intention' => $merchant_intention,
                ];
//                insert into rrx_merchant_admin(mer_id,uid,account,pwd,phone,real_name,level) values (#{mer_id},#{uid},#{account},#{pwd},#{phone},#{real_name},#{level})
            }
        }
        Db::name('log')->insert(['data' => "更新邀请关系数据备份" . json_encode($logs, JSON_UNESCAPED_UNICODE)]);
    }
}