143 lines
4.4 KiB
PHP
143 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers\Dealer;
|
|
|
|
use App\Endpoint\Api\Http\Controllers\Controller;
|
|
use App\Endpoint\Api\Http\Resources\Dealer\UserProductLogResource;
|
|
use App\Endpoint\Api\Http\Resources\Dealer\UserProductResource;
|
|
use App\Exceptions\BizException;
|
|
use App\Helpers\Paginator;
|
|
use App\Models\DealerUserProductLog;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class UserProductController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$list = $request->user()->dealerProducts()
|
|
->with('product')
|
|
->orderBy('product_id', 'asc')
|
|
->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
|
return UserProductResource::collection($list);
|
|
}
|
|
|
|
public function show($id, Request $request)
|
|
{
|
|
$product = $request->user()->dealerProducts()
|
|
->with('product')
|
|
->where('product_id', $id)
|
|
->first();
|
|
return UserProductResource::make($product);
|
|
}
|
|
|
|
public function logs(Request $request)
|
|
{
|
|
$list = $request->user()->dealerProductLogs()->filter($request->all())
|
|
->with('product')
|
|
->orderBy('created_at', 'desc')
|
|
->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
|
return UserProductLogResource::collection($list);
|
|
}
|
|
|
|
/**
|
|
* 线下去库存
|
|
*
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function offlineOutQty(Request $request)
|
|
{
|
|
$input = $request->validate([
|
|
'product_id'=>['bail', 'required', 'int', 'min:0'],
|
|
'num'=>['bail', 'required', 'int', 'min:1'],
|
|
'remark'=> ['bail', 'string', 'max:200'],
|
|
], [], [
|
|
'product_id' => '商品',
|
|
'num' => '数量',
|
|
'remark'=>'备注',
|
|
]);
|
|
$product = $request->user()->dealerProducts()
|
|
->where('product_id', $input['product_id'])
|
|
->first();
|
|
if (!$product) {
|
|
throw new BizException('您还没有该商品');
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
$product->decrement('stock', $input['num']);
|
|
|
|
DealerUserProductLog::create([
|
|
'user_id'=> $request->user()->id,
|
|
'product_id'=> $input['product_id'],
|
|
'type' => DealerUserProductLog::TYPE_OFFLINE_OUT,
|
|
'qty'=>$input['num'],
|
|
'remark'=>$input['remark'] ?? null,
|
|
]);
|
|
DB::commit();
|
|
} catch (QueryException $e) {
|
|
DB::rollBack();
|
|
if (strpos($e->getMessage(), 'Numeric value out of range') !== false) {
|
|
$e = new BizException('库存不足');
|
|
}
|
|
throw $e;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('系统繁忙,请稍后再试');
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* 撤销线下去库存
|
|
*
|
|
*/
|
|
public function revokeQtyLog(DealerUserProductLog $log, Request $request)
|
|
{
|
|
//判断是否是自己的日志
|
|
if ($log->user_id != $request->user()->id) {
|
|
throw new BizException('日志未找到');
|
|
}
|
|
//是否可以撤销
|
|
if (!$log->canRevoke()) {
|
|
throw new BizException('该日志无法撤销');
|
|
}
|
|
|
|
$product = $request->user()->dealerProducts()
|
|
->where('product_id', $log->product_id)
|
|
->first();
|
|
if (!$product) {
|
|
throw new BizException('您还没有该商品');
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$product->increment('stock', $log->qty);
|
|
|
|
$revokeLog = DealerUserProductLog::create([
|
|
'user_id'=> $request->user()->id,
|
|
'product_id'=> $log->product_id,
|
|
'type' => DealerUserProductLog::TYPE_REVOKE_IN,
|
|
'qty' => $log->qty,
|
|
'remark'=> '撤销回滚',
|
|
]);
|
|
|
|
$log->update([
|
|
'revoke_id' => $revokeLog->id,
|
|
]);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('系统繁忙,请稍后再试');
|
|
}
|
|
|
|
return response()->noContent();
|
|
}
|
|
}
|