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.
		
		
		
		
		
			
		
			
				
					
					
						
							339 lines
						
					
					
						
							10 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							339 lines
						
					
					
						
							10 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminSupplier\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminAgent\Repositories\Workorder;
							 | 
						|
								use App\Http\Controllers\Controller;
							 | 
						|
								use App\Models\Agent;
							 | 
						|
								use App\Models\AgentProductItem;
							 | 
						|
								use App\Models\Guide;
							 | 
						|
								use App\Models\OrderProductItem;
							 | 
						|
								use App\Models\Supplier;
							 | 
						|
								use App\Models\WorkorderItem;
							 | 
						|
								use App\Service\Admin\GlobalNoticeService;
							 | 
						|
								use App\Traits\DemandTraits;
							 | 
						|
								use App\Traits\WorkorderTraits;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Layout\Content;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Illuminate\Http\Request;
							 | 
						|
								use App\Traits\ResponseHelper;
							 | 
						|
								use Illuminate\Support\Facades\Storage;
							 | 
						|
								use Illuminate\Support\Facades\Validator;
							 | 
						|
								
							 | 
						|
								class WorkorderController extends AdminController
							 | 
						|
								{
							 | 
						|
									use ResponseHelper;
							 | 
						|
								
							 | 
						|
									public function index(Content $content)
							 | 
						|
									{
							 | 
						|
										$workorderId = \request('workorder_id',0);
							 | 
						|
										$userList = $msgList = [];
							 | 
						|
								
							 | 
						|
										$workorder = \App\Models\Workorder::query()
							 | 
						|
											->with(['point','publisher'])
							 | 
						|
											->withCount(['items' => function($query) {
							 | 
						|
												$query->where('is_read', 0)
							 | 
						|
													->where('point_id',Admin::user()->id)
							 | 
						|
													->where('point_type',DemandTraits::$col[1]);
							 | 
						|
											}])
							 | 
						|
											->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[1]])
							 | 
						|
											->orWhere(function ($query) {
							 | 
						|
												$query->where('point_id',Admin::user()->id)
							 | 
						|
													->where('point_type',DemandTraits::$col[1]);
							 | 
						|
											})
							 | 
						|
											->orderByDesc('updated_at')
							 | 
						|
											->get();
							 | 
						|
								
							 | 
						|
										// 先这么处理吧
							 | 
						|
										if ($workorderId == 0 && !empty($workorder->toArray())) {
							 | 
						|
											return redirect('/admin-supplier/workorder_item?workorder_id='.$workorder[0]->id);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										foreach ($workorder as $v) {
							 | 
						|
											$item = WorkorderItem::query()->where('workorder_id',$v->id)->orderByDesc('id')->first();
							 | 
						|
											$arr = [
							 | 
						|
												'workorder_id' => $v->id,
							 | 
						|
												'unread' => true,
							 | 
						|
												'avatar' => Storage::url($v->publisher_type == DemandTraits::$col[1] ? $v->point->avatar : $v->publisher->avatar),
							 | 
						|
												'name' => $v->publisher_type == DemandTraits::$col[1] ? $v->point->name : $v->publisher->name,
							 | 
						|
												'content' => $v->title,
							 | 
						|
												'last_message' => $item ? ($item->type == 1 ? $item->content : '【 图片 】') : '',
							 | 
						|
												'last_message_time' => $item->created_at ?? '',
							 | 
						|
												'items_count' => $workorderId != $v->id ? $v->items_count : 0,
							 | 
						|
											];
							 | 
						|
											$userList [] = $arr;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$workorderItem = WorkorderItem::query()
							 | 
						|
											->with(['publisher'])
							 | 
						|
											->where('workorder_id',$workorderId)
							 | 
						|
											->get();
							 | 
						|
								
							 | 
						|
										foreach ($workorderItem as $v) {
							 | 
						|
											$arr = [
							 | 
						|
												'type' => $v->publisher_type == DemandTraits::$col[1] ? 'my' : 'op',
							 | 
						|
												'it_type' => $v->type,
							 | 
						|
												'avatar' => Storage::url($v->publisher->avatar),
							 | 
						|
												'name' => $v->publisher->name,
							 | 
						|
												'content' => $v->content,
							 | 
						|
												'created_at_text' => $v->created_at,
							 | 
						|
												'is_read' => $v->is_read,
							 | 
						|
											];
							 | 
						|
											$msgList [] = $arr;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										// 更新已读未读,此处没有考虑性能代码解藕等问题
							 | 
						|
										WorkorderItem::query()->where('workorder_id',$workorderId)->update(['is_read' => 1]);
							 | 
						|
								
							 | 
						|
										$data = [
							 | 
						|
											'user_list' => $userList,
							 | 
						|
											'msg_list' => $msgList,
							 | 
						|
											'user' => Admin::user(),
							 | 
						|
											'workorder' => [
							 | 
						|
												'workorder_id' => $workorderId
							 | 
						|
											],
							 | 
						|
											'route' => 'admin-supplier',
							 | 
						|
										];
							 | 
						|
								
							 | 
						|
										return $content
							 | 
						|
											->header('工单')
							 | 
						|
											->description('列表')
							 | 
						|
											->body(admin_view('admin.pages.workorder',$data));
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
								    ///**
							 | 
						|
								    // * Make a grid builder.
							 | 
						|
								    // *
							 | 
						|
								    // * @return Grid
							 | 
						|
								    // */
							 | 
						|
								    //protected function grid()
							 | 
						|
								    //{
							 | 
						|
								    //    return Grid::make(new Workorder(['publisher','point']), function (Grid $grid) {
							 | 
						|
									//		$grid->model()
							 | 
						|
									//			->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[1]])
							 | 
						|
									//			->orWhere(['point_id' => Admin::user()->id,'point_type' => DemandTraits::$col[1]]);
							 | 
						|
								    //        $grid->column('id')->sortable();
							 | 
						|
								    //        $grid->column('title');
							 | 
						|
								    //        $grid->column('content_modal','内容')->modal('详情',function ($modal) {
							 | 
						|
									//			$modal->xl();
							 | 
						|
									//			return $this->content;
							 | 
						|
									//		});
							 | 
						|
								    //        $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
							 | 
						|
								    //        $grid->column('publisher.name','发布人');
							 | 
						|
								    //        $grid->column('point_type')->using(DemandTraits::$polymorphic);
							 | 
						|
								    //        $grid->column('point.name','接收人');
							 | 
						|
								    //        $grid->column('status')
							 | 
						|
									//			->using(WorkorderTraits::$stateText)
							 | 
						|
									//			->dot(
							 | 
						|
									//			[
							 | 
						|
									//				1 => 'yellow',
							 | 
						|
									//				2 => 'danger',
							 | 
						|
									//				3 => 'success',
							 | 
						|
									//			]);
							 | 
						|
								    //        $grid->column('close_time');
							 | 
						|
								    //        $grid->column('created_at');
							 | 
						|
								    //        $grid->column('updated_at')->sortable();
							 | 
						|
									//		$grid->disableActions();
							 | 
						|
								    //        $grid->filter(function (Grid\Filter $filter) {
							 | 
						|
								    //            $filter->equal('id');
							 | 
						|
									//
							 | 
						|
								    //        });
							 | 
						|
								    //    });
							 | 
						|
								    //}
							 | 
						|
									//
							 | 
						|
								    ///**
							 | 
						|
								    // * Make a show builder.
							 | 
						|
								    // *
							 | 
						|
								    // * @param mixed $id
							 | 
						|
								    // *
							 | 
						|
								    // * @return Show
							 | 
						|
								    // */
							 | 
						|
								    //protected function detail($id)
							 | 
						|
								    //{
							 | 
						|
								    //    return Show::make($id, new Workorder(), function (Show $show) {
							 | 
						|
								    //        $show->field('id');
							 | 
						|
								    //        $show->field('title');
							 | 
						|
								    //        $show->field('content');
							 | 
						|
								    //        $show->field('publisher_type');
							 | 
						|
								    //        $show->field('publisher_id');
							 | 
						|
								    //        $show->field('point_type');
							 | 
						|
								    //        $show->field('point_id');
							 | 
						|
								    //        $show->field('status');
							 | 
						|
								    //        $show->field('close_time');
							 | 
						|
								    //        $show->field('created_at');
							 | 
						|
								    //        $show->field('updated_at');
							 | 
						|
								    //    });
							 | 
						|
								    //}
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new Workorder(), function (Form $form) {
							 | 
						|
								            $form->display('id');
							 | 
						|
								            //$form->text('title')->required();
							 | 
						|
								            $form->textarea('content')->required();
							 | 
						|
								            $form->hidden('point_type')
							 | 
						|
												->options([
							 | 
						|
													0 => '代理商',
							 | 
						|
												])
							 | 
						|
												->default(0)
							 | 
						|
												->required();
							 | 
						|
											$form->select('point_id', '代理商')->options(function () {
							 | 
						|
												$agentIds = AgentProductItem::query()->where('supplier_id',Admin::user()->id)->distinct()->pluck('agent_id');
							 | 
						|
												return Agent::query()->whereIn('id',$agentIds)->pluck('name','id');
							 | 
						|
											})->required();
							 | 
						|
											$form->hidden('publisher_type');
							 | 
						|
											$form->hidden('publisher_id');
							 | 
						|
											$form->saving(function (Form $form) {
							 | 
						|
												// 判断是否是新增操作
							 | 
						|
												if ($form->isCreating()) {
							 | 
						|
													$form->deleteInput('supplier_id');
							 | 
						|
													$form->deleteInput('guide_id');
							 | 
						|
													$form->point_type = DemandTraits::$col[$form->point_type];
							 | 
						|
													//发布人身份
							 | 
						|
													$form->publisher_type = DemandTraits::$col[1];
							 | 
						|
													$form->publisher_id = Admin::user()->id;
							 | 
						|
												}
							 | 
						|
											});
							 | 
						|
								
							 | 
						|
											$form->saved(function (Form $form) {
							 | 
						|
												if ($form->isCreating()) {
							 | 
						|
													$item = new WorkorderItem();
							 | 
						|
													$item->workorder_id = $form->getKey();
							 | 
						|
													$item->content = $form->content;
							 | 
						|
													$item->type = 1;
							 | 
						|
													$item->publisher_type = DemandTraits::$col[1];
							 | 
						|
													$item->publisher_id = Admin::user()->id;
							 | 
						|
													$item->point_type = $form->point_type;
							 | 
						|
													$item->point_id = $form->point_id;
							 | 
						|
													$item->save();
							 | 
						|
								
							 | 
						|
													// 缓存提示信息
							 | 
						|
													(new GlobalNoticeService())->cacheWOrderNotice($item->id);
							 | 
						|
												}
							 | 
						|
											});
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
									public function sendImage(Request $request)
							 | 
						|
									{
							 | 
						|
										$validator = Validator::make(request()->all(), [
							 | 
						|
											'workorder_id' => 'required|int',
							 | 
						|
										], [
							 | 
						|
											'*' => '参数异常',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										if ($validator->fails()) {
							 | 
						|
											return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$image = $request->file('image');
							 | 
						|
										if (empty($image) || !$image->isValid()) {
							 | 
						|
											return $this->error('您未上传任何文件');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$mime = $image->getMimeType();
							 | 
						|
										if (!in_array($mime, ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg'])) {
							 | 
						|
											return $this->error('上传图片格式错误');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										// 指定发送对象
							 | 
						|
										$workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
							 | 
						|
										if (empty($workorder)) {
							 | 
						|
											return $this->error('工单异常');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if ($workorder->publisher_type == DemandTraits::$col[1] && $workorder->publisher_id == Admin::user()->id) {
							 | 
						|
											$pointType = $workorder->point_type;
							 | 
						|
											$pointId = $workorder->point_id;
							 | 
						|
										} else {
							 | 
						|
											$pointType = $workorder->publisher_type;
							 | 
						|
											$pointId = $workorder->publisher_id;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$path = $request->image->store('public/images/workorder');
							 | 
						|
										$path = (Storage::url($path));
							 | 
						|
										$item = new WorkorderItem();
							 | 
						|
										$item->workorder_id = request('workorder_id',0);
							 | 
						|
										$item->content = $path;
							 | 
						|
										$item->type = 2;
							 | 
						|
										$item->publisher_type = DemandTraits::$col[1];
							 | 
						|
										$item->publisher_id = Admin::user()->id;
							 | 
						|
										$item->point_type = $pointType;
							 | 
						|
										$item->point_id = $pointId;
							 | 
						|
										$item->save();
							 | 
						|
								
							 | 
						|
										// 更新一下主表的时间做排序用
							 | 
						|
										$workorder->updated_at = time();
							 | 
						|
										$workorder->save();
							 | 
						|
								
							 | 
						|
										// 缓存提示信息
							 | 
						|
										(new GlobalNoticeService())->cacheWOrderNotice($item->id);
							 | 
						|
								
							 | 
						|
										return $this->jsonSuccess($item);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function sendText(Request $request)
							 | 
						|
									{
							 | 
						|
										$validator = Validator::make(request()->all(), [
							 | 
						|
											'workorder_id' => 'required|int',
							 | 
						|
											'content' => 'required',
							 | 
						|
										], [
							 | 
						|
											'*' => '参数异常',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										if ($validator->fails()) {
							 | 
						|
											return $this->jsonFailValidated('数据不全:'.$validator->errors()->first());
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										// 指定发送对象
							 | 
						|
										$workorder = \App\Models\Workorder::query()->find(request('workorder_id',0));
							 | 
						|
										if (empty($workorder)) {
							 | 
						|
											return $this->error('工单异常');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if ($workorder->publisher_type == DemandTraits::$col[1] && $workorder->publisher_id == Admin::user()->id) {
							 | 
						|
											$pointType = $workorder->point_type;
							 | 
						|
											$pointId = $workorder->point_id;
							 | 
						|
										} else {
							 | 
						|
											$pointType = $workorder->publisher_type;
							 | 
						|
											$pointId = $workorder->publisher_id;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$item = new WorkorderItem();
							 | 
						|
										$item->workorder_id = request('workorder_id',0);
							 | 
						|
										$item->content = request('content','');
							 | 
						|
										$item->type = 1;
							 | 
						|
										$item->publisher_type = DemandTraits::$col[1];
							 | 
						|
										$item->publisher_id = Admin::user()->id;
							 | 
						|
										$item->point_type = $pointType;
							 | 
						|
										$item->point_id = $pointId;
							 | 
						|
										$item->save();
							 | 
						|
								
							 | 
						|
										// 更新一下主表的时间做排序用
							 | 
						|
										$workorder->updated_at = time();
							 | 
						|
										$workorder->save();
							 | 
						|
								
							 | 
						|
										// 缓存提示信息
							 | 
						|
										(new GlobalNoticeService())->cacheWOrderNotice($item->id);
							 | 
						|
								
							 | 
						|
										return $this->jsonSuccess($item);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function checkUnread()
							 | 
						|
									{
							 | 
						|
										if (\request('badge', '')) {
							 | 
						|
											return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[1], true);
							 | 
						|
										} else {
							 | 
						|
											return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[1]);
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								}
							 |