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

248 lines
7.3 KiB

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