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

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