213 lines
6.9 KiB
PHP
213 lines
6.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class GoodsSkusController extends Controller
|
|
{
|
|
public function __construct(Request $request)
|
|
{
|
|
// $this->middleware(['role:super-admin','permission:publish articles|edit articles']);
|
|
$this->log = new LogModel([
|
|
'module' => 'goods',
|
|
'action' => $request->getMethod(),
|
|
'target_type' => 'goods_sku',
|
|
]);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$goods = Goods::query()->filter()->get()->toArray();
|
|
$goodsIds = array_column($goods, 'id');
|
|
// 状态变更时间查询,日志
|
|
$day = date('Y-m-d'); //早上7点之前是昨天,7点之后是今天
|
|
$goodsSkus = GoodsSku::query()
|
|
->whereIn('goods_id', $goodsIds)
|
|
->filter()
|
|
->with(['goods' => function ($query) {
|
|
$query->with(['type:id,name', 'brand:id,name']);
|
|
}])
|
|
->with(['daily' => function ($query) use ($day){
|
|
$query->where('day', $day)->with(['daily:id,sku_id,day,arrived_today_num,loss_num,inventory']);
|
|
}])
|
|
->paginate();
|
|
|
|
return GoodsSkuResource::collection($goodsSkus);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
return new GoodsSkuResource(GoodsSku::query()
|
|
->with(['goods' => function ($query) {
|
|
$query->with(['type:id,name', 'brand:id,name']);
|
|
}])
|
|
->find($id));
|
|
}
|
|
|
|
public function update($id, Request $request)
|
|
{
|
|
$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 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)
|
|
{
|
|
$appendRules = [
|
|
'skus' => ['required', 'array'],
|
|
'skus.*.id' => [
|
|
'required',
|
|
Rule::exists('goods_skus', 'id'),
|
|
],
|
|
];
|
|
$data = $request->all();
|
|
$this->validateUpdate($data, $appendRules);
|
|
GoodsSku::whereIn('id', array_column($data, 'id'))
|
|
->update($data);
|
|
}
|
|
|
|
public function updateField($id, Request $request)
|
|
{
|
|
$appendRules = [
|
|
'updateField' => [
|
|
'required',
|
|
Rule::in(['reference_price', 'reserve', 'loss_num', 'status'])
|
|
],
|
|
];
|
|
if ($request->has('loss_num')) {
|
|
$appendRules['reason'] = ['required', 'string'];
|
|
}
|
|
$this->validateUpdate($request->all(), $appendRules);
|
|
$updateField = \request('updateField');
|
|
$sku = GoodsSku::query()->find($id);
|
|
$this->setBeforeUpdate($sku->$updateField);
|
|
$sku->$updateField = $request->$updateField;
|
|
$sku->save();
|
|
$this->setAfterUpdate($sku->$updateField);
|
|
$this->addLog($id, $updateField);
|
|
|
|
return new GoodsSkuResource($sku);
|
|
}
|
|
|
|
public function updateStock($id, Request $request)
|
|
{
|
|
$this->validateUpdate($request->all());
|
|
$sku = GoodsSku::query()->where('id', $id)->get(['id', 'two_days_ago_num', 'yesterday_num']);
|
|
if ($sku->two_days_ago_num != \request('t wo_days_ago_num') || $sku->yesterday_num != \request('yesterday_num')) {
|
|
$this->setBeforeUpdate($sku->toArray());
|
|
}
|
|
|
|
}
|
|
|
|
private function validateUpdate($data, $appendRules = [])
|
|
{
|
|
$rules = [
|
|
'*.two_days_ago_num' => [
|
|
'sometimes',
|
|
'integer',
|
|
],
|
|
'*.yesterday_num' => [
|
|
'sometimes',
|
|
'integer',
|
|
],
|
|
'*.arrived_today_num' => [
|
|
'sometimes',
|
|
'integer',
|
|
],
|
|
'*.cost' => [
|
|
'sometimes',
|
|
'numeric',
|
|
'gt:0'
|
|
],
|
|
'*.reference_price' => [
|
|
'sometimes',
|
|
'numeric',
|
|
'gt:0'
|
|
],
|
|
'*.reserve' => [
|
|
'sometimes',
|
|
'integer',
|
|
],
|
|
'*.loss_num' => [
|
|
'sometimes',
|
|
'integer',
|
|
],
|
|
'*.inventory' => [
|
|
'sometimes',
|
|
'integer',
|
|
],
|
|
'*.status' => [
|
|
'sometimes',
|
|
'integer',
|
|
Rule::in([0, 1, 2])
|
|
],
|
|
];
|
|
// $validator = Validator::make($data, array_merge($rules, $appendRules))->validate();
|
|
$validator = Validator::make($data, array_merge($rules, $appendRules));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
if (!$request->hasFile('goodsSkus')) {
|
|
$this->res = [
|
|
'httpCode' => 404,
|
|
'errorCode' => 404404,
|
|
'errorMessage' => 'not found goodsSkus file',
|
|
];
|
|
}
|
|
try {
|
|
$collection = Excel::import(new GoodsSkusImport(), $request->file('goodsSkus'));
|
|
$this->setAfterUpdate($collection->toArray());
|
|
$this->addLog(0, 'import');
|
|
} catch (ValidationException $exception) {
|
|
$this->setValidatorFailResponse($exception->validator->getMessageBag()->getMessages());
|
|
}
|
|
|
|
return response($this->res, $this->res['httpCode']);
|
|
}
|
|
}
|