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

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