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

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