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

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