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

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