Commit 3291bb79 by 赵增煜

首页统计

parent 4f00e9de
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Admin\Controllers; namespace App\Admin\Controllers;
use App\Admin\Metrics\Statistics;
use App\Admin\Metrics\Examples; use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard; use Dcat\Admin\Http\Controllers\Dashboard;
...@@ -17,19 +18,28 @@ public function index(Content $content) ...@@ -17,19 +18,28 @@ public function index(Content $content)
->header('首页') ->header('首页')
->body(function (Row $row) { ->body(function (Row $row) {
// $row->column(6, function (Column $column) { // $row->column(6, function (Column $column) {
// $column->row(Dashboard::title()); // $column->row(Dashboard::title());
// $column->row(new Examples\Tickets()); // $column->row(new Examples\Tickets());
// }); // });
// $row->column(6, function (Column $column) { $row->column(12, function (Column $column) {
// $column->row(function (Row $row) { $column->row(function (Row $row) {
// $row->column(6, new Examples\NewUsers()); $row->column(3, new Statistics\Pharmacy());
// $row->column(6, new Examples\NewDevices()); $row->column(3, new Statistics\Pharmacist());
// }); $row->column(3, new Statistics\Patient());
$row->column(3, new Statistics\Prescription());
// $column->row(new Examples\Sessions());
// $column->row(new Examples\ProductOrders()); //$row->column(6, new Examples\Sessions());
// }); $row->column(6,new Statistics\PrescriptionChart());
$row->column(6,new Statistics\PharmacyChart());
});
//$column->row(new Statistics\PrescriptionChart());
// $row->column(6, new Examples\NewDevices());
//$column->row(new Examples\ProductOrders());
//$column->row(new Examples\Tickets());
});
}); });
} }
} }
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
class NewUsers extends Line
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$this->title('药店数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
// $generator = function ($len, $min = 10, $max = 300) {
// for ($i = 0; $i <= $len; $i++) {
// yield mt_rand($min, $max);
// }
// };
// switch ($request->get('option')) {
// case '365':
// // 卡片内容
// $this->withContent(mt_rand(1000, 5000).'k');
// // 图表数据
// $this->withChart(collect($generator(30))->toArray());
// break;
// case '30':
// // 卡片内容
// $this->withContent(mt_rand(400, 1000).'k');
// // 图表数据
// $this->withChart(collect($generator(30))->toArray());
// break;
// case '28':
// // 卡片内容
// $this->withContent(mt_rand(400, 1000).'k');
// // 图表数据
// $this->withChart(collect($generator(28))->toArray());
// break;
// case '7':
// default:
// 卡片内容
$this->withContent('89.2k');
// 图表数据
// $this->withChart([28, 40, 36, 52, 38, 60, 55]);
//}
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
return $this->chart([
'series' => [
[
'name' => $this->title,
'data' => $data,
],
],
]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\PatientModel;
class Patient extends Line
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$this->title('问诊人数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
$query_time = date('Y-m-d 23:59:59');
$patient_count = PatientModel::where('created_at','<=',$query_time)->count();
// 卡片内容
$this->withContent($patient_count);
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
return $this->chart([
'series' => [
[
'name' => $this->title,
'data' => $data,
],
],
]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\PharmacistModel;
class Pharmacist extends Line
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$this->title('药师数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
$query_time = date('Y-m-d 23:59:59');
$pharmacist_count = PharmacistModel::where('created_at','<=',$query_time)->count();
// 卡片内容
$this->withContent($pharmacist_count);
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
// return $this->chart([
// 'series' => [
// [
// 'name' => $this->title,
// 'data' => $data,
// ],
// ],
// ]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\PharmacyModel;
class Pharmacy extends Line
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$this->title('药店数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
$query_time = date('Y-m-d 23:59:59');
$pharmacy_count = PharmacyModel::where('created_at','<=',$query_time)->count();
// 卡片内容
$this->withContent($pharmacy_count);
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
// return $this->chart([
// 'series' => [
// [
// 'name' => $this->title,
// 'data' => $data,
// ],
// ],
// ]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\PrescriptionModel;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class PharmacyChart extends Bar
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$date = date('Y-m-d',strtotime('-1 day'));
$this->title($date .'开方药店数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
// $query_time = date('Y-m-d 23:59:59');
// $prescription_total = PrescriptionModel::where('created_at','<=',$query_time)->count();
$yesterdayEnd = Carbon::yesterday()->endOfDay();
$eightDaysAgoStart = clone $yesterdayEnd;
$eightDaysAgoStart->subDays(7)->startOfDay();
$datesRange = [];
for ($date = clone $eightDaysAgoStart; $date->lte($yesterdayEnd); $date->addDay()) {
$query_date = $date->format('Y-m-d');
$res = DB::select(
'SELECT COUNT(DISTINCT pharmacy_id) AS pharmacy_count
FROM prescription
WHERE created_at >= :start_of_day
AND created_at < :next_day
AND status = 2',
[
'start_of_day' => $query_date . ' 00:00:00',
'next_day' => $query_date . ' 23:59:59'
]
);
$datesRange[$query_date] = $res[0]->pharmacy_count;
}
// Log::info('结果123:'.json_encode($datesRange));
$labels = array_keys($datesRange);
$data = array_values($datesRange);
// 卡片内容
$this->withContent($data[6]);
// 图表数据
$this->withChart([
[
'name' => '近七日开方药店量',
'data' => $data,
],
]);
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
return $this->chart([
'series' => $data,
]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\PrescriptionModel;
class Prescription extends Line
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$this->title('处方数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
$query_time = date('Y-m-d 23:59:59');
$prescription_total = PrescriptionModel::where('created_at','<=',$query_time)->count();
// 卡片内容
$this->withContent('253');
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
// return $this->chart([
// 'series' => [
// [
// 'name' => $this->title,
// 'data' => $data,
// ],
// ],
// ]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
<?php
namespace App\Admin\Metrics\Statistics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\PrescriptionModel;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class PrescriptionChart extends Bar
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$date = date('Y-m-d',strtotime('-1 day'));
$this->title($date .'开方数量');
}
/**
* 处理请求
*
*
* @return mixed|void
*/
public function handle(Request $request)
{
// $query_time = date('Y-m-d 23:59:59');
// $prescription_total = PrescriptionModel::where('created_at','<=',$query_time)->count();
$yesterdayEnd = Carbon::yesterday()->endOfDay(); // 昨天的23:59:59
// Log::info('参数:' . $yesterdayEnd->format('Y-m-d H:i:s')); // 输出日志信息
$eightDaysAgoStart = clone $yesterdayEnd;
$eightDaysAgoStart->subDays(7)->startOfDay(); // 八天前的00:00:00
// 创建包含过去7天日期的完整数组
$datesRange = [];
for ($date = clone $eightDaysAgoStart; $date->lte($yesterdayEnd); $date->addDay()) {
$datesRange[$date->format('Y-m-d')] = 0; // 初始化所有日期为0
}
$prescriptionCounts = DB::table('prescription')
->select(
DB::raw('DATE(created_at) as date'), // 只获取日期部分
DB::raw('COUNT(*) as prescriptions_count') // 计算每一天的总数
)
->whereBetween('created_at', [$eightDaysAgoStart, $yesterdayEnd]) // 筛选从昨天23:59:59起过去七天的数据
->groupBy('date') // 按日期分组
->orderBy('date') // 按日期排序
->get(); // 执行查询并获取结果
// 将查询结果合并到日期范围数组中
foreach ($prescriptionCounts as $day) {
if (isset($datesRange[$day->date])) {
$datesRange[$day->date] = $day->prescriptions_count;
}
}
// Log::info('结果:'.json_encode($datesRange));
$labels = array_keys($datesRange);
$data = array_values($datesRange);
// 卡片内容
$this->withContent($data[6]);
// 图表数据
$this->withChart([
[
'name' => '近七日开方量',
'data' => $data,
],
]);
}
/**
* 设置图表数据.
*
*
* @return $this
*/
public function withChart(array $data)
{
return $this->chart([
'series' => $data,
]);
}
/**
* 设置卡片内容.
*
* @param string $content
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
HTML
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment