VaccineController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Community\Controllers;
  3. use App\Models\Order;
  4. use App\Models\Organization;
  5. use App\Models\Vaccine;
  6. use Encore\Admin\Controllers\AdminController;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Show;
  11. class VaccineController 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 Vaccine());
  27. $org_id = Admin::user()->org_id;
  28. if($org_id){
  29. $grid->model()->where(['org_id'=>$org_id]);
  30. }
  31. $grid->disableCreateButton(false);
  32. $grid->column('id', __('ID'));
  33. $grid->column('name', __('疫苗名称'));
  34. $grid->column('type', __('类别'))->editable('select',[1=>'一类',2=>'二类']);
  35. $grid->column('stock', __('剩余库存'))->editable();
  36. $grid->column('org_id', __('已使用量'))->display(function () use ($org_id) {
  37. return Order::where(['product_type'=>4,'organization_id'=>$org_id])->wherehas('orderVaccine',function ($query){
  38. $query->where('vaccine_id',$this->id);
  39. })->count();
  40. });
  41. $grid->column('today_num', __('今日预约'))->display(function () use ($org_id) {
  42. return Order::where(['product_type'=>4,'organization_id'=>$org_id])->wherehas('orderVaccine',function ($query){
  43. $query->where('vaccine_id',$this->id);
  44. })->count();
  45. });;
  46. $grid->column('price', __('价格'))->editable();
  47. $grid->column('remark', __('备注'))->editable('textarea');
  48. $grid->column('supplier', __('厂家'));
  49. // $grid->column('created_at', __('Created at'));
  50. // $grid->column('updated_at', __('Updated at'));
  51. $grid->filter(function ($fliter){
  52. $fliter->equal('type','类别')->select([1=>'一类',2=>'二类']);
  53. });
  54. $grid->actions(function ($actions) {
  55. // append一个操作
  56. $actions->append('<a href=""><i class="fa fa-header">跳转</i></a>');
  57. // prepend一个操作
  58. $actions->prepend('<a href="http://www.baidu.com" target="_blank"><i class="fa fa-cab"></i></a>');
  59. });
  60. return $grid;
  61. }
  62. /**
  63. * Make a show builder.
  64. *
  65. * @param mixed $id
  66. * @return Show
  67. */
  68. protected function detail($id)
  69. {
  70. $show = new Show(Vaccine::findOrFail($id));
  71. $show->field('id', __('Id'));
  72. $show->field('type', __('Type'));
  73. $show->field('price', __('Price'));
  74. $show->field('name', __('Name'));
  75. $show->field('remark', __('Remark'));
  76. $show->field('supplier', __('Supplier'));
  77. $show->field('created_at', __('Created at'));
  78. $show->field('updated_at', __('Updated at'));
  79. return $show;
  80. }
  81. /**
  82. * Make a form builder.
  83. *
  84. * @return Form
  85. */
  86. protected function form()
  87. {
  88. $form = new Form(new Vaccine());
  89. $orglist = Organization::pluck('name','id');
  90. $org_id = Admin::user()->org_id;
  91. if($org_id){
  92. $orglist = Organization::where(['id'=>$org_id])->pluck('name','id');
  93. }
  94. $form->select('type', __('类型'))->options([1=>'一类',2=>'二类'])->default(1);
  95. $form->select('org_id','机构')->options($orglist)->rules('required',['requried'=>'请选择机构']);
  96. $form->text('name', __('疫苗名称'))->rules('required',['requried'=>'请填写疫苗名称']);
  97. $form->text('supplier', __('厂家'))->rules('required',['requried'=>'请填写厂家信息']);
  98. $form->number('price', __('价格'))->rules('required',['requried'=>'请填写价格']);
  99. $form->number('stock', __('库存'))->rules('required',['requried'=>'请填写库存']);
  100. $form->textarea('remark', __('备注'));
  101. return $form;
  102. }
  103. }