6
0
Fork 0

add dealer-orders

release
vine_liutk 2022-01-14 14:36:09 +08:00 committed by 李静
parent c0e5a5817b
commit aad0551eca
11 changed files with 225 additions and 12 deletions

View File

@ -0,0 +1,34 @@
<?php
namespace App\Endpoint\Api\Filters;
use EloquentFilter\ModelFilter;
class DealerOrderFilter extends ModelFilter
{
public function status($status)
{
switch ($status) {
case 'pending'://待确认
$this->onlyPending();
break;
case 'wait_pay'://待付款
$this->onlyPendinged();
break;
case 'wait_shipping'://待发货
$this->onlyPaid();
break;
case 'wait_shippinged'://待收货
$this->onlyShipping();
break;
case 'completed'://已完成
$this->onlyCompleted();
break;
case 'cancelled'://已取消
$this->onlyCancelled();
break;
default:
break;
}
}
}

View File

@ -4,7 +4,10 @@ namespace App\Endpoint\Api\Http\Controllers\Dealer;
use App\Endpoint\Api\Http\Controllers\Controller;
use App\Endpoint\Api\Http\Resources\Dealer\OrderResource;
use App\Endpoint\Api\Http\Resources\Dealer\OrderSimpleResource;
use App\Exceptions\BizException;
use App\Helpers\Paginator as PaginatorHelper;
use App\Models\DealerOrder;
use App\Models\DealerProduct;
use App\Services\Dealer\OrderService;
use Illuminate\Http\Request;
@ -19,6 +22,24 @@ class OrderController extends Controller
*/
public function index(Request $request)
{
$cate = $request->input('cate', 'purchase');//获取订单类别
$user = $request->user();
switch ($cate) {
case 'consignor'://发货单
$query = $user->dealerConsignOrders();
break;
default://采购单
$query = $user->dealerOrders();
break;
}
$perPage = PaginatorHelper::resolvePerPage('per_page', 20, 50);
$orders = $query->with('products')
->filter($request->all())
->latest('id')
->simplePaginate($perPage);
return OrderSimpleResource::collection($orders);
}
public function store(Request $request, OrderService $orderService)
@ -51,6 +72,14 @@ class OrderController extends Controller
public function show($id, Request $request)
{
$order = DealerOrder::findOrFail($id);
$userId = $request->user()->id;
//既不是采购人,也不是发货人
if (!$order->canCurd($userId)) {
throw new BizException('订单未找到');
}
return OrderResource::make($order);
}
/**

View File

@ -22,6 +22,7 @@ class OrderResource extends JsonResource
'status' => $this->status,
'pay_info' => $this->pay_info??$this->consignor->dealer->pay_info,
'pay_image'=> $this->pay_image,
'is_consignor' => $request->user()->id == $this->consignor_id, //是否发货人身份
];
}
}

View File

@ -19,6 +19,7 @@ class OrderSimpleResource extends JsonResource
'total_amount' => $this->total_amount,
'created_at' => $this->created_at->toDateTimeString(),
'status' => $this->status,
'products' => OrderProductResource::collection($this->whenLoaded('products')),
];
}
}

View File

@ -23,8 +23,8 @@ class ProductResource extends JsonResource
'price' => (string) $this->price,
'is_online' => $this->isOnline(),
'description' => (string) $this->description,
'lvl_rules' => $this->whenLoaded('lvlRules', ProductLvlRuleResource::collection($this->lvlRules)),
'sale_rules' => $this->whenLoaded('saleRules', ProductSaleRuleResource::collection($this->saleRules)),
'lvl_rules' => ProductLvlRuleResource::collection($this->whenLoaded('lvlRules')),
'sale_rules' => ProductSaleRuleResource::collection($this->whenLoaded('saleRules')),
];
}
}

View File

@ -225,7 +225,11 @@ Route::group([
//计算商品下单价格
Route::get('orders/total-amount', [Dealer\OrderController::class, 'totalAmount']);
//订单列表
Route::get('orders', [Dealer\OrderController::class, 'index']);
//下单
Route::post('orders', [Dealer\OrderController::class, 'store']);
//订单详情
Route::get('orders/{order}', [Dealer\OrderController::class, 'show']);
});
});

View File

@ -5,10 +5,26 @@ namespace App\Models;
use App\Casts\JsonArray;
use App\Enums\DealerOrderSettleState;
use App\Enums\DealerOrderStatus;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class DealerOrder extends Model
{
use Filterable;
use HasDateTimeFormatter;
/**
* 订单状态
*/
public const STATUS_PENDING = 0; // 待确认
public const STATUS_PENDINGED = 1; // 已确认 待付款
public const STATUS_PAY = 2; // 已付款 待收款
public const STATUS_PAID = 3; // 已收款 待发货
public const STATUS_SHIPPING = 4; // 已发货 待收货
public const STATUS_COMPLETED = 9; // 已完成
public const STATUS_CANCELLED = 10; // 已取消
protected $attributes = [
'status' => DealerOrderStatus::Pending,
'settle_state' => DealerOrderSettleState::Pending,
@ -20,6 +36,54 @@ class DealerOrder extends Model
'pay_info'=>JsonArray::class,
];
/**
* 获取待确认订单
*/
public function scopeOnlyPending($query)
{
return $query->where('status', static::STATUS_PENDING);
}
/**
* 已确认/待付款
*/
public function scopeOnlyPendinged($query)
{
return $query->where('status', static::STATUS_PENDINGED);
}
/**
* 已收款/待发货
*/
public function scopeOnlyPaid($query)
{
return $query->where('status', static::STATUS_PAID);
}
/**
* 已发货/待收货
*/
public function scopeOnlyShipping($query)
{
return $query->where('status', static::STATUS_SHIPPING);
}
/**
* 已完成
*/
public function scopeOnlyCompleted($query)
{
return $query->where('status', static::STATUS_COMPLETED);
}
/**
* 已取消
*/
public function scopeOnlyCancelled($query)
{
return $query->where('status', static::STATUS_CANCELLED);
}
/**
* 此订单所属的用户的信息
*/
@ -45,4 +109,19 @@ class DealerOrder extends Model
{
return $this->belongsTo(User::class, 'consignor_id');
}
public function isUser($userId)
{
return $this->user_id == $userId;
}
public function isConsignor($userId)
{
return $this->consignor_id == $userId;
}
public function canCurd($userId)
{
return $this->isUser($userId) || $this->isConsignor($userId);
}
}

