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

245 lines
7.2 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\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. foreach ($workorder as $v) {
  40. $item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('updated_at')->first();
  41. $arr = [
  42. 'workorder_id' => $v->id,
  43. 'unread' => true,
  44. 'avatar' => $v->publisher_type == DemandTraits::$col[1] ? $v->point->avatar : $v->publisher->avatar,
  45. 'name' => $v->publisher_type == DemandTraits::$col[1] ? $v->point->name : $v->publisher->name,
  46. 'content' => $v->title,
  47. 'last_message' => $item ? ($item->type == 1 ? $item->content : '【 图片 】') : '',
  48. 'last_message_time' => $item->created_at ?? '',
  49. ];
  50. $userList [] = $arr;
  51. }
  52. $workorderItem = WorkorderItem::query()
  53. ->with(['publisher'])
  54. ->where('workorder_id',$workorderId)
  55. ->get();
  56. foreach ($workorderItem as $v) {
  57. $arr = [
  58. 'type' => $v->publisher_type == DemandTraits::$col[1] ? 'my' : 'op',
  59. 'it_type' => $v->type,
  60. 'avatar' => $v->publisher->avatar,
  61. 'name' => $v->publisher->name,
  62. 'content' => $v->content,
  63. ];
  64. $msgList [] = $arr;
  65. }
  66. $data = [
  67. 'user_list' => $userList,
  68. 'msg_list' => $msgList,
  69. 'user' => Admin::user(),
  70. 'workorder' => [
  71. 'workorder_id' => $workorderId
  72. ],
  73. ];
  74. return $content
  75. ->header('工单')
  76. ->description('列表')
  77. ->body(admin_view('admin.pages.supplier-index',$data));
  78. }
  79. ///**
  80. // * Make a grid builder.
  81. // *
  82. // * @return Grid
  83. // */
  84. //protected function grid()
  85. //{
  86. // return Grid::make(new Workorder(['publisher','point']), function (Grid $grid) {
  87. // $grid->model()
  88. // ->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[1]])
  89. // ->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[1]]);
  90. // $grid->column('id')->sortable();
  91. // $grid->column('title');
  92. // $grid->column('content_modal','内容')->modal('详情',function ($modal) {
  93. // $modal->xl();
  94. // return $this->content;
  95. // });
  96. // $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  97. // $grid->column('publisher.name','发布人');
  98. // $grid->column('point_type')->using(DemandTraits::$polymorphic);
  99. // $grid->column('point.name','接收人');
  100. // $grid->column('status')
  101. // ->using(WorkorderTraits::$stateText)
  102. // ->dot(
  103. // [
  104. // 1 => 'yellow',
  105. // 2 => 'danger',
  106. // 3 => 'success',
  107. // ]);
  108. // $grid->column('close_time');
  109. // $grid->column('created_at');
  110. // $grid->column('updated_at')->sortable();
  111. // $grid->disableActions();
  112. // $grid->filter(function (Grid\Filter $filter) {
  113. // $filter->equal('id');
  114. //
  115. // });
  116. // });
  117. //}
  118. //
  119. ///**
  120. // * Make a show builder.
  121. // *
  122. // * @param mixed $id
  123. // *
  124. // * @return Show
  125. // */
  126. //protected function detail($id)
  127. //{
  128. // return Show::make($id, new Workorder(), function (Show $show) {
  129. // $show->field('id');
  130. // $show->field('title');
  131. // $show->field('content');
  132. // $show->field('publisher_type');
  133. // $show->field('publisher_id');
  134. // $show->field('point_type');
  135. // $show->field('point_id');
  136. // $show->field('status');
  137. // $show->field('close_time');
  138. // $show->field('created_at');
  139. // $show->field('updated_at');
  140. // });
  141. //}
  142. /**
  143. * Make a form builder.
  144. *
  145. * @return Form
  146. */
  147. protected function form()
  148. {
  149. return Form::make(new Workorder(), function (Form $form) {
  150. $form->display('id');
  151. $form->text('title');
  152. $form->textarea('content');
  153. $form->select('point_type')
  154. ->options([
  155. 0 => '代理商',
  156. ])
  157. ->default(1);
  158. $form->select('point_id', '代理商')->options(function () {
  159. $agentIds = AgentProductItem::query()->where('supplier_id',Admin::user()->id)->distinct()->pluck('agent_id');
  160. return Agent::query()->whereIn('id',$agentIds)->pluck('name','id');
  161. });
  162. $form->hidden('publisher_type');
  163. $form->hidden('publisher_id');
  164. $form->saving(function (Form $form) {
  165. // 判断是否是新增操作
  166. if ($form->isCreating()) {
  167. $form->deleteInput('supplier_id');
  168. $form->deleteInput('guide_id');
  169. $form->point_type = DemandTraits::$col[$form->point_type];
  170. //发布人身份
  171. $form->publisher_type = DemandTraits::$col[1];
  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[1];
  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[1];
  222. $item->publisher_id = Admin::user()->id;
  223. $item->save();
  224. return $this->jsonSuccess($item);
  225. }
  226. }