249 lines
8.8 KiB
PHP
249 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use Log;
|
|
|
|
class Neblogistics extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'neblogistics:sync';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Will send Push notification to IOS and Android';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
|
|
public function handle_new()
|
|
{
|
|
$records = DB::table('neblogistic')
|
|
->where('request_letter', 'like', '%https://d1zmfdp4rdvnc4.cloudfront.net/d1zmfdp4rdvnc4.cloudfront.net%')
|
|
->get();
|
|
|
|
foreach ($records as $record) {
|
|
$updated = str_replace(
|
|
'https://d1zmfdp4rdvnc4.cloudfront.net/d1zmfdp4rdvnc4.cloudfront.net/',
|
|
'https://d1zmfdp4rdvnc4.cloudfront.net/',
|
|
$record->request_letter
|
|
);
|
|
|
|
$this->info("Updated: " . $updated);
|
|
|
|
if ($updated !== $record->request_letter) {
|
|
DB::table('neblogistic')
|
|
->where('id', $record->id)
|
|
->update(['request_letter' => $updated]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
// Fetch 100 records from neblogistic where is_sync = 0 (not yet synced)
|
|
$records = DB::table('neblogistic')
|
|
->where('is_sync', 0)
|
|
->orderBy('id', 'desc')
|
|
->limit(5000)
|
|
->get();
|
|
|
|
if ($records->isEmpty()) {
|
|
$records = DB::table('neblogistic')
|
|
->where('is_sync', 2)
|
|
->where('updated_at', '<=', now()->subHour())
|
|
->orderBy('id', 'desc')
|
|
->limit(5000)
|
|
->get();
|
|
}
|
|
|
|
if ($records->isEmpty()) {
|
|
$this->info('No unsynced records found.');
|
|
return 0;
|
|
}
|
|
|
|
// Update all selected records to is_sync = 2 so they are not picked up by another process
|
|
$ids = $records->pluck('id')->all();
|
|
if (!empty($ids)) {
|
|
DB::table('neblogistic')
|
|
->whereIn('id', $ids)
|
|
->update(['is_sync' => 2, 'updated_at' => now()]);
|
|
}
|
|
|
|
// First, fetch all emp_id_tm mappings in one query
|
|
$empIds = $records->pluck('emp_id')->unique()->all();
|
|
// Fetch TMS mappings (without depot_name) first
|
|
$tmsMappings = DB::table('team3_neblogistics_tms')
|
|
->whereIn('emp_id_tm', $empIds)
|
|
->select('emp_id_tm', 'user_id', 'hierarchy_id', 'depot_id', 'tm_email')
|
|
->get()
|
|
->keyBy('emp_id_tm');
|
|
|
|
// Collect all unique depot_ids from TMS mappings
|
|
$depotIds = $tmsMappings->pluck('depot_id')->filter()->unique()->all();
|
|
|
|
// Fetch depot names for these depot_ids
|
|
$depotMappings = [];
|
|
if (!empty($depotIds)) {
|
|
$depotMappings = DB::table('team3_neblogistics_depot')
|
|
->whereIn('id', $depotIds)
|
|
->select('id', 'name')
|
|
->get()
|
|
->keyBy('id');
|
|
}
|
|
|
|
// Pre-fetch all emp_email and tm_email from records and tms for fallback matching
|
|
$empEmails = $records->pluck('emp_email')->filter()->unique()->all();
|
|
$tmsEmailMappings = DB::table('team3_neblogistics_tms')
|
|
->whereIn('tm_email', $empEmails)
|
|
->select('emp_id_tm', 'user_id', 'hierarchy_id', 'depot_id', 'tm_email')
|
|
->get();
|
|
|
|
foreach ($records as $record) {
|
|
// Log the record being processed
|
|
Log::info('Processing Neblogistic record', ['id' => $record->id, 'doctor_name' => $record->doctor_name]);
|
|
|
|
// Lookup mapping from the pre-fetched mapping
|
|
$tms = $tmsMappings->get($record->emp_id);
|
|
|
|
// If not found by emp_id, try to match using emp_email and tm_email
|
|
if (empty($tms) && !empty($record->emp_email)) {
|
|
$tms = $tmsEmailMappings->first(function ($item) use ($record) {
|
|
return strtolower($item->tm_email) === strtolower($record->emp_email);
|
|
});
|
|
}
|
|
|
|
$requestLetter = $record->request_letter;
|
|
if (!empty($requestLetter) && stripos($requestLetter, 'https') === 0) {
|
|
// If already a full https URL, skip this block
|
|
} elseif (!empty($requestLetter)) {
|
|
// Remove any leading slashes to avoid double slashes in URL
|
|
$requestLetter = ltrim($requestLetter, '/');
|
|
$requestLetterUrl = 'https://d1zmfdp4rdvnc4.cloudfront.net/' . $requestLetter;
|
|
} else {
|
|
// If empty, still set to base URL (with no file)
|
|
$requestLetterUrl = 'https://d1zmfdp4rdvnc4.cloudfront.net/';
|
|
}
|
|
|
|
$requestLetterUrl = str_replace(
|
|
'https://d1zmfdp4rdvnc4.cloudfront.net/d1zmfdp4rdvnc4.cloudfront.net/',
|
|
'https://d1zmfdp4rdvnc4.cloudfront.net/',
|
|
$requestLetterUrl
|
|
);
|
|
$requestLetterUrl = str_replace(
|
|
'https://d1zmfdp4rdvnc4.cloudfront.net/https://did94hhhcgq1d.cloudfront.net/',
|
|
'https://d1zmfdp4rdvnc4.cloudfront.net/',
|
|
$requestLetterUrl
|
|
);
|
|
|
|
// If mapping not found, skip processing and set is_sync = 3
|
|
if (empty($tms)) {
|
|
// Get old data before update for audit trail
|
|
$oldDataArray = json_decode(json_encode($record), true);
|
|
|
|
DB::table('neblogistic')
|
|
->where('id', $record->id)
|
|
->update(['is_sync' => 3, 'updated_at' => now(), 'request_letter' => $requestLetterUrl]);
|
|
|
|
// Log UPDATE operation (console command - no request object)
|
|
\App\Services\NeblogisticAuditService::logUpdate(
|
|
$record->id,
|
|
$oldDataArray,
|
|
['is_sync' => 3, 'updated_at' => now(), 'request_letter' => $requestLetterUrl],
|
|
null,
|
|
null // No request object in console command
|
|
);
|
|
|
|
$this->info("Record ID {$record->id} skipped due to missing mapping (is_sync=3).");
|
|
continue;
|
|
}
|
|
|
|
// Prepare update data with whatever is found
|
|
// Always update request_letter with the full URL
|
|
|
|
// If user_id or hierarchy_id is missing, set is_sync = 3, else is_sync = 1 and update mapping fields
|
|
$oldDataArray = json_decode(json_encode($record), true);
|
|
|
|
if (empty($tms->user_id) || empty($tms->hierarchy_id)) {
|
|
$updateData = [
|
|
'is_sync' => 3,
|
|
'updated_at' => now(),
|
|
'request_letter' => $requestLetterUrl
|
|
];
|
|
|
|
DB::table('neblogistic')
|
|
->where('id', $record->id)
|
|
->update($updateData);
|
|
|
|
// Log UPDATE operation (console command - no request object)
|
|
\App\Services\NeblogisticAuditService::logUpdate(
|
|
$record->id,
|
|
$oldDataArray,
|
|
$updateData,
|
|
null,
|
|
null // No request object in console command
|
|
);
|
|
|
|
$this->info("Record ID {$record->id} skipped due to missing user_id or hierarchy_id (is_sync=3).");
|
|
continue;
|
|
} else {
|
|
$updateData = [
|
|
'is_sync' => 1,
|
|
'updated_at' => now(),
|
|
'user_id' => $tms->user_id,
|
|
'hierarchy_id' => $tms->hierarchy_id,
|
|
'request_letter' => $requestLetterUrl
|
|
];
|
|
if (!empty($tms->depot_id)) {
|
|
$updateData['depot_id'] = $tms->depot_id;
|
|
// Lookup depot name from depotMappings using depot_id
|
|
if (isset($depotMappings[$tms->depot_id])) {
|
|
$updateData['depot_name'] = $depotMappings[$tms->depot_id]->name;
|
|
}
|
|
}
|
|
|
|
DB::table('neblogistic')
|
|
->where('id', $record->id)
|
|
->update($updateData);
|
|
|
|
// Log UPDATE operation (console command - no request object)
|
|
\App\Services\NeblogisticAuditService::logUpdate(
|
|
$record->id,
|
|
$oldDataArray,
|
|
$updateData,
|
|
null,
|
|
null // No request object in console command
|
|
);
|
|
}
|
|
|
|
$this->info("Record ID {$record->id} updated with mapping and marked as synced (is_sync=1).");
|
|
}
|
|
|
|
$this->info('Sync process completed for up to 100 records.');
|
|
}
|
|
}
|
|
|
|
// UPDATE `rapport_serverless`.`neblogistic` SET is_sync = 1, depot_name = (SELECT name from team3_neblogistics_depot where team3_neblogistics_depot.id = neblogistic.depot_id ) WHERE `is_sync` = 2 and depot_id IS NOT NULL and user_id is not null and hierarchy_id is not null and depot_name is null;
|