diff --git a/app/Http/Controllers/Goods/GoodsController.php b/app/Http/Controllers/Goods/GoodsController.php index a29b695..9d95c27 100644 --- a/app/Http/Controllers/Goods/GoodsController.php +++ b/app/Http/Controllers/Goods/GoodsController.php @@ -5,10 +5,13 @@ namespace App\Http\Controllers\Goods; use App\Http\Controllers\Controller; use App\Http\Requests\GoodsSkuRequest; use App\Http\Resources\GoodsResource; +use App\Models\GoodsType; use App\Models\Log as LogModel; +use App\Services\Good\GoodService; use App\Utils\DateTimeUtils; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; use App\Models\Goods; use App\Http\Requests\GoodsRequest; @@ -42,28 +45,22 @@ class GoodsController extends Controller return response($this->res, $this->res['httpCode']); } + DB::beginTransaction(); try { - if (!empty($request->goods_id)) { - $goods = Goods::query()->find($request->goods_id); - } else { - $goods = new Goods(); - $goods->title = $request->title; - $goods->img_url = $request->img_url; - $goods->type_id = $request->type_id; - $goods->brand_id = $request->brand_id ?? 0; - $goods->goods_code = $request->goods_code; - $goods->save(); - } + $goodService = new GoodService(); + $goods = $goodService->saveDefaultGoodsByGoodType($request->type_id); $goodsSkus = []; foreach ($request->skus as $item) { $item['goods_id'] = $goods->id; - $item['stock'] = $item['num']; + $item['stock'] = $item['num'] ?? 0; $item['reference_price'] = $item['cost'] * 1.5; - $item['external_sku_id'] = $goods->goods_code . '_' . $item['sku_code']; + $item['sku_code'] = !empty($item['sku_code']) ? $item['sku_code'] : $goodService->getRandomCode(); + $item['external_sku_id'] = $goods->goods_code . '_' .$item['sku_code']; $item['name'] = $goods->title . $item['title']; $goodsSkus[] = $item; } + $collection = $goods->skus()->createMany($goodsSkus)->toArray(); $this->setAfterUpdateForLog($collection); $this->addLog(0, 'add'); @@ -89,6 +86,7 @@ class GoodsController extends Controller return response($this->res, $this->res['httpCode']); } + public function download() { $file = resource_path('templates/goods_skus_import.xlsx'); diff --git a/app/Http/Controllers/Goods/GoodsTypesController.php b/app/Http/Controllers/Goods/GoodsTypesController.php index 2c620dd..fc51b71 100644 --- a/app/Http/Controllers/Goods/GoodsTypesController.php +++ b/app/Http/Controllers/Goods/GoodsTypesController.php @@ -24,7 +24,11 @@ class GoodsTypesController extends Controller public function index(Request $request) { - $goodsTypes = GoodsType::query()->paginate($request->get('per_page')); + $build = GoodsType::query(); + if(!empty($request->level)){ + $build->where("level","=",$request->level); + } + $goodsTypes = $build->paginate($request->get('per_page')); return GoodsTypeResource::collection($goodsTypes); } diff --git a/app/Http/Requests/GoodsRequest.php b/app/Http/Requests/GoodsRequest.php index dc865e6..9f7676c 100644 --- a/app/Http/Requests/GoodsRequest.php +++ b/app/Http/Requests/GoodsRequest.php @@ -25,10 +25,7 @@ class GoodsRequest extends FormRequest public function rules() { return [ - 'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'], - 'title' => ['required', 'string', 'max:191'], 'type_id' => ['required', 'integer', 'exists:goods_types,id'], - 'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))], ]; } diff --git a/app/Http/Requests/GoodsSkuRequest.php b/app/Http/Requests/GoodsSkuRequest.php index 9b4d9fc..d8f7409 100644 --- a/app/Http/Requests/GoodsSkuRequest.php +++ b/app/Http/Requests/GoodsSkuRequest.php @@ -28,7 +28,6 @@ class GoodsSkuRequest extends FormRequest 'id' => ['sometimes', 'required', 'integer', 'exists:goods_skus,id'], 'goods_id' => ['sometimes', 'required', 'integer', 'exists:goods,id'], 'title' => ['sometimes', 'required', 'string', 'max:191'], - 'sku_code' => ['sometimes', 'required', 'distinct', 'alpha_dash', 'max:32'], 'status' => ['sometimes', 'required', 'integer', Rule::in([0, 1, 2])], 'num' => ['sometimes', 'required', 'integer'], 'cost' => ['sometimes', 'required', 'numeric'], diff --git a/app/Models/GoodsType.php b/app/Models/GoodsType.php index 53c496b..3a20090 100644 --- a/app/Models/GoodsType.php +++ b/app/Models/GoodsType.php @@ -10,4 +10,9 @@ class GoodsType extends Model * @var array */ protected $hidden = ['deleted_at']; + + public function parent_type() + { + return $this->belongsTo(GoodsType::class, 'id', 'parent_id'); + } } diff --git a/app/Services/Good/GoodService.php b/app/Services/Good/GoodService.php new file mode 100644 index 0000000..590a7c0 --- /dev/null +++ b/app/Services/Good/GoodService.php @@ -0,0 +1,74 @@ +with("parent_type")->where('id', "=", $typeId) + ->first()->toArray(); + $params['type_id'] = $typeId; + $params['goods_code'] = static::getChCode($goodsType['id']); + if (!empty($goodsType['parent_type']['id'])) { + $params['goods_code'] .= "|" . static::getChCode($goodsType['parent_type']['id']); + } + $saveData = $params; + $saveData['title'] = $goodsType['name'] . " " . $goodsType['parent_type']['name'] ?? ''; + return Goods::query()->firstOrCreate($params, $saveData); + } + + public function getRandomCode() + { + $time = time(); + return substr($time, -7) . static::randomString(3); + } + + public static function randomString($len = 32) + { + $chars = [ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", + "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", + "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", + "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", + "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", + "3", "4", "5", "6", "7", "8", "9" + ]; + + // 将数组打乱 + shuffle($chars); + + $charsLen = count($chars) - 1; + $output = ""; + for ($i = 0; $i < $len; $i++) { + $output .= $chars[mt_rand(0, $charsLen)]; + } + + return $output; + } + + public function getChCode($intValue) + { + $ch = range('A', 'Z'); + $result = ''; + while ($intValue) { + $result = $ch[$intValue % 26] . $result; + $intValue = intval($intValue / 26); + } + return $result; + } + +}