allocatr/application/admin/controller/Orderplan.php
2025-05-06 18:15:03 +08:00

140 lines
3.5 KiB
PHP

<?php
namespace app\admin\controller;
use app\admin\addresmart\Address;
use app\admin\model\AuthGroupAccess;
use app\admin\model\order\Invoice;
use app\admin\model\OrderDispatch;
use app\admin\model\Worker;
use app\admin\model\WorkerItem;
use app\common\controller\Backend;
use app\common\Logic\OrderLogic;
use fast\Tree;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Hook;
use think\Model;
use function Symfony\Component\Clock\now;
/**
* 订单列管理
*
* @icon fa fa-circle-o
*/
class Orderplan extends Backend
{
/**
* Order模型对象
* @var \app\admin\model\Order
*/
protected $model = null;
protected $sources = null;
protected $items = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Order;
}
public function dashboard()
{
$top = $this->getTopTotal();
$lines = $this->getLine();
dd($lines);
return $this->fetch('orderplan/index');
}
private function getTopTotal()
{
$build = new \app\admin\model\Order();
$res = $build->field([
'sum(total) total',
'count(id) count',
'sum(performance) performance'
])->limit(1)->select();
$res = $res[0] ?? false;
if ($res){
return $res->toArray();
}
return [
'total' => 0,
'count' => 0,
'performance' => 0,
];
}
private function getLine()
{
$build = new \app\admin\model\Order();
$start = now()->modify('-14 days')->format('Y-m-d');
$end_at = now()->format('Y-m-d');
$res = $build->field([
'DATE(create_time) day',
'sum(total) total',
'count(id) count',
'sum(performance) performance'
])->group(' DATE(create_time)')
->select();
$data = [];
foreach ($res as $re){
$re = $re->getData();
$data []= $re;
}
return $this->prepareEchartsBarData($data,$start,$end_at);
}
private function prepareEchartsBarData(array $data, string $startDate, string $endDate): array {
// 将原始数据用日期作为键索引
$indexed = [];
foreach ($data as $item) {
$indexed[$item['day']] = $item;
}
$start = strtotime($startDate);
$end = strtotime($endDate);
$xAxis = [];
$series_total = [];
$series_count = [];
$series_performance = [];
$rate = [];
for ($date = $start; $date <= $end; $date += 86400) {
$day = date('Y-m-d', $date);
$xAxis[] = $day;
$total = isset($indexed[$day]) ? (float)$indexed[$day]['total'] : 0;
$count = isset($indexed[$day]) ? (int)$indexed[$day]['count'] : 0;
$performance = isset($indexed[$day]) ? (float)$indexed[$day]['performance'] : 0;
$series_total[] = $total;
$series_count[] = $count;
$series_performance[] = $performance;
$rate[] = $total ? number_format($performance / $total * 100,2) : '0';
}
return [
'xAxis' => $xAxis,
'series' => [
'total' => $series_total,
'count' => $series_count,
'performance' => $series_performance,
'rate' => $rate,
],
];
}
}