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

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