TextLabelController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Post\ImportExcel;
  4. use App\Http\Controllers\Controller;
  5. use App\Model\TextLabel;
  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\Http\Request;
  12. use SebastianBergmann\CodeCoverage\Report\Html\Renderer;
  13. class TextLabelController extends Controller
  14. {
  15. use ModelForm;
  16. public function index()
  17. {
  18. return Admin::content(function (Content $content) {
  19. $content->header('文案标注');
  20. $content->description('列表');
  21. $content->body($this->grid());
  22. });
  23. }
  24. // public function create()
  25. // {
  26. // return Admin::content(function (Content $content) {
  27. // $content->header('视频');
  28. // $content->description('新增');
  29. // $content->body($this->form());
  30. // });
  31. // }
  32. public function edit($id)
  33. {
  34. return Admin::content(function (Content $content) use ($id) {
  35. $content->header('文案标注');
  36. $content->description('列表');
  37. $content->body($this->form($id)->edit($id));
  38. });
  39. }
  40. protected function form($id = '')
  41. {
  42. $data = [
  43. 'symptom' => [],
  44. 'operation' => [],
  45. 'medicine' => [],
  46. 'disease' => [],
  47. 'inspection' => [],
  48. 'test' => [],
  49. 'date' => []
  50. ];
  51. self::botLabel($id,$data);
  52. return Admin::form(TextLabel::class, function (Form $form) use ($data){
  53. $form->display('id' , '编号');
  54. $form->display('text' , '文案');
  55. $form->embeds('symptom','症状',function ($form) use ($data){
  56. $num = 0;
  57. foreach ($data['symptom'] as $item){
  58. $form->text($num,'terms')->default($item);
  59. $num ++;
  60. }
  61. });
  62. $form->list('add_symptom');
  63. $form->embeds('disease','疾病',function ($form) use ($data){
  64. $num = 0;
  65. foreach ($data['disease'] as $item){
  66. $form->text($num,'terms')->default($item);
  67. $num ++;
  68. }
  69. });
  70. $form->list('add_disease');
  71. $form->embeds('operation','操作',function ($form) use ($data){
  72. $num = 0;
  73. foreach ($data['operation'] as $item){
  74. $form->text($num,'terms')->default($item);
  75. $num ++;
  76. }
  77. });
  78. $form->list('add_operation');
  79. $form->embeds('medicine','药品',function ($form) use ($data){
  80. $num = 0;
  81. foreach ($data['medicine'] as $item){
  82. $form->text($num,'terms')->default($item);
  83. $num ++;
  84. }
  85. });
  86. $form->list('add_medicine');
  87. $form->table('inspection' , '检查',function ($form){
  88. $form->text('terms');
  89. $form->text('tag');
  90. });
  91. $form->table('test' , '检验',function ($form){
  92. $form->text('terms');
  93. $form->text('tag');
  94. });
  95. $form->embeds('date','时间',function ($form) use ($data){
  96. $num = 0;
  97. foreach ($data['date'] as $item){
  98. $form->text($num,'terms')->default($item);
  99. $num ++;
  100. }
  101. });
  102. $form->list('add_date');
  103. $form->disableViewCheck();
  104. $form->disableEditingCheck();
  105. $form->disableCreatingCheck();
  106. $form->submitted(function (Form $form){
  107. $fields = [
  108. 'inspection',
  109. 'test',
  110. 'symptom',
  111. 'add_symptom',
  112. 'medicine',
  113. 'add_medicine',
  114. 'operation',
  115. 'add_operation',
  116. 'disease',
  117. 'add_disease',
  118. 'date',
  119. 'add_date'];
  120. $form->ignore($fields);
  121. });
  122. });
  123. }
  124. protected function botLabel($id,&$data){
  125. $text = TextLabel::query()->where('id',$id)->first(['text as sentence']);
  126. if ($text){
  127. $text = $text->toArray();
  128. }else{
  129. return;
  130. }
  131. $url = 'http://121.36.94.218:10090/disease/ner/predict?'.http_build_query($text);
  132. $result = file_get_contents($url);
  133. $result = json_decode($result,true);
  134. if ($result['code'] == 200){
  135. $botLabel = $result['data'];
  136. if (!empty($botLabel['symptom'])){
  137. $data['symptom'] = array_keys($botLabel['symptom']);
  138. }
  139. if (!empty($botLabel['operation'])){
  140. $data['operation'] = array_keys($botLabel['operation']);
  141. }
  142. if (!empty($botLabel['medicine'])){
  143. $data['medicine'] = array_keys($botLabel['medicine']);
  144. }
  145. if (!empty($botLabel['disease'])){
  146. $data['disease'] = array_keys($botLabel['disease']);
  147. }
  148. if (!empty($botLabel['inspection'])){
  149. $data['inspection'] = array_keys($botLabel['inspection']);
  150. }
  151. if (!empty($botLabel['test'])){
  152. $data['test'] = array_keys($botLabel['test']);
  153. }
  154. if (!empty($botLabel['date'])){
  155. $data['date'] = array_keys($botLabel['date']);
  156. }
  157. }
  158. }
  159. // Grid
  160. protected function grid()
  161. {
  162. return Admin::grid(TextLabel::class, function (Grid $grid) {
  163. $grid->id('编号')->sortable();
  164. $grid->model()->where('status',0)->orderBy('id','desc');
  165. $grid->text('文案');
  166. $grid->status('状态')->display(function ($status){
  167. if(0 == $status) {
  168. return "<span class='btn btn-success'>未标注</span>";
  169. } else {
  170. return "<span class='btn btn-danger'>已标注</span>";
  171. }
  172. });
  173. $grid->created_at('创建时间');
  174. $grid->updated_at('更新时间');
  175. $grid->disableCreation();
  176. $grid->disableExport();;
  177. $grid->tools(function ($tools) {
  178. // 禁用批量删除按钮
  179. $tools->batch(function ($batch) {
  180. $batch->disableDelete();
  181. });
  182. $tools->append(new ImportExcel());
  183. });
  184. $grid->actions(function ($actions) {
  185. $actions->disableView();
  186. //$actions->disableEdit();
  187. });
  188. });
  189. }
  190. }