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

368 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
  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\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. }])
  37. ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[0]])
  38. ->orWhere(function ($query) {
  39. $query->where('point_id',Admin::user()->id)
  40. ->where('point_type',DemandTraits::$col[0]);
  41. })
  42. ->orderByDesc('updated_at')
  43. ->get();
  44. // 先这么处理吧
  45. if ($workorderId == 0 && !empty($workorder)) {
  46. return redirect('/admin-agent/workorder_item?workorder_id='.$workorder[0]->id);
  47. }
  48. foreach ($workorder as $v) {
  49. $item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('updated_at')->first();
  50. $arr = [
  51. 'workorder_id' => $v->id,
  52. 'unread' => true,
  53. 'avatar' => Storage::url($v->publisher_type == DemandTraits::$col[0] ? $v->point->avatar : $v->publisher->avatar),
  54. 'name' => $v->publisher_type == DemandTraits::$col[0] ? $v->point->name : $v->publisher->name,
  55. 'content' => $v->title,
  56. 'last_message' => $item ? ($item->type == 1 ? $item->content : '【 图片 】') : '',
  57. 'last_message_time' => $item->created_at ?? '',
  58. 'items_count' => $workorderId != $v->id ? $v->items_count : 0,
  59. ];
  60. $userList [] = $arr;
  61. }
  62. $workorderItem = WorkorderItem::query()
  63. ->with(['publisher'])
  64. ->where('workorder_id',$workorderId)
  65. ->get();
  66. foreach ($workorderItem as $v) {
  67. $arr = [
  68. 'type' => $v->publisher_type == DemandTraits::$col[0] ? 'my' : 'op',
  69. 'it_type' => $v->type,
  70. 'avatar' => Storage::url($v->publisher->avatar),
  71. 'name' => $v->publisher->name,
  72. 'content' => $v->content,
  73. 'created_at_text' => $v->created_at,
  74. 'is_read' => $v->is_read,
  75. ];
  76. $msgList [] = $arr;
  77. }
  78. // 更新已读未读,此处没有考虑性能代码解藕等问题
  79. WorkorderItem::query()
  80. ->where('workorder_id',$workorderId)
  81. ->where([
  82. 'point_type' => DemandTraits::$col[0],
  83. 'point_id' => Admin::user()->id,
  84. ])
  85. ->update(['is_read' => 1]);
  86. $data = [
  87. 'user_list' => $userList,
  88. 'msg_list' => $msgList,
  89. 'user' => Admin::user(),
  90. 'workorder' => [
  91. 'workorder_id' => $workorderId
  92. ],
  93. 'route' => 'admin-agent',
  94. ];
  95. return $content
  96. ->header('工单')
  97. ->description('列表')
  98. ->body(admin_view('admin.pages.workorder',$data));
  99. }
  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[0]])
  111. // ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[0]]);
  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. if (Admin::user()->type == AgentType::CLUSTER) {
  176. $form->select('point_type')
  177. ->when([1], function (Form $form) {
  178. $form->select('supplier_id', '供应商')->options(function () {
  179. $supplierIds = AgentProductItem::query()->where('agent_id', Admin::user()->id)->distinct()->pluck('supplier_id');
  180. return Supplier::query()->whereIn('id', $supplierIds)->pluck('name', 'id');
  181. });
  182. })
  183. ->when([2], function (Form $form) {
  184. $form->select('guide_id', '地接')->options(function () {
  185. return Guide::query()->pluck('name', 'id');
  186. });
  187. })
  188. ->options([
  189. 1 => '供应商',
  190. 2 => '地接'
  191. ])
  192. ->default(1)->required();
  193. } else {
  194. $form->select('point_type')
  195. ->options([
  196. 1 => '供应商',
  197. ])
  198. ->default(1)->required();
  199. $form->select('supplier_id', '供应商')->options(function () {
  200. $supplierIds = AgentProductItem::query()->where('agent_id', Admin::user()->id)->distinct()->pluck('supplier_id');
  201. return Supplier::query()->whereIn('id', $supplierIds)->pluck('name', 'id');
  202. })->required();
  203. }
  204. $form->hidden('point_id');
  205. $form->hidden('publisher_type');
  206. $form->hidden('publisher_id');
  207. $form->saving(function (Form $form) {
  208. // 判断是否是新增操作
  209. if ($form->isCreating()) {
  210. if ($form->point_type == 1) {
  211. $form->point_id = $form->supplier_id;
  212. } elseif ($form->point_type == 2) {
  213. $form->point_id = $form->guide_id;
  214. }
  215. $form->deleteInput('supplier_id');
  216. $form->deleteInput('guide_id');
  217. $form->point_type = DemandTraits::$col[$form->point_type];
  218. //发布人身份
  219. $form->publisher_type = DemandTraits::$col[0];
  220. $form->publisher_id = Admin::user()->id;
  221. }
  222. });
  223. $form->saved(function (Form $form) {
  224. if ($form->isCreating()) {
  225. $item = new WorkorderItem();
  226. $item->workorder_id = $form->getKey();
  227. $item->content = $form->content;
  228. $item->type = 1;
  229. $item->publisher_type = DemandTraits::$col[0];
  230. $item->publisher_id = Admin::user()->id;
  231. $item->point_type = $form->point_type;
  232. $item->point_id = $form->point_id;
  233. $item->save();
  234. // 缓存提示信息
  235. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  236. }
  237. });
  238. });
  239. }
  240. public function sendImage(Request $request)
  241. {
  242. $validator = Validator::make(request()->all(), [
  243. 'workorder_id' => 'required|int',
  244. ], [
  245. '*' => '参数异常',
  246. ]);
  247. if ($validator->fails()) {
  248. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  249. }
  250. $image = $request->file('image');
  251. if (empty($image) || !$image->isValid()) {
  252. return $this->error('您未上传任何文件');
  253. }
  254. $mime = $image->getMimeType();
  255. if (!in_array($mime, ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg'])) {
  256. return $this->error('上传图片格式错误');
  257. }
  258. $path = $request->image->store('public/images/workorder');
  259. $path = (Storage::url($path));
  260. // 指定发送对象
  261. $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
  262. if (empty($workorder)) {
  263. return $this->error('工单异常');
  264. }
  265. if ($workorder->publisher_type == DemandTraits::$col[0] && $workorder->publisher_id == Admin::user()->id) {
  266. $pointType = $workorder->point_type;
  267. $pointId = $workorder->point_id;
  268. } else {
  269. $pointType = $workorder->publisher_type;
  270. $pointId = $workorder->publisher_id;
  271. }
  272. $item = new WorkorderItem();
  273. $item->workorder_id = request('workorder_id',0);
  274. $item->content = $path;
  275. $item->type = 2;
  276. $item->publisher_type = DemandTraits::$col[0];
  277. $item->publisher_id = Admin::user()->id;
  278. $item->point_type = $pointType;
  279. $item->point_id = $pointId;
  280. $item->save();
  281. // 更新一下主表的时间做排序用
  282. $workorder->updated_at = $item->updated_at;
  283. $workorder->save();
  284. // 缓存提示信息
  285. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  286. return $this->jsonSuccess($item);
  287. }
  288. public function sendText(Request $request)
  289. {
  290. $validator = Validator::make(request()->all(), [
  291. 'workorder_id' => 'required|int',
  292. 'content' => 'required',
  293. ], [
  294. '*' => '参数异常',
  295. ]);
  296. if ($validator->fails()) {
  297. return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
  298. }
  299. // 指定发送对象
  300. $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
  301. if (empty($workorder)) {
  302. return $this->error('工单异常');
  303. }
  304. if ($workorder->publisher_type == DemandTraits::$col[0] && $workorder->publisher_id == Admin::user()->id) {
  305. $pointType = $workorder->point_type;
  306. $pointId = $workorder->point_id;
  307. } else {
  308. $pointType = $workorder->publisher_type;
  309. $pointId = $workorder->publisher_id;
  310. }
  311. $item = new WorkorderItem();
  312. $item->workorder_id = request('workorder_id',0);
  313. $item->content = request('content','');
  314. $item->type = 1;
  315. $item->publisher_type = DemandTraits::$col[0];
  316. $item->publisher_id = Admin::user()->id;
  317. $item->point_type = $pointType;
  318. $item->point_id = $pointId;
  319. $item->save();
  320. // 更新一下主表的时间做排序用
  321. $workorder->updated_at = $item->updated_at;
  322. $workorder->save();
  323. // 缓存提示信息
  324. (new GlobalNoticeService())->cacheWOrderNotice($item->id);
  325. return $this->jsonSuccess($item);
  326. }
  327. public function checkUnread()
  328. {
  329. return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[0]);
  330. }
  331. }