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

140 lines
4.2 KiB

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