Browse Source

Merge branch 'goods_supervise'

# Conflicts:
#	app/Service/v3/Implementations/GoodsService.php
master
Lemon 5 years ago
parent
commit
359a2c8b18
  1. 10
      app/Constants/v3/LogLabel.php
  2. 17
      app/Controller/v3/CategoryController.php
  3. 84
      app/Controller/v3/GoodsController.php
  4. 29
      app/Model/v3/Goods.php
  5. 10
      app/Request/v3/GoodsEditorRequest.php
  6. 17
      app/Service/v3/Implementations/CategoryService.php
  7. 127
      app/Service/v3/Implementations/GoodsService.php
  8. 2
      app/Service/v3/Interfaces/CategoryServiceInterface.php
  9. 2
      app/Service/v3/Interfaces/GoodsServiceInterface.php
  10. 6
      config/routes.php

10
app/Constants/v3/LogLabel.php

@ -112,4 +112,14 @@ class LogLabel extends AbstractConstants
*/
const ORDER_AUTO_CANCEL_FAIL_LOG = 'order_auto_cancel_fail_log';
/**
* @Message("创建商品记录")
*/
const CREATE_GOODS_LOG = 'create_goods_log';
/**
* @Message("更新商品记录")
*/
const UPDATE_GOODS_LOG = 'update_goods_log';
}

17
app/Controller/v3/CategoryController.php

@ -60,4 +60,21 @@ class CategoryController extends BaseController
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->getThird($categoryId));
}
}

84
app/Controller/v3/GoodsController.php

@ -3,7 +3,10 @@
namespace App\Controller\v3;
use App\Controller\BaseController;
use App\Model\v3\Category;
use App\Model\v3\GoodsCategory;
use App\Request\v3\GoodsEditorRequest;
use App\Service\v3\Interfaces\CategoryServiceInterface;
use App\Service\v3\Interfaces\ShopCartServiceInterface;
use App\Service\v3\Implementations\GoodsActivityService;
use App\Service\v3\Interfaces\CollectStoreServiceInterface;
@ -38,6 +41,12 @@ class GoodsController extends BaseController
*/
protected $shopCartService;
/**
* @Inject
* @var CategoryServiceInterface
*/
protected $categoryService;
public function detail(GoodsRequest $request)
{
$params = $this->request->all();
@ -81,14 +90,85 @@ class GoodsController extends BaseController
public function update(GoodsEditorRequest $request)
{
$res = $this->goodsService->update($this->request->all());
$data['id'] = $this->request->input('id',0);
$data['market_id'] = $this->request->input('market_id',0);
$data['store_id'] = $this->request->input('store_id',0);
$data['name'] = $this->request->input('name','');
$data['category_id'] = $this->request->input('category_id',0);
$data['goods_category_id'] = $this->request->input('goods_category_id',0);
$data['goods_unit'] = $this->request->input('goods_unit','');
$data['price'] = $this->request->input('price',0);
$data['original_price'] = $this->request->input('original_price',0);
$data['inventory'] = $this->request->input('inventory',0);
$data['restrict_num'] = $this->request->input('restrict_num',0);
$data['start_num'] = $this->request->input('start_num',0);
$data['spec'] = $this->request->input('spec');
$data['tags'] = $this->request->input('tags');
$data['remark'] = $this->request->input('remark','');
$data['on_sale'] = $this->request->input('on_sale',0);
$data['is_infinite'] = $this->request->input('is_infinite',0);
$data['user_id'] = $this->request->input('user_id',0);
$res = $this->goodsService->update($data);
return $this->success($res);
}
public function info()
{
$goodsId = $this->request->input('goods_id',0);
$res = $this->goodsService->info($goodsId);
$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['first']['id']);
$third = $this->categoryService->getThird($goods['category_id']);
$tags = $this->goodsService->getTags();
return $this->success([
'goods' => $goods,
'first' => $first,
'secend' => $secend,
'third' => $third,
'tags' => $tags
]);
}
public function create(GoodsEditorRequest $request)
{
$data['id'] = $this->request->input('id',0);
$data['market_id'] = $this->request->input('market_id',0);
$data['store_id'] = $this->request->input('store_id',0);
$data['name'] = $this->request->input('name','');
$data['category_id'] = $this->request->input('category_id',0);
$data['goods_category_id'] = $this->request->input('goods_category_id',0);
$data['goods_unit'] = $this->request->input('goods_unit','');
$data['price'] = $this->request->input('price',0);
$data['original_price'] = $this->request->input('original_price',0);
$data['inventory'] = $this->request->input('inventory',0);
$data['restrict_num'] = $this->request->input('restrict_num',0);
$data['start_num'] = $this->request->input('start_num',0);
$data['spec'] = $this->request->input('spec');
$data['tags'] = $this->request->input('tags');
$data['remark'] = $this->request->input('remark','');
$data['on_sale'] = $this->request->input('on_sale',0);
$data['is_infinite'] = $this->request->input('is_infinite',0);
$data['user_id'] = $this->request->input('user_id',0);
$res = $this->goodsService->create($data);
return $this->success($res);
}
public function getTags()
{
$tags = $this->goodsService->getTags();
return $this->success(['tags' => $tags]);
}
public function updateOnSale()
{
$goodsId = $this->request->input('id',0);
return $this->success($this->goodsService->updateOnSale($goodsId));
}
}

