model = new \app\admin\model\UserWallet; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['company','user','staff']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->getRelation('company')->visible(['name']); $row->getRelation('user')->visible(['nickname']); $row->getRelation('staff')->visible(['truename']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加客户 */ public function newone(){ if (!$this->request->post()) { $data = [ '线下新客','老带新','平台引流','自然进店' ]; $this->assign('comefrom',$data); return $this->view->fetch(); } $field = ['nickname','mobile','car_number','address','comefrom','remark']; $data = request_post_hub($field); $user = Db::name('user')->field('id,nickname,mobile')->where('mobile',$data['mobile'])->find(); //已经存在的用户 if($user){ //已经是我公司的客户 $map = [ 'w.user_id' => $user['id'], 'w.company_id' => $this->auth->company_id, ]; $check = Db::name('user_wallet')->alias('w') ->field('w.*,staff.truename') ->join('company_staff staff','w.staff_id = staff.id','LEFT') ->where($map)->find(); if($check){ $this->error('已经是['.$check['truename'].']的客户,无需重复添加'); } Db::startTrans(); //添加新客户 $new_data = [ 'user_id' => $user['id'], 'company_id' => $this->auth->company_id, 'staff_id' => $this->auth->staff_id, 'money' => 0, 'address' => $data['address'], 'createtime' => time(), 'updatetime' => time(), 'comefrom' => $data['comefrom'], 'remark' => $data['remark'], ]; $rs_customer = Db::name('user_wallet')->insertGetId($new_data); if(!$rs_customer){ Db::rollback(); $this->error('客户添加失败'); } //加新车 $car_map = [ 'user_id' => $user['id'], 'car_number' => $data['car_number'], ]; $car_info = Db::name('user_car')->where($car_map)->find(); if(empty($car_info)){ $car_map['createtime'] = time(); $car_map['updatetime'] = time(); $rs_car = Db::name('user_car')->insertGetId($car_map); if(!$rs_car){ Db::rollback(); $this->error('车辆添加失败'); } } Db::commit(); $this->success('添加完成'); }else{ //注册新用户 //$introcode = User::column("introcode"); $user_data = [ 'nickname' => $data['nickname'], 'mobile' => $data['mobile'], 'avatar' => '/assets/img/avatar.png', //'introcode' => $this->getUinqueNo(8, $introcode), 'jointime' => time(), 'joinip' => request()->ip(), 'status' => 1, 'company_id'=> $this->auth->company_id, ]; Db::startTrans(); $user_id = Db::name('user')->insertGetId($user_data); if(!$user_id){ Db::rollback(); $this->error('添加客户失败'); } $username = 'u' . (10000 + $user_id); Db::name('user')->where('id',$user_id)->update(['username'=>$username]); //添加新客户 $new_data = [ 'user_id' => $user_id, 'company_id' => $this->auth->company_id, 'staff_id' => $this->auth->staff_id, 'money' => 0, 'address' => $data['address'], 'createtime' => time(), 'updatetime' => time(), 'comefrom' => $data['comefrom'], 'remark' => $data['remark'], ]; $rs_customer = Db::name('user_wallet')->insertGetId($new_data); if(!$rs_customer){ Db::rollback(); $this->error('客户添加失败'); } //加新车 $car_map = [ 'user_id' => $user_id, 'car_number' => $data['car_number'], 'createtime' => time(), 'updatetime' => time(), ]; $rs_car = Db::name('user_car')->insertGetId($car_map); if(!$rs_car){ Db::rollback(); $this->error('车辆添加失败'); } Db::commit(); $this->success('添加完成'); } $this->success('添加成功'); } }