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

339 lines
9.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
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\AdminGuide\Controllers;
  3. use App\AdminGuide\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\Service\Admin\GlobalNoticeService;
  13. use App\Traits\DemandTraits;
  14. use App\Traits\WorkorderTraits;
  15. use Dcat\Admin\Admin;
  16. use Dcat\Admin\Form;
  17. use Dcat\Admin\Grid;
  18. use Dcat\Admin\Layout\Content;
  19. use Dcat\Admin\Show;
  20. use Dcat\Admin\Http\Controllers\AdminController;
  21. use Illuminate\Http\Request;
  22. use App\Traits\ResponseHelper;
  23. use Illuminate\Support\Facades\Storage;
  24. use Illuminate\Support\Facades\Validator;
  25. class WorkorderController extends AdminController
  26. {
  27. use ResponseHelper;
  28. public function index(Content $content)
  29. {
  30. $workorderId = \request('workorder_id',0);
  31. $userList = $msgList = [];
  32. $workorder = \App\Models\Workorder::query()
  33. ->with(['point' => function($query) {
  34. $query->withTrashed();
  35. },'publisher' => function($query) {
  36. $query->withTrashed();
  37. }])
  38. ->withCount(['items' => function($query) {
  39. $query->where('is_read', 0)
  40. ->where('point_id',Admin::user()->id)
  41. ->where('point_type',DemandTraits::$col[2]);
  42. }])
  43. ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[2]])
  44. ->orWhere(function ($query) {
  45. $query->where('point_id',Admin::user()->id)
  46. ->where('point_type',DemandTraits::$col[2]);
  47. })
  48. ->orderByDesc('updated_at')
  49. ->get();
  50. // 先这么处理吧
  51. if ($workorderId == 0 && !empty($workorder->toArray())) {
  52. return redirect('/admin-guide/workorder_item?workorder_id='.$workorder[0]->id);
  53. }
  54. foreach ($workorder as $v) {
  55. $item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('id')->first();
  56. $arr = [
  57. 'workorder_id' => $v->id,
  58. 'unread' => true,
  59. 'avatar' => Storage::url($v->publisher_type == DemandTraits::$col[2] ? $v->point->avatar : $v->publisher->avatar),
  60. 'name' => $v->publisher_type == DemandTraits::$col[2] ? $v->point->name : $v->publisher->name,
  61. 'content' => $v->title,
  62. 'last_message' => $item ? ($item->type == 1 ? $item->content : '【 图片 】') : '',
  63. 'last_message_time' => $item->created_at ?? '',
  64. 'items_count' => $workorderId != $v->id ? $v->items_count : 0,
  65. ];
  66. $userList [] = $arr;
  67. }
  68. $workorderItem = WorkorderItem::query()
  69. ->with(['publisher' => function($query) {
  70. $query->withTrashed();
  71. }])
  72. ->where('workorder_id',$workorderId)
  73. ->get();
  74. foreach ($workorderItem as $v) {
  75. $arr = [
  76. 'type' => $v->publisher_type == DemandTraits::$col[2] ? 'my' : 'op',
  77. 'it_type' => $v->type,
  78. 'avatar' => Storage::url($v->publisher->avatar),
  79. 'name' => $v->publisher->name,
  80. 'content' => $v->content,
  81. 'created_at_text' => $v->created_at,
  82. 'is_read' => $v->is_read,
  83. ];
  84. $msgList [] = $arr;
  85. }
  86. // 更新已读未读,此处没有考虑性能代码解藕等问题
  87. WorkorderItem::query()->where('workorder_id',$workorderId)->update(['is_read' => 1]);
  88. $data = [
  89. 'user_list' => $userList,
  90. 'msg_list' => $msgList,
  91. 'user' => Admin::user(),
  92. 'workorder' => [
  93. 'workorder_id' => $workorderId
  94. ],
  95. 'route' => 'admin-guide',
  96. ];
  97. return $content
  98. ->header('工单')
  99. ->description('列表')
  100. ->body(admin_view('admin.pages.workorder',$data));
  101. }
  102. ///**
  103. // * Make a grid builder.
  104. // *
  105. // * @return Grid
  106. // */
  107. //protected function grid()
  108. //{
  109. // return Grid::make(new Workorder(['publisher','point']), function (Grid $grid) {
  110. // $grid->model()
  111. // ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[2]])
  112. // ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[2]]);
  113. // $grid->column('id')->sortable();
  114. // $grid->column('title');
  115. // $grid->column('content_modal','内容')->modal('详情',function ($modal) {
  116. // $modal->xl();
  117. // return $this->content;
  118. // });
  119. // $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  120. // $grid->column('publisher.name','发布人');
  121. // $grid->column('point_type')->using(DemandTraits::$polymorphic);
  122. // $grid->column('point.name','接收人');
  123. // $grid->column('status')
  124. // ->using(WorkorderTraits::$stateText)
  125. // ->dot(
  126. // [
  127. // 1 => 'yellow',
  128. // 2 => 'danger',
  129. // 3 => 'success',
  130. // ]);
  131. // $grid->column('close_time');
  132. // $grid->column('created_at');
  133. // $grid->column('updated_at')->sortable();
  134. // $grid->disableActions();
  135. // $grid->filter(function (Grid\Filter $filter) {
  136. // $filter->equal('id');
  137. //
  138. // });
  139. // });
  140. //}
  141. //
  142. ///**
  143. // * Make a show builder.
  144. // *
  145. // * @param mixed $id
  146. // *
  147. // * @return Show
  148. // */
  149. //protected function detail($id)
  150. //{
  151. // return Show::make($id, new Workorder(), function (Show $show) {
  152. // $show->field('id');
  153. // $show->field('title');
  154. // $show->field('content');
  155. // $show->field('publisher_type');
  156. // $show->field('publisher_id');
  157. // $show->field('point_type');
  158. // $show->field('point_id');
  159. // $show->field('status');
  160. // $show->field('close_time');
  161. // $show->field('created_at');
  162. // $show->field('updated_at');
  163. // });
  164. //}
  165. /**
  166. * Make a form builder.
  167. *
  168. * @return Form
  169. */
  170. protected function form()
  171. {
  172. return Form::make(new Workorder(), function (Form $form) {
  173. $form->display('id');
  174. //$form->text('title')->required();
  175. $form->textarea('content')->required();
  176. $form->hidden('point_type')
  177. ->options([
  178. 0 => '代理商',
  179. ])
  180. ->default(0)
  181. ->required();
  182. $form->select('point_id', '代理商')->options(function () {
  183. return Agent::query()->where('type',AgentType::CLUSTER)->pluck('name','id');
  184. })
  185. ->required();
  186. $form->hidden('publisher_type');
  187. $form->hidden('publisher_id');
  188. $form->saving(function (Form $form) {
  189. // 判断是否是新增操作
  190. if ($form->isCreating()) {
  191. $form->point_type = DemandTraits::$col[$form->point_type];
  192. //发布人身份
  193. $form->publisher_type = DemandTraits::$col[2];
  194. $form->publisher_id = Admin::user()->id;
  195. }
  196. });
  197. $form->saved(function (Form $form) {
  198. if ($form->isCreating()) {
  199. $item = new WorkorderItem();
  200. $item->workorder_id = $form->getKey();
  201. $item->content = $form->content;
  202. $item->type = 1;
  203. $item->publisher_type = DemandTraits::$col[2];
  204. $item->publisher_id = Admin::user()->id;
  205. $item->point_type = $form->point_type;
  206. $item->point_id = $form->point_id;
  207. $item->save();
  208. // 缓存提示信息
  209. (new GlobalNoticeService())->cacheWOrderNotice($item->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. $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
  235. if (empty($workorder)) {
  236. return $this->error('工单异常');
  237. }
  238. if ($workorder->publisher_type == DemandTraits::$col[2] && $workorder->publisher_id == Admin::user()->id) {
  239. $pointType = $workorder->point_type;
  240. $pointId = $workorder->point_id;
  241. } else {
  242. $pointType = $workorder->publisher_type;
  243. $pointId = $workorder->publisher_id;
  244. }
  245. $item = new WorkorderItem();
  246. $item->workorder_id = request('workorder_id',0);
  247. $item->content = $path;
  248. $item->type = 2;
  249. $item->publisher_type = DemandTraits::$col[2];
  250. $item->publisher_id = Admin::user()->id;
  251. $item->point_type = $pointType;
  252. $item->point_id = $pointId;
  253. $item->save();
  254. // 更新一下主表的时间做排序用
  255. $workorder->updated_at = time();
  256. $workorder->save();
  257. // 缓存提示信息
  258. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  259. return $this->jsonSuccess($item);
  260. }
  261. public function sendText(Request $request)
  262. {
  263. $validator = Validator::make(request()->all(), [
  264. 'workorder_id' => 'required|int',
  265. 'content' => 'required',
  266. ], [
  267. '*' => '参数异常',
  268. ]);
  269. if ($validator->fails()) {
  270. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  271. }
  272. $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
  273. if (empty($workorder)) {
  274. return $this->error('工单异常');
  275. }
  276. if ($workorder->publisher_type == DemandTraits::$col[2] && $workorder->publisher_id == Admin::user()->id) {
  277. $pointType = $workorder->point_type;
  278. $pointId = $workorder->point_id;
  279. } else {
  280. $pointType = $workorder->publisher_type;
  281. $pointId = $workorder->publisher_id;
  282. }
  283. $item = new WorkorderItem();
  284. $item->workorder_id = request('workorder_id',0);
  285. $item->content = request('content','');
  286. $item->type = 1;
  287. $item->publisher_type = DemandTraits::$col[2];
  288. $item->publisher_id = Admin::user()->id;
  289. $item->point_type = $pointType;
  290. $item->point_id = $pointId;
  291. $item->save();
  292. // 更新一下主表的时间做排序用
  293. $workorder->updated_at = time();
  294. $workorder->save();
  295. // 缓存提示信息
  296. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  297. return $this->jsonSuccess($item);
  298. }
  299. public function checkUnread()
  300. {
  301. return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[2]);
  302. }
  303. }