cqpCow vor 1 Jahr
Ursprung
Commit
f9ea887727

+ 13 - 0
app/Http/Controllers/Api/CustomerController.php

@@ -70,4 +70,17 @@ class CustomerController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function customerGrabbing(Request $request)
+    {
+        $service = new CustomerService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerGrabbing($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 1 - 1
app/Model/Customer.php

@@ -22,7 +22,7 @@ class Customer extends Model
     public static $user = [];
     public static $is_search = false;
     public static $is_pond_state = true;
-
+    public static $limitKey = "customerPondState";
     public function __construct(array $attributes = [])
     {
         if(! empty($attributes['userData'])) {

+ 4 - 1
app/Model/CustomerInfo.php

@@ -31,7 +31,10 @@ class CustomerInfo extends Model
     public static $man = [
         self::type_two,
         self::type_three,
-        self::type_four,
         self::type_eight,
     ];
+    public static $man2 = [
+        self::type_two,
+        self::type_three,
+    ];
 }

+ 66 - 10
app/Service/CustomerService.php

@@ -441,20 +441,20 @@ class CustomerService extends Service
                 $customer['customer_contact'][] = $tmp;
             }elseif ($value['type'] == CustomerInfo::type_two){
                 $tmp = [
-                    'id' => $value['employee_id'],
-                    'name' => $emp_map[$value['employee_id']] ?? '',
+                    'id' => $value['data_id'],
+                    'name' => $emp_map[$value['data_id']] ?? '',
                 ];
                 $customer['employee_one'][] = $tmp;
             }elseif ($value['type'] == CustomerInfo::type_three){
                 $tmp = [
-                    'id' => $value['employee_id'],
-                    'name' => $emp_map[$value['employee_id']] ?? '',
+                    'id' => $value['data_id'],
+                    'name' => $emp_map[$value['data_id']] ?? '',
                 ];
                 $customer['employee_two'][] = $tmp;
             }elseif ($value['type'] == CustomerInfo::type_four){
                 $tmp = [
-                    'id' => $value['employee_id'],
-                    'name' => $emp_map[$value['employee_id']] ?? '',
+                    'id' => $value['data_id'],
+                    'name' => $emp_map[$value['data_id']] ?? '',
                 ];
                 $customer['employee_three'][] = $tmp;
             }elseif ($value['type'] == CustomerInfo::type_five){
@@ -471,14 +471,14 @@ class CustomerService extends Service
                 $customer['file'][] = $tmp;
             }elseif ($value['type'] == CustomerInfo::type_seven){
                 $tmp = [
-                    'id' => $value['depart_id'],
-                    'name' => $depart_map[$value['depart_id']],
+                    'id' => $value['data_id'],
+                    'name' => $depart_map[$value['data_id']],
                 ];
                 $customer['depart'][] = $tmp;
             }elseif ($value['type'] == CustomerInfo::type_eight){
                 $tmp = [
-                    'id' => $value['employee_id'],
-                    'name' => $emp_map[$value['employee_id']] ?? '',
+                    'id' => $value['data_id'],
+                    'name' => $emp_map[$value['data_id']] ?? '',
                 ];
                 $customer['employee'][] = $tmp;
             }
@@ -631,4 +631,60 @@ class CustomerService extends Service
 
         return $data;
     }
+
+    //抢客户
+    public function customerGrabbing($data, $user){
+        $key = Customer::$limitKey . $data['customer_id'];
+        list($status,$msg) = $this->customerGrabbingRule($data,$key);
+        if(! $status) return [false,$msg];
+
+        try {
+            DB::beginTransaction();
+            $time = time();
+
+            //销售/负责/协同人清空
+            CustomerInfo::where('del_time',0)
+                ->where('customer_id',$data['customer_id'])
+                ->whereIn('type', CustomerInfo::$man2)
+                ->update(['del_time' => $time]);
+            //增加自己为负责人
+            $insert[] = [
+                'customer_id' => $data['customer_id'],
+                'data_id' => $user['id'],
+                'type' => CustomerInfo::type_two,
+                'crt_time' => $time
+            ];
+            CustomerInfo::insert($insert);
+            Customer::where('id',$data['customer_id'])->update([
+                'pond_state' => 0
+            ]);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            //释放锁
+            $this->dellimitingSendRequestBackg($key);
+
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        //释放锁
+        $this->dellimitingSendRequestBackg($key);
+
+        return [true, ''];
+    }
+
+    public function customerGrabbingRule($data, $key){
+        if(empty($data['customer_id'])) return [false,'请选择客户'];
+        //上锁
+        list($status,$msg) = $this->limitingSendRequestBackg($key);
+        if(! $status) return [false,$msg];
+
+        $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
+        if(empty($customer)) return [false,'客户不存在或已被删除'];
+        $customer = $customer->toArray();
+        if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
+
+        return [true,''];
+    }
 }

+ 1 - 0
routes/api.php

@@ -87,6 +87,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('customerAdd', 'Api\CustomerController@customerAdd');
     $route->any('customerDel', 'Api\CustomerController@customerDel');
     $route->any('customerDetail', 'Api\CustomerController@customerDetail');
+    $route->any('customerGrabbing', 'Api\CustomerController@customerGrabbing');
 
     //联系人
     $route->any('contactsList', 'Api\ContactsController@contactsList');