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

280 lines
8.3 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
  1. <?php
  2. namespace App\AdminGuide\Controllers;
  3. use App\AdminAgent\Repositories\Workorder;
  4. use App\Common\AgentType;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Agent;
  7. use App\Models\AgentProductItem;
  8. use App\Models\Guide;
  9. use App\Models\OrderProductItem;
  10. use App\Models\Supplier;
  11. use App\Models\WorkorderItem;
  12. use App\Traits\DemandTraits;
  13. use App\Traits\WorkorderTraits;
  14. use Dcat\Admin\Admin;
  15. use Dcat\Admin\Form;
  16. use Dcat\Admin\Grid;
  17. use Dcat\Admin\Layout\Content;
  18. use Dcat\Admin\Show;
  19. use Dcat\Admin\Http\Controllers\AdminController;
  20. use Illuminate\Http\Request;
  21. use App\Traits\ResponseHelper;
  22. use Illuminate\Support\Facades\Storage;
  23. use Illuminate\Support\Facades\Validator;
  24. class WorkorderController extends AdminController
  25. {
  26. use ResponseHelper;
  27. public function index(Content $content)
  28. {
  29. $workorderId = \request('workorder_id',0);
  30. $userList = $msgList = [];
  31. $workorder = \App\Models\Workorder::query()
  32. ->with(['point','publisher'])
  33. ->withCount(['items' => function($query) {
  34. $query->where('is_read', 0);
  35. }])
  36. ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[2]])
  37. ->orWhere(function ($query) {
  38. $query->where('point_id',Admin::user()->id)
  39. ->where('point_type',DemandTraits::$col[2]);
  40. })
  41. ->orderByDesc('updated_at')
  42. ->get();
  43. // 先这么处理吧
  44. if ($workorderId == 0 && !empty($workorder)) {
  45. return redirect('/admin-guide/workorder_item?workorder_id='.$workorder[0]->id);
  46. }
  47. foreach ($workorder as $v) {
  48. $item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('updated_at')->first();
  49. $arr = [
  50. 'workorder_id' => $v->id,
  51. 'unread' => true,
  52. 'avatar' => Storage::url($v->publisher_type == DemandTraits::$col[2] ? $v->point->avatar : $v->publisher->avatar),
  53. 'name' => $v->publisher_type == DemandTraits::$col[2] ? $v->point->name : $v->publisher->name,
  54. 'content' => $v->title,
  55. 'last_message' => $item ? ($item->type == 1 ? $item->content : '【 图片 】') : '',
  56. 'last_message_time' => $item->created_at ?? '',
  57. 'items_count' => $workorderId != $v->id ? $v->items_count : 0,
  58. ];
  59. $userList [] = $arr;
  60. }
  61. $workorderItem = WorkorderItem::query()
  62. ->with(['publisher'])
  63. ->where('workorder_id',$workorderId)
  64. ->get();
  65. foreach ($workorderItem as $v) {
  66. $arr = [
  67. 'type' => $v->publisher_type == DemandTraits::$col[2] ? 'my' : 'op',
  68. 'it_type' => $v->type,
  69. 'avatar' => Storage::url($v->publisher->avatar),
  70. 'name' => $v->publisher->name,
  71. 'content' => $v->content,
  72. 'created_at_text' => $v->created_at,
  73. 'is_read' => $v->is_read,
  74. ];
  75. $msgList [] = $arr;
  76. }
  77. // 更新已读未读,此处没有考虑性能代码解藕等问题
  78. WorkorderItem::query()->where('workorder_id',$workorderId)->update(['is_read' => 1]);
  79. $data = [
  80. 'user_list' => $userList,
  81. 'msg_list' => $msgList,
  82. 'user' => Admin::user(),
  83. 'workorder' => [
  84. 'workorder_id' => $workorderId
  85. ],
  86. ];
  87. return $content
  88. ->header('工单')
  89. ->description('列表')
  90. ->body(admin_view('admin.pages.guide-index',$data));
  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[2]])
  102. // ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[2]]);
  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. $form->select('point_type')
  167. ->options([
  168. 0 => '代理商',
  169. ])
  170. ->default(0)
  171. ->required();
  172. $form->select('point_id', '代理商')->options(function () {
  173. return Agent::query()->where('type',AgentType::CLUSTER)->pluck('name','id');
  174. })
  175. ->required();
  176. $form->hidden('publisher_type');
  177. $form->hidden('publisher_id');
  178. $form->saving(function (Form $form) {
  179. // 判断是否是新增操作
  180. if ($form->isCreating()) {
  181. $form->point_type = DemandTraits::$col[$form->point_type];
  182. //发布人身份
  183. $form->publisher_type = DemandTraits::$col[2];
  184. $form->publisher_id = Admin::user()->id;
  185. }
  186. });
  187. $form->saved(function (Form $form) {
  188. if ($form->isCreating()) {
  189. $item = new WorkorderItem();
  190. $item->workorder_id = $form->getKey();
  191. $item->content = $form->content;
  192. $item->type = 1;
  193. $item->publisher_type = DemandTraits::$col[2];
  194. $item->publisher_id = Admin::user()->id;
  195. $item->save();
  196. }
  197. });
  198. });
  199. }
  200. public function sendImage(Request $request)
  201. {
  202. $validator = Validator::make(request()->all(), [
  203. 'workorder_id' => 'required|int',
  204. ], [
  205. '*' => '参数异常',
  206. ]);
  207. if ($validator->fails()) {
  208. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  209. }
  210. $image = $request->file('image');
  211. if (empty($image) || !$image->isValid()) {
  212. return $this->error('您未上传任何文件');
  213. }
  214. $mime = $image->getMimeType();
  215. if (!in_array($mime, ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg'])) {
  216. return $this->error('上传图片格式错误');
  217. }
  218. $path = $request->image->store('public/images/workorder');
  219. $path = (Storage::url($path));
  220. $item = new WorkorderItem();
  221. $item->workorder_id = request('workorder_id',0);
  222. $item->content = $path;
  223. $item->type = 2;
  224. $item->publisher_type = DemandTraits::$col[2];
  225. $item->publisher_id = Admin::user()->id;
  226. $item->save();
  227. // 更新一下主表的时间做排序用
  228. \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]);
  229. return $this->jsonSuccess($item);
  230. }
  231. public function sendText(Request $request)
  232. {
  233. $validator = Validator::make(request()->all(), [
  234. 'workorder_id' => 'required|int',
  235. 'content' => 'required',
  236. ], [
  237. '*' => '参数异常',
  238. ]);
  239. if ($validator->fails()) {
  240. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  241. }
  242. $item = new WorkorderItem();
  243. $item->workorder_id = request('workorder_id',0);
  244. $item->content = request('content','');
  245. $item->type = 1;
  246. $item->publisher_type = DemandTraits::$col[2];
  247. $item->publisher_id = Admin::user()->id;
  248. $item->save();
  249. // 更新一下主表的时间做排序用
  250. \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]);
  251. return $this->jsonSuccess($item);
  252. }
  253. }