diff --git a/app/Http/Controllers/Goods/GoodsController.php b/app/Http/Controllers/Goods/GoodsController.php index b50aa0f..9a114dd 100644 --- a/app/Http/Controllers/Goods/GoodsController.php +++ b/app/Http/Controllers/Goods/GoodsController.php @@ -3,15 +3,14 @@ namespace App\Http\Controllers\Goods; use App\Http\Controllers\Controller; +use App\Http\Requests\GoodsSkuRequest; use App\Http\Resources\GoodsResource; -use App\Http\Resources\GoodsSkuResource; use App\Models\Log as LogModel; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; -use Illuminate\Validation\Rule; use App\Models\Goods; -use App\Models\GoodsSku; +use App\Http\Requests\GoodsRequest; class GoodsController extends Controller { @@ -33,28 +32,9 @@ class GoodsController extends Controller public function store(Request $request) { - $goodsRules = [ - 'title' => ['required', 'string', 'max:255'], - 'img_url' => ['required', 'string', 'max:255'], - 'type_id' => ['required', 'integer', 'exists:goods_types,id'], - 'brand_id' => ['integer', 'exists:goods_brands,id'], - 'goods_code' => ['required', 'alpha_dash', 'max:32', 'unique:goods,goods_code'], - ]; - - if ($useGoods = !empty($request->goods_id)) { - $goodsRules = [ - 'goods_id' => ['required', 'integer', 'exists:goods,id'], - ]; - } - $skuRules = [ - 'skus' => ['required', 'array'], - 'skus.*.title' => ['required', 'string', 'max:255'], - 'skus.*.sku_code' => ['required', 'distinct', 'alpha_dash', 'max:32'], - 'skus.*.status' => ['required', 'integer', Rule::in([0, 1, 2])], - 'skus.*.num' => ['required', 'integer'], - 'skus.*.cost' => ['required', 'numeric'], - ]; - $validator = Validator::make($request->all(), array_merge($goodsRules, $skuRules)); + $goodsRules = (new GoodsRequest())->rules(); + $skuRules = (new GoodsSkuRequest())->arrayRules('skus.*.'); + $validator = Validator::make($request->all(), array_merge($goodsRules, ['skus' => ['required', 'array']], $skuRules)); if ($validator->fails()) { $this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); @@ -62,8 +42,8 @@ class GoodsController extends Controller } DB::beginTransaction(); try { - if ($useGoods) { - $goods = Goods::query()->find($request->goods_id); + if (!empty($request->id)) { + $goods = Goods::query()->find($request->id); } else { $goods = new Goods(); $goods->title = $request->title; @@ -79,9 +59,9 @@ class GoodsController extends Controller $goodsSkus[] = $item; } $collection = $goods->skus()->createMany($goodsSkus); - DB::commit(); $this->setAfterUpdate($collection->toArray()); $this->addLog(0, 'add'); + DB::commit(); } catch (\Exception $exception) { DB::rollBack(); $this->res = [ diff --git a/app/Http/Controllers/Goods/GoodsSkusController.php b/app/Http/Controllers/Goods/GoodsSkusController.php index fa8af86..d3344c8 100644 --- a/app/Http/Controllers/Goods/GoodsSkusController.php +++ b/app/Http/Controllers/Goods/GoodsSkusController.php @@ -3,12 +3,15 @@ namespace App\Http\Controllers\Goods; use App\Http\Controllers\Controller; +use App\Http\Requests\GoodsRequest; +use App\Http\Requests\GoodsSkuRequest; use App\Models\Goods; use App\Models\Log as LogModel; use Illuminate\Http\Request; use App\Models\GoodsSku; use App\Http\Resources\GoodsSkuResource; use App\Imports\GoodsSkusImport; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; use Illuminate\Validation\ValidationException; @@ -57,15 +60,39 @@ class GoodsSkusController extends Controller public function update($id, Request $request) { - $data = $request->all(); - $this->validateUpdate($data); - $sku = GoodsSku::query()->find($id); - $this->setBeforeUpdate($sku->toArray()); - $sku->update($data); - $this->setAfterUpdate($sku->toArray()); - $this->addLog($id, 'update'); + $goodsRules = (new GoodsRequest())->arrayRules('goods.'); + $skuRules = (new GoodsSkuRequest())->arrayRules('sku.'); + $validator = Validator::make($request->all(), array_merge($goodsRules, $skuRules)); + if ($validator->fails()) { + $this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); - return new GoodsSkuResource($sku); + return response($this->res, $this->res['httpCode']); + } + DB::beginTransaction(); + try { + // 商品规格更新 + $sku = GoodsSku::query()->find($id); + $this->setBeforeUpdate($sku->toArray()); + $sku->update($request->sku); + $this->setAfterUpdate($sku->toArray()); + $this->addLog($id, 'update'); + // 商品更新 + $goods = Goods::query()->find($sku->goods_id); + $this->setBeforeUpdate($goods->toArray()); + $goods->update($request->goods); + $this->setAfterUpdate($goods->toArray()); + $this->addLog($sku->goods_id, 'update', 'goods'); + DB::commit(); + } catch (\Exception $exception) { + DB::rollBack(); + $this->res = [ + 'httpCode' => 400, + 'errorCode' => 400416, + 'errorMessage' => $exception->getMessage(), + ]; + } + + return response($this->res, $this->res['httpCode']); } public function batchUpdate(Request $request) diff --git a/app/Http/Requests/GoodsRequest.php b/app/Http/Requests/GoodsRequest.php new file mode 100644 index 0000000..e0ecc5f --- /dev/null +++ b/app/Http/Requests/GoodsRequest.php @@ -0,0 +1,47 @@ + ['sometimes', 'required', 'integer', 'exists:goods,id'], + 'title' => ['required', 'string', 'max:255'], + 'img_url' => ['required', 'string', 'max:255'], + 'type_id' => ['required', 'integer', 'exists:goods_types,id'], + 'brand_id' => ['integer', 'exists:goods_brands,id'], + 'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))], + ]; + } + + public function arrayRules($arrayName) + { + $arrayRules = []; + $rules = $this->rules(); + foreach ($rules as $key => $val) { + $arrayRules[$arrayName . $key] = $val; + } + + return $arrayRules; + } +} diff --git a/app/Http/Requests/GoodsSkuPost.php b/app/Http/Requests/GoodsSkuPost.php deleted file mode 100644 index cabbf76..0000000 --- a/app/Http/Requests/GoodsSkuPost.php +++ /dev/null @@ -1,30 +0,0 @@ - ['sometimes', 'required', 'integer', 'exists:goods_skus,id'], + 'goods_id' => ['sometimes', 'required', 'integer', 'exists:goods,id'], + 'title' => ['required', 'string', 'max:255'], + 'sku_code' => ['required', 'distinct', 'alpha_dash', 'max:32'], + 'status' => ['required', 'integer', Rule::in([0, 1, 2])], + 'num' => ['required', 'integer'], + 'cost' => ['required', 'numeric'], + ]; + } + + public function arrayRules($arrayName) + { + $arrayRules = []; + $rules = $this->rules(); + foreach ($rules as $key => $val) { + $arrayRules[$arrayName . $key] = $val; + } + + return $arrayRules; + } +} diff --git a/app/Models/Goods.php b/app/Models/Goods.php index 3498be6..f9f9ff2 100644 --- a/app/Models/Goods.php +++ b/app/Models/Goods.php @@ -15,6 +15,8 @@ class Goods extends Model 'brand_id', ]; + protected $guarded = []; + /** * 多规格 */