multi-store/app/admin/logic/statistic/TradeStatisticLogic.php

971 lines
40 KiB
PHP

<?php
namespace app\admin\logic\statistic;
use app\common\logic\BaseLogic;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_order\StoreOrder;
use app\common\model\user_recharge\UserRecharge;
use app\common\model\user_sign\UserSign;
/**
* Class 交易数据
* @package app\services\statistic
*/
class TradeStatisticLogic extends BaseLogic
{
public $_day = ['00时', '01时', '02时', '03时', '04时', '05时', '06时', '07时', '08时', '09时', '10时', '11时', '12时', '13时', '14时', '15时', '16时', '17时', '18时', '19时', '20时', '21时', '22时', '23时', '24时'];
/**
* 基本概况
* @param $where
* @return mixed
*/
public function getTopLeftTrade($where)
{
//总交易额
$selectType = "sum";
$tradeTotalMoney = $this->tradeTotalMoney($where, $selectType);
//交易曲线
$selectType = "group";
$hourTotalMoney = $this->tradeGroupMoney($where, $selectType);
return ['total_money' => $tradeTotalMoney, 'curve' => $hourTotalMoney];
}
public function getTopRightOneTrade()
{
$storeOrder = new StoreOrder();
/** day订单数 */
//今日订单数
$orderCountWhere['paid'] = 1;
$timeKey = $this->TimeConvert("today");
$todayOrderCount = $storeOrder->where($orderCountWhere)->whereDay('create_time')->count();
$where['timeKey'] = [
"start_time" => strtotime($timeKey['start_time']),
"end_time" => strtotime($timeKey['end_time']),
"days" => $timeKey['days']
];
//今日订单数曲线
$todayHourOrderCount = $storeOrder->getCurveData($orderCountWhere, $where, 'count(*)');
$todayHourOrderCount = $this->trendYdata((array)$todayHourOrderCount, $timeKey);
//昨日订单数
$yesTodayOrderCount = $storeOrder->where($orderCountWhere)->whereDay('create_time', 'yesterday')->count();
//昨日订单曲线
// $yestodayHourOrderCount = $orderService->getOrderGroupCountByWhere($yestodayWhere);
// $yestodayHourOrderCount = $this->trendYdata((array)$yestodayHourOrderCount, 'day');
//订单数环比增长率
$orderCountDayChain = countRate($todayOrderCount, $yesTodayOrderCount);
$data[] = [
'name' => "今日订单数",
'now_value' => $todayOrderCount,
'last_value' => $yesTodayOrderCount,
'rate' => $orderCountDayChain,
'curve' => $todayHourOrderCount
];
/** day支付人数 */
//今日支付人数
$todayPayOrderPeople = $storeOrder->where($orderCountWhere)->whereDay('create_time')->group('uid')->count();
//今日支付人数曲线
$todayHourOrderPeople = $storeOrder->getCurveData($orderCountWhere, $where, 'count(distinct uid)');
$todayHourOrderPeople = $this->trendYdata((array)$todayHourOrderPeople, $where['timeKey']);
//昨日支付人数
$yestodayPayOrderPeople = $storeOrder->where($orderCountWhere)->whereDay('create_time', 'yesterday')->group('uid')->count();
//昨日支付曲线
// $yestodayHourOrderPeople = $orderService->getPayOrderGroupPeopleByWhere($yestodayOrderPeopleWhere);
// $yestodayHourOrderPeople = $this->trendYdata((array)$yestodayHourOrderPeople, 'day');
//订单支付人数环比
$orderPeopleDayChain = countRate($todayPayOrderPeople, $yestodayPayOrderPeople);
$data[] = [
'name' => "今日支付人数",
'now_value' => $todayPayOrderPeople,
'last_value' => $yestodayPayOrderPeople,
'rate' => $orderPeopleDayChain,
'curve' => $todayHourOrderPeople
];
$new_data = [];
foreach ($data as $k => $v) {
$new_data['x'] = $v['curve']['x'];
$new_data['series'][$k]['name'] = $v['name'];
$new_data['series'][$k]['now_money'] = $v['now_value'];
$new_data['series'][$k]['last_money'] = $v['last_value'];
$new_data['series'][$k]['rate'] = $v['rate'];
$new_data['series'][$k]['value'] = array_values($v['curve']['y']);
}
return $new_data;
}
public function getTopRightTwoTrade()
{
$storeOrder = new StoreOrder();
/** month订单数 */
$monthOrderCountWhere['paid'] = 1;
$timeKey = $this->TimeConvert("month");
$where['timeKey'] = [
"start_time" => strtotime($timeKey['start_time']),
"end_time" => strtotime($timeKey['end_time']),
"days" => $timeKey['days']
];
$monthOrderCount = $storeOrder->where($monthOrderCountWhere)->whereBetweenTime('create_time', $where['timeKey']['start_time'], $where['timeKey']['end_time'])->count();
//本月订单数曲线
$monthCurveOrderCount = $storeOrder->getCurveData($monthOrderCountWhere, $where, 'count(*)');
$monthCurveOrderCount = $this->trendYdata((array)$monthCurveOrderCount, $timeKey);
//上月订单数
$lastOrderCountWhere['timeKey'] = $this->TimeConvert("last_month");
$where['timeKey'] = [
"start_time" => strtotime($timeKey['start_time']),
"end_time" => strtotime($timeKey['end_time']),
"days" => $timeKey['days']
];
$lastOrderCount = $storeOrder->where($monthOrderCountWhere)->whereBetweenTime('create_time', $where['timeKey']['start_time'], $where['timeKey']['end_time'])->count();
//上月订单曲线
// $lastCurveOrderCount = $orderService->getOrderGroupCountByWhere($lastOrderCountWhere);
// $lastCurveOrderCount = $this->trendYdata((array)$lastCurveOrderCount, 'month');
//订单数环比增长率
// $orderCountMonthChain = (($monthOrderCount - $lastOrderCount) / $lastOrderCount) * 100;
$orderCountMonthChain = countRate($monthOrderCount, $lastOrderCount);
$data[] = [
'name' => "本月订单数",
'now_value' => $monthOrderCount,
'last_value' => $lastOrderCount,
'rate' => $orderCountMonthChain,
'curve' => $monthCurveOrderCount
];
/** month下单人数 */
//本月支付人数
$timeKey = $this->TimeConvert("month");
$monthOrderPeopleWhere['paid'] = 1;
$where['timeKey'] = [
"start_time" => strtotime($timeKey['start_time']),
"end_time" => strtotime($timeKey['end_time']),
"days" => $timeKey['days']
];
$monthPayOrderPeople = $storeOrder->where($monthOrderPeopleWhere)->whereMonth('create_time')->group('uid')->count();
//本月支付人数曲线
$monthCurveOrderPeople = $storeOrder->getCurveData($monthOrderPeopleWhere, $where, 'count(distinct uid)');
$monthCurveOrderPeople = $this->trendYdata((array)$monthCurveOrderPeople, $timeKey);
//上月支付人数
// $timeKey = $this->TimeConvert("last_month");
$lastPayOrderPeople = $storeOrder->where($monthOrderPeopleWhere)->whereMonth('create_time')->group('uid')->count();
//上月支付曲线
// $lastCurveOrderPeople = $orderService->getPayOrderGroupPeopleByWhere($lastOrderPeopleWhere);
// $lastCurveOrderPeople = $this->trendYdata((array)$lastCurveOrderPeople, 'month');
//订单支付人数环比
$orderPeopleDayChain = countRate($monthPayOrderPeople, $lastPayOrderPeople);
$data[] = [
'name' => "本月支付人数",
'now_value' => $monthPayOrderPeople,
'last_value' => $lastPayOrderPeople,
'rate' => $orderPeopleDayChain,
'curve' => $monthCurveOrderPeople
];
$new_data = [];
foreach ($data as $k => $v) {
$new_data[$k]['name'] = $v['name'];
$new_data[$k]['now_money'] = $v['now_value'];
$new_data[$k]['last_money'] = $v['last_value'];
$new_data[$k]['rate'] = $v['rate'];
$new_data[$k]['value'] = $v['curve']['y'];
}
return $new_data;
}
/**
* 底部数据
* @param $where
* @return array
* @throws \Exception
*/
public function getBottomTrade($where)
{
if (!$where['data']) {
$where['create_time'] = ['start_time' => date('Y-m-d 00:00:00', time()), "end_time" => date('Y-m-d 23:59:59', time())];
} else {
$time = explode("-", $where['data']);
$where['create_time'] = ['start_time' => date('Y-m-d 00:00:00', strtotime($time[0])), "end_time" => date('Y-m-d 23:59:59', strtotime($time[1]))];
}
unset($where['data']);
// /** @var ExportServices $exportService */
// $exportService = app()->make(ExportServices::class);
$chainTime = $this->chainTime($where['create_time']);
$isNum = false;
if ($chainTime == "other") $isNum = true;
$dateWhere['create_time'] = $isNum ? $where['create_time'] : $chainTime;
$topData = array();
$Chain = array();
/** 商品支付金额 */
$OrderMoney = $this->getOrderTotalMoney($where, "sum");
$lastOrderMoney = $this->getOrderTotalMoney($dateWhere, "sum", "", $isNum);
$OrderCurve = $this->getOrderTotalMoney($where, "group", "create_time");
$OrderChain = countRate($OrderMoney, $lastOrderMoney);
$topData[2] = [
'title' => '商品支付金额',
'desc' => '选定条件下,用户购买商品的实际支付金额,包括微信支付、余额支付、支付宝支付、线下支付金额',
'total_money' => $OrderMoney,
'rate' => $OrderChain,
'value' => $OrderCurve['y'],
'type' => 1,
'sign' => 'goods',
];
$Chain['goods'] = $OrderCurve;
//微信支付商品
$wechatOrderMoney = $this->getOrderTotalMoney(['pay_type' => [7, 9], 'create_time' => $where['create_time']], 'sum');
$lastWechatOrderMoney = $this->getOrderTotalMoney(['pay_type' => [7, 9], 'create_time' => $dateWhere['create_time']], 'sum', "", $isNum);
$wechatOrderCurve = $this->getOrderTotalMoney(['pay_type' => [7, 9], 'create_time' => $where['create_time']], 'group', 'create_time');
$wechatOrderChain = countRate($wechatOrderMoney, $lastWechatOrderMoney);
$topData[3] = [
'title' => '微信支付金额',
'desc' => '用户下单时使用微信实际支付的金额',
'total_money' => $wechatOrderMoney,
'rate' => $wechatOrderChain,
'value' => $wechatOrderCurve['y'],
'type' => 1,
'sign' => 'wechat',
];
$Chain['wechat'] = $wechatOrderCurve;
//支付宝支付商品
$aliPayOrderMoney = $this->getOrderTotalMoney(['pay_type' => 13, 'create_time' => $where['create_time']], 'sum');
$lastAlipayOrderMoney = $this->getOrderTotalMoney(['pay_type' => 13, 'create_time' => $dateWhere['create_time']], 'sum', "", $isNum);
$aliPayOrderCurve = $this->getOrderTotalMoney(['pay_type' => 13, 'create_time' => $where['create_time']], 'group', 'create_time');
$aliPayOrderChain = countRate($aliPayOrderMoney, $lastAlipayOrderMoney);
$topData[4] = [
'title' => '支付宝支付金额',
'desc' => '用户下单时使用支付宝实际支付的金额',
'total_money' => $aliPayOrderMoney,
'rate' => $aliPayOrderChain,
'value' => $aliPayOrderCurve['y'],
'type' => 1,
'sign' => 'ali_pay',
];
$Chain['ali_pay'] = $aliPayOrderCurve;
//采购款支付商品
$fundsOrderMoney = $this->getOrderTotalMoney(['pay_type' => 18, 'create_time' => $where['create_time']], 'sum');
$lastFundsOrderMoney = $this->getOrderTotalMoney(['pay_type' => 18, 'create_time' => $dateWhere['create_time']], 'sum', "", $isNum);
$fundsOrderCurve = $this->getOrderTotalMoney(['pay_type' => 18, 'create_time' => $where['create_time']], 'group', 'create_time');
$fundsOrderChain = countRate($fundsOrderMoney, $lastFundsOrderMoney);
$topData[5] = [
'title' => '采购支付金额',
'desc' => '用户下单时使用采购实际支付的金额',
'total_money' => $fundsOrderMoney,
'rate' => $fundsOrderChain,
'value' => $fundsOrderCurve['y'],
'type' => 1,
'sign' => 'funds',
];
$Chain['funds'] = $fundsOrderCurve;
/** 线下收银 */
$offlineMoney = $this->getOfflineTotalMoney($where, 'sum');
$lastOfflineMoney = $this->getOfflineTotalMoney($dateWhere, 'sum', "", $isNum);
$offlineCurve = $this->getOfflineTotalMoney($where, 'group', "create_time");
$offlineChain = countRate($offlineMoney, $lastOfflineMoney);
$topData[6] = [
'title' => '现金收银金额',
'desc' => '选定条件下,用户在线下现金支付的金额',
'total_money' => $offlineMoney,
'rate' => $offlineChain,
'value' => $offlineCurve['y'],
'type' => 0,
'sign' => 'offline',
];
$Chain['offline'] = $offlineCurve;
/** 支出*/
//余额支付商品
$outYeOrderMoney = $this->getOrderTotalMoney(['pay_type' => 3, 'create_time' => $where['create_time']], 'sum');
$lastOutYeOrderMoney = $this->getOrderTotalMoney(['pay_type' => 3, 'create_time' => $dateWhere['create_time']], 'sum', "", $isNum);
$outYeOrderCurve = $this->getOrderTotalMoney(['pay_type' => 3, 'create_time' => $where['create_time']], 'group', 'create_time');
$outYeOrderChain = countRate($outYeOrderMoney, $lastOutYeOrderMoney);
//余额支付
$outYeMoney = $outYeOrderMoney;
$outYeCurve = $this->totalArrData([$outYeOrderCurve, 0]);
$outYeChain = countRate($outYeOrderChain, 0);
$topData[7] = [
'title' => '余额支付金额',
'desc' => '用户下单时使用余额实际支付的金额',
'total_money' => $outYeMoney,
'rate' => $outYeChain,
'value' => $outYeCurve['y'],
'type' => 0,
'sign' => 'yue',
];
$Chain['out_ye'] = $outYeCurve;
//保证金金额
$depositWehre = $where;
$depositWehre['financial_type'] = 11;
$orderDepositMoney = $this->getFinanceFlow($depositWehre, "sum");
$lastOrderDepositMoney = $this->getFinanceFlow($dateWhere, "sum", "", $isNum);
$OrderDepositCurve = $this->getFinanceFlow($where, "group", "create_time");
$OrderDepositChain = countRate($orderDepositMoney, $lastOrderDepositMoney);
$topData[8] = [
'title' => '保证金金额',
'desc' => '门店未交满,订单扣除的保证金',
'total_money' => $orderDepositMoney,
'rate' => $OrderDepositChain,
'value' => $OrderDepositCurve['y'],
'type' => 1,
'sign' => 'deposit',
];
$Chain['deposit'] = $OrderDepositCurve;
//兑换礼品券
// $userSign = $this->getOrderTotalMoney(['pay_type' => 19, 'create_time' => $where['create_time']], 'sum');
// $userSignTwo = $this->getOrderTotalMoney(['pay_type' => 19, 'create_time' => $dateWhere['create_time']], 'sum', "", $isNum);
// $userSignGroup = $this->getOrderTotalMoney(['pay_type' => 19, 'create_time' => $where['create_time']], 'group', 'create_time');
// $userSignRate = countRate($userSign, $userSignTwo);
// $topData[9] = [
// 'title' => '礼品券消耗',
// 'desc' => '用户下单时使用礼品券实际支付的金额',
// 'total_money' => $userSign,
// 'rate' => $userSignRate,
// 'value' => $userSignGroup['y'],
// 'type' => 1,
// 'sign' => 'user_sign',
// ];
// $Chain['user_sign'] = $userSignGroup;
/** 充值金额 */
$rechgeMoneyHome = $this->getRechargeTotalMoney($where, 'sum');
$rechgeMoneyAdmin = $this->getBillYeTotalMoney($where, 'sum');
$rechgeMoneyTotal = bcadd($rechgeMoneyHome, $rechgeMoneyAdmin, 2);
$lastRechgeMoneyHome = $this->getRechargeTotalMoney($dateWhere, 'sum', "", $isNum);
$lastRechgeMoneyAdmin = $this->getBillYeTotalMoney($dateWhere, 'sum', "", $isNum);
$lastRechgeMoneyTotal = bcadd($lastRechgeMoneyHome, $lastRechgeMoneyAdmin, 2);
$RechgeHomeCurve = $this->getRechargeTotalMoney($where, 'group', "create_time");
$RechgeAdminCurve = $this->getBillYeTotalMoney($where, 'group', "create_time");
$RechgeTotalCurve = $this->totalArrData([$RechgeHomeCurve, $RechgeAdminCurve]);
$RechgeChain = countRate($rechgeMoneyTotal, $lastRechgeMoneyTotal);
$topData[10] = [
'title' => '消费金金额',
'desc' => '选定条件下,用户购买的消费金',
'total_money' => $rechgeMoneyTotal,
'rate' => $RechgeChain,
'value' => $RechgeTotalCurve['y'],
'type' => 1,
'sign' => 'rechge',
];
$Chain['rechage'] = $RechgeTotalCurve;
$topData[9] = [
'title' => '微信渠道金额',
'desc' => '用户使用微信下单和购买消费金的金额',
'total_money' => bcadd($wechatOrderMoney,$rechgeMoneyTotal,2),
'rate' => 0,
'value' => [],
'type' => 1,
'sign' => 'wechat_all',
];
//支出金额
// $outTotalMoney = bcadd($outYeMoney, $outExtractMoney, 2);
// $lastOutTotalMoney = bcadd($lastOutYeMoney, $lastOutExtractMoney, 2);
// $outTotalCurve = $this->totalArrData([$outYeCurve, $OutExtractCurve]);
// $outTotalMoney = bcadd($outYeMoney, 0, 2);
// $lastOutTotalMoney = bcadd($lastOutYeMoney, 0, 2);
// $outTotalCurve = $this->totalArrData([$outYeCurve, 0]);
// $outTotalChain = countRate($outTotalMoney, $lastOutTotalMoney);
// $topData[11] = [
// 'title' => '支出金额',
// 'desc' => '余额支付金额、支付佣金金额',
// 'total_money' => $outTotalMoney,
// 'rate' => $outTotalChain,
// 'value' => $outTotalCurve['y'],
// 'type' => 1,
// 'sign' => 'out',
// ];
// $Chain['out'] = $outTotalCurve;
//商品退款金额
// $outOrderRefund = $this->getOrderRefundTotalMoney(['create_time' => $where['create_time']], 'sum');
// $lastOutOrderRefund = $this->getOrderRefundTotalMoney(['create_time' => $dateWhere['create_time']], 'sum', "", $isNum);
// $outOrderRefundCurve = $this->getOrderRefundTotalMoney(['create_time' => $where['create_time']], 'group', 'create_time');
// $orderRefundChain = countRate($outOrderRefund, $lastOutOrderRefund);
// $topData[12] = [
// 'title' => '商品退款金额',
// 'desc' => '用户成功退款的商品金额',
// 'total_money' => $outOrderRefund,
// 'rate' => $orderRefundChain,
// 'value' => $outOrderRefundCurve['y'],
// 'type' => 0,
// 'sign' => 'refund',
// ];
// $Chain['refund'] = $outOrderRefundCurve;
/** @var 营业额 $inTotalMoney */
$inTotalMoney = $this->tradeTotalMoney($where, "sum");
$lastInTotalMoney = $this->tradeTotalMoney($dateWhere, "sum", $isNum);
$inTotalCurve = $this->tradeGroupMoney($where, "group");
$inTotalChain = countRate($inTotalMoney, $lastInTotalMoney);
$topData[0] = [
'title' => '营业额',
'desc' => '商品支付金额、采购支付、线下收银金额',
'total_money' => $inTotalMoney,
'rate' => $inTotalChain,
'value' => $inTotalCurve['y'],
'type' => 1,
'sign' => 'in',
];
ksort($topData);
$data = [];
foreach ($topData as $k => $v) {
// $data['x'] = $Chain['out']['x'];
$data['series'][$k]['name'] = $v['title'];
$data['series'][$k]['desc'] = $v['desc'];
$data['series'][$k]['money'] = $v['total_money'];
$data['series'][$k]['type'] = $v['type'];
$data['series'][$k]['rate'] = $v['rate'];
$data['series'][$k]['value'] = array_values($v['value']);
}
// $export = $exportService->tradeData($data, '交易统计', 2);
// $data['export'] = $export[0];
return $data;
}
/**
* 交易总额
* @param $where
* @param $selectType
* @return array|float|int|mixed
*/
public function tradeTotalMoney($where, $selectType, $isNum = false)
{
/** 收入营业额 */
//商品订单收入
$inOrderMoney = $this->getOrderTotalMoney($where, $selectType, "", $isNum);
//用户充值收入
// $inRechargeMoneyHome = $this->getRechargeTotalMoney($where, $selectType, "", $isNum);
$inrechgeMoneyAdmin = $this->getBillYeTotalMoney($where, $selectType, '', $isNum);
$inRechargeMoney = bcadd(0, $inrechgeMoneyAdmin, 2);
//购买会员收入
$inMemberMoney = $this->getMemberTotalMoney($where, $selectType, "", $isNum);
//线下收款收入
// $inOfflineMoney = $this->getOfflineTotalMoney($where, $selectType, "", $isNum);
//总交易额
// $inTotalMoney = bcadd(bcadd($inOrderMoney, $inRechargeMoney, 2), bcadd($inMemberMoney, $inOfflineMoney, 2), 2);/* - $outExtractUserMoney*/
$inTotalMoney = bcadd(bcadd($inOrderMoney, $inRechargeMoney, 2), $inMemberMoney, 2);/* - $outExtractUserMoney*/
return $inTotalMoney;
}
/**
* 交易额曲线图
* @param $where
* @param $selectType
* @return array
*/
public function tradeGroupMoney($where, $selectType)
{
//商品订单收入
$orderGroup = "create_time";
$OrderMoney = $this->getOrderTotalMoney($where, $selectType, $orderGroup);
//用户充值收入
$rechargeGroup = "create_time";
$RechargeMoneyHome = $this->getRechargeTotalMoney($where, $selectType, $rechargeGroup);
$RechargeMoneyAdmin = $this->getBillYeTotalMoney($where, $selectType, $rechargeGroup);
$RechargeMoney = $this->totalArrData([$RechargeMoneyHome, $RechargeMoneyAdmin]);
//购买会员收入
$memberGroup = "create_time";
$MemberMoney = $this->getMemberTotalMoney($where, $selectType, $memberGroup);
//线下收款收入
$offlineGroup = "create_time";
$OfflineMoney = $this->getOfflineTotalMoney($where, $selectType, $offlineGroup);
return $this->totalArrData([$OrderMoney, $RechargeMoney, $MemberMoney, $OfflineMoney]);
}
/**
* 获取订单退款
* @param $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|float|int
* @throws \Exception
*/
public function getOrderRefundTotalMoney($where, string $selectType, string $group = '', bool $isNum = false)
{
$storeOrder = new StoreOrder();
$orderSumField = "pay_price";
$whereOrder[] = ['refund_status', '>', 0];
$whereOrder['refund_type'] = 6;
$timeKey = $this->TimeConvert($where['create_time'], $isNum);
$where['timeKey'] = $timeKey;
// $where['is_cancel'] = 0;
switch ($selectType) {
case "sum":
$totalMoney = $storeOrder->where($whereOrder)->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('create_time', strtotime($where['timeKey']['start_time']), strtotime($where['timeKey']['end_time']));
})->sum($orderSumField);
break;
case "group":
$totalMoney = $storeOrder->getCurveData($whereOrder, $where, 'sum(pay_price)');
break;
default:
throw new \Exception('getOrderRefundTotalMoney:selectType参数错误');
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $this->TimeConvert($where['create_time'], $isNum));
}
return $totalMoney;
}
/**
* 获取兑换卷
* @param $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|float|int
* @throws \Exception
*/
public function getUserSign($where, string $selectType, string $group = '', bool $isNum = false)
{
$UserSign = new UserSign();
$orderSumField = "number";
$whereUserSign = ['financial_pm' => 1];
$timeKey = $this->TimeConvert($where['create_time'], $isNum);
$where['timeKey'] = $timeKey;
switch ($selectType) {
case "sum":
$totalMoney = $UserSign->where($whereUserSign)->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('create_time', strtotime($where['timeKey']['start_time']), strtotime($where['timeKey']['end_time']));
})->sum($orderSumField);
break;
case "group":
$totalMoney = $UserSign->getCurveData($whereUserSign, $where, 'sum(number)');
break;
default:
throw new \Exception('getUserSign:selectType参数错误');
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $this->TimeConvert($where['create_time'], $isNum));
}
return $totalMoney;
}
/**
* 财务流水
* @param $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|float|int
* @throws \Exception
*/
public function getFinanceFlow($where, string $selectType, string $group = '', bool $isNum = false)
{
$store_finance_flow = new StoreFinanceFlow();
$timeKey = $this->TimeConvert($where['create_time'], $isNum);
unset($where['create_time']);
$time['timeKey'] = $timeKey;
switch ($selectType) {
case "sum":
$totalMoney = $store_finance_flow->where($where)->when(isset($time['timeKey']), function ($query) use ($time) {
$query->whereBetweenTime('create_time', strtotime($time['timeKey']['start_time']), strtotime($time['timeKey']['end_time']));
})->sum('number');
break;
case "group":
$totalMoney = $store_finance_flow->getCurveData($where, $time, 'sum(number)');
break;
default:
throw new \Exception('getFinanceFlow:selectType参数错误');
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $this->TimeConvert($timeKey, $isNum));
}
return $totalMoney;
}
/**
* 获取商品营收
* @param $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|float|int
* @throws \Exception
*/
public function getOrderTotalMoney($where, string $selectType, string $group = "", bool $isNum = false)
{
/** 普通商品订单支付金额 */
$storeOrder = new StoreOrder();
$where['refund_status'] = 0;
$where['paid'] = 1;
$timeKey = $this->TimeConvert($where['create_time'], $isNum);
unset($where['create_time']);
$time['timeKey'] = $timeKey;
switch ($selectType) {
case "sum":
$totalMoney = $storeOrder->where($where)->when(isset($time['timeKey']), function ($query) use ($time) {
$query->whereBetweenTime('create_time', strtotime($time['timeKey']['start_time']), strtotime($time['timeKey']['end_time']));
})->sum('pay_price');
break;
case "group":
$totalMoney = $storeOrder->getCurveData($where, $time, 'sum(pay_price)', $group);
break;
default:
throw new \Exception('getOrderTotalMoney:selectType参数错误');
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $timeKey);
}
return $totalMoney;
}
/**
* 获取用户充值营收
* @param array $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|float|int
* @throws \Exception
*/
public function getRechargeTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
{
/** 用户充值金额 */
$userRechage = new UserRecharge();
$rechargeSumField = "price";
$timeKey = $this->TimeConvert($where['create_time'], $isNum);
$where['timeKey'] = $timeKey;
switch ($selectType) {
case "sum":
$totalMoney = $userRechage->where(['paid' => 1,'status'=>1])
->when(isset($where['create_time']), function ($query) use ($where) {
$query->whereBetweenTime('create_time', strtotime($where['timeKey']['start_time']), strtotime($where['timeKey']['end_time']));
})
->sum($rechargeSumField);
break;
case "group":
$totalMoney = $userRechage->getCurveData(['paid' => 1], $where, 'sum(price)', $group);
break;
default:
$totalMoney = 0.00;
break;
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $timeKey);
}
return $totalMoney;
}
/**
* 购买会员总额
* @param array $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|mixed
* @throws \Exception
*/
public function getMemberTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
{
/** 购买会员 */
// /** @var OtherOrderServices $otherOrderService */
// $otherOrderService = app()->make(OtherOrderServices::class);
// $memberSumField = "pay_price";
// $whereInMember['type'] = 1;
// $whereInMember['paid'] = 1;
// $whereInMember['store_id'] = 0;
// $whereInMember['timeKey'] = $this->TimeConvert($where['time'], $isNum);
// $totalMoney = $otherOrderService->getMemberMoneyByWhere($whereInMember, $memberSumField, $selectType, $group);
// if ($group) {
// $totalMoney = $this->trendYdata((array)$totalMoney, $whereInMember['timeKey']);
// }
return 0;
}
/**
* 后台手动充值
* @param array $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|float|int
* @throws \Exception
*/
public function getBillYeTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
{
/** 后台用户充值金额 */
// $rechargeSumField = "number";
// $whereInRecharge['pm'] = 1;
// $whereInRecharge['type'] = 'system_add';
// $whereInRecharge['timeKey'] = $this->TimeConvert($where['time'], $isNum);
// $whereInRecharge['store_id'] = 0;
// /** @var UserMoneyServices $userMoneyServices */
// $userMoneyServices = app()->make(UserMoneyServices::class);
// $totalMoney = $userMoneyServices->getRechargeMoneyByWhere($whereInRecharge, $rechargeSumField, $selectType, $group);
// if ($group) {
// $totalMoney = $this->trendYdata((array)$totalMoney, $whereInRecharge['timeKey']);
// }
return 0;
}
/**
* 线下付款总额
* @param array $where
* @param string $selectType
* @param string $group
* @param bool $isNum
* @return array|mixed
* @throws \Exception
*/
public function getOfflineTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
{
/** 线下付款总额 */
$storeOrder = new StoreOrder();
$offlineSumField = "pay_price";
// $whereOffline['type'] = 3;
// $whereOffline['paid'] = 1;
// $whereOffline['store_id'] = 0;
// // $whereOffline['pay_type_no'] = 'yue';
$where['timeKey'] = $this->TimeConvert($where['create_time'], $isNum);
// $totalMoney = $otherOrderService->getMemberMoneyByWhere($whereOffline, $offlineSumField, $selectType, $group);
// if ($group) {
// $totalMoney = $this->trendYdata((array)$totalMoney, $whereOffline['timeKey']);
// }
switch ($selectType) {
case "sum":
$totalMoney = $storeOrder->where('pay_type', 17)->when(isset($where['timeKey']), function ($query) use ($where) {
$query->whereBetweenTime('create_time', strtotime($where['timeKey']['start_time']), strtotime($where['timeKey']['end_time']));
})->sum($offlineSumField);
break;
case "group":
$totalMoney = $storeOrder->getCurveData(['pay_type' => 17], $where, 'sum(pay_price)', $group);
break;
default:
throw new \Exception('getOfflineTotalMoney:selectType参数错误');
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $this->TimeConvert($where['create_time'], $isNum));
}
return $totalMoney;
}
/**
* 处理Y坐标数据
* @param array $data
* @param array $timeKey
* @return array
* @throws \Exception
*/
public function trendYdata(array $data, array $timeKey)
{
$hourMoney = array();
$timeData = array();
//获取日期之间的天数
$getDayRange = function ($date, $timeKey) {
$datearr = [];
$stime = strtotime($timeKey['start_time']);
$etime = strtotime($timeKey['end_time']);
while ($stime <= $etime) {
$datearr['x'][] = date($date, $stime);
$datearr['y'][] = date($date, $stime);
$stime = $stime + 86400;
}
return $datearr;
};
//获取日期之间的月份
$getMonthRange = function ($date, $timeKey) {
$datearr = [];
$stime = date('Y-m-d', strtotime($timeKey['start_time']));
$etime = date('Y-m-d', strtotime($timeKey['end_time']));
$start = new \DateTime($stime);
$end = new \DateTime($etime);
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
$datearr['x'][] = $dt->format($date);
$datearr['y'][] = $dt->format($date);
}
return $datearr;
};
if ($timeKey['days'] == 1) {
for ($i = 0; $i <= 24; $i++) {
$timeData['x'][] = (string)($i < 10 ? ('0' . $i) : $i);
$timeData['y'][] = $i < 10 ? ('0' . $i) : $i;
//$timeData['y'][] = $i < 10 ? ('0' . $i . ":00") : $i . ":00";
//$timeData['x'][] = $i < 10 ? ('0' . $i . ":00") : $i . ":00";
}
} elseif ($timeKey['days'] == 30) {
$timeData = $getDayRange('Y-m-d', $timeKey);
} elseif ($timeKey['days'] == 365) {
$timeData = $getMonthRange('Y-m', $timeKey);
} elseif ($timeKey['days'] > 1 && $timeKey['days'] < 30) {
$timeData = $getDayRange('Y-m-d', $timeKey);
} elseif ($timeKey['days'] > 30 && $timeKey['days'] < 365) {
$timeData = $getMonthRange('Y-m', $timeKey);
}
if ($data) {
$hourMoney = array_column($data, 'number', 'time');
}
$y = array();
foreach ($timeData['y'] as $k => $v) {
if (array_key_exists($v, $hourMoney)) {
$y[$v] = $hourMoney[$v];
} else {
$y[$v] = 0;
}
}
return ['x' => $timeData['x'], 'y' => $y];
}
/**
* 获取环比时间类型
* @param $timeKey
* @return string
*/
public function chainTime($timeKey)
{
switch ($timeKey) {
case "today":
return "yestoday";
case "month":
return "last_month";
case "year":
return "last_year";
default:
return "other";
}
}
/**
* 多个数组相加
* @param array $arr
* @return array|false
*/
public function totalArrData(array $arr)
{
if (!$arr || !is_array($arr)) return false;
$item = array();
$y = array_column($arr, "y");
$x = array_column($arr, "x")[0];
foreach ($y as $key => $value) {
foreach ($value as $k => $v) {
if (isset($item[$k])) {
$item[$k] = bcadd($item[$k], $v, 2);
} else {
$item[$k] = $v;
}
}
}
return ['x' => $x, 'y' => $item];
}
/**
* 数组相减
* @param array $arr1
* @param array $arr2
* @return array
*/
public function subdutionArrData(array $arr1, array $arr2)
{
$item = array();
foreach ($arr1['y'] as $key => $value) {
$item['y'][$key] = bcsub($value, $arr2['y'][$key], 2);
}
$item['x'] = $arr1['x'];
return $item;
}
/**
* 搜索时间转换
* @param $timeKey
* @param false $isNum
* @return array
* @throws \Exception
*/
public function TimeConvert($timeKey, $isNum = false)
{
switch ($timeKey) {
case "today":
$data['start_time'] = date('Y-m-d 00:00:00', time());
$data['end_time'] = date('Y-m-d 23:59:59', time());
$data['days'] = 1;
break;
case "yestoday":
$data['start_time'] = date('Y-m-d 00:00:00', strtotime('-1 day'));
$data['end_time'] = date('Y-m-d 23:59:59', strtotime('-1 day'));
$data['days'] = 1;
break;
case "last_month":
$data['start_time'] = date('Y-m-01 00:00:00', strtotime('-1 month'));
$data['end_time'] = date('Y-m-t 23:59:59', strtotime('-1 month'));
$data['days'] = 30;
break;
case "month":
$data['start_time'] = $month_start_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
$data['end_time'] = date('Y-m-d 23:59:59', strtotime("$month_start_time +1 month -1 day"));
$data['days'] = 30;
break;
case "year":
$data['start_time'] = date('Y-01-01 00:00:00', time());
$data['end_time'] = date('Y-12-t 23:59:59', time());
$data['days'] = 365;
break;
case "last_year":
$data['start_time'] = date('Y-01-01 00:00:00', strtotime('-1 year'));
$data['end_time'] = date('Y-12-t 23:59:59', strtotime('-1 year'));
$data['days'] = 365;
break;
case 30:
case 15:
case 7:
if (!$isNum) {
$data['start_time'] = date("Y-m-d 00:00:00", strtotime("-$timeKey day"));
$data['end_time'] = date('Y-m-d 23:59:59', time());
$data['days'] = $timeKey;
} else {
$day = $timeKey * 2;
$data['start_time'] = date("Y-m-d 00:00:00", strtotime("-$day day"));
$data['end_time'] = date("Y-m-d 23:59:59", strtotime("-$timeKey day"));
$data['days'] = $timeKey;
}
break;
default:
$datetime_start = new \DateTime($timeKey['start_time']);
$datetime_end = new \DateTime($timeKey['end_time']);
$days = $datetime_start->diff($datetime_end)->days;
$days = $days > 0 ? $days : 1;
if (!$isNum) {
$data['start_time'] = $timeKey['start_time'];
$data['end_time'] = $timeKey['end_time'];
$data['days'] = $days;
} else {
$data['start_time'] = date("Y-m-d 00:00:00", strtotime("-$days day"));
$data['end_time'] = $timeKey['start_time'];
$data['days'] = $days;
}
}
return $data;
}
}