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

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