View File

@ -152,6 +152,24 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
return $this->hasOne(Dealer::class, 'user_id');
}
/**
* 经销商订购订单
*
*/
public function dealerOrders()
{
return $this->hasMany(DealerOrder::class, 'user_id');
}
/**
* 经销商的发货订单
*
*/
public function dealerConsignOrders()
{
return $this->hasMany(DealerOrder::class, 'consignor_id');
}
/**
* 确认此用户是否是 VIP
*

View File

@ -160,6 +160,14 @@ class UserInfo extends Model
return $this->belongsTo(UserInfo::class, 'inviter_id');
}
/**
* 获取此用户的真实邀请人信息
*/
public function realInviterInfo()
{
return $this->belongsTo(UserInfo::class, 'real_inviter_id');
}
/**
* 获取此用户的预收益
*/
@ -401,6 +409,17 @@ class UserInfo extends Model
return [];
}
public function getRealParentIdsAttribute(): array
{
$rpids = (array) $this->realInviterInfo?->parent_ids;
if ($this->real_parent_id) {
array_push($rpids, $this->real_parent_id);
}
return $rpids;
}
/**
* 代理等级排名
*

View File

@ -2,6 +2,7 @@
namespace App\Services\Dealer;
use App\Enums\DealerLvl;
use App\Exceptions\BizException;
use App\Models\DealerOrder;
use App\Models\DealerProduct;
@ -57,7 +58,7 @@ class OrderService
}
//找到发货人
$consignor = $this->getConsignor($user);
$consignor = $this->getConsignor($user, $totalAmount);
//找到收货地址
$shippingAddress = $this->getShippingAddress($user, $shippingAddressId);
@ -68,7 +69,7 @@ class OrderService
try {
$order->sn = serial_number();
$order->user_id = $user->id;
$order->consignor_id = $consignor->user_id;
$order->consignor_id = $consignor?->user_id;
$order->total_amount = $totalAmount;
$order->consignee_name = $shippingAddress->consignee;
$order->consignee_telephone = $shippingAddress->telephone;
@ -107,7 +108,7 @@ class OrderService
{
$consignor = $this->getConsignor($order->user, $order->consignor);
$order->update([
'consignor_id' => $consignor->user_id,
'consignor_id' => $consignor?->user_id,
]);
}
@ -129,18 +130,45 @@ class OrderService
return $user->shippingAddresses()->where('is_default', true)->first();
}
public function getConsignor(User $user, ?User $lastConsignor = null)
public function getConsignor(User $user, $totalAmount, ?User $lastConsignor = null)
{
//todo-改成配置
$rules = [
[
'amount'=>'26400.00',
'lvl'=>DealerLvl::Contracted,
],
[
'amount' =>'8600.00',
'lvl'=>DealerLvl::Special,
],
[
'amount' =>'2520.00',
'lvl'=>DealerLvl::Gold,
],
];
$lvl = $user->dealer->lvl;
foreach ($rules as $rule) {
if ($totalAmount >= $rule['amount'] && $lvl->value < $rule['lvl']->value) {
$lvl = $rule['lvl'];
}
}
//如果是签约单,直接抛到公司后台发货
if ($lvl->value >= DealerLvl::Contracted->value) {
return null;
}
$query = UserInfo::with('dealer');
if ($lastConsignor) {
$query->whereIn('user_id', $lastConsignor->userInfo->parent_ids);//上个发货人的上级
$query->whereIn('user_id', $lastConsignor->userInfo->real_parent_ids);//上个发货人的上级
} else {
$query->whereIn('user_id', $user->userInfo->parent_ids);//自己的上级
$query->whereIn('user_id', $user->userInfo->real_parent_ids);//自己的上级
}
$consignor = $query->whereHas('dealer', function ($q) use ($user) {
return $q->where('lvl', '>', $user->dealer->lvl);//经销商身份大于自己的
$consignor = $query->whereHas('dealer', function ($q) use ($lvl) {
return $q->where('lvl', '>', $lvl);//经销商身份大于自己的
})->orderBy('depth', 'desc')->first();//深度逆序第一个
if (!$consignor) {
$consignor = $lastConsignor;
}
return $consignor;
}
}

View File

@ -19,7 +19,7 @@ class CreateDealerOrdersTable extends Migration
$table->unsignedBigInteger('user_id')->comment('下单用户');
$table->unsignedBigInteger('consignor_id')->nullable()->comment('发货用户');
$table->unsignedDecimal('total_amount', 10, 2)->default(0.00)->comment('订单价格');
$table->unsignedTinyInteger('status')->default(0)->comment('状态0待接单1待打款2待收款3待发货4待收货5已完成9已取消');
$table->unsignedTinyInteger('status')->default(0)->comment('状态0待接单1待打款2待收款3待发货4待收货9已完成10已取消');
$table->unsignedTinyInteger('settle_state')->default(0)->comment('结算状态0待处理1已生成2已结算');
$table->string('consignee_name')->nullable()->comment('收货人姓名');
$table->string('consignee_telephone')->nullable()->comment('收货人联系方式');