Browse Source

商品管理

master
Lemon 5 years ago
parent
commit
6b49f0eade
  1. 17
      app/Controller/v3/CategoryController.php
  2. 28
      app/Controller/v3/GoodsController.php
  3. 20
      app/Model/v3/Goods.php
  4. 17
      app/Request/v3/GoodsEditorRequest.php
  5. 17
      app/Service/v3/Implementations/CategoryService.php
  6. 37
      app/Service/v3/Implementations/GoodsService.php
  7. 2
      app/Service/v3/Interfaces/CategoryServiceInterface.php
  8. 3
      config/routes.php

17
app/Controller/v3/CategoryController.php

@ -60,4 +60,21 @@ class CategoryController extends BaseController
return $this->success(['category' => $category]); return $this->success(['category' => $category]);
} }
public function first()
{
return $this->success($this->categoryService->getByParentId(0));
}
public function secend()
{
$parentId = $this->request->input('parent_id');
return $this->success($this->categoryService->getByParentId($parentId));
}
public function third()
{
$categoryId = $this->request->input('category_id');
return $this->success($this->categoryService->getByParentId($categoryId));
}
} }

28
app/Controller/v3/GoodsController.php

@ -3,7 +3,10 @@
namespace App\Controller\v3; namespace App\Controller\v3;
use App\Controller\BaseController; use App\Controller\BaseController;
use App\Model\v3\Category;
use App\Model\v3\GoodsCategory;
use App\Request\v3\GoodsEditorRequest; use App\Request\v3\GoodsEditorRequest;
use App\Service\v3\Interfaces\CategoryServiceInterface;
use App\Service\v3\Interfaces\ShopCartServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface;
use App\Service\v3\Implementations\GoodsActivityService; use App\Service\v3\Implementations\GoodsActivityService;
use App\Service\v3\Interfaces\CollectStoreServiceInterface; use App\Service\v3\Interfaces\CollectStoreServiceInterface;
@ -38,6 +41,12 @@ class GoodsController extends BaseController
*/ */
protected $shopCartService; protected $shopCartService;
/**
* @Inject
* @var CategoryServiceInterface
*/
protected $categoryService;
public function detail(GoodsRequest $request) public function detail(GoodsRequest $request)
{ {
$params = $this->request->all(); $params = $this->request->all();
@ -88,7 +97,22 @@ class GoodsController extends BaseController
public function info() public function info()
{ {
$goodsId = $this->request->input('goods_id',0); $goodsId = $this->request->input('goods_id',0);
$res = $this->goodsService->info($goodsId);
return $this->success($res);
$goods = $this->goodsService->info($goodsId);
$goods['secend'] = Category::find($goods['category_id']);
$goods['first'] = Category::find($goods['secend']['parent_id']);
if(!empty($goods['goods_category_id'])){
$goods['third'] = GoodsCategory::find($goods['goods_category_id']);
}else{
$goods['third'] = null;
}
$first = $this->categoryService->getByParentId(0);
$secend = $this->categoryService->getByParentId($goods['category_id']);
$third = $this->categoryService->getThird($goods['category_id']);
return $this->success([
'goods' => $goods,
'first' => $first,
'secend' => $secend,
'third' => $third
]);
} }
} }

20
app/Model/v3/Goods.php

