|
@@ -3,88 +3,75 @@
|
|
|
namespace App\Modules\Admin\Services;
|
|
|
|
|
|
use App\Base\BaseService;
|
|
|
-use App\Exceptions\ClientException;
|
|
|
-use App\Models\Goods\Spec;
|
|
|
+use App\Models\Goods\Goods;
|
|
|
+use App\Models\Goods\GoodsCategoryMap;
|
|
|
+use App\Models\Goods\GoodsSku;
|
|
|
use App\Models\Goods\SpecAttr;
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Support\Arr;
|
|
|
-use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class GoodsService extends BaseService
|
|
|
{
|
|
|
- public function specPaginate($params)
|
|
|
+ public function paginate($params)
|
|
|
{
|
|
|
- $p = Spec::query()->when($params['name'], function (Builder $query) use ($params) {
|
|
|
- return $query->where("name", 'like', "%{$params['name']}%");
|
|
|
+ $p = Goods::query()->when($params['status'], function (Builder $query) use ($params) {
|
|
|
+ return $query->where("status", $params['status']);
|
|
|
+ })->when($params['name'], function (Builder $query) use ($params) {
|
|
|
+ return $query->where("name", "like", "%{$params['name']}%");
|
|
|
})->orderByDesc("id")->paginate($params['page_size']);
|
|
|
|
|
|
return [
|
|
|
"total" => $p->total(),
|
|
|
"page_total" => $p->lastPage(),
|
|
|
- "list" => array_map(function (Spec $u) {
|
|
|
- return $u->format(Spec::FORMAT_ATTR);
|
|
|
+ "list" => array_map(function (Goods $model) {
|
|
|
+
|
|
|
+ return [
|
|
|
+ "id" => $model->id,
|
|
|
+ "name" => $model->name,
|
|
|
+ "thumb" => Arr::first($model->image_list),
|
|
|
+ "weight" => $model->weight,
|
|
|
+ "status" => $model->status,
|
|
|
+ "created_at" => $model->created_at->format("Y-m-d H:i:s"),
|
|
|
+ ];
|
|
|
}, $p->items()),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- public function specStore($params)
|
|
|
+ public function info($params)
|
|
|
{
|
|
|
- $id = $params['id'] ?? 0;
|
|
|
- if ($id) {
|
|
|
- $spec = Spec::find($id);
|
|
|
- } else {
|
|
|
- $spec = new Spec();
|
|
|
- }
|
|
|
- $spec->name = Arr::get($params, 'name');
|
|
|
- $spec->index_weight = Arr::get($params, 'index_weight');
|
|
|
- $spec->category_weight = Arr::get($params, 'category_weight');
|
|
|
- $spec->search_weight = Arr::get($params, 'search_weight');
|
|
|
- $spec->is_custom = (int)Arr::get($params, 'is_custom');
|
|
|
-
|
|
|
- DB::transaction(function () use ($params, $spec) {
|
|
|
- $spec->save();
|
|
|
- $attr = Arr::get($params, "attr", []);
|
|
|
- $holdIdArr = Arr::pluck($attr, "id");
|
|
|
- SpecAttr::query()->where("spec_id", $spec->id)->whereNotIn("id", $holdIdArr)->delete();
|
|
|
-
|
|
|
- foreach ($attr as $k => $v) {
|
|
|
- $id = Arr::get($v, "id");
|
|
|
- if ($id) {
|
|
|
- $m = SpecAttr::whereSpecId($spec->id)->find($id);
|
|
|
- if (is_null($m)) {
|
|
|
- logger()->error("无法保存这个属性值:" . $v['name'] ?? "");
|
|
|
- throw new ClientException("无法保存这个属性值:" . $v['name'] ?? "");
|
|
|
- }
|
|
|
- } else {
|
|
|
- $m = new SpecAttr();
|
|
|
- }
|
|
|
- $m->spec_id = $spec->id;
|
|
|
- $m->name = $v['name'] ?? "";
|
|
|
- $m->save();
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
+ $id = $params['id'];
|
|
|
+ $model = Goods::findOrFail($id);
|
|
|
|
|
|
- public function specInfo($data)
|
|
|
- {
|
|
|
- $id = $data['id'];
|
|
|
- $spec = Spec::findOrFail($id);
|
|
|
- return $spec->format(Spec::FORMAT_ATTR);
|
|
|
- }
|
|
|
-
|
|
|
- public function specDelete($data)
|
|
|
- {
|
|
|
- $id = $data['id'] ?? 0;
|
|
|
-
|
|
|
+ $return = [
|
|
|
+ "id" => $model->id,
|
|
|
+ "image_list" => $model->image_list,
|
|
|
+ "category" => $model->map->map(function (GoodsCategoryMap $map) {
|
|
|
+ return [
|
|
|
+ "id" => $map->category->id,
|
|
|
+ "is_public" => $map->is_public,
|
|
|
+ ];
|
|
|
+ }),
|
|
|
+ "spec" => $model->spec,
|
|
|
+ "sku" => $model->sku->map(function (GoodsSku $sku) {
|
|
|
+ return [
|
|
|
+ "id" => $sku->id,
|
|
|
+ "sn" => $sku->sn,
|
|
|
+ "3d_url" => $sku->url_3d,
|
|
|
+ "is_use" => $sku->is_use,
|
|
|
+ "spec_attr" => array_map(function ($specAttrId) {
|
|
|
+ return [
|
|
|
+ "id" => $specAttrId,
|
|
|
+ "name" => SpecAttr::where("id", $specAttrId)->value("name"),
|
|
|
+ ];
|
|
|
+ }, $sku->spec_attr_list),
|
|
|
+ ];
|
|
|
+ }),
|
|
|
+ "weight" => $model->weight,
|
|
|
+ "desc_html" => $model->desc_html,
|
|
|
+ "spec_attr_html" => $model->spec_attr_html,
|
|
|
+ "faq_html" => $model->faq_html,
|
|
|
+ ];
|
|
|
|
|
|
- $spec = Spec::find($id);
|
|
|
- if ($spec) {
|
|
|
- SpecAttr::where("spec_id", $spec->id)->delete();
|
|
|
- $spec->delete();
|
|
|
- }
|
|
|
- return true;
|
|
|
+ return $return;
|
|
|
}
|
|
|
}
|