|
@@ -70,4 +70,132 @@ class UserWallet extends Backend
|
|
|
return $this->view->fetch();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加客户
|
|
|
+ */
|
|
|
+ public function newone(){
|
|
|
+ if (!$this->request->post()) {
|
|
|
+ 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->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('添加成功');
|
|
|
+ }
|
|
|
+
|
|
|
}
|