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

223 lines
6.9 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 MigrateStoreFinancial extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:storeFinancial';
  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_store_account';
  37. $oldStoreTableName = 'ims_cjdc_store';
  38. $newTableName = 'lanzu_financial_record';
  39. // 判断表是否存在
  40. if(!Schema::hasTable($oldTableName)){
  41. var_dump('旧表不存在');
  42. return 0;
  43. }
  44. if(!Schema::hasTable($newTableName)){
  45. var_dump('新表不存在');
  46. return 0;
  47. }
  48. $oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
  49. $oldStoreData = DB::table($oldStoreTableName)->pluck('user_id','id');
  50. $bar = $this->output->createProgressBar(count($oldData));
  51. $bar->start();
  52. $startTime = time();
  53. $error = [];
  54. foreach ($oldData as $key => $value){
  55. $type = $value->type;
  56. $note = $value->note;
  57. $storeId = $value->store_id;
  58. $userId = $value->user_id;
  59. if(empty($storeId) || empty($type) || empty($note)){
  60. $error[] = ['id'=>$value->id];
  61. continue;
  62. }
  63. if(empty($userId)){
  64. if(isset($oldStoreData[$storeId])){
  65. $userId = $oldStoreData[$storeId];
  66. }else{
  67. $error[] = ['id'=>$value->id,'msg'=>'找不到用户'];
  68. continue;
  69. }
  70. }
  71. $typeArr = $this->getType($type,$note);
  72. $userType = 5;
  73. $money = $value->money;
  74. $moneyType = $typeArr['money_type'];
  75. $sourceId = $value->order_id;
  76. $sourceType = $typeArr['source_type'];
  77. $desc = $typeArr['desc'];
  78. $comment = $typeArr['comment'];
  79. $newData =[
  80. 'user_id'=>$userId,
  81. 'user_type'=>$userType,
  82. 'money'=>$money,
  83. 'money_type'=>$moneyType,
  84. 'source_id'=>$sourceId,
  85. 'source_type'=>$sourceType,
  86. 'desc'=>$desc,
  87. 'comment'=>$comment,
  88. 'status'=>1,
  89. 'created_at' => strtotime($value->time),
  90. 'updated_at' => time(),
  91. ];
  92. // 判断是否存在 总账
  93. $exist = DB::table($newTableName)
  94. ->where('user_id',$userId)
  95. ->where('user_type',$userType)
  96. ->where('source_id',$sourceId)
  97. ->where('source_type',$sourceType)
  98. ->where('money',$money)
  99. ->where('money_type',$moneyType)
  100. ->exists();
  101. if(!$exist){
  102. if(!DB::table($newTableName)->insert($newData)){
  103. $error[] = ['id'=>$value->id,'msg'=>'总账添加失败'];
  104. break;
  105. }
  106. }
  107. // 判断是否存在 用户账
  108. $mod = bcmod((string)$userId, '5', 0);
  109. $newSubTableName = $newTableName.'_'.$mod;
  110. $existSub = DB::table($newSubTableName)
  111. ->where('user_id',$userId)
  112. ->where('user_type',$userType)
  113. ->where('source_id',$sourceId)
  114. ->where('source_type',$sourceType)
  115. ->where('money',$money)
  116. ->where('money_type',$moneyType)
  117. ->exists();
  118. if(!$existSub){
  119. if(!DB::table($newSubTableName)->insert($newData)){
  120. $error[] = ['id'=>$value->id,'msg'=>'用户账添加失败'];
  121. break;
  122. }
  123. }
  124. $bar->advance();
  125. }
  126. $bar->finish();
  127. var_dump([time()-$startTime]);
  128. var_dump($error);
  129. return 0;
  130. }
  131. public function getType($type,$note)
  132. {
  133. $array = [];
  134. // 1加,2减
  135. switch($type){
  136. case 1:
  137. if($note == '用户下单成功,平台奖励'){
  138. $array['money_type'] = 5;
  139. $array['source_type'] = 1;
  140. $array['desc'] = '用户店铺首单奖励';
  141. $array['comment'] = '用户当面付商户奖励';
  142. }else if($note == '新用户下单成功,平台奖励'){
  143. $array['money_type'] = 4;
  144. $array['source_type'] = 1;
  145. $array['desc'] = '新用户下单奖励';
  146. $array['comment'] = '用户当面付商户奖励';
  147. }else if($note == '当面付订单收入'){
  148. $array['money_type'] = 7;
  149. $array['source_type'] = 1;
  150. $array['desc'] = '线下当面付订单收入';
  151. $array['comment'] = '用户订单完成';
  152. }else if($note == '线上订单' || $note == '线上订单收入' || $note == '线上订单收入-测试'){
  153. $array['money_type'] = 6;
  154. $array['source_type'] = 1;
  155. $array['desc'] = '线上外卖订单收入';
  156. $array['comment'] = '用户订单完成';
  157. }
  158. break;
  159. case 2:
  160. if($note == '提现到微信'){
  161. $array['money_type'] = 105;
  162. $array['source_type'] = 2;
  163. $array['desc'] = '商户提现';
  164. $array['comment'] = '商户提现打款';
  165. }
  166. break;
  167. default:
  168. break;
  169. }
  170. return $array;
  171. }
  172. // 用户下单成功,平台奖励
  173. // $desc='用户店铺首单奖励';
  174. // $comment='用户当面付商户奖励';
  175. // 新用户下单成功,平台奖励
  176. // $desc='新用户下单奖励';
  177. // $comment='用户当面付商户奖励';
  178. // 线上订单
  179. // $desc = '线上外卖订单收入';
  180. // $comment = '用户订单完成';
  181. // 当面付订单收入
  182. // $desc = '线下当面付订单收入';
  183. // $comment = '用户订单完成';
  184. // 线上订单退款
  185. // $desc = '线上订单退款';
  186. // $comment = '线上订单退款到微信';
  187. // 提现到微信
  188. // $desc = '商户提现';
  189. // $comment = '商户提现打款';
  190. }