<?php
|
|
namespace app\operations\model\plus\groupbuy;
|
|
use app\common\model\plus\groupbuy\Bill as BillModel;
|
|
/**
|
* 商家端团购订单模型
|
*/
|
class Bill extends BillModel
|
{
|
// 定义全局的查询范围
|
protected $globalScope = ['status'];
|
public function scopeStatus($query)
|
{
|
$shop_supplier_ids = \app\operations\model\supplier\Supplier::getSupplierIdsByUser(session('jjjshop_operations')['user']);
|
if (empty($shop_supplier_ids)){
|
$query->where($query->getTable() . '.shop_supplier_id', -1);
|
}else{
|
$query->where($query->getTable() . '.shop_supplier_id', 'in', $shop_supplier_ids);
|
}
|
}
|
/**
|
* 获取团购订单列表
|
*/
|
public function getList($param)
|
{
|
$filter = [];
|
if (!empty($param['search'])) {
|
$filter[] = ['order_no', 'like', '%' . trim($param['search']) . '%'];
|
}
|
|
if (!empty($param['groupbuy_active_id'])) {
|
$filter[] = ['groupbuy_active_id', '=', (int)$param['groupbuy_active_id']];
|
}
|
|
if (!empty($param['groupbuy_product_id'])) {
|
$filter[] = ['groupbuy_product_id', '=', (int)$param['groupbuy_product_id']];
|
}
|
|
if (!empty($param['user_id'])) {
|
$filter[] = ['user_id', '=', (int)$param['user_id']];
|
}
|
|
if (isset($param['status']) && $param['status'] !== '') {
|
$filter[] = ['status', '=', (int)$param['status']];
|
}
|
|
// 店铺ID筛选
|
$filter[] = ['shop_supplier_id', '=', $param['shop_supplier_id'] ?? 0];
|
|
$order = ['create_time' => 'desc'];
|
if (!empty($param['order'])) {
|
$order = [$param['order_field'] => $param['order']];
|
}
|
|
return $this->with(['user', 'product', 'active'])
|
->where($filter)
|
->order($order)
|
->paginate([
|
'list_rows' => $param['list_rows'] ?? 15,
|
'query' => $param
|
]);
|
}
|
|
/**
|
* 获取团购订单统计
|
*/
|
public function getStatistics($param)
|
{
|
$filter = [];
|
if (!empty($param['groupbuy_active_id'])) {
|
$filter[] = ['groupbuy_active_id', '=', (int)$param['groupbuy_active_id']];
|
}
|
|
if (!empty($param['groupbuy_product_id'])) {
|
$filter[] = ['groupbuy_product_id', '=', (int)$param['groupbuy_product_id']];
|
}
|
|
// 店铺ID筛选
|
$filter[] = ['shop_supplier_id', '=', $param['shop_supplier_id'] ?? 0];
|
|
// 总订单数
|
$totalOrders = $this->where($filter)->count();
|
|
// 总金额
|
$totalAmount = $this->where($filter)->sum('total_price');
|
|
// 已支付订单数
|
$paidOrders = $this->where($filter)->where('status', '>', 0)->count();
|
|
// 已支付金额
|
$paidAmount = $this->where($filter)->where('status', '>', 0)->sum('total_price');
|
|
return [
|
'total_orders' => $totalOrders,
|
'total_amount' => $totalAmount,
|
'paid_orders' => $paidOrders,
|
'paid_amount' => $paidAmount
|
];
|
}
|
|
/**
|
* 导出团购订单
|
*/
|
public function exportList($param)
|
{
|
$list = $this->getList($param)->items();
|
|
// 构建导出数据
|
$exportData = [];
|
$exportData[] = ['订单号', '团购活动', '团购商品', '用户', '总金额', '实付金额', '状态', '创建时间'];
|
|
foreach ($list as $item) {
|
$exportData[] = [
|
$item['order_no'],
|
$item['active']['active_name'] ?? '',
|
$item['product']['product']['product_name'] ?? '',
|
$item['user']['nickName'] ?? '',
|
$item['total_price'],
|
$item['actual_price'],
|
$this->getStatusText($item['status']),
|
date('Y-m-d H:i:s', $item['create_time'])
|
];
|
}
|
|
// 导出到Excel
|
$fileName = 'groupbuy_orders_' . date('YmdHis') . '.xlsx';
|
$filePath = 'uploads/export/' . $fileName;
|
|
// 这里应该使用PHPExcel或类似的库来导出Excel
|
// 由于当前系统可能已有相关服务,这里简化处理
|
|
return $filePath;
|
}
|
|
/**
|
* 获取状态文本
|
*/
|
private function getStatusText($status)
|
{
|
$statusMap = [
|
-1 => '已取消',
|
0 => '待支付',
|
1 => '已支付',
|
2 => '已完成'
|
];
|
|
return $statusMap[$status] ?? '未知状态';
|
}
|
|
/**
|
* 更新订单状态
|
*/
|
public function updateStatus($status)
|
{
|
return $this->save(['status' => $status]) !== false;
|
}
|
}
|