RelationshipController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Model\Relationship;
  5. use Encore\Admin\Show;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Facades\Admin;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Controllers\ModelForm;
  11. use Illuminate\Support\Facades\Request;
  12. use Illuminate\Support\MessageBag;
  13. use function Clue\StreamFilter\fun;
  14. class RelationshipController extends Controller
  15. {
  16. use ModelForm;
  17. public function index()
  18. {
  19. return Admin::content(function (Content $content) {
  20. $content->header('西医关系');
  21. $content->description('列表');
  22. $content->body($this->grid());
  23. });
  24. }
  25. public function create()
  26. {
  27. return Admin::content(function (Content $content) {
  28. $content->header('西医关系');
  29. $content->description('新增');
  30. $content->body($this->form());
  31. });
  32. }
  33. public function edit($id)
  34. {
  35. return Admin::content(function (Content $content) use ($id) {
  36. $content->header('西医关系');
  37. $content->description('编辑');
  38. $content->body($this->form($id)->edit($id));
  39. });
  40. }
  41. public function form($id = '')
  42. {
  43. return Admin::form(Relationship::class, function (Form $form) {
  44. $form->display('id' , '序号');
  45. $form->select('from_label' , '标签')->options(Relationship::labels())->required();
  46. $form->text('from_name' , '属性名')->required();
  47. $form->select('to_label' , '关联标签')->options(Relationship::labels())->required();
  48. $form->text('to_name' , '关联属性名')->required();
  49. $form->select('relationship' , '关系名称')->options(Relationship::relationships())->required();
  50. $form->saving(function(Form $form) {
  51. if(Relationship::checkIsadd($form->from_name , $form->to_name)) {
  52. $error = new MessageBag([
  53. 'title' => '错误提示',
  54. 'message' => '已建立关联关系,不可重复建立!',
  55. ]);
  56. return back()->with(compact('error'));
  57. } else {
  58. $form->saved(function (Form $form) {
  59. $query = "match (n:".$form->from_label."{name:"."'".$form->from_name."'"."}),"."(m:".$form->to_label."{name:"."'".$form->to_name."'"."})".
  60. "CREATE (n)-[r:".$form->relationship."]->(m) RETURN r";
  61. $result = file_get_contents("https://zskweb.jiankangche.cn/neo4j/setNode?tag=xy&query=$query");
  62. if("{}" == $result) {
  63. Relationship::setStatus($form->id , Relationship::success);
  64. } else {
  65. Relationship::setStatus($form->id , Relationship::failure);
  66. }
  67. });
  68. }
  69. });
  70. });
  71. }
  72. // Grid
  73. protected function grid()
  74. {
  75. return Admin::grid(Relationship::class, function (Grid $grid) {
  76. $grid->id('编号')->sortable();
  77. $grid->model()->orderBy('id','desc');
  78. $grid->from_label('标签');
  79. $grid->from_name('标签属性名称');
  80. $grid->to_label('标签');
  81. $grid->to_name('标签属性名称');
  82. $grid->relationship('关系名称');
  83. $grid->status('状态')->display(function($status) {
  84. return $status == Relationship::success ? "<span class='btn btn-success'>成功</span>" : "<span class='btn btn-danger'>失败</span>";
  85. });
  86. $grid->tag('分类');
  87. $grid->created_at('创建时间');
  88. $grid->updated_at('更新时间');
  89. // 禁用导出数据按钮
  90. $grid->actions(function ($actions) {
  91. $actions->disableView();
  92. //$actions->disableEdit();
  93. });
  94. $grid->filter(function($filter){
  95. // 去掉默认的id过滤器
  96. $filter->disableIdFilter();
  97. // 在这里添加字段过滤器
  98. $filter->equal('relationship', '关系')->select(Relationship::relationships());
  99. $filter->equal('tag' , '分类')->select((new Relationship)->tag);
  100. $filter->equal('status' , '状态')->select((new Relationship)->status);
  101. });
  102. });
  103. }
  104. }