123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- namespace App\Admin\Controllers\Company;
- use App\Http\Controllers\Controller;
- use App\Models\Admin\AdminRole;
- use App\Models\Admin\AdminUser;
- use App\Models\CompanyConsultant;
- use App\Models\Consultant;
- use App\Models\Subsite;
- use App\Validators\Rules\ChineseUsernameRule;
- use App\Validators\Rules\MobileRule;
- use App\Validators\Rules\QqRule;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Facades\Admin;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- class ConsultantController extends Controller
- {
- use HasResourceActions;
-
- /**
- * Index interface.
- *
- * @param Content $content
- * @param $type_id
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->header('顾问管理')
- ->description('列表')
- ->body(view('admin.company.consultant')->with(['gird'=>$this->grid()->render()]));
- }
-
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header('顾问管理')
- ->description('详情')
- ->body($this->detail($id));
- }
-
- /**
- * Edit interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header('顾问管理')
- ->description('编辑')
- ->body($this->editForm($id)->edit($id));
- }
-
- /**
- * Create interface.
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header('顾问管理')
- ->description('创建')
- ->body($this->createForm());
- }
-
- /**
- * Make a grid builder.
- *
- * @param $type_id
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Consultant);
-
- $grid->model()->when(get_subsite_id()>0, function ($query) {
- $query->where('subsite_id', get_subsite_id());
- $query->whereHas('subsite',function ($query){
- $query->where('effective', 1);
- });
- })->orderBy('id', 'desc');
-
- $grid->id('ID');
-
- $grid->name('顾问姓名')->width(200);
-
- $grid->mobile('顾问手机')->width(200);
-
- $grid->qq('顾问QQ')->width(200);
- if(get_subsite_open()){
- $grid->subsite_id('所属分站')->display(function ($subsite_id) {
- if ($subsite_id) {
- $Subsite = Subsite::find($subsite_id);
- return isset($Subsite->sitename) ? $Subsite->sitename : '未知';
- }
- return '总站';
- })->width(150);
- }
- $grid->created_at('创建时间')->sortable();
-
- $grid->updated_at('更新时间')->sortable();
-
-
- $grid->actions(function ($actions) use ($grid) {
- if (Admin::user()->can('company_consultant_set')) {
- $url=route("consultant.list",['id'=>$actions->row['id']]);
- $actions->prepend("<button class='btn btn-xs'><a target='_blank' href='{$url}' >关联企业</a></button>");
- }
- if (Admin::user()->can('company_consultant_list_edit')) {
- $actions->disableEdit(false);
- }
- if (Admin::user()->can('company_consultant_list_delete')) {
- $actions->disableDelete(false);
- }
- $actions->disableView(true);
- });
-
- if (Admin::user()->can('company_consultant_list_delete')) {
- $grid->tools(function ($tools) {
- $tools->batch(function ($batch) {
- $batch->disableDelete(false);
- });
- });
- $grid->disableRowSelector(false);
- }
-
- if (Admin::user()->can('company_consultant_list_create')) {
- $grid->disableCreateButton(false);
- }
-
- $grid->disableFilter();
- return $grid;
- }
-
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function editForm($id)
- {
- $form = new Form(new Consultant);
- $Con = Consultant::whereHas('adminUser')->find($id);
- $form->text('name', '姓名')->rules([
- 'required', new ChineseUsernameRule()
- ])->setWidth(3)->setMustMark();
-
- $form->mobile('mobile', '手机')->rules([
- 'required', new MobileRule()
- ])->setWidth(4)->setMustMark();
-
- $form->text('telephone', '电话')->setWidth(3);
-
- $form->text('qq', 'QQ')->rules([
- 'required', new QqRule()
- ])->setWidth(3)->setMustMark();
- $no_user = AdminUser::whereDoesntHave('consultant')->pluck('username','id')->toArray();
- $default = 0;
- if($Con){
- $no_user[$Con->adminUser->id] = $Con->adminUser->username;
- $default = $Con->adminUser->id;
- }
-
- $form->select('admin_users_id','绑定用户')->options($no_user)->default($default)->setWidth(3);
-
- $form->image('avatar', '照片')->setWidth(3);
-
- $form->saving(function (Form $form){
- if(!$form->admin_users_id){
- $form->admin_users_id=0;
- }
- });
-
- if (get_subsite_open()) {
- if (get_subsite_id() == 0) {
- //添加所属分站
- $form->radio('subsite_id', '所属分站')
- ->options(array_column(get_all_subsite(), 'sitename', 'id'))
- ->default(0)->setWidth(6);
- }
- }
-
- return $form;
- }
-
- protected function createForm()
- {
- $form = new Form(new Consultant);
-
- $form->text('name', '姓名')->rules([
- 'required', new ChineseUsernameRule()
- ])->setWidth(3)->setMustMark();
-
- $form->mobile('mobile', '手机')->rules([
- 'required', new MobileRule()
- ])->setWidth(4)->setMustMark();
-
- $form->text('telephone', '电话')->setWidth(3);
-
- $form->text('qq', 'QQ')->rules([
- 'required', new QqRule()
- ])->setWidth(3)->setMustMark();
-
- $form->select('admin_users_id','绑定用户')->options(AdminUser::whereDoesntHave('consultant')->pluck('username','id'))->setWidth(3);
-
- $form->image('avatar', '照片')->setWidth(3);
-
- $form->saving(function (Form $form){
- if(!$form->admin_users_id){
- $form->admin_users_id=0;
- }
- });
-
- if (get_subsite_open()) {
- if (get_subsite_id() == 0) {
- //添加所属分站
- $form->radio('subsite_id', '所属分站')->options(array_column(get_all_subsite(), 'sitename', 'id'))->default(0)->setWidth(6);
- }else{
- $form->hidden('subsite_id')->default(get_subsite_id());
- }
- }
-
- return $form;
- }
-
-
- /**
- * Store a newly created resource in storage.
- *
- * @return mixed
- */
- public function store()
- {
- return $this->createForm()->store();
- }
-
- public function update($id)
- {
- return $this->editForm($id)->update($id);
- }
-
-
- public function destroy($id)
- {
- if (!$id) {
- $data = [
- 'status' => false,
- 'message' => '顾问不存在!!',
- ];
- return response()->json($data);
- }
- $id_arr = explode(',', $id);
-
- \DB::beginTransaction();
- try {
- Consultant::destroy($id_arr);
- CompanyConsultant::whereIn('consultant_id', $id_arr)->delete();
- $data = [
- 'status' => true,
- 'message' => '删除成功!',
- ];
- \DB::commit();
- return response()->json($data);
- } catch (\Exception $e) {
- \DB::rollback();
- $data = [
- 'status' => false,
- 'message' => '删除失败!',
- ];
- return response()->json($data);
- }
- }
-
-
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(Consultant::findOrFail($id));
-
- return $show;
- }
-
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new Consultant);
-
- $form->display('ID');
- $form->switch('status');
- $form->text('points', '积分');
- $form->text('times', '每天赠送积分上限');
- $form->display('Created at');
- $form->display('Updated at');
-
- return $form;
- }
-
- public function consultantList(Content $content)
- {
- $username = '';
- $qq = '';
- $id = request()->id;
- if(empty($id)){
- admin_toastr('顾问不存在!', 'error');
- return back();
- }
- $grid = new Grid(new CompanyConsultant);
-
- $grid->model()->whereHas('companys')->whereHas('consultants')->where('consultant_id', $id)->orderBy('id', 'desc');
-
- $grid->column('companys.username','用户名')->display(function ($username){
- return $username ? $username : '未填写';
- });
-
- $grid->column('companys.companyname','企业信息')->display(function ($companyname){
- if($companyname){
- return '<a href="/content/jobs/company?id='.$this->company_id.'" target="_blank">'.$companyname.'</a>';
- }else{
- return '未填写';
- }
- });
-
- $grid->column('companys.audit','审核状态')->display(function ($audit){
- if ($audit==3) {
- return'<span style="color:#666666">审核未通过</span>';
- } elseif ($audit==1) {
- return'<span style="color: #009900">审核通过</span>';
- } else {
- return'<span style="color:#FF6600">等待审核</span>';
- }
- });
-
- $grid->column('companys.mobile','手机')->display(function ($mobile){
- return $mobile ? $mobile : '未填写';
- });
-
- $grid->column('companys.email','邮箱')->display(function ($email){
- return $email ? $email : '未填写';
- });
-
- $grid->column('companys.reg_time','注册时间')->display(function ($reg_time){
- return $reg_time ? date('Y-m-d H:i:s', $reg_time) : '';
- })->sortable();
-
- $grid->column('companys.last_login_time','最后登录时间')->display(function ($last_login_time){
- return $last_login_time ? date('Y-m-d H:i:s', $last_login_time) : '';
- })->sortable();
- $Consultant = Consultant::find($id);
- if($Consultant){
- $username = $Consultant->name;
- $qq = $Consultant->qq;
- }
-
- $grid->filter(function ($filter) {
-
- $filter->column(1/2, function ($filter) {
- $filter->like('companys.username', '用户名');
- $filter->like('companys.companyname', '企业信息');
- });
- $filter->column(1/2, function ($filter) {
- $filter->like('companys.mobile', '手机');
- $filter->like('companys.email', '邮箱');
- });
- });
-
-
- $grid->actions(function ($actions){
- $actions->disableView();
- if (Admin::user()->can('company_consultant_reset')) {
- $actions->append("<button class='btn btn-xs btn-primary consultant_reset' ls='".$actions->row['id']."'>重置</button>");
- }
- });
-
- $grid->tools(function ($tools){
- if (Admin::user()->can('company_consultant_reset')) {
- $but = <<<EOT
- <div class="btn-group" data-toggle="buttons">
- <label class="btn btn-google btn-sm" id="consultant_reset_list" title="重置顾问">
- <i class="fa fa-audio-description"></i>
- <input type="radio" class="user-gender">重置顾问
- </label>
- </div>
- EOT;
- $tools->append($but);
- }
- });
- return $content
- ->header('顾问列表')
- ->description('顾问姓名:'.$username.',顾问QQ:'.$qq)
- ->body(view('admin.company.consultant_list')->with(['gird'=>$grid->render()]));
- }
-
- public function consultantReset(Request $request)
- {
- $id = $request->id;
- $arr_id = array_filter(explode(',', $id));
- if(empty($arr_id)){
- return response()->json(['code'=>0,'info'=>'请勾选需要重置的会员']);
- }
- $result = CompanyConsultant::destroy($arr_id);
- if($result){
- return response()->json(['code'=>1,'info'=>'重置成功!']);
- }else{
- return response()->json(['code'=>0,'info'=>'重置失败!']);
- }
- }
-
-
- }
|