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

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