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