鲜花2.0-接口测试bug修复
This commit is contained in:
parent
235388bbaa
commit
dda4b5eb2a
@ -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');
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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'))],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -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'],
|
||||
|
||||
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
74
app/Services/Good/GoodService.php
Normal file
74
app/Services/Good/GoodService.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Good;
|
||||
|
||||
use App\Events\BatchStockUpdateEvent;
|
||||
use App\Models\BusinessOrderItem;
|
||||
use App\Models\CombinationGood;
|
||||
use App\Models\DailyStockRecord;
|
||||
use App\Models\Goods;
|
||||
use App\Models\GoodsSku;
|
||||
use App\Models\GoodsType;
|
||||
use App\Utils\DateTimeUtils;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Predis\Command\Redis\SUBSCRIBE;
|
||||
|
||||
class GoodService
|
||||
{
|
||||
public function saveDefaultGoodsByGoodType($typeId)
|
||||
{
|
||||
$goodsType = GoodsType::query()->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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user