82 lines
3.0 KiB
PHP
82 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Beike\Admin\Services;
|
|
|
|
use Beike\Models\Logistics;
|
|
use Beike\Models\Product;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class LogisticsService
|
|
{
|
|
public function create(array $data): Logistics
|
|
{
|
|
$logistics = new Logistics;
|
|
|
|
return $this->createOrUpdate($logistics, $data);
|
|
}
|
|
|
|
public function update(Logistics $logistics, array $data): Logistics
|
|
{
|
|
return $this->createOrUpdate($logistics, $data);
|
|
}
|
|
|
|
public function copy(Product $oldProduct): Product
|
|
{
|
|
$product = new Product;
|
|
$data = $oldProduct->toArray();
|
|
$data['id'] = 0;
|
|
$data['created_at'] = now();
|
|
$data['variables'] = json_encode($data['variables'],JSON_UNESCAPED_UNICODE);
|
|
$data['descriptions'] = $oldProduct->descriptions()->get()->toArray();
|
|
foreach ($data['descriptions'] as $locale => $description) {
|
|
$data['descriptions'][$description['locale']] = $description;
|
|
unset($data['descriptions'][$locale]);
|
|
}
|
|
$data['attributes'] = $oldProduct->attributes()->get()->toArray();
|
|
$data['skus'] = $oldProduct->skus()->get()->toArray();
|
|
$data['numPrices'] = $oldProduct->numPrices()->get()->toArray();
|
|
$data['categories'] = $oldProduct->categories()->get()->toArray();
|
|
$data['relations'] = $oldProduct->relations()->get()->toArray();
|
|
$data['categories'] = array_column($data['categories'],'id');
|
|
$data['relations'] = array_column($data['relations'],'id');
|
|
return $this->createOrUpdate($product, $data);
|
|
}
|
|
|
|
protected function createOrUpdate(Logistics $logistics, array $data): Logistics
|
|
{
|
|
$isUpdating = $logistics->id > 0;
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$data['country_ids'] = (String) ($data['country_ids'] ? implode(',',$data['country_ids']) : '');
|
|
$data['country_id'] = (int) ($data['country_id'] ?? 0);
|
|
$data['throwing_ratio'] = (int) ($data['throwing_ratio'] ?? 0);
|
|
$data['day_min'] = (int) ($data['day_min'] ?? 0);
|
|
$data['day_max'] = (int) ($data['day_max'] ?? 0);
|
|
$data['first_weight'] = (float) ($data['first_weight'] ?? 0);
|
|
$data['first_weight_fee'] = (float) ($data['first_weight_fee'] ?? 0);
|
|
$data['continuation_weight_max'] = (float) ($data['continuation_weight_max'] ?? 0);
|
|
$data['add_weight'] = (float) ($data['add_weight'] ?? 0);
|
|
$data['continuation_weight_fee'] = (float) ($data['continuation_weight_fee'] ?? 0);
|
|
$data['num_fee'] = (float) ($data['num_fee'] ?? 0);
|
|
$data['variables'] = json_decode($data['variables'] ?? '[]');
|
|
|
|
$logistics->fill($data);
|
|
$logistics->updated_at = now();
|
|
$logistics->save();
|
|
|
|
if ($isUpdating) {}
|
|
|
|
|
|
DB::commit();
|
|
|
|
return $logistics;
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|