You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/service/statistics/OrderData.php

126 lines
4.0 KiB

<?php
namespace app\api\service\statistics;
use app\api\model\Order;
use app\api\model\user\IdentityOrder;
use app\common\enum\order\OrderStatus;
use app\common\enum\order\OrderStatus as OrderStatusEnum;
use app\common\enum\order\PayStatus as PayStatusEnum;
use app\common\enum\user\IdentityEnum;
use app\common\service\BaseService;
class OrderData extends BaseService
{
//查询对象
protected Order $orderModel;
public function initialize()
{
parent::initialize();
$this->orderModel = new Order();
}
public function getOrderData($startDate = null, $endDate = null): array
{
return [
//订单金额
"orderMoney" => $this->getOrderTotalPrice($startDate, $endDate),
//下单总数
"orderCount" => $this->getPayOrderTotal($startDate, $endDate),
//下单人数
'orderPayUser' => $this->getConsumeUsers(),
//订单成本金额
'orderCostMoney' => 0,
//预估利益
'orderInterest' => 0,
//付费会员数
'payMemberCount' => $this->getPayCount(IdentityEnum::MEMBER, $startDate, $endDate),
//付费分销商数
'payDealerCount' => $this->getPayCount(IdentityEnum::DEALER, $startDate, $endDate)
];
}
/**
* 获取某天的总销售额
* @param null $startDate
* @param null $endDate
* @return float
*/
public function getOrderTotalPrice($startDate = null, $endDate = null): float
{
// 查询对象
$query = $this->orderModel;
// 设置查询条件
if (!is_null($startDate) && !is_null($endDate)) {
$query->where('pay_time', '>=', strtotime($startDate))
->where('pay_time', '<', strtotime($endDate) + 86400);
}
// 总销售额
return $query->where('pay_status', '=', PayStatusEnum::SUCCESS)
->where('order_status', '<>', OrderStatusEnum::CANCELLED)
->where('is_delete', '=', 0)
->sum('pay_price');
}
/**
* 消费人数
* @param null $startDate
* @param null $endDate
* @return string
*/
public function getConsumeUsers($startDate = null, $endDate = null): string
{
// 检索查询条件
$filter = [];
if (!is_null($startDate) && !is_null($endDate)) {
$filter[] = ['pay_time', '>=', strtotime($startDate)];
$filter[] = ['pay_time', '<', strtotime($endDate) + 86400];
}
// 查询总记录
$value = $this->orderModel->field('user_id')
->where($filter)
->where('pay_status', '=', PayStatusEnum::SUCCESS)
// ->where('order_status', '<>', OrderStatusEnum::CANCELLED)
->where('is_delete', '=', '0')
->group('user_id')
->count();
return number_format($value);
}
/**
* 获取已付款订单总数 (可指定某天)
* @param null $startDate
* @param null $endDate
* @return int
*/
public function getPayOrderTotal($startDate = null, $endDate = null): int
{
$filter = [
['pay_status', '=', PayStatusEnum::SUCCESS],
['order_status', '<>', OrderStatusEnum::CANCELLED]
];
if (!is_null($startDate) && !is_null($endDate)) {
$filter[] = ['pay_time', '>=', strtotime($startDate)];
$filter[] = ['pay_time', '<', strtotime($endDate) + 86400];
}
// 获取订单总数量
return $this->orderModel->where($filter)
->where('is_delete', '=', 0)
->count();
}
/**
* 付费会员数
*/
public function getPayCount($order_type, $startDate = null, $endDate = null) : int {
$query = new IdentityOrder();
if(!empty($startDate) && !empty($endDate)) {
$query->where('create_time', '>=', strtotime($startDate))
->where('create_time', '<', strtotime($endDate) + 86400);
}
return $query->where('order_type', '=', $order_type)->count();
}
}