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

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