| 
					
				 | 
			
			
				@@ -5,6 +5,9 @@ namespace app\api\controller\company; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 use app\common\controller\Apic; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 use think\Db; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use app\common\model\User; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use fast\Random; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * 客户 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  */ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -82,11 +85,148 @@ class Customer extends Apic 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     //新增 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public function add(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $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->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('添加成功'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 生成不重复的随机数字字母组合 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    function getUinqueNo($length = 8, $nos = []) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $newid = Random::build("alnum", $length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (in_array($newid, $nos)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $newid = $this->getUinqueNo($length, $nos); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return $newid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    //检索用户 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function searchuser(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $mobile = input('mobile',''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $check = Db::name('user')->field('id,nickname,mobile')->where('mobile',$mobile)->find(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->success(1,$check); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     //余额管理 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public function changemoney(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $id = input('id',0); 
			 |