|
|
|
@ -5,7 +5,9 @@ use App\Common\ProductStatus; |
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
use App\Models\Advertising; |
|
|
|
use App\Models\AgentProduct; |
|
|
|
use App\Models\AgentProductSpec; |
|
|
|
use App\Models\Category; |
|
|
|
use App\Models\DiyForm; |
|
|
|
use App\Models\UserFav; |
|
|
|
use App\Models\Views\AgentProduct as AgentProductView; |
|
|
|
use Illuminate\Support\Facades\DB; |
|
|
|
@ -150,11 +152,13 @@ SQL |
|
|
|
$where = ['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]; |
|
|
|
} |
|
|
|
$agent_product = AgentProduct::with([ |
|
|
|
'diyForm.fields', |
|
|
|
'coupon:tag,agent_product_id', |
|
|
|
'product' => function ($query) { |
|
|
|
$query->select(['id', 'type', 'extends', 'diy_form_id'])->with('diyForm', function ($query) { |
|
|
|
$query->select(['id', 'name'])->with('fields'); |
|
|
|
}); |
|
|
|
$query->select(['id', 'type', 'extends', 'diy_form_id', 'longitude', 'latitude', 'address']) |
|
|
|
->with('diyForm', function ($query) { |
|
|
|
$query->select(['id', 'name'])->with('fields'); |
|
|
|
}); |
|
|
|
}, |
|
|
|
'spec' => function($query) { |
|
|
|
return $query->has('productSpec')->with('productSpec', function ($query) { |
|
|
|
@ -218,32 +222,69 @@ SQL |
|
|
|
return $v; |
|
|
|
}, $specs); |
|
|
|
|
|
|
|
//去年name和date为空的数组
|
|
|
|
$specs = array_filter($specs, fn($v) => isset($v['name'], $v['date'])); |
|
|
|
|
|
|
|
$names = array_column($specs, 'name'); |
|
|
|
$names = array_values(array_unique($names)); |
|
|
|
|
|
|
|
$result = []; |
|
|
|
foreach ($names as $name) { |
|
|
|
$list = array_filter($specs, fn($v) => $v['name'] == $name); |
|
|
|
$result[] = [ |
|
|
|
'name' => $name, |
|
|
|
'date_start' => min(array_column($list, 'date')), |
|
|
|
'date_end' => max(array_column($list, 'date')), |
|
|
|
'original_price' => min(array_column($list, 'original_price')), |
|
|
|
'price' => min(array_column($list, 'price')), |
|
|
|
'list' => array_values($list), |
|
|
|
]; |
|
|
|
} |
|
|
|
unset($agent_product->spec); |
|
|
|
$agent_product->spec = $result; |
|
|
|
unset($agent_product->spec, $agent_product->product->longitude, $agent_product->product->latitude, $agent_product->product->address); //unset后才能重新赋值
|
|
|
|
$agent_product->spec = $this->spec_format($specs); |
|
|
|
|
|
|
|
//位置信息
|
|
|
|
$agent_product->longitude = $agent_product->product->longitude ?? 0; |
|
|
|
$agent_product->latitude = $agent_product->product->latitude ?? 0; |
|
|
|
$agent_product->address = $agent_product->product->address ?? ''; |
|
|
|
} |
|
|
|
|
|
|
|
//自营产品
|
|
|
|
if ($agent_product->product_id == 0) { |
|
|
|
//自营产品规格
|
|
|
|
$specs = AgentProductSpec::orderBy('date', 'asc') |
|
|
|
->where([ |
|
|
|
['agent_product_id', '=', $agent_product->id], |
|
|
|
['date', '>=', date('Y-m-d')], |
|
|
|
]) |
|
|
|
->get(['id', 'name', 'date', 'stock', 'original_price', 'price'])->toArray(); |
|
|
|
unset($agent_product->spec); //unset后才能重新赋值
|
|
|
|
$agent_product->spec = $this->spec_format($specs); |
|
|
|
|
|
|
|
//自营产品product
|
|
|
|
$product = [ |
|
|
|
'product_id' => 0, |
|
|
|
'type' => $agent_product->tpl_type, |
|
|
|
'extends' => $agent_product->extends, |
|
|
|
'diy_form' => $agent_product->diyForm ? [ |
|
|
|
'id' => $agent_product->diyForm->id ?? 0, |
|
|
|
'name' => $agent_product->diyForm->name ?? '', |
|
|
|
'fields' => $agent_product->diyForm->fields ?? null, |
|
|
|
] : null, |
|
|
|
]; |
|
|
|
unset($agent_product->product, $agent_product->tpl_type, $agent_product->extends, $agent_product->diyForm); |
|
|
|
$agent_product->product = $product; |
|
|
|
} |
|
|
|
|
|
|
|
unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at); |
|
|
|
return $this->success($agent_product); |
|
|
|
} |
|
|
|
|
|
|
|
private function spec_format(array $specs) |
|
|
|
{ |
|
|
|
//去掉name和date为空的数组
|
|
|
|
$specs = array_filter($specs, fn($v) => isset($v['name'], $v['date'])); |
|
|
|
|
|
|
|
$names = array_column($specs, 'name'); |
|
|
|
$names = array_values(array_unique($names)); |
|
|
|
|
|
|
|
$result = []; |
|
|
|
foreach ($names as $name) { |
|
|
|
$list = array_filter($specs, fn($v) => $v['name'] == $name); |
|
|
|
$result[] = [ |
|
|
|
'name' => $name, |
|
|
|
'date_start' => min(array_column($list, 'date')), |
|
|
|
'date_end' => max(array_column($list, 'date')), |
|
|
|
'original_price' => min(array_column($list, 'original_price')), |
|
|
|
'price' => min(array_column($list, 'price')), |
|
|
|
'list' => array_values($list), |
|
|
|
]; |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
// 猜你喜欢
|
|
|
|
public function guessLike() |
|
|
|
{ |
|
|
|
|