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.

179 lines
5.5 KiB

5 years ago
<?php
/**
* Created by PhpStorm.
* User: weizongsong
* Date: 2019-04-12
* Time: 22:34
*/
namespace App\Http\Controllers\Admin;
use App\Events\ProjectSaved;
use App\Forms\ProjectForm;
use App\Models\Area;
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 Kris\LaravelFormBuilder\FormBuilder;
class ProjectController extends CommonController
{
public $bladePath = "admin.project";
public $urlPrefix = "admin/project";
public $modelName = "医院/项目";
public $modelClass = Project::class;
public $formClass = ProjectForm::class;
public function index(Request $request)
{
$data = $this->model->with("paramedicLevels")->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" => function ($query) {
$query->with(["areas" => function ($query) {
$query->with("rooms");
}]);
}])->find($id);
5 years ago
$data = (new Bed())->where("project_id", $id)->orderBy("id","desc")->with(["project", "building", "area", "room"])->paginate(10);
5 years ago
return view($this->bladePath . ".beds", compact("project", "data"));
}
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);
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);
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 editDepartment(Request $request)
{
$data = [
"name" => $request->name,
"myindex" => $request->myindex
];
switch ($request->type) {
case "building":
$res = (new Building())->find($request->id)->update($data);
break;
case "area":
$res = (new Area())->find($request->id)->update($data);
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("删除失败");
}
}
}