29
app/Model/v3/Goods.php

@ -45,7 +45,25 @@ class Goods extends Model
*
* @var array
*/
protected $fillable = [];
protected $fillable = [
'id',
'market_id',
'store_id',
'name',
'category_id',
'goods_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.
*
@ -146,4 +164,13 @@ class Goods extends Model
return $this->morphMany(ShoppingCart::class, 'goods');
}
public function setTagsAttribute($value)
{
$this->attributes['tags'] = json_encode(json_decode($value, true));
}
public function setSpecAttribute($value)
{
$this->attributes['spec'] = json_encode(json_decode($value, true));
}
}

10
app/Request/v3/GoodsEditorRequest.php

@ -15,7 +15,15 @@ class GoodsEditorRequest extends BaseFormRequest
public function rules(): array
{
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',
'price' => 'required|nonempty',
'is_infinite' => 'required',
'inventory' => 'required',
'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\Model\v3\Category;
use App\Model\v3\Goods;
use App\Model\v3\GoodsCategory;
use App\Service\v3\Interfaces\AttachmentServiceInterface;
use App\Service\v3\Interfaces\CategoryServiceInterface;
use App\TaskWorker\SSDBTask;
@ -120,4 +121,20 @@ class CategoryService implements CategoryServiceInterface
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;
}
}

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

@ -15,13 +15,11 @@ use Hyperf\Di\Annotation\Inject;
class GoodsService implements GoodsServiceInterface
{
/**
* @Inject
* @var Log
*/
protected $log;
public function do($goodsId)
{
@ -93,22 +91,102 @@ class GoodsService implements GoodsServiceInterface
return Goods::query()->withoutGlobalScope('normal')->where(['store_id' => $storeId,'category_id' => $typeId])->orderByDesc('on_sale')->orderByDesc('created_at')->get()->toArray();
}
public function create($params)
{
$data =
[
'market_id' => $params['market_id'],
'store_id' => $params['store_id'],
'name' => $params['name'],
'category_id' => $params['category_id'],
'goods_category_id' => $params['goods_category_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']
];
$this->log->event(LogLabel::CREATE_GOODS_LOG, ['logData' => json_encode(
[
'id' => $params['id'],
'market_id' => $params['market_id'],
'store_id' => $params['store_id'],
'name' => $params['name'],
'category_id' => $params['category_id'],
'goods_category_id' => $params['goods_category_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'],
'user_id' => $params['user_id']
]
)]);
return Goods::create($data);
}
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()->withoutGlobalScope('normal')
->where(
[
'id' => $params['id'],
'market_id' => $params['market_id'],
'store_id' => $params['store_id']
]
)
->update(
[
'name' => $params['name'],
'category_id' => $params['category_id'],
'goods_category_id' => $params['goods_category_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']
]);
$this->log->event(LogLabel::UPDATE_GOODS_LOG, ['logData' => json_encode(
[
'id' => $params['id'],
'market_id' => $params['market_id'],
'store_id' => $params['store_id'],
'name' => $params['name'],
'category_id' => $params['category_id'],
'goods_category_id' => $params['goods_category_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'],
'user_id' => $params['user_id']
]
)]);
return $goods;
}
public function info($goodsId)
@ -116,4 +194,21 @@ class GoodsService implements GoodsServiceInterface
$res = Goods::query()->withoutGlobalScope('normal')->where('id',$goodsId)->first();
return $res;
}
public function getTags()
{
return [
'新品',
'热销',
'新鲜'
];
}
public function updateOnSale($goodsId)
{
$goods = Goods::query()->withoutGlobalScope('normal')->find($goodsId);
$onSale = $goods->on_sale + 1;
$goods->on_sale = $onSale % 2;
return $goods->save();
}
}

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

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

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

@ -14,4 +14,6 @@ interface GoodsServiceInterface
public function getByType($storeId,$typeId);
public function update($params);
public function info($goodsId);
public function getTags();
public function updateOnSale($goodsId);
}

6
config/routes.php

@ -96,6 +96,7 @@ Router::addGroup('/v3/', function () {
Router::post('orderOffline/review', 'App\Controller\v3\OrderOfflineController@review');
Router::post('paramsToken/generate', 'App\Controller\v3\ParamsTokenController@generate');
Router::post('paramsToken/analyze', 'App\Controller\v3\ParamsTokenController@analyze');
Router::post('goods/getTags', 'App\Controller\v3\GoodsController@getTags');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]);
// 需要登录的路由
@ -157,6 +158,11 @@ Router::addGroup('/v3/', function () {
Router::post('store/getList', 'App\Controller\v3\StoreController@getList');
Router::post('goods/info', 'App\Controller\v3\GoodsController@info');
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');
Router::post('goods/create', 'App\Controller\v3\GoodsController@create');
Router::post('goods/updateOnSale', 'App\Controller\v3\GoodsController@updateOnSale');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]);
// 微信支付回调

Loading…
Cancel
Save