U8Settle.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\Record;
  4. use App\Service\U8DatabaseServerService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Log;
  7. class U8Settle extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'command:u8_settle';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. echo '任务--------start---------------';
  38. $this->syncApprovedRecords();
  39. echo '任务结束--------end---------------';
  40. }
  41. public function syncApprovedRecords()
  42. {
  43. try {
  44. $databases = [
  45. "UFDATA_200_2021",
  46. "UFDATA_002_2021",
  47. ];
  48. //初始化连接池
  49. $connections = [];
  50. $error = true;
  51. foreach ($databases as $db) {
  52. if(! $error) continue;
  53. $service = new U8DatabaseServerService(['zt_database' => $db]);
  54. if ($service->error) {
  55. Log::channel('apiLog')->info('SQLServer连接失败', [
  56. "database" => $db,
  57. "error" => $service->error,
  58. ]);
  59. $error = false;
  60. $connections[$db] = null; // 标记连接失败
  61. } else {
  62. $connections[$db] = $service;
  63. }
  64. }
  65. if(! $error ) return;
  66. $time = date("Y-m-d H:i:s");
  67. $time1 = date("Y-m-d 00:00:00");
  68. // 分批同步数据
  69. Record::where("del_time", 2)
  70. ->select("id","type","database","order_number")
  71. ->orderBy("id","desc")
  72. ->chunkById(10, function ($data) use($connections,$time,$time1){
  73. $data = Collect($data)->map(function ($object) {
  74. return (array)$object;
  75. })->toArray();
  76. $id = [];
  77. foreach ($data as $record) {
  78. $database = $record['database'];
  79. $service = $connections[$database] ?? null;
  80. if (! $service) continue;
  81. $type = $record['type'];
  82. $order_number = $record['order_number'];
  83. if($type == 1){
  84. $service->db->table("PO_Pomain")
  85. ->where("cPOID", $order_number)
  86. ->update([
  87. "cVerifier" => "system",
  88. "iverifystateex" => 1,
  89. "cState" => 1,
  90. "cAuditTime" => $time . ".000",
  91. "cAuditDate" => $time1 . ".000",
  92. ]);
  93. }elseif($type == 2){
  94. $service->db->table("PU_AppVouch")
  95. ->where("cCode", $order_number)
  96. ->update([
  97. "cVerifier" => "system",
  98. "cAuditTime" => $time . ".000",
  99. "cAuditDate" => $time1 . ".000",
  100. ]);
  101. }else{
  102. $service->db->table("AP_ApplyPayVouch")
  103. ->where("cVouchID", $order_number)
  104. ->update([
  105. "cCheckMan" => "system",
  106. "dverifysystime" => $time . ".000",
  107. "dverifydate" => $time1 . ".000",
  108. ]);
  109. }
  110. $id[] = $record['id'];
  111. }
  112. // 更新本地数据
  113. if(! empty($id)) Record::whereIn("id", $id)->update(['del_time' => 3]);
  114. });
  115. } catch (\Throwable $e) {
  116. Log::channel('apiLog')->info('U8数据更新异常', ['msg' => $e->getMessage()]);
  117. }
  118. }
  119. }