Csm.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\command;
  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 Csm extends Command
  9. {
  10. protected function configure()
  11. {
  12. // 指令配置
  13. $this->setName('csm')
  14. ->setDescription('长寿美数据同步');
  15. }
  16. protected function execute(Input $input, Output $output)
  17. {
  18. //用户
  19. $this -> syncUser();
  20. $output->writeln('执行成功');
  21. }
  22. public function syncUser(){
  23. Db::startTrans();
  24. try {
  25. $data = Db::connect('csm')
  26. ->name('user')
  27. -> where('sync',0)
  28. ->select()->toArray();
  29. $userRepository = app()->make(UserRepository::class);
  30. foreach ($data as $k => $v) {
  31. $find = Db::name('user')->where("account",$v['account'])->find();
  32. if (!$find){
  33. $pwd = $userRepository->encodePassword("12345678");
  34. $data = [
  35. 'account' => $v['account'],
  36. 'pwd' => $pwd,
  37. 'nickname' =>$v['nickname'],
  38. 'avatar' => $v['avatar'],
  39. 'phone' => $v['phone'],
  40. 'last_ip' => app('request')->ip(),
  41. 'external' => 2,
  42. 'external_id' => $v['uid'],
  43. "identity"=>5,
  44. "user_type" => $v['user_type'],
  45. ];
  46. Db::name('user')->insert($data);
  47. }
  48. Db::connect('csm') ->name('user') -> where("uid",$v['uid']) -> update(['sync'=>1]);
  49. }
  50. //更新用户推荐人
  51. $this -> updateUserSpread();
  52. Db::commit();
  53. }catch (\Exception $e){
  54. dump($e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  55. Db::rollback();
  56. }
  57. }
  58. public function updateUserSpread(){
  59. $data = Db::name('user')->where('external',2)-> where('spread_uid',0) -> select() -> toArray();
  60. foreach ($data as $k => $v) {
  61. $csmUser = Db::connect('csm') ->name('user') -> where('uid',$v['external_id']) -> field("spread_uid") -> find();
  62. if ($csmUser['spread_uid'] == 0) continue;
  63. $csmAccount = Db::connect('csm') ->name('user') -> where('uid',$csmUser['spread_uid']) -> field("account") -> find();
  64. Db::name('user')->where("uid",$v['uid']) -> update(
  65. ['spread_uid' => Db::name('user')->where("account",$csmAccount['account']) -> value("uid")]
  66. );
  67. }
  68. }
  69. }