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

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