194 lines
6.4 KiB
PHP
194 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace App\Service\business;
|
||
|
||
use App\BaseApp\BaseService;
|
||
use App\Models\business\OrderModel;
|
||
use App\Models\business\OrderPaymentModel;
|
||
use App\Models\business\WxUserModel;
|
||
|
||
/**
|
||
* 订单管理(后台)
|
||
*
|
||
* 建单、收款、发货的规则都在 OrderCoreService,这里只做列表查询与管理员身份的透传。
|
||
*/
|
||
class OrderService extends BaseService
|
||
{
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->model = OrderModel::class;
|
||
$this->selectField = [
|
||
'id', 'order_no', 'list_id', 'user_id', 'enterprise_id', 'total_amount', 'paid_amount',
|
||
'pay_type', 'pay_status', 'paid_at', 'delivery_type', 'receiver_name', 'receiver_phone',
|
||
'receiver_address', 'status', 'remark', 'created_at', 'updated_at',
|
||
];
|
||
$this->queryField = [
|
||
'order_no' => 'like',
|
||
'user_id' => '=',
|
||
'enterprise_id' => '=',
|
||
'status' => '=',
|
||
'pay_status' => '=',
|
||
'pay_type' => '=',
|
||
'delivery_type' => '=',
|
||
'receiver_phone' => 'like',
|
||
];
|
||
$this->with = ['user', 'enterprise'];
|
||
}
|
||
|
||
public function list(): array
|
||
{
|
||
$keyword = trim((string) request()->get('user_keyword', ''));
|
||
if ($keyword !== '') {
|
||
$userIds = WxUserModel::where('deleted_at', 0)
|
||
->where(function ($query) use ($keyword) {
|
||
$query->where('nick_name', 'like', '%' . $keyword . '%')
|
||
->orWhere('phone', 'like', '%' . $keyword . '%');
|
||
})->pluck('id')->all();
|
||
$this->whereIn = ['user_id', empty($userIds) ? [0] : $userIds];
|
||
}
|
||
$result = $this->getPageList();
|
||
$price = PriceService::getInstance();
|
||
foreach ($result['items'] as &$item) {
|
||
$item['user_name'] = $item['user']['nick_name'] ?? '';
|
||
$item['user_phone'] = $item['user']['phone'] ?? '';
|
||
$item['enterprise_name'] = $item['enterprise']['name'] ?? '';
|
||
$item['total_amount_text'] = $price->centsToYuan((int) $item['total_amount']);
|
||
$item['paid_amount_text'] = $price->centsToYuan((int) $item['paid_amount']);
|
||
}
|
||
unset($item);
|
||
return $result;
|
||
}
|
||
|
||
public function option(): mixed
|
||
{
|
||
$this->optionField = ['id', 'order_no'];
|
||
return $this->getOption();
|
||
}
|
||
|
||
public function detail($id): mixed
|
||
{
|
||
$order = OrderCoreService::getInstance()->detail((int) $id);
|
||
$price = PriceService::getInstance();
|
||
$order['total_amount_text'] = $price->centsToYuan((int) $order['total_amount']);
|
||
$order['paid_amount_text'] = $price->centsToYuan((int) $order['paid_amount']);
|
||
foreach ($order['items'] as &$item) {
|
||
$item['unit_price_text'] = $price->centsToYuan((int) $item['unit_price']);
|
||
$item['total_price_text'] = $price->centsToYuan((int) $item['total_price']);
|
||
}
|
||
unset($item);
|
||
return $order;
|
||
}
|
||
|
||
/**
|
||
* 后台代客建单
|
||
*/
|
||
public function create($params): mixed
|
||
{
|
||
$listId = (int) ($params['list_id'] ?? 0);
|
||
if ($listId <= 0) {
|
||
$this->utils->errorThrow('请选择清单');
|
||
}
|
||
return OrderCoreService::getInstance()->createFromList($listId, 0, $params);
|
||
}
|
||
|
||
/**
|
||
* 只允许改收件信息与备注:金额与状态必须走各自的业务入口
|
||
*/
|
||
public function update($id, $params): mixed
|
||
{
|
||
$allowed = array_intersect_key($params, array_flip([
|
||
'receiver_name', 'receiver_phone', 'receiver_address', 'remark', 'delivery_type',
|
||
]));
|
||
if (empty($allowed)) {
|
||
$this->utils->errorThrow('没有可修改的字段');
|
||
}
|
||
return $this->save($id, $allowed);
|
||
}
|
||
|
||
public function delete($ids): mixed
|
||
{
|
||
return $this->del($ids);
|
||
}
|
||
|
||
/**
|
||
* 审核转账凭证
|
||
*/
|
||
public function auditPayment(array $params): array
|
||
{
|
||
$paymentId = (int) ($params['payment_id'] ?? 0);
|
||
$pass = (int) ($params['status'] ?? 1) === 1;
|
||
return OrderCoreService::getInstance()->auditPayment(
|
||
$paymentId,
|
||
$pass ? OrderPaymentModel::STATUS_CONFIRMED : OrderPaymentModel::STATUS_REJECTED,
|
||
$this->userId,
|
||
(string) ($params['remark'] ?? '')
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 发货
|
||
*/
|
||
public function ship(array $params): array
|
||
{
|
||
return OrderCoreService::getInstance()->ship(
|
||
(int) ($params['id'] ?? 0),
|
||
$params,
|
||
$this->userId
|
||
);
|
||
}
|
||
|
||
public function cancel(int $id): array
|
||
{
|
||
return OrderCoreService::getInstance()->transition($id, OrderModel::STATUS_CANCELLED);
|
||
}
|
||
|
||
public function complete(int $id): array
|
||
{
|
||
return OrderCoreService::getInstance()->transition($id, OrderModel::STATUS_DONE);
|
||
}
|
||
|
||
/**
|
||
* 某个用户的订单,用户详情模态框里用
|
||
*/
|
||
public function byUser(int $userId): array
|
||
{
|
||
$price = PriceService::getInstance();
|
||
$rows = OrderModel::where('user_id', $userId)->where('deleted_at', 0)->orderBy('id', 'desc')->get([
|
||
'id', 'order_no', 'total_amount', 'paid_amount', 'pay_status', 'status', 'created_at',
|
||
])->toArray();
|
||
foreach ($rows as &$row) {
|
||
$row['total_amount_text'] = $price->centsToYuan((int) $row['total_amount']);
|
||
}
|
||
unset($row);
|
||
return $rows;
|
||
}
|
||
|
||
/**
|
||
* 概览:各状态数量与金额,给列表页顶部的统计条
|
||
*/
|
||
public function stat(): array
|
||
{
|
||
$price = PriceService::getInstance();
|
||
$rows = OrderModel::where('deleted_at', 0)
|
||
->selectRaw('status, count(*) as total, sum(total_amount) as amount')
|
||
->groupBy('status')
|
||
->get();
|
||
$stat = ['total' => 0, 'amount' => 0, 'status' => []];
|
||
foreach ($rows as $row) {
|
||
$stat['total'] += (int) $row['total'];
|
||
$stat['amount'] += (int) $row['amount'];
|
||
$stat['status'][] = [
|
||
'status' => (int) $row['status'],
|
||
'count' => (int) $row['total'],
|
||
'amount' => (int) $row['amount'],
|
||
];
|
||
}
|
||
$stat['amount_text'] = $price->centsToYuan((int) $stat['amount']);
|
||
$stat['auditing'] = OrderPaymentModel::where('deleted_at', 0)
|
||
->where('status', OrderPaymentModel::STATUS_AUDITING)
|
||
->count();
|
||
return $stat;
|
||
}
|
||
}
|