U8Settle.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = Collect($data)->map(function ($object) {
  75. return (array)$object;
  76. })->toArray();
  77. $id = [];
  78. foreach ($data as $record) {
  79. $database = $record['database'];
  80. $service = $connections[$database] ?? null;
  81. if (! $service) continue;
  82. $type = $record['type'];
  83. $order_number = $record['order_number'];
  84. if($type == 1){
  85. $service->db->table("PO_Pomain")
  86. ->where("cPOID", $order_number)
  87. ->update([
  88. "cVerifier" => "system",
  89. "iverifystateex" => 1,
  90. "cState" => 1,
  91. "cAuditTime" => $time . ".000",
  92. "cAuditDate" => $time1 . ".000",
  93. ]);
  94. }elseif($type == 2){
  95. $service->db->table("PU_AppVouch")
  96. ->where("cCode", $order_number)
  97. ->update([
  98. "cVerifier" => "system",
  99. "cAuditTime" => $time . ".000",
  100. "cAuditDate" => $time1 . ".000",
  101. ]);
  102. }else{
  103. $service->db->table("AP_ApplyPayVouch")
  104. ->where("cVouchID", $order_number)
  105. ->update([
  106. "cCheckMan" => "system",
  107. "dverifysystime" => $time . ".000",
  108. "dverifydate" => $time1 . ".000",
  109. ]);
  110. }
  111. $id[] = $record['id'];
  112. }
  113. // 更新本地数据
  114. if(! empty($id)) Record::whereIn("id", $id)->update(['del_time' => 3]);
  115. });
  116. } catch (\Throwable $e) {
  117. Log::channel('apiLog')->info('U8数据更新异常', ['msg' => $e->getMessage()]);
  118. }
  119. }
  120. }