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

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