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

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