海南旅游SAAS
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.

208 lines
5.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Repositories\Workorder;
  4. use App\Models\AgentProductItem;
  5. use App\Models\Guide;
  6. use App\Models\Supplier;
  7. use App\Models\WorkorderItem;
  8. use App\Traits\DemandTraits;
  9. use App\Traits\WorkorderTraits;
  10. use Dcat\Admin\Admin;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Layout\Content;
  14. use Dcat\Admin\Show;
  15. use Dcat\Admin\Http\Controllers\AdminController;
  16. use Illuminate\Http\Request;
  17. use App\Traits\ResponseHelper;
  18. use Illuminate\Support\Facades\Validator;
  19. class WorkorderController extends AdminController
  20. {
  21. use ResponseHelper;
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(new Workorder(['publisher','point']), function (Grid $grid) {
  30. $grid->model()
  31. ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[0]])
  32. ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[0]]);
  33. $grid->column('id')->sortable();
  34. $grid->column('title');
  35. $grid->column('content_modal','内容')->modal('详情',function ($modal) {
  36. $modal->xl();
  37. return $this->content;
  38. });
  39. $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  40. $grid->column('publisher.name','发布人');
  41. $grid->column('point_type')->using(DemandTraits::$polymorphic);
  42. $grid->column('point.name','接收人');
  43. $grid->column('status')
  44. ->using(WorkorderTraits::$stateText)
  45. ->dot(
  46. [
  47. 1 => 'yellow',
  48. 2 => 'danger',
  49. 3 => 'success',
  50. ]);
  51. $grid->column('close_time');
  52. $grid->column('created_at');
  53. $grid->column('updated_at')->sortable();
  54. $grid->disableActions();
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->equal('id');
  57. });
  58. });
  59. }
  60. /**
  61. * Make a show builder.
  62. *
  63. * @param mixed $id
  64. *
  65. * @return Show
  66. */
  67. protected function detail($id)
  68. {
  69. return Show::make($id, new Workorder(), function (Show $show) {
  70. $show->field('id');
  71. $show->field('title');
  72. $show->field('content');
  73. $show->field('publisher_type');
  74. $show->field('publisher_id');
  75. $show->field('point_type');
  76. $show->field('point_id');
  77. $show->field('status');
  78. $show->field('close_time');
  79. $show->field('created_at');
  80. $show->field('updated_at');
  81. });
  82. }
  83. /**
  84. * Make a form builder.
  85. *
  86. * @return Form
  87. */
  88. protected function form()
  89. {
  90. return Form::make(new Workorder(), function (Form $form) {
  91. $form->display('id');
  92. $form->text('title');
  93. $form->textarea('content');
  94. $form->select('point_type')
  95. ->when([1],function (Form $form) {
  96. $form->select('supplier_id', '供应商')->options(function () {
  97. $supplierIds = AgentProductItem::query()->where('agent_id',Admin::user()->id)->distinct()->pluck('supplier_id');
  98. return Supplier::query()->whereIn('id',$supplierIds)->pluck('name','id');
  99. });
  100. })
  101. ->when([2],function (Form $form) {
  102. $form->select('guide_id', '地接')->options(function () {
  103. return Guide::query()->pluck('name','id');
  104. });
  105. })
  106. ->options([
  107. 1 => '供应商',
  108. 2 => '地接'
  109. ])
  110. ->default(1);
  111. $form->hidden('point_id');
  112. $form->hidden('publisher_type');
  113. $form->hidden('publisher_id');
  114. $form->saving(function (Form $form) {
  115. // 判断是否是新增操作
  116. if ($form->isCreating()) {
  117. if ($form->point_type == 1) {
  118. $form->point_id = $form->supplier_id;
  119. } elseif ($form->point_type == 2) {
  120. $form->point_id = $form->guide_id;
  121. }
  122. $form->deleteInput('supplier_id');
  123. $form->deleteInput('guide_id');
  124. $form->point_type = DemandTraits::$col[$form->point_type];
  125. //发布人身份
  126. $form->publisher_type = DemandTraits::$col[0];
  127. $form->publisher_id = Admin::user()->id;
  128. }
  129. });
  130. });
  131. }
  132. //图片上传
  133. public function upload(Request $request)
  134. {
  135. $image = $request->file('image');
  136. if (empty($image) || !$image->isValid()) {
  137. return $this->error('您未上传任何文件');
  138. }
  139. $mime = $image->getMimeType();
  140. if (!in_array($mime, ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg'])) {
  141. return $this->error('上传图片格式错误');
  142. }
  143. $path = $request->image->store('public/images/workorder');
  144. return $this->jsonSuccess(['path' => config('filesystems.disks.public.url') . '/' . $path]);
  145. }
  146. public function send(Request $request)
  147. {
  148. $validator = Validator::make(request()->all(), [
  149. 'workorder_id' => 'required|int',
  150. 'content' => 'required',
  151. 'type' => 'required|int',
  152. ], [
  153. '*' => '参数异常',
  154. ]);
  155. if ($validator->fails()) {
  156. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  157. }
  158. $item = new WorkorderItem();
  159. $item->workorder_id = request('workorder_id',0);
  160. $item->content = request('content','');
  161. $item->type = request('type',1);
  162. $item->publisher_type = DemandTraits::$col[0];
  163. $item->publisher_id = 1;
  164. return $this->jsonSuccess($item->save());
  165. }
  166. public function render()
  167. {
  168. $validator = Validator::make(request()->all(), [
  169. 'workorder_id' => 'required|int',
  170. ], [
  171. '*' => '参数异常',
  172. ]);
  173. if ($validator->fails()) {
  174. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  175. }
  176. $data = WorkorderItem::query()
  177. ->with('publisher')
  178. ->where('workorder_id',request('workorder_id',0))
  179. ->orderBy('created_at')
  180. ->get();
  181. foreach ($data as &$v) {
  182. if ($v->publisher_type == DemandTraits::$col[0]) {
  183. $v->location = 'right';
  184. } else {
  185. $v->location = 'left';
  186. }
  187. }
  188. return $this->jsonSuccess($data);
  189. }
  190. }