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

314 lines
9.4 KiB

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