VaccinesController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Admin\Controllers\VaccinesManagement;
  3. use App\Models\Organization;
  4. use App\Models\Vaccines;
  5. use App\Admin\Actions\Vaccines\ImportAction;
  6. use Encore\Admin\Controllers\AdminController;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Show;
  10. use Encore\Admin\Widgets\Table;
  11. class VaccinesController extends AdminController
  12. {
  13. /**
  14. * Title for current resource.
  15. *
  16. * @var string
  17. */
  18. protected $title = '疫苗管理';
  19. /**
  20. * Make a grid builder.
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. $grid = new Grid(new Vaccines());
  27. $grid->model()->orderBy('id','desc');
  28. $grid->column('id', __('Id'));
  29. // $grid->column('type', '类型')->using([1=>'Ⅰ类疫苗',2=>'Ⅱ类疫苗']);
  30. $grid->column('name', __('名称'));
  31. $states = [
  32. 'on' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
  33. 'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
  34. ];
  35. $grid->column('organizations', '机构库存')->display(function (){
  36. return '点击查看';
  37. })->modal('机构库存', function ($model) {
  38. $org = $model->organizationvaccines()->get()->map(function ($comment) {
  39. return $comment->only(['org_id','vaccine_id','stock']);
  40. });
  41. $org = $org->toArray();
  42. for($i=0;$i<count($org);$i++)
  43. {
  44. $id = $org[$i]['org_id'];
  45. $org[$i]['vaccine_id'] = $this->name;
  46. $org[$i]['org_name'] = Organization::where('id',$id)->value('name');
  47. }
  48. return new Table(['ID','疫苗','库存','机构名称'], $org);
  49. });
  50. $grid->column('kc','库存');
  51. $grid->column('updated_at', __('更新时间'));
  52. $grid->filter(function($filter){
  53. /*
  54. 搜索:按关键字、ID搜索
  55. 筛选:类别筛选
  56. 导入、导出
  57. */
  58. // 在这里添加字段过滤器
  59. $filter->column(1/2, function ($filter) {
  60. $filter->equal('type', '类型')->select(Vaccines::$_post_type);
  61. // $filter->equal('supplier','厂家');
  62. });
  63. $filter->column(1/2, function ($filter) {
  64. $filter->like('name','疫苗名称');
  65. $filter->equal('organizationvaccines.org_id','机构')->select(Organization::pluck('name','id'));
  66. });
  67. });
  68. //按关键字查询
  69. $grid->quickSearch(function ($model, $query) {
  70. //$model->whereHas('Vaccines',function ($model) use ($query) {
  71. $model->where('type', 'like', "%{$query}%")
  72. ->orWhere('name', 'like', "%{$query}%")
  73. ->orWhere('id',$query)
  74. ->orwhere('supplier','like',"%{$query}%")
  75. ->orwhere('remark','like',"{$query}");
  76. //});
  77. })->placeholder('请输入疫苗id/名称');
  78. $grid->export(function ($export) {
  79. //文件名
  80. $export->filename('测试');
  81. //排除字段不用导出
  82. $export->except(['created_at', 'updated_at']);
  83. $export->originalValue(['states']);
  84. });
  85. $grid->tools(function (Grid\Tools $tools) {
  86. $tools->append(new ImportAction());
  87. });
  88. return $grid;
  89. }
  90. /**
  91. * Make a show builder.
  92. *
  93. * @param mixed $id
  94. * @return Show
  95. */
  96. protected function detail($id)
  97. {
  98. $show = new Show(Vaccines::findOrFail($id));
  99. $show->field('id', __('Id'));
  100. // $show->field('type', __('分类'))->using([1=>'Ⅰ类疫苗',2=>'Ⅱ类疫苗']);
  101. // $show->field('introduction','简介');
  102. // $show->field('price', __('价钱'));
  103. $show->field('name', __('名称'));
  104. // $show->field('remark', __('备注'));
  105. // $show->field('supplier', __('厂家'));
  106. $show->field('created_at', __('创建时间'));
  107. $show->field('updated_at', __('更新时间'));
  108. return $show;
  109. }
  110. /**
  111. * Make a form builder.
  112. *
  113. * @return Form
  114. */
  115. protected function form()
  116. {
  117. $form = new Form(new Vaccines());
  118. $form->editing(function ($f){
  119. $f->model()->price /= 100;
  120. });
  121. // $form->select('type', __('分类'))->options(Vaccines::$_post_type)->default('1');
  122. $form->text('name', __('名称'))->rules('required|min:3|max:255',['required'=>'请填写名称','min'=>'名称不能少于3个字符!','max'=>'名称长度过长!']);
  123. // $form->text('price', __('价格'));
  124. // $form->text('remark', __('备注'));
  125. // $form->text('supplier', __('厂家'))->rules('required',['required'=>'请填写厂家!']);
  126. // $states = [
  127. // 'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
  128. // 'on' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
  129. // ];
  130. // $form->switch('states','状态')->states($states)->default(1);
  131. $form->saving(function ($f){
  132. $f->price *=100;
  133. });
  134. return $form;
  135. }
  136. }