链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\v3\Store as StoreModel;
  5. use Illuminate\Support\Facades\DB;
  6. class SetStoreBalance extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:setStoreBalance';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command 在user_balance表添加商户钱包';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. * 添加商户钱包
  32. * 先判断是否存在,不存在则添加一条店铺的钱包记录
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $oldTableName = 'ims_cjdc_store';
  38. $newTableName = 'lanzu_user_balance';
  39. $oldData = DB::table($oldTableName)->get();
  40. $bar = $this->output->createProgressBar(count($oldData));
  41. $bar->start();
  42. $data = [];
  43. $newData = [];
  44. foreach ($oldData as $key => $value){
  45. // 查询店铺是否存在balance
  46. $exist = DB::table($newTableName)
  47. ->where('source_id',$value->id)
  48. ->where('user_type',5)
  49. ->first();
  50. if(empty($exist)){
  51. $newData[] = [
  52. 'source_id' => $value->id,
  53. 'user_type' => 5,
  54. 'balance' => 0,
  55. 'created_at' => time(),
  56. 'updated_at' => time()
  57. ];
  58. }else{
  59. $data[] = [
  60. 'store_id' => $value->id,
  61. 'balance_id' => $exist->id
  62. ];
  63. }
  64. $bar->advance();
  65. }
  66. // 添加到新表
  67. $res = DB::table($newTableName)->insert($newData);
  68. $bar->finish();
  69. var_dump('new_total: '.count($newData));
  70. var_dump('exist: '.count($data));
  71. var_dump($data);
  72. var_dump('res: '.$res);
  73. return 0;
  74. }
  75. }