|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
use App\Models\AdminAreaLink;
|
|
|
use App\Models\Area;
|
|
|
use App\Models\Bed;
|
|
|
use App\Models\FactorItems;
|
|
|
use App\Models\OrderItems;
|
|
|
use App\Models\Orders;
|
|
|
use App\Models\Paramedic;
|
|
|
use App\Models\Product;
|
|
|
use App\Models\ProductItems;
|
|
|
use App\Models\Project;
|
|
|
use App\Models\Refund;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
class HomeController extends CommonController
|
|
|
{
|
|
|
public $bladePath = "admin";
|
|
|
|
|
|
public function index()
|
|
|
{
|
|
|
$project_id = request()->project_id;
|
|
|
$projects = Project::get();
|
|
|
if (empty($project_id)) {
|
|
|
$project_id = Project::first()->id;
|
|
|
}
|
|
|
$counts = [];
|
|
|
// 订单总数
|
|
|
$counts["orders_all"] = (new Orders())->ofProject($project_id)->count();
|
|
|
// 待处理
|
|
|
$counts["orders_pending"] = (new Orders())->ofProject($project_id)->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED])->count();
|
|
|
// 在陪患者
|
|
|
$counts["orders_ongoing"] = (new Orders())->ofProject($project_id)->where("status", Orders::STATUS_ONGOING)->count();
|
|
|
// 日派工
|
|
|
$counts["orders_ongoing_in_today"] = (new Orders())->ofProject($project_id)
|
|
|
->where("status", Orders::STATUS_ONGOING)
|
|
|
->whereRaw("DATEDIFF(`from_date`,now()) = 0")
|
|
|
->count();
|
|
|
// 已完成
|
|
|
$counts["orders_finished"] = (new Orders())->ofProject($project_id)->where("status", Orders::STATUS_FINISHED)->count();
|
|
|
// 日结算
|
|
|
$counts["orders_finished_in_today"] = (new Orders())->ofProject($project_id)
|
|
|
->where("status", Orders::STATUS_FINISHED)
|
|
|
->whereRaw("DATEDIFF(`to_date`,now()) = 0")
|
|
|
->count();
|
|
|
// 护工数量
|
|
|
$counts["paramedics"] = (new Paramedic())->ofProject($project_id)->count();
|
|
|
// 今日应收
|
|
|
$counts["bill_today"] = (new OrderItems())->where("service_date", date("Y-m-d"))->whereHas("order", function ($query) use ($project_id) {
|
|
|
$query->ofProject($project_id);
|
|
|
})->sum("total");
|
|
|
// 今日退款
|
|
|
$counts["refund_today"] = (new Refund())->whereNotNull("paid_at")->whereRaw("DATEDIFF(`paid_at`,now()) = 0")->whereHas("order", function ($query) use ($project_id) {
|
|
|
$query->ofProject($project_id);
|
|
|
})->sum("money");
|
|
|
// 在岗护工
|
|
|
$counts["paramedic_has_order"] = (new Paramedic())->ofProject($project_id)->whereHas("orders", function ($query) {
|
|
|
$query->where("status", Orders::STATUS_ONGOING);
|
|
|
})->count();
|
|
|
|
|
|
// 各产品占比
|
|
|
$lies = [];
|
|
|
$product = Product::where('project_id', $project_id)->first();
|
|
|
if ($product) {
|
|
|
$productItem = ProductItems::where('product_id', $product->id)->get();
|
|
|
$factor = FactorItems::where('factor_id', $product->statistic_factor_id)->get();
|
|
|
$order_total = OrderItems::whereIn('product_item_id', $productItem->pluck('id'))->sum('total');
|
|
|
|
|
|
$lies = Cache::remember('cache_lies', 24 * 60 * 60, function () use ($productItem, $factor, $order_total) {
|
|
|
return $this->getLies($productItem, $factor, $order_total);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
$allMontn = [
|
|
|
date("Y") . '-01',
|
|
|
date("Y") . '-02',
|
|
|
date("Y") . '-03',
|
|
|
date("Y") . '-04',
|
|
|
date("Y") . '-05',
|
|
|
date("Y") . '-06',
|
|
|
date("Y") . '-07',
|
|
|
date("Y") . '-08',
|
|
|
date("Y") . '-09',
|
|
|
date("Y") . '-10',
|
|
|
date("Y") . '-11',
|
|
|
date("Y") . '-12',
|
|
|
];
|
|
|
// 销售额
|
|
|
$saleList = [];
|
|
|
foreach ($allMontn as $month) {
|
|
|
$saleList[] = [
|
|
|
'month' => $month,
|
|
|
'total' => (new OrderItems())->where("service_date", 'like', $month . '%')->whereHas("order", function ($query) use ($project_id) {
|
|
|
$query->ofProject($project_id);
|
|
|
})->sum("total")
|
|
|
];
|
|
|
}
|
|
|
// 订单量
|
|
|
$orderList = [];
|
|
|
foreach ($allMontn as $month) {
|
|
|
$orderList[] = [
|
|
|
'month' => $month,
|
|
|
'total' => (new Orders())->ofProject($project_id)->where('created_at', 'like', $month . '%')->count()
|
|
|
];
|
|
|
}
|
|
|
// 判断是否有权限
|
|
|
$userId = auth()->id();
|
|
|
$roleId = Role::where('name', 'like', '%首页统计%')->where('guard_name', 'admin')->value('id');
|
|
|
$tongji = DB::table('model_has_roles')->where('role_id', $roleId)
|
|
|
->where('model_type', 'App\Admin')->where('model_id', $userId)->count();
|
|
|
// 判断是否有大屏幕权限
|
|
|
$roleIdBydp = Role::where('name', 'like', '%大屏统计%')->where('guard_name', 'admin')->value('id');
|
|
|
$dp = DB::table('model_has_roles')->where('role_id', $roleIdBydp)
|
|
|
->where('model_type', 'App\Admin')->where('model_id', $userId)->count();
|
|
|
|
|
|
if (is_mobile() || empty($tongji)) {
|
|
|
return view($this->bladePath . ".mobile_home");
|
|
|
} else {
|
|
|
$jump_dp = 0;
|
|
|
if ($dp) $jump_dp = 1;
|
|
|
return view($this->bladePath . ".home", compact('jump_dp', 'project_id', 'projects', 'counts', 'lies', 'saleList', 'orderList'));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取动态列
|
|
|
*/
|
|
|
public function getLies($productItem, $factor, $order_total)
|
|
|
{
|
|
|
$list = [];
|
|
|
foreach ($productItem as $item) {
|
|
|
foreach ($factor as $factor_item) {
|
|
|
// 修复:使用 MySQL 5.7 的 JSON 函数进行精确查询,防止SQL注入
|
|
|
$factorItemId = (int) $factor_item->id;
|
|
|
$total = OrderItems::where('product_item_id', $item->id)
|
|
|
->whereRaw("JSON_SEARCH(factors, 'one', ?, NULL, '$[*].factor_item_id') IS NOT NULL", [$factorItemId])
|
|
|
->sum('total');
|
|
|
|
|
|
// 修复:明确计算价格总和,避免运算符优先级问题
|
|
|
$totalPrice = (float) $item->price + (float) $factor_item->price;
|
|
|
|
|
|
$list[] = [
|
|
|
'name' => $totalPrice . '元/天',
|
|
|
'total_price' => $totalPrice,
|
|
|
'product_item_id' => $item->id,
|
|
|
'factor_item_id' => $factor_item->id,
|
|
|
'total' => $total,
|
|
|
'product_item_name' => $item->name,
|
|
|
'rate' => round($total / $order_total, 2)
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
return $list;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 统计
|
|
|
*/
|
|
|
public function statistic()
|
|
|
{
|
|
|
// 护工陪护
|
|
|
$paramedic = [
|
|
|
'total' => Paramedic::count(),
|
|
|
'has_accompany' => Paramedic::whereHas('ongoingOrders')->count(),
|
|
|
'no_accompany' => Paramedic::whereDoesntHave('ongoingOrders')->count(),
|
|
|
'accompany_order' => Orders::where('status', Orders::STATUS_ONGOING)->count(),
|
|
|
];
|
|
|
// 床位陪护
|
|
|
$bed = [
|
|
|
'total' => Bed::count(),
|
|
|
'has_accompany' => Bed::whereHas('onGoingOrder')->count(),
|
|
|
'no_accompany' => Bed::whereDoesntHave('onGoingOrder')->count(),
|
|
|
];
|
|
|
// 陪护统计表
|
|
|
$project = Project::get();
|
|
|
foreach ($project as $item) {
|
|
|
$item->paramedic_total = Paramedic::where('project_id', $item->id)->count();
|
|
|
$item->total = Orders::where('project_id', $item->id)->count();
|
|
|
}
|
|
|
// 最新陪护单
|
|
|
$lastOrder = Orders::with('project')
|
|
|
->orderBy('id', 'desc')
|
|
|
->limit(8)
|
|
|
->get();
|
|
|
// 最新出院信息
|
|
|
$outOrder = Orders::with('project')
|
|
|
->where('status', Orders::STATUS_FINISHED)
|
|
|
->orderBy('id', 'desc')
|
|
|
->limit(8)
|
|
|
->get();
|
|
|
// 最新投诉信息
|
|
|
$tip = Orders::with('project')
|
|
|
->whereNotNull('comment')
|
|
|
->orderBy('id', 'desc')
|
|
|
->limit(8)
|
|
|
->get();
|
|
|
// 上月满意度评价分析
|
|
|
$lastMonth = date('Y-m', strtotime('-1 month'));
|
|
|
$satisfied = Orders::with('project', 'paramedic')
|
|
|
->whereNotNull('comment')
|
|
|
->where('created_at', 'like', '%' . $lastMonth . '%')
|
|
|
->orderBy('score', 'desc')
|
|
|
->limit(8)
|
|
|
->get();
|
|
|
// 本月营收分析
|
|
|
$income = Project::get();
|
|
|
foreach ($income as $item) {
|
|
|
$item->total = Orders::where('project_id', $item->id)
|
|
|
->where('created_at', 'like', '%' . date('Y-m') . '%')
|
|
|
->where('status', Orders::STATUS_FINISHED)
|
|
|
->sum('paid_total');
|
|
|
}
|
|
|
// 本月陪护订单
|
|
|
$monthAccompany = Project::get();
|
|
|
foreach ($monthAccompany as $item) {
|
|
|
$item->total = Orders::where('project_id', $item->id)
|
|
|
->where('created_at', 'like', '%' . date('Y-m') . '%')
|
|
|
->count();
|
|
|
}
|
|
|
return $this->ajaxSuccess('获取成功', compact('paramedic', 'bed', 'project',
|
|
|
'lastOrder', 'outOrder', 'tip', 'satisfied', 'income', 'monthAccompany'));
|
|
|
}
|
|
|
}
|