81 lines
2.9 KiB
PHP
81 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class PersianizeDemoData extends Command
|
|
{
|
|
protected $signature = 'pos:persianize';
|
|
|
|
protected $description = 'ترجمه محصولات، دستهبندیها، واحدها و تنوعهای دمو به فارسی';
|
|
|
|
public function handle(): int
|
|
{
|
|
$translations = require database_path('seeders/data/persian_demo_translations.php');
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$productCount = $this->updateById('products', 'name', $translations['products']);
|
|
$categoryCount = $this->updateById('categories', 'name', $translations['categories']);
|
|
$unitCount = $this->updateById('units', 'actual_name', $translations['units']);
|
|
$unitShortCount = $this->updateById('units', 'short_name', $translations['unit_short_names']);
|
|
$templateCount = $this->updateById('variation_templates', 'name', $translations['variation_templates']);
|
|
|
|
$productVariationCount = $this->updateByName('product_variations', 'name', $translations['product_variations']);
|
|
$variationValueCount = $this->updateByName('variation_value_templates', 'name', $translations['variation_values']);
|
|
$variationCount = $this->updateByName('variations', 'name', $translations['variation_values']);
|
|
|
|
DB::commit();
|
|
|
|
$this->info("فارسیسازی با موفقیت انجام شد:");
|
|
$this->line(" محصولات: {$productCount}");
|
|
$this->line(" دستهبندیها: {$categoryCount}");
|
|
$this->line(" واحدها: {$unitCount} (+ {$unitShortCount} نام کوتاه)");
|
|
$this->line(" قالب تنوع: {$templateCount}");
|
|
$this->line(" تنوع محصول: {$productVariationCount}");
|
|
$this->line(" مقادیر تنوع: {$variationValueCount}");
|
|
$this->line(" variations: {$variationCount}");
|
|
|
|
return self::SUCCESS;
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
$this->error('خطا در فارسیسازی: '.$e->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
|
|
private function updateById(string $table, string $column, array $map): int
|
|
{
|
|
$count = 0;
|
|
|
|
foreach ($map as $id => $persianName) {
|
|
$updated = DB::table($table)
|
|
->where('id', $id)
|
|
->update([$column => $persianName]);
|
|
|
|
$count += $updated;
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
private function updateByName(string $table, string $column, array $map): int
|
|
{
|
|
$count = 0;
|
|
|
|
foreach ($map as $englishName => $persianName) {
|
|
$updated = DB::table($table)
|
|
->where($column, $englishName)
|
|
->update([$column => $persianName]);
|
|
|
|
$count += $updated;
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
}
|