U8Settle.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "UFDATA_999_2021",
  48. ];
  49. //初始化连接池
  50. $connections = [];
  51. $error = true;
  52. foreach ($databases as $db) {
  53. if(! $error) continue;
  54. $service = new U8DatabaseServerService(['zt_database' => $db]);
  55. if ($service->error) {
  56. Log::channel('apiLog')->info('SQLServer连接失败', [
  57. "database" => $db,
  58. "error" => $service->error,
  59. ]);
  60. $error = false;
  61. $connections[$db] = null; // 标记连接失败
  62. } else {
  63. $connections[$db] = $service;
  64. }
  65. }
  66. if(! $error ) return;
  67. $time = date("Y-m-d H:i:s");
  68. $time1 = date("Y-m-d 00:00:00");
  69. // 分批同步数据
  70. Record::where("del_time", 2)
  71. ->select("id","type","database","order_number")
  72. ->orderBy("id","desc")
  73. ->chunkById(10, function ($data) use($connections,$time,$time1){
  74. $data = $data->toArray();
  75. $id = [];
  76. foreach ($data as $record) {
  77. $database = $record['database'];
  78. $service = $connections[$database] ?? null;
  79. if (! $service) continue;
  80. $type = $record['type'];
  81. $order_number = $record['order_number'];
  82. if($type == 1){
  83. $service->db->table("PO_Pomain")
  84. ->where("cPOID", $order_number)
  85. ->update([
  86. "cVerifier" => "system",
  87. "iverifystateex" => 1,
  88. "cState" => 1,
  89. "cAuditTime" => $time . ".000",
  90. "cAuditDate" => $time1 . ".000",
  91. ]);
  92. }elseif($type == 2){
  93. $service->db->table("PU_AppVouch")
  94. ->where("cCode", $order_number)
  95. ->update([
  96. "cVerifier" => "system",
  97. "cAuditTime" => $time . ".000",
  98. "cAuditDate" => $time1 . ".000",
  99. ]);
  100. }else{
  101. $service->db->table("AP_ApplyPayVouch")
  102. ->where("cVouchID", $order_number)
  103. ->update([
  104. "cCheckMan" => "system",
  105. "dverifysystime" => $time . ".000",
  106. "dverifydate" => $time1 . ".000",
  107. ]);
  108. }
  109. $id[] = $record['id'];
  110. }
  111. // 更新本地数据
  112. if(! empty($id)) Record::whereIn("id", $id)->update(['del_time' => 3]);
  113. });
  114. } catch (\Throwable $e) {
  115. Log::channel('apiLog')->info('U8数据更新异常', ['msg' => $e->getMessage()]);
  116. }
  117. }
  118. }