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

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