feat: #20220803 商品更新

This commit is contained in:
赵世界 2022-08-03 16:41:15 +08:00
parent ada42eaae6
commit 1ee5c5eb85
6 changed files with 140 additions and 66 deletions

View File

@ -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 = [

View File

@ -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)

View File

@ -0,0 +1,47 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class GoodsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => ['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;
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class GoodsSkuPost extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class GoodsSkuRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => ['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;
}
}

View File

@ -15,6 +15,8 @@ class Goods extends Model
'brand_id',
];
protected $guarded = [];
/**
* 多规格
*/