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.

620 lines
23 KiB

<?php
/**
* Created by PhpStorm.
* User: weizongsong
* Date: 2019-04-12
* Time: 22:34
*/
namespace App\Http\Controllers\Admin;
use App\Admin;
use App\Events\ProjectSaved;
use App\Exports\CommonExport;
use App\Forms\AskSubmitForm;
use App\Forms\AskSubmitScForm;
use App\Forms\ProjectForm;
use App\Models\AdminAreaLink;
use App\Models\AdminBuildingLink;
use App\Models\Area;
use App\Models\AskSubmit;
use App\Models\Bed;
use App\Models\Building;
use App\Models\ParamedicLevel;
use App\Models\Project;
use App\Models\Room;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Kris\LaravelFormBuilder\Field;
use Kris\LaravelFormBuilder\FormBuilder;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Spatie\Permission\Models\Role;
class ProjectController extends CommonController
{
public $bladePath = "admin.project";
public $urlPrefix = "admin/project";
public $modelName = "医院/项目";
public $modelClass = Project::class;
public $formClass = ProjectForm::class;
public function _checkProjects()
{
$projects = (new Project())->adminProject()->orderBy("id", "desc")->get();
view()->share(compact("projects"));
return $projects;
}
public function index(Request $request)
{
$data = $this->model->with(["paramedicLevels","wechatpayAccount","alipayAccount"])->paginate(10);
return view($this->bladePath . ".index", compact("data"));
}
public function stored($model)
{
event(new ProjectSaved($model));
}
public function edit($id = null, Request $request, FormBuilder $formBuilder)
{
$vo = (new Project())->with("paramedicLevels")->find($id ?: $request->id);
$form = $formBuilder->create($this->formClass, [
"method" => "POST",
"id" => "fm",
"url" => url($this->urlPrefix . "/update/" . $vo->id),
"class" => "form form-horizontal validate-form",
"model" => $vo
]);
$form->add("_previous", "hidden", ["value" => (url()->previous())]);
return view($this->bladePath . ".create", compact("form"));
}
public function updated($model)
{
event(new ProjectSaved($model));
}
public function beds($id)
{
$project = (new Project())->with(["buildings"])->find($id);
$data = (new Bed())->where("project_id", $id)->orderBy("id", "desc")->with(["project", "building", "area", "room"]);
if (request()->room_id) {
$data = $data->where("room_id", request()->room_id);
$room = (new Room())->find(request()->room_id);
$rooms = (new Room())->where("area_id", $room->area_id)->get();
$area = (new Area())->where("id", $room->area_id)->first();
$areas = (new Area())->where("building_id", $area->building_id)->get();
view()->share(compact("rooms", "areas"));
} elseif (request()->area_id) {
$data = $data->where("area_id", request()->area_id);
$area = (new Area())->find(request()->area_id);
$areas = (new Area())->where("building_id", $area->building_id)->get();
$rooms = (new Room())->where("area_id", $area->id)->get();
view()->share(compact("areas", "rooms"));
} elseif (request()->building_id) {
$areas = (new Area())->where("building_id", request()->building_id)->get();
$data = $data->where("building_id", request()->building_id);
view()->share(compact("areas"));
}
$data = $data->paginate(10);
return view($this->bladePath . ".beds", compact("project", "data"));
}
public function getHeadList()
{
$areaId = \request('area_id', 0);
$project_id = \request('project_id');
// 获取护士长
$roleId = Role::where('name', 'like', '%护士长%')->where('guard_name', 'admin')->value('id');
$adminIds = DB::table('model_has_roles')->where('role_id', $roleId)->where('model_type', 'App\Admin')->pluck('model_id');
// 全量护工数据
$headList = Admin::whereIn('id', $adminIds)->whereRaw("find_in_set('$project_id',project_ids)")->get();
// 获取病区选中的护工
foreach ($headList as $item) {
$item->selected = '';
if ($areaId) {
$has = AdminAreaLink::where('area_id', $areaId)->where('admin_id', $item->id)->count();
if ($has) $item->selected = 'selected';
}
}
return $this->ajaxResponse($headList);
}
public function getYuanfangList()
{
$buildingId = \request('building_id', 0);
$project_id = \request('project_id');
// 获取院方管理
$roleId = Role::where('name', 'like', '%院方管理%')->where('guard_name', 'admin')->value('id');
$adminIds = DB::table('model_has_roles')->where('role_id', $roleId)->where('model_type', 'App\Admin')->pluck('model_id');
// 全量院方管理数据
$yuanfangList = Admin::whereIn('id', $adminIds)->whereRaw("find_in_set('$project_id',project_ids)")->get();
// 获取楼栋选中的院方管理
foreach ($yuanfangList as $item) {
$item->selected = '';
if ($buildingId) {
$has = AdminBuildingLink::where('building_id', $buildingId)->where('admin_id', $item->id)->count();
if ($has) $item->selected = 'selected';
}
}
return $this->ajaxResponse($yuanfangList);
}
public function createSub(Request $request)
{
switch ($request->type) {
case "project":
$data = [
"project_id" => $request->id,
"name" => $request->name,
"myindex" => $request->myindex
];
$res = (new Building())->create($data);
// 添加选中的院方管理
if (isset($request->yuanfang) && is_array($request->yuanfang)) {
$links = [];
foreach ($request->yuanfang as $item) {
$links[] = [
'admin_id' => $item,
'building_id' => $res->id,
'project_id' => $res->project_id,
'created_at' => date('Y-m-d H:i:s')
];
}
if (!empty($links)) {
AdminBuildingLink::insert($links);
}
}
break;
case "building":
$parent = Building::find($request->id);
$data = [
"project_id" => $parent->project_id,
"building_id" => $request->id,
"name" => $request->name,
"myindex" => $request->myindex
];
$res = (new Area())->create($data);
// 添加选中的护工
$links = [];
foreach ($request->head as $item) {
$links[] = [
'admin_id' => $item,
'area_id' => $res->id,
'project_id' => $res->project_id,
'created_at' => date('Y-m-d H:i:s')
];
}
AdminAreaLink::insert($links);
break;
case "area":
$parent = Area::find($request->id);
$data = [
"project_id" => $parent->project_id,
"building_id" => $parent->building_id,
"area_id" => $request->id,
"name" => $request->name,
"myindex" => $request->myindex
];
$res = (new Room())->create($data);
break;
case "room":
$parent = Room::find($request->id);
$data = [
"project_id" => $parent->project_id,
"building_id" => $parent->building_id,
"area_id" => $parent->area_id,
"room_id" => $request->id,
"name" => $request->name,
"myindex" => $request->myindex
];
$res = (new Bed())->create($data);
break;
default:
return $this->error("不正确的类型");
}
return $this->success("添加成功!", '', $res);
}
public function getSubs(Request $request)
{
switch ($request->type) {
case "building":
$res = (new Area())->where("building_id", $request->id)->get();
break;
case "area":
$res = (new Room())->where("area_id", $request->id)->get();
break;
default:
$res = [];
}
return $this->ajaxResponse($res);
}
public function editDepartment(Request $request)
{
$data = [
"name" => $request->name,
"myindex" => $request->myindex
];
switch ($request->type) {
case "building":
$res = (new Building())->find($request->id);
$res->update($data);
// 删除原来关联,添加新关联
AdminBuildingLink::where('building_id', $request->id)->delete();
if (isset($request->yuanfang) && is_array($request->yuanfang)) {
$links = [];
foreach ($request->yuanfang as $item) {
$links[] = [
'admin_id' => $item,
'building_id' => $request->id,
'project_id' => $res->project_id,
'created_at' => date('Y-m-d H:i:s')
];
}
if (!empty($links)) {
AdminBuildingLink::insert($links);
}
}
break;
case "area":
$res = (new Area())->find($request->id);
$res->update($data);
// 删除原来关联,添加新关联
AdminAreaLink::where('area_id', $request->id)->delete();
$links = [];
foreach ($request->head as $item) {
$links[] = [
'admin_id' => $item,
'area_id' => $request->id,
'project_id' => $res->project_id,
'created_at' => date('Y-m-d H:i:s')
];
}
AdminAreaLink::insert($links);
break;
case "room":
$res = (new Room())->find($request->id)->update($data);
break;
case "bed":
$res = (new Bed())->find($request->id)->update($data);
break;
default:
return $this->error("不正确的类型");
}
return $this->success($res);
}
public function deleteDepartment(Request $request)
{
switch ($request->type) {
case "building":
$vo = (new Building())->find($request->id);
break;
case "area":
$vo = (new Area())->find($request->id);
break;
case "room":
$vo = (new Room())->find($request->id);
break;
case "bed":
$vo = (new Bed())->find($request->id);
break;
default:
return $this->error("不正确的类型");
}
$res = $vo->delete();
if ($res) {
return $this->success("删除成功");
} else {
return $this->success("删除失败");
}
}
function exportBeds(Request $request)
{
$beds = Bed::with(["area", "building", "room"])
->whereHas("area")
->whereHas("building")
->whereHas("room")
->where("project_id", $request->project_id)
->orderBy("building_id")
->orderBy("area_id")
->orderBy("room_id")
->get();
$data = [];
foreach ($beds as $bed) {
$data[] = [
"楼栋" => $bed->building ? $bed->building->name : "",
"病区" => $bed->area ? $bed->area->name : "",
"病房" => $bed->room ? $bed->room->name : "",
"床号" => $bed->name,
];
}
$data = collect($data);
return Excel::download(new CommonExport($data), "beds.xlsx");
}
/**
* 满意度调查
*/
public function askSubmit()
{
$is_export = \request('export', 0);
$userId = auth()->id();
$projects = (new ProjectController())->_checkProjects();
$project_id = \request('project_id', ($projects[0]->id) ?? '');
// 判断是否护士长
$roleId = Role::where('name', 'like', '%护士长%')->where('guard_name', 'admin')->value('id');
$hushizhang = DB::table('model_has_roles')->where('role_id', $roleId)
->where('model_type', 'App\Admin')
->where('model_id', $userId)->count();
$data = AskSubmit::with("admin", "project")
->where(function ($qeury) use ($hushizhang, $userId, $project_id) {
if ($hushizhang) $qeury->where('admin_id', $userId);
if ($project_id) $qeury->where('project_id', $project_id);
});
// 导出
if ($is_export) {
$data = $data->get()->toArray();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', '日期');
$sheet->setCellValue('B1', '名字');
$sheet->setCellValue('C1', '医院');
$sheet->setCellValue('D1', '详情');
$sheet->setCellValue('E1', '建议');
$sheet->setCellValue('F1', '总分');
$sheet->getColumnDimension('A')->setWidth(20);
$sheet->getColumnDimension('B')->setWidth(20);
$sheet->getColumnDimension('C')->setWidth(20);
$sheet->getColumnDimension('D')->setWidth(60);
$sheet->getColumnDimension('E')->setWidth(20);
$sheet->getColumnDimension('F')->setWidth(20);
$count = count($data); //计算有多少条数据1
for ($i = 2; $i <= $count + 1; $i++) {
$content = '';
foreach ($data[$i - 2]['content'] as $item) {
$content .= $item['ask'] . ':' . $item['score'] . "\n";
}
if ($data[$i - 2]['mobile']) {
$name = $data[$i - 2]['name'] . "(" . $data[$i - 2]['mobile'] . ")";
} else {
$name = $data[$i - 2]['admin']['name'];
}
$sheet->setCellValue('A' . $i, $data[$i - 2]['date']);
$sheet->setCellValue('B' . $i, $name);
$sheet->setCellValue('C' . $i, $data[$i - 2]['project']['name']);
$sheet->setCellValue('D' . $i, $content);
$sheet->setCellValue('E' . $i, $data[$i - 2]['tip']);
$sheet->setCellValue('F' . $i, $data[$i - 2]['score']);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . '满意度调查_' . date('YmdHis') . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;
}
$data = $data->orderBy('id', 'desc')->paginate(10);
$data = $data->appends(['project_id' => $project_id]);
return view($this->bladePath . ".asksubmit_index", compact("data", "projects", "project_id"));
}
public function askSubmitCreate(FormBuilder $formBuilder)
{
$project_ids = auth()->user()->project_ids;
$project_id = \request('project_id');
if ($project_ids) {
$project_ids = explode(',', $project_ids);
}
if ($project_id == 7) {
$askSubmitForm = AskSubmitScForm::class;
} else {
$askSubmitForm = AskSubmitForm::class;
}
$form = $formBuilder->create($askSubmitForm, [
"method" => "POST",
"id" => "fm",
"url" => url($this->urlPrefix . "/asksubmit_store?project_id=" . $project_id)
]);
$choices = (new Project())->where(function ($query) use ($project_ids) {
if ($project_ids) {
$query->whereIn("id", $project_ids);
}
})->get()->pluck("name", "id");
$form->modify("project_id", "select", [
"choices" => $choices->toArray(),
"selected" => $choices->keys()->first()
]);
$form->modify('project_id', Field::HIDDEN, ["default_value" => $project_id]);
$form->modify('date', Field::DATE, ['default_value' => date('Y-m-d')]);
return view($this->bladePath . ".asksubmit_create", compact("form"));
}
public function askSubmitStore(Request $request)
{
$all = \request()->all();
$project_id = \request('project_id');
$userId = auth()->user()->id;
// 一个月只能提交一次
$has = AskSubmit::where('admin_id', $userId)
->where('project_id', $all['project_id'])
->where('date', 'like', '%' . date('Y-m') . '%')
->first();
if ($has) return $this->error("每个月只能提交一次");
$content = [];
$socre = 0;
$askList = AskSubmit::$askList;
if ($project_id == 7) {
$askList = AskSubmit::$askListSc;
}
foreach ($all['ask'] as $key => $item) {
$content[] = [
'ask' => $askList[$key],
'score' => $item
];
$socre += $item;
}
$all['admin_id'] = $userId;
$all['score'] = $socre;
$all['content'] = $content;
$model = AskSubmit::create($all);
$url = "/admin/project/asksubmit";
return $this->success("新增成功", $url, $model);
}
public function askSubmitEdit(Request $request, FormBuilder $formBuilder)
{
$vo = AskSubmit::find($request->id);
if ($vo->project_id == 7) {
$askSubmitForm = AskSubmitScForm::class;
} else {
$askSubmitForm = AskSubmitForm::class;
}
$form = $formBuilder->create($askSubmitForm, [
"method" => "POST",
"id" => "fm",
"url" => url($this->urlPrefix . "/asksubmit_update/" . $vo->id),
"class" => "form form-horizontal validate-form",
"model" => $vo
]);
$project_ids = auth()->user()->project_ids;
if ($project_ids) {
$project_ids = explode(',', $project_ids);
}
$form->modify("project_id", "select", [
"choices" => (new Project())->where(function ($query) use ($project_ids) {
if ($project_ids) {
$query->whereIn("id", $project_ids);
}
})->get()->pluck("name", "id")->toArray()
]);
$form->modify('project_id', Field::HIDDEN, ["default_value" => $vo->project_id]);
$form->add("_previous", "hidden", ["value" => (url()->previous())]);
return view($this->bladePath . ".asksubmit_create", compact("form"));
}
public function askSubmitUpdate($id = null, Request $request)
{
try {
$all = $request->all();
$model = AskSubmit::find($id);
$content = [];
$socre = 0;
foreach ($all['ask'] as $key => $item) {
$content[] = [
'ask' => AskSubmit::$askList[$key],
'score' => $item
];
$socre += $item;
}
$all['score'] = $socre;
$all['content'] = $content;
$model->fill($all);
$model->save();
$url = "/admin/project/asksubmit/" . $request->project_id;
return $this->success("修改成功", $url, $model);
} catch (\Exception $exception) {
return $this->error("修改失败:" . $exception->getMessage());
}
}
public function delete(Request $request)
{
try {
$array = explode('-', $request->id);
if (count($array) == 2) {
$model = AskSubmit::find($array[1]);
} else {
$model = $this->model->find($request->id);
}
$model->delete();
$this->deleted($model);
return $this->success("删除成功");
} catch (\Exception $exception) {
return $this->error("删除失败:" . $exception->getMessage());
}
}
public function askSubmitCreateV2(FormBuilder $formBuilder)
{
$project_id = \request('project_id');
if ($project_id == 7) {
$askSubmitForm = AskSubmitScForm::class;
} else {
$askSubmitForm = AskSubmitForm::class;
}
$form = $formBuilder->create($askSubmitForm, [
"method" => "POST",
"id" => "fm",
"url" => url($this->urlPrefix . "/asksubmit_store_v2?project_id=" . $project_id)
]);
$choices = (new Project())->get()->pluck("name", "id");
$form->modify("project_id", "select", [
"choices" => $choices->toArray(),
"selected" => $choices->keys()->first()
]);
$form->modify('project_id', Field::HIDDEN, ["default_value" => $project_id]);
$form->modify('date', Field::DATE, ['default_value' => date('Y-m-d')]);
return view($this->bladePath . ".asksubmit_create_v2", compact("form"));
}
public function askSubmitStoreV2(Request $request)
{
$all = \request()->all();
if (!isset($all['mobile']) || empty($all['mobile'])) {
return $this->error("手机号不存在", '');
}
$project_id = \request('project_id');
// 一个月只能提交一次
$has = AskSubmit::where('project_id', $all['project_id'])
->where('mobile', $all['mobile'])
->first();
if ($has) return $this->error("请勿重复提交");
$content = [];
$socre = 0;
$askList = AskSubmit::$askList;
if ($project_id == 7) {
$askList = AskSubmit::$askListSc;
}
foreach ($all['ask'] as $key => $item) {
$content[] = [
'ask' => $askList[$key],
'score' => $item
];
$socre += $item;
}
$all['score'] = $socre;
$all['content'] = $content;
$model = AskSubmit::create($all);
return $this->success("新增成功", '', $model);
}
}