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

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