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

260 lines
7.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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\Http\Controllers\Controller;
  5. use App\Models\AgentProductItem;
  6. use App\Models\Guide;
  7. use App\Models\OrderProductItem;
  8. use App\Models\Supplier;
  9. use App\Models\WorkorderItem;
  10. use App\Traits\DemandTraits;
  11. use App\Traits\WorkorderTraits;
  12. use Dcat\Admin\Admin;
  13. use Dcat\Admin\Form;
  14. use Dcat\Admin\Grid;
  15. use Dcat\Admin\Layout\Content;
  16. use Dcat\Admin\Show;
  17. use Dcat\Admin\Http\Controllers\AdminController;
  18. use Illuminate\Http\Request;
  19. use App\Traits\ResponseHelper;
  20. use Illuminate\Support\Facades\Storage;
  21. use Illuminate\Support\Facades\Validator;
  22. class WorkorderController extends AdminController
  23. {
  24. use ResponseHelper;
  25. public function index(Content $content)
  26. {
  27. $workorderId = \request('workorder_id',0);
  28. $userList = $msgList = [];
  29. $workorder = \App\Models\Workorder::query()
  30. ->with(['point','publisher'])
  31. ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[0]])
  32. ->orWhere(function ($query) {
  33. $query->where('point_id',Admin::user()->id)
  34. ->where('point_type',DemandTraits::$col[0]);
  35. })
  36. ->orderByDesc('updated_at')
  37. ->get();
  38. foreach ($workorder as $v) {
  39. $item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('updated_at')->first();
  40. $arr = [
  41. 'workorder_id' => $v->id,
  42. 'unread' => true,
  43. 'avatar' => $v->publisher_type == DemandTraits::$col[0] ? $v->point->avatar : $v->publisher->avatar,
  44. 'name' => $v->publisher_type == DemandTraits::$col[0] ? $v->point->name : $v->publisher->name,
  45. 'content' => $v->title,
  46. 'last_message' => $item ? ($item->type == 1 ? $item->content : '【 图片 】') : '',
  47. 'last_message_time' => $item->created_at ?? '',
  48. ];
  49. $userList [] = $arr;
  50. }
  51. $workorderItem = WorkorderItem::query()
  52. ->with(['publisher'])
  53. ->where('workorder_id',$workorderId)
  54. ->get();
  55. foreach ($workorderItem as $v) {
  56. $arr = [
  57. 'type' => $v->publisher_type == DemandTraits::$col[0] ? 'my' : 'op',
  58. 'it_type' => $v->type,
  59. 'avatar' => $v->publisher->avatar,
  60. 'name' => $v->publisher->name,
  61. 'content' => $v->content,
  62. ];
  63. $msgList [] = $arr;
  64. }
  65. $data = [
  66. 'user_list' => $userList,
  67. 'msg_list' => $msgList,
  68. 'user' => Admin::user(),
  69. 'workorder' => [
  70. 'workorder_id' => $workorderId
  71. ],
  72. ];
  73. return $content
  74. ->header('工单')
  75. ->description('列表')
  76. ->body(admin_view('admin.pages.index',$data));
  77. }
  78. //
  79. ///**
  80. // * Make a grid builder.
  81. // *
  82. // * @return Grid
  83. // */
  84. //protected function grid()
  85. //{
  86. // return Grid::make(new Workorder(['publisher','point']), function (Grid $grid) {
  87. // $grid->model()
  88. // ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[0]])
  89. // ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[0]]);
  90. // $grid->column('id')->sortable();
  91. // $grid->column('title');
  92. // $grid->column('content_modal','内容')->modal('详情',function ($modal) {
  93. // $modal->xl();
  94. // return $this->content;
  95. // });
  96. // $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  97. // $grid->column('publisher.name','发布人');
  98. // $grid->column('point_type')->using(DemandTraits::$polymorphic);
  99. // $grid->column('point.name','接收人');
  100. // $grid->column('status')
  101. // ->using(WorkorderTraits::$stateText)
  102. // ->dot(
  103. // [
  104. // 1 => 'yellow',
  105. // 2 => 'danger',
  106. // 3 => 'success',
  107. // ]);
  108. // $grid->column('close_time');
  109. // $grid->column('created_at');
  110. // $grid->column('updated_at')->sortable();
  111. // $grid->disableActions();
  112. // $grid->filter(function (Grid\Filter $filter) {
  113. // $filter->equal('id');
  114. //
  115. // });
  116. // });
  117. //}
  118. //
  119. ///**
  120. // * Make a show builder.
  121. // *
  122. // * @param mixed $id
  123. // *
  124. // * @return Show
  125. // */
  126. //protected function detail($id)
  127. //{
  128. // return Show::make($id, new Workorder(), function (Show $show) {
  129. // $show->field('id');
  130. // $show->field('title');
  131. // $show->field('content');
  132. // $show->field('publisher_type');
  133. // $show->field('publisher_id');
  134. // $show->field('point_type');
  135. // $show->field('point_id');
  136. // $show->field('status');
  137. // $show->field('close_time');
  138. // $show->field('created_at');
  139. // $show->field('updated_at');
  140. // });
  141. //}
  142. /**
  143. * Make a form builder.
  144. *
  145. * @return Form
  146. */
  147. protected function form()
  148. {
  149. return Form::make(new Workorder(), function (Form $form) {
  150. $form->display('id');
  151. $form->text('title');
  152. $form->textarea('content');
  153. $form->select('point_type')
  154. ->when([1],function (Form $form) {
  155. $form->select('supplier_id', '供应商')->options(function () {
  156. $supplierIds = AgentProductItem::query()->where('agent_id',Admin::user()->id)->distinct()->pluck('supplier_id');
  157. return Supplier::query()->whereIn('id',$supplierIds)->pluck('name','id');
  158. });
  159. })
  160. ->when([2],function (Form $form) {
  161. $form->select('guide_id', '地接')->options(function () {
  162. return Guide::query()->pluck('name','id');
  163. });
  164. })
  165. ->options([
  166. 1 => '供应商',
  167. 2 => '地接'
  168. ])
  169. ->default(1);
  170. $form->hidden('point_id');
  171. $form->hidden('publisher_type');
  172. $form->hidden('publisher_id');
  173. $form->saving(function (Form $form) {
  174. // 判断是否是新增操作
  175. if ($form->isCreating()) {
  176. if ($form->point_type == 1) {
  177. $form->point_id = $form->supplier_id;
  178. } elseif ($form->point_type == 2) {
  179. $form->point_id = $form->guide_id;
  180. }
  181. $form->deleteInput('supplier_id');
  182. $form->deleteInput('guide_id');
  183. $form->point_type = DemandTraits::$col[$form->point_type];
  184. //发布人身份
  185. $form->publisher_type = DemandTraits::$col[0];
  186. $form->publisher_id = Admin::user()->id;
  187. }
  188. });
  189. });
  190. }
  191. public function sendImage(Request $request)
  192. {
  193. $validator = Validator::make(request()->all(), [
  194. 'workorder_id' => 'required|int',
  195. ], [
  196. '*' => '参数异常',
  197. ]);
  198. if ($validator->fails()) {
  199. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  200. }
  201. $image = $request->file('image');
  202. if (empty($image) || !$image->isValid()) {
  203. return $this->error('您未上传任何文件');
  204. }
  205. $mime = $image->getMimeType();
  206. if (!in_array($mime, ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg'])) {
  207. return $this->error('上传图片格式错误');
  208. }
  209. $path = $request->image->store('public/images/workorder');
  210. $path = (Storage::url($path));
  211. $item = new WorkorderItem();
  212. $item->workorder_id = request('workorder_id',0);
  213. $item->content = $path;
  214. $item->type = 2;
  215. $item->publisher_type = DemandTraits::$col[0];
  216. $item->publisher_id = Admin::user()->id;
  217. $item->save();
  218. return $this->jsonSuccess($item);
  219. }
  220. public function sendText(Request $request)
  221. {
  222. $validator = Validator::make(request()->all(), [
  223. 'workorder_id' => 'required|int',
  224. 'content' => 'required',
  225. ], [
  226. '*' => '参数异常',
  227. ]);
  228. if ($validator->fails()) {
  229. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  230. }
  231. $item = new WorkorderItem();
  232. $item->workorder_id = request('workorder_id',0);
  233. $item->content = request('content','');
  234. $item->type = 1;
  235. $item->publisher_type = DemandTraits::$col[0];
  236. $item->publisher_id = Admin::user()->id;
  237. $item->save();
  238. return $this->jsonSuccess($item);
  239. }
  240. }