U8Settle.php 4.7 KB

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