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

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