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

244 lines
7.1 KiB

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