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

337 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
  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\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','publisher'])
  34. ->withCount(['items' => function($query) {
  35. $query->where('is_read', 0)
  36. ->where('point_id',Admin::user()->id)
  37. ->where('point_type',DemandTraits::$col[2]);
  38. }])
  39. ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[2]])
  40. ->orWhere(function ($query) {
  41. $query->where('point_id',Admin::user()->id)
  42. ->where('point_type',DemandTraits::$col[2]);
  43. })
  44. ->orderByDesc('updated_at')
  45. ->get();
  46. // 先这么处理吧
  47. if ($workorderId == 0 && !empty($workorder->toArray())) {
  48. return redirect('/admin-guide/workorder_item?workorder_id='.$workorder[0]->id);
  49. }
  50. foreach ($workorder as $v) {
  51. $item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('id')->first();
  52. $arr = [
  53. 'workorder_id' => $v->id,
  54. 'unread' => true,
  55. 'avatar' => Storage::url($v->publisher_type == DemandTraits::$col[2] ? $v->point->avatar : $v->publisher->avatar),
  56. 'name' => $v->publisher_type == DemandTraits::$col[2] ? $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. 'items_count' => $workorderId != $v->id ? $v->items_count : 0,
  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. 'is_read' => $v->is_read,
  77. ];
  78. $msgList [] = $arr;
  79. }
  80. // 更新已读未读,此处没有考虑性能代码解藕等问题
  81. WorkorderItem::query()->where('workorder_id',$workorderId)->update(['is_read' => 1]);
  82. $data = [
  83. 'user_list' => $userList,
  84. 'msg_list' => $msgList,
  85. 'user' => Admin::user(),
  86. 'workorder' => [
  87. 'workorder_id' => $workorderId
  88. ],
  89. 'route' => 'admin-guide',
  90. ];
  91. return $content
  92. ->header('工单')
  93. ->description('列表')
  94. ->body(admin_view('admin.pages.workorder',$data));
  95. }
  96. ///**
  97. // * Make a grid builder.
  98. // *
  99. // * @return Grid
  100. // */
  101. //protected function grid()
  102. //{
  103. // return Grid::make(new Workorder(['publisher','point']), function (Grid $grid) {
  104. // $grid->model()
  105. // ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[2]])
  106. // ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[2]]);
  107. // $grid->column('id')->sortable();
  108. // $grid->column('title');
  109. // $grid->column('content_modal','内容')->modal('详情',function ($modal) {
  110. // $modal->xl();
  111. // return $this->content;
  112. // });
  113. // $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  114. // $grid->column('publisher.name','发布人');
  115. // $grid->column('point_type')->using(DemandTraits::$polymorphic);
  116. // $grid->column('point.name','接收人');
  117. // $grid->column('status')
  118. // ->using(WorkorderTraits::$stateText)
  119. // ->dot(
  120. // [
  121. // 1 => 'yellow',
  122. // 2 => 'danger',
  123. // 3 => 'success',
  124. // ]);
  125. // $grid->column('close_time');
  126. // $grid->column('created_at');
  127. // $grid->column('updated_at')->sortable();
  128. // $grid->disableActions();
  129. // $grid->filter(function (Grid\Filter $filter) {
  130. // $filter->equal('id');
  131. //
  132. // });
  133. // });
  134. //}
  135. //
  136. ///**
  137. // * Make a show builder.
  138. // *
  139. // * @param mixed $id
  140. // *
  141. // * @return Show
  142. // */
  143. //protected function detail($id)
  144. //{
  145. // return Show::make($id, new Workorder(), function (Show $show) {
  146. // $show->field('id');
  147. // $show->field('title');
  148. // $show->field('content');
  149. // $show->field('publisher_type');
  150. // $show->field('publisher_id');
  151. // $show->field('point_type');
  152. // $show->field('point_id');
  153. // $show->field('status');
  154. // $show->field('close_time');
  155. // $show->field('created_at');
  156. // $show->field('updated_at');
  157. // });
  158. //}
  159. /**
  160. * Make a form builder.
  161. *
  162. * @return Form
  163. */
  164. protected function form()
  165. {
  166. return Form::make(new Workorder(), function (Form $form) {
  167. $form->display('id');
  168. //$form->text('title')->required();
  169. $form->textarea('content')->required();
  170. $form->hidden('point_type')
  171. ->options([
  172. 0 => '代理商',
  173. ])
  174. ->default(0)
  175. ->required();
  176. $form->select('point_id', '代理商')->options(function () {
  177. return Agent::query()->where('type',AgentType::CLUSTER)->pluck('name','id');
  178. })
  179. ->required();
  180. $form->hidden('publisher_type');
  181. $form->hidden('publisher_id');
  182. $form->saving(function (Form $form) {
  183. // 判断是否是新增操作
  184. if ($form->isCreating()) {
  185. $form->point_type = DemandTraits::$col[$form->point_type];
  186. //发布人身份
  187. $form->publisher_type = DemandTraits::$col[2];
  188. $form->publisher_id = Admin::user()->id;
  189. }
  190. });
  191. $form->saved(function (Form $form) {
  192. if ($form->isCreating()) {
  193. $item = new WorkorderItem();
  194. $item->workorder_id = $form->getKey();
  195. $item->content = $form->content;
  196. $item->type = 1;
  197. $item->publisher_type = DemandTraits::$col[2];
  198. $item->publisher_id = Admin::user()->id;
  199. $item->point_type = $form->point_type;
  200. $item->point_id = $form->point_id;
  201. $item->save();
  202. // 缓存提示信息
  203. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  204. }
  205. });
  206. });
  207. }
  208. public function sendImage(Request $request)
  209. {
  210. $validator = Validator::make(request()->all(), [
  211. 'workorder_id' => 'required|int',
  212. ], [
  213. '*' => '参数异常',
  214. ]);
  215. if ($validator->fails()) {
  216. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  217. }
  218. $image = $request->file('image');
  219. if (empty($image) || !$image->isValid()) {
  220. return $this->error('您未上传任何文件');
  221. }
  222. $mime = $image->getMimeType();
  223. if (!in_array($mime, ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg'])) {
  224. return $this->error('上传图片格式错误');
  225. }
  226. $path = $request->image->store('public/images/workorder');
  227. $path = (Storage::url($path));
  228. $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
  229. if (empty($workorder)) {
  230. return $this->error('工单异常');
  231. }
  232. if ($workorder->publisher_type == DemandTraits::$col[2] && $workorder->publisher_id == Admin::user()->id) {
  233. $pointType = $workorder->point_type;
  234. $pointId = $workorder->point_id;
  235. } else {
  236. $pointType = $workorder->publisher_type;
  237. $pointId = $workorder->publisher_id;
  238. }
  239. $item = new WorkorderItem();
  240. $item->workorder_id = request('workorder_id',0);
  241. $item->content = $path;
  242. $item->type = 2;
  243. $item->publisher_type = DemandTraits::$col[2];
  244. $item->publisher_id = Admin::user()->id;
  245. $item->point_type = $pointType;
  246. $item->point_id = $pointId;
  247. $item->save();
  248. // 更新一下主表的时间做排序用
  249. $workorder->updated_at = time();
  250. $workorder->save();
  251. // 缓存提示信息
  252. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  253. return $this->jsonSuccess($item);
  254. }
  255. public function sendText(Request $request)
  256. {
  257. $validator = Validator::make(request()->all(), [
  258. 'workorder_id' => 'required|int',
  259. 'content' => 'required',
  260. ], [
  261. '*' => '参数异常',
  262. ]);
  263. if ($validator->fails()) {
  264. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  265. }
  266. $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
  267. if (empty($workorder)) {
  268. return $this->error('工单异常');
  269. }
  270. if ($workorder->publisher_type == DemandTraits::$col[2] && $workorder->publisher_id == Admin::user()->id) {
  271. $pointType = $workorder->point_type;
  272. $pointId = $workorder->point_id;
  273. } else {
  274. $pointType = $workorder->publisher_type;
  275. $pointId = $workorder->publisher_id;
  276. }
  277. $item = new WorkorderItem();
  278. $item->workorder_id = request('workorder_id',0);
  279. $item->content = request('content','');
  280. $item->type = 1;
  281. $item->publisher_type = DemandTraits::$col[2];
  282. $item->publisher_id = Admin::user()->id;
  283. $item->point_type = $pointType;
  284. $item->point_id = $pointId;
  285. $item->save();
  286. // 更新一下主表的时间做排序用
  287. $workorder->updated_at = time();
  288. $workorder->save();
  289. // 缓存提示信息
  290. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  291. return $this->jsonSuccess($item);
  292. }
  293. public function checkUnread()
  294. {
  295. if (\request('badge', '')) {
  296. return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[2], true);
  297. } else {
  298. return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[2]);
  299. }
  300. }
  301. }