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

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