链街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.

192 lines
5.5 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. class MigrateUserAccount extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:userAccount';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command 迁移用户流水数据';
  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. * @return int
  33. */
  34. public function handle()
  35. {
  36. $oldTableName = 'ims_cjdc_qbmx';
  37. $newTableName = 'lanzu_financial_record';
  38. // 判断表是否存在
  39. if(!Schema::hasTable($oldTableName)){
  40. var_dump('旧表不存在');
  41. return 0;
  42. }
  43. if(!Schema::hasTable($newTableName)){
  44. var_dump('新表不存在');
  45. return 0;
  46. }
  47. $oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
  48. $bar = $this->output->createProgressBar(count($oldData));
  49. $bar->start();
  50. $startTime = time();
  51. $error = [];
  52. foreach ($oldData as $key => $value){
  53. $type = $value->type;
  54. $note = $value->note;
  55. $userId = $value->user_id;
  56. if(empty($userId) || empty($type) || empty($note)){
  57. $error[] = ['id'=>$value->id];
  58. continue;
  59. }
  60. $typeArr = $this->getType($type,$note);
  61. $userType = 1;
  62. $money = $value->money;
  63. $moneyType = $typeArr['money_type'];
  64. $sourceId = $value->order_id;
  65. $sourceType = $typeArr['source_type'];
  66. $desc = $typeArr['desc'];
  67. $comment = $typeArr['comment'];
  68. $newData =[
  69. 'user_id'=>$userId,
  70. 'user_type'=>$userType,
  71. 'money'=>$money,
  72. 'money_type'=>$moneyType,
  73. 'source_id'=>$sourceId,
  74. 'source_type'=>$sourceType,
  75. 'desc'=>$desc,
  76. 'comment'=>$comment,
  77. 'status'=>1,
  78. 'created_at' => $moneyType == 8 ? $value->time : strtotime($value->time),
  79. 'updated_at' => time(),
  80. ];
  81. // 判断是否存在 总账
  82. $exist = DB::table($newTableName)
  83. ->where('user_id',$userId)
  84. ->where('user_type',$userType)
  85. ->where('source_id',$sourceId)
  86. ->where('source_type',$sourceType)
  87. ->where('money',$money)
  88. ->where('money_type',$moneyType)
  89. ->exists();
  90. if(!$exist){
  91. if(!DB::table($newTableName)->insert($newData)){
  92. $error[] = ['id'=>$value->id,'msg'=>'总账添加失败'];
  93. break;
  94. }
  95. }
  96. // 判断是否存在 用户账
  97. $mod = bcmod((string)$userId, '5', 0);
  98. $newSubTableName = $newTableName.'_'.$mod;
  99. $existSub = DB::table($newSubTableName)
  100. ->where('user_id',$userId)
  101. ->where('user_type',$userType)
  102. ->where('source_id',$sourceId)
  103. ->where('source_type',$sourceType)
  104. ->where('money',$money)
  105. ->where('money_type',$moneyType)
  106. ->exists();
  107. if(!$existSub){
  108. if(!DB::table($newSubTableName)->insert($newData)){
  109. $error[] = ['id'=>$value->id,'msg'=>'用户账添加失败'];
  110. break;
  111. }
  112. }
  113. $bar->advance();
  114. }
  115. $bar->finish();
  116. var_dump([time()-$startTime]);
  117. var_dump($error);
  118. return 0;
  119. }
  120. public function getType($type,$note)
  121. {
  122. $array = [];
  123. // 1加,2减
  124. switch($type){
  125. case 1:
  126. if($note == '当面付订单'){
  127. $array['money_type'] = 100;
  128. $array['source_type'] = 1;
  129. $array['desc'] = '用户下单(线下)';
  130. $array['comment'] = '用户下单';
  131. }else if($note == '外卖订单'){
  132. $array['money_type'] = 101;
  133. $array['source_type'] = 1;
  134. $array['desc'] = '用户下单(线上)';
  135. $array['comment'] = '用户下单';
  136. }
  137. break;
  138. case 2:
  139. if($note == '订单退款'){
  140. $array['money_type'] = 8;
  141. $array['source_type'] = 1;
  142. $array['desc'] = '线上订单退款';
  143. $array['comment'] = '线上订单退款到微信';
  144. }
  145. break;
  146. default:
  147. break;
  148. }
  149. return $array;
  150. }
  151. // 当面付订单
  152. // $desc='用户下单(线下)',
  153. // $comment='用户下单'
  154. // 外卖订单
  155. // $desc='用户下单(线上)',
  156. // $comment='用户下单'
  157. // 订单退款
  158. // $desc = '线上订单退款',
  159. // $comment = '线上订单退款到微信'
  160. // 后台充值
  161. // 在线充值
  162. // 订单拒绝
  163. }