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

139 lines
4.2 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\library\helper;
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($startDate, $endDate),
//订单成本金额
'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;
// 设置查询条件
$filter = $this->getOrderFilter($startDate, $endDate);
// 总销售额
$data = $query->where($filter)
->where('is_delete', '=', 0)
->sum('pay_price');
return helper::number2($data);
}
/**
* 消费人数
* @param null $startDate
* @param null $endDate
* @return string
*/
public function getConsumeUsers($startDate = null, $endDate = null): string
{
// 检索查询条件
$filter = $this->getOrderFilter($startDate, $endDate);
// 查询总记录
$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
{
$filter = $this->getOrderFilter($startDate, $endDate);
// 获取订单总数量
return $this->orderModel->where($filter)
->where('is_delete', '=', 0)
->count();
}
/**
* 付费会员数
*/
public function getPayCount($order_type, $startDate = null, $endDate = null) : int {
$query = new IdentityOrder();
$filter = [];
if (!is_null($startDate) && !is_null($endDate)) {
$filter[] = ['create_time', '>=', strtotime($startDate)];
$filter[] = ['create_time', '<', strtotime($endDate) + 86400];
}
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);
}
}