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

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