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

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