@ -45,7 +45,24 @@ class Goods extends Model
* *
* @var array * @var array
*/ */
protected $fillable = [];
protected $fillable = [
'id',
'market_id',
'store_id',
'name',
'category_id',
'goods_unit',
'price',
'original_price',
'inventory',
'restrict_num',
'start_num',
'spec',
'tags',
'remark',
'on_sale',
'is_infinite'
];
/** /**
* The attributes that should be cast to native types. * The attributes that should be cast to native types.
* *
@ -145,5 +162,4 @@ class Goods extends Model
{ {
return $this->morphMany(ShoppingCart::class, 'goods'); return $this->morphMany(ShoppingCart::class, 'goods');
} }
} }

17
app/Request/v3/GoodsEditorRequest.php

@ -15,7 +15,22 @@ class GoodsEditorRequest extends BaseFormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'id' => 'required|nonempty|integer',
'id' => 'required',
'market_id' => 'required|nonempty|integer',
'store_id' => 'required|nonempty|integer',
'name' => 'required|nonempty',
'category_id' => 'required|nonempty|integer',
'goods_unit' => 'required|nonempty',
'price' => 'required|nonempty',
'original_price' => 'required|nonempty',
'is_infinite' => 'required',
'inventory' => 'required',
'restrict_num' => 'required',
'start_num' => 'required',
'spec' => 'required|nonempty',
'tags' => 'required|nonempty',
'remark' => 'required|nonempty',
'on_sale' => 'required',
]; ];
} }

17
app/Service/v3/Implementations/CategoryService.php

@ -6,6 +6,7 @@ use App\Constants\v3\OssThumbnail;
use App\Constants\v3\SsdbKeys; use App\Constants\v3\SsdbKeys;
use App\Model\v3\Category; use App\Model\v3\Category;
use App\Model\v3\Goods; use App\Model\v3\Goods;
use App\Model\v3\GoodsCategory;
use App\Service\v3\Interfaces\AttachmentServiceInterface; use App\Service\v3\Interfaces\AttachmentServiceInterface;
use App\Service\v3\Interfaces\CategoryServiceInterface; use App\Service\v3\Interfaces\CategoryServiceInterface;
use App\TaskWorker\SSDBTask; use App\TaskWorker\SSDBTask;
@ -120,4 +121,20 @@ class CategoryService implements CategoryServiceInterface
return $categories; return $categories;
} }
public function getByParentId($parentId)
{
$categories = Category::query()
->where('parent_id',$parentId)
->get();
return $categories;
}
public function getThird($categoryId)
{
$categories = GoodsCategory::query()
->where('category_id',$categoryId)
->get();
return $categories;
}
} }

37
app/Service/v3/Implementations/GoodsService.php

@ -78,20 +78,29 @@ class GoodsService implements GoodsServiceInterface
public function update($params) public function update($params)
{ {
$goods = Goods::query()->withoutGlobalScope('normal')->where('id',$params['id'])->first();
if (empty($goods)) {
throw new ErrorCodeException( ErrorCode::GOODS_NOT_EXISTS);
}
if(isset($params['name']) && !empty($params['name'])) {
$goods->name = $params['name'];
}
if(isset($params['price']) && !empty($params['price'])) {
$goods->price = $params['price'];
}
if(isset($params['on_sale'])) {
$goods->on_sale = $params['on_sale'];
}
return $goods->save();
$goods = Goods::query()->where(
[
'id' => $params['id'],
'market_id' => $params['market_id'],
'store_id' => $params['store_id']
])
->update(
[
'name' => $params['name'],
'category_id' => $params['market_id'],
'goods_unit' => $params['goods_unit'],
'price' => $params['price'],
'original_price' => $params['original_price'],
'inventory' => $params['inventory'],
'restrict_num' => $params['restrict_num'],
'start_num' => $params['start_num'],
//'spec' => $params['spec'],
//'tags' => $params['tags'],
'remark' => $params['remark'],
'on_sale' => $params['on_sale'],
'is_infinite' => $params['is_infinite'],
]);
return $goods;
} }
public function info($goodsId) public function info($goodsId)

2
app/Service/v3/Interfaces/CategoryServiceInterface.php

@ -12,4 +12,6 @@ interface CategoryServiceInterface
public function allForStore($storeId); public function allForStore($storeId);
public function allForAppletIndex(); public function allForAppletIndex();
public function allForStoreIncludeOff($storeId); public function allForStoreIncludeOff($storeId);
public function getByParentId($parentId);
public function getThird($categoryId);
} }

3
config/routes.php

@ -157,6 +157,9 @@ Router::addGroup('/v3/', function () {
Router::post('store/getList', 'App\Controller\v3\StoreController@getList'); Router::post('store/getList', 'App\Controller\v3\StoreController@getList');
Router::post('goods/info', 'App\Controller\v3\GoodsController@info'); Router::post('goods/info', 'App\Controller\v3\GoodsController@info');
Router::post('store/getCategory', 'App\Controller\v3\StoreController@getCategory'); Router::post('store/getCategory', 'App\Controller\v3\StoreController@getCategory');
Router::post('category/first', 'App\Controller\v3\CategoryController@first');
Router::post('category/secend', 'App\Controller\v3\CategoryController@secend');
Router::post('category/third', 'App\Controller\v3\CategoryController@third');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]);
// 微信支付回调 // 微信支付回调

Loading…
Cancel
Save