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

300 lines
8.9 KiB

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