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

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