| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\command;
- use app\common\repositories\user\UserRepository;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Db;
- class Csm extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('csm')
- ->setDescription('长寿美数据同步');
- }
- protected function execute(Input $input, Output $output)
- {
- //用户
- $this -> syncUser();
- $output->writeln('执行成功');
- }
- public function syncUser(){
- Db::startTrans();
- try {
- $data = Db::connect('csm')
- ->name('user')
- -> where('sync',0)
- ->select()->toArray();
- $userRepository = app()->make(UserRepository::class);
- foreach ($data as $k => $v) {
- $find = Db::name('user')->where("account",$v['account'])->find();
- if (!$find){
- $pwd = $userRepository->encodePassword("123456");
- $data = [
- 'account' => $v['account'],
- 'pwd' => $pwd,
- 'nickname' =>$v['nickname'],
- 'avatar' => $v['avatar'],
- 'phone' => $v['phone'],
- 'last_ip' => app('request')->ip(),
- 'external' => 2,
- 'external_id' => $v['uid'],
- "identity"=>5,
- "user_type" => $v['user_type'],
- ];
- Db::name('user')->insert($data);
- }
- Db::connect('csm') ->name('user') -> where("uid",$v['uid']) -> update(['sync'=>1]);
- }
- //更新用户推荐人
- $this -> updateUserSpread();
- Db::commit();
- }catch (\Exception $e){
- dump($e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
- Db::rollback();
- }
- }
- public function updateUserSpread(){
- $data = Db::name('user')->where('external',2)-> where('spread_uid',0) -> select() -> toArray();
- foreach ($data as $k => $v) {
- $csmUser = Db::connect('csm') ->name('user') -> where('uid',$v['external_id']) -> field("spread_uid") -> find();
- if ($csmUser['spread_uid'] == 0) continue;
- $csmAccount = Db::connect('csm') ->name('user') -> where('uid',$csmUser['spread_uid']) -> field("account") -> find();
- Db::name('user')->where("uid",$v['uid']) -> update(
- ['spread_uid' => Db::name('user')->where("account",$csmAccount['account']) -> value("uid")]
- );
- }
- }
- }
|