123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Console\Commands;
- use App\Model\Record;
- use App\Service\U8DatabaseServerService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- class U8Settle extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'command:u8_settle';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- echo '任务--------start---------------';
- $this->syncApprovedRecords();
- echo '任务结束--------end---------------';
- }
- public function syncApprovedRecords()
- {
- try {
- $databases = [
- "UFDATA_200_2021",
- "UFDATA_002_2021",
- ];
- //初始化连接池
- $connections = [];
- $error = true;
- foreach ($databases as $db) {
- if(! $error) continue;
- $service = new U8DatabaseServerService(['zt_database' => $db]);
- if ($service->error) {
- Log::channel('apiLog')->info('SQLServer连接失败', [
- "database" => $db,
- "error" => $service->error,
- ]);
- $error = false;
- $connections[$db] = null; // 标记连接失败
- } else {
- $connections[$db] = $service;
- }
- }
- if(! $error ) return;
- $time = date("Y-m-d H:i:s");
- $time1 = date("Y-m-d 00:00:00");
- // 分批同步数据
- Record::where("del_time", 2)
- ->select("id","type","database","order_number")
- ->orderBy("id","desc")
- ->chunkById(10, function ($data) use($connections,$time,$time1){
- $data = Collect($data)->map(function ($object) {
- return (array)$object;
- })->toArray();
- $id = [];
- foreach ($data as $record) {
- $database = $record['database'];
- $service = $connections[$database] ?? null;
- if (! $service) continue;
- $type = $record['type'];
- $order_number = $record['order_number'];
- if($type == 1){
- $service->db->table("PO_Pomain")
- ->where("cPOID", $order_number)
- ->update([
- "cVerifier" => "system",
- "iverifystateex" => 1,
- "cState" => 1,
- "cAuditTime" => $time . ".000",
- "cAuditDate" => $time1 . ".000",
- ]);
- }elseif($type == 2){
- $service->db->table("PU_AppVouch")
- ->where("cCode", $order_number)
- ->update([
- "cVerifier" => "system",
- "cAuditTime" => $time . ".000",
- "cAuditDate" => $time1 . ".000",
- ]);
- }else{
- $service->db->table("AP_ApplyPayVouch")
- ->where("cVouchID", $order_number)
- ->update([
- "cCheckMan" => "system",
- "dverifysystime" => $time . ".000",
- "dverifydate" => $time1 . ".000",
- ]);
- }
- $id[] = $record['id'];
- }
- // 更新本地数据
- if(! empty($id)) Record::whereIn("id", $id)->update(['del_time' => 3]);
- });
- } catch (\Throwable $e) {
- Log::channel('apiLog')->info('U8数据更新异常', ['msg' => $e->getMessage()]);
- }
- }
- }
|