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

229 lines
7.2 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 ?? 0;
  56. $note = $value->note ?? '';
  57. $storeId = $value->store_id ?? 0;
  58. $userId = $value->user_id ?? 0;
  59. if(empty($storeId) || empty($type) || empty($note)){
  60. $error[] = ['id'=>$value->id,'msg'=>'数据错误或者为空'];
  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. $error[] = ['id'=>$value->id,'msg'=>'总帐已存在'];
  103. continue;
  104. }else{
  105. if(!DB::table($newTableName)->insert($newData)){
  106. $error[] = ['id'=>$value->id,'msg'=>'总账添加失败'];
  107. break;
  108. }
  109. }
  110. // 判断是否存在 用户账
  111. $mod = bcmod((string)$userId, '5', 0);
  112. $newSubTableName = $newTableName.'_'.$mod;
  113. $existSub = DB::table($newSubTableName)
  114. ->where('user_id',$userId)
  115. ->where('user_type',$userType)
  116. ->where('source_id',$sourceId)
  117. ->where('source_type',$sourceType)
  118. ->where('money',$money)
  119. ->where('money_type',$moneyType)
  120. ->exists();
  121. if($existSub){
  122. $error[] = ['id'=>$value->id,'msg'=>'已存在'];
  123. continue;
  124. }else{
  125. if(!DB::table($newSubTableName)->insert($newData)){
  126. $error[] = ['id'=>$value->id,'msg'=>'用户账添加失败'];
  127. break;
  128. }
  129. }
  130. $bar->advance();
  131. }
  132. $bar->finish();
  133. var_dump([time()-$startTime]);
  134. var_dump(count($error));
  135. return 0;
  136. }
  137. public function getType($type,$note)
  138. {
  139. $array = [];
  140. // 1加,2减
  141. switch($type){
  142. case 1:
  143. if($note == '用户下单成功,平台奖励'){
  144. $array['money_type'] = 5;
  145. $array['source_type'] = 1;
  146. $array['desc'] = '用户店铺首单奖励';
  147. $array['comment'] = '用户当面付商户奖励';
  148. }else if($note == '新用户下单成功,平台奖励'){
  149. $array['money_type'] = 4;
  150. $array['source_type'] = 1;
  151. $array['desc'] = '新用户下单奖励';
  152. $array['comment'] = '用户当面付商户奖励';
  153. }else if($note == '当面付订单收入'){
  154. $array['money_type'] = 7;
  155. $array['source_type'] = 1;
  156. $array['desc'] = '线下当面付订单收入';
  157. $array['comment'] = '用户订单完成';
  158. }else if($note == '线上订单' || $note == '线上订单收入' || $note == '线上订单收入-测试'){
  159. $array['money_type'] = 6;
  160. $array['source_type'] = 1;
  161. $array['desc'] = '线上外卖订单收入';
  162. $array['comment'] = '用户订单完成';
  163. }
  164. break;
  165. case 2:
  166. if($note == '提现到微信'){
  167. $array['money_type'] = 105;
  168. $array['source_type'] = 2;
  169. $array['desc'] = '商户提现';
  170. $array['comment'] = '商户提现打款';
  171. }
  172. break;
  173. default:
  174. break;
  175. }
  176. return $array;
  177. }
  178. // 用户下单成功,平台奖励
  179. // $desc='用户店铺首单奖励';
  180. // $comment='用户当面付商户奖励';
  181. // 新用户下单成功,平台奖励
  182. // $desc='新用户下单奖励';
  183. // $comment='用户当面付商户奖励';
  184. // 线上订单
  185. // $desc = '线上外卖订单收入';
  186. // $comment = '用户订单完成';
  187. // 当面付订单收入
  188. // $desc = '线下当面付订单收入';
  189. // $comment = '用户订单完成';
  190. // 线上订单退款
  191. // $desc = '线上订单退款';
  192. // $comment = '线上订单退款到微信';
  193. // 提现到微信
  194. // $desc = '商户提现';
  195. // $comment = '商户提现打款';
  196. }