Explorar el Código

客户&商机

cqp hace 11 meses
padre
commit
f3fe12b2c4

+ 74 - 0
app/Http/Controllers/Api/BusinessOpportunityController.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\BusinessOpportunityService;
+use Illuminate\Http\Request;
+
+class BusinessOpportunityController extends BaseController
+{
+    public function BusinessOpportunityAdd(Request $request)
+    {
+        $service = new BusinessOpportunityService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->BusinessOpportunityAdd($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function BusinessOpportunityEdit(Request $request)
+    {
+        $service = new BusinessOpportunityService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->BusinessOpportunityEdit($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function BusinessOpportunityDel(Request $request)
+    {
+        $service = new BusinessOpportunityService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->BusinessOpportunityDel($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function BusinessOpportunityList(Request $request)
+    {
+        $service = new BusinessOpportunityService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->BusinessOpportunityList($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function BusinessOpportunityDetail(Request $request)
+    {
+        $service = new BusinessOpportunityService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->BusinessOpportunityDetail($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 17 - 0
app/Model/BusinessOpportunity.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Model;
+
+class BusinessOpportunity extends UseScopeBaseModel
+{
+    protected $table = "business_opportunity"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    public static $prefix = "sj";
+
+    public static $user = [];
+    public static $search = [];
+    public static $limitKey = "";
+    const range_function = '';
+}

+ 22 - 0
app/Model/BusinessOpportunityInfo.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class BusinessOpportunityInfo extends Model
+{
+    protected $table = "business_opportunity_info"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const type_one = 1; // 联系方式
+    const type_five = 5; // 图片
+    const type_six = 6; // 文件
+    public static $type = [
+        self::type_one,
+        self::type_five,
+        self::type_six,
+    ];
+
+}

+ 3 - 1
app/Model/FollowUpRecord.php

@@ -10,8 +10,10 @@ class FollowUpRecord extends Model
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
-    const type_one = 'customer';
+    const type_one = 'customer'; //客户
+    const type_two = 'business_opportunity'; //商机
     public static $type = [
         self::type_one => '客户跟进记录',
+        self::type_two => '商机跟进记录',
     ];
 }

+ 388 - 0
app/Service/BusinessOpportunityService.php

@@ -0,0 +1,388 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\BusinessOpportunity;
+use App\Model\BusinessOpportunityInfo;
+use App\Model\Employee;
+use App\Model\FollowUpRecord;
+use Illuminate\Support\Facades\DB;
+
+class BusinessOpportunityService extends Service
+{
+    public function BusinessOpportunityEdit($data,$user){
+        list($status,$msg) = $this->BusinessOpportunityRule($data,$user, false);
+        if(!$status) return [$status,$msg];
+
+        $params = $this->getDataFile($data);
+        (new OperationLogService())->setOperationList($params,$user,2);
+
+        try {
+            DB::beginTransaction();
+
+            $model = BusinessOpportunity::where('id',$data['id'])->first();
+            $model->title = $data['title'] ?? "";
+            $model->relationship_level = $data['relationship_level'] ?? 0;
+            $model->is_company = $data['is_company'] ?? 0;
+            $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
+            $model->address2 = $data['address2'] ?? '';
+            $model->mark = $data['mark'] ?? '';
+            $model->intention = $data['intention'] ?? '';
+            $model->customer_type = $data['customer_type'] ?? '';
+            $model->save();
+            $time = time();
+
+            $old = BusinessOpportunityInfo::where('del_time',0)
+                ->where('business_opportunity_id',$data['id'])
+                ->whereIn('type',[BusinessOpportunityInfo::type_five,BusinessOpportunityInfo::type_six])
+                ->select('file')
+                ->get()->toArray();
+            $old = array_column($old,'file');
+            BusinessOpportunityInfo::where('del_time',0)
+                ->where('customer_id',$data['id'])
+                ->update(['del_time' => $time]);
+
+            if(! empty($data['customer_contact'])){
+                $insert = [];
+                foreach ($data['customer_contact'] as $value){
+                    $insert[] = [
+                        'business_opportunity_id' => $model->id,
+                        'contact_type' => $value['type'],
+                        'contact_info' => $value['info'],
+                        'type' => BusinessOpportunityInfo::type_one,
+                        'crt_time' => $time,
+                    ];
+                }
+                BusinessOpportunityInfo::insert($insert);
+            }
+
+            $new = [];
+            if(! empty($data['img'])){
+                $insert = [];
+                foreach ($data['img'] as $value){
+                    $insert[] = [
+                        'business_opportunity_id' => $model->id,
+                        'file' => $value['url'],
+                        'type' => BusinessOpportunityInfo::type_five,
+                        'name' => $value['name'],
+                        'crt_time' => $time,
+                    ];
+                    if(in_array($value['url'], $old)) {
+                        foreach ($old as $o_k => $o_v){
+                            if($o_v == $value['url']) unset($old[$o_k]);
+                        }
+                    }else{
+                        $new[] = $value['url'];
+                    }
+                }
+                BusinessOpportunityInfo::insert($insert);
+            }
+
+            if(! empty($data['file'])){
+                $insert = [];
+                foreach ($data['file'] as $value){
+                    $insert[] = [
+                        'business_opportunity_id' => $model->id,
+                        'file' => $value['url'],
+                        'type' => BusinessOpportunityInfo::type_six,
+                        'name' => $value['name'],
+                        'crt_time' => $time,
+                    ];
+                    if(in_array($value['url'], $old)) {
+                        foreach ($old as $o_k => $o_v){
+                            if($o_v == $value['url']) unset($old[$o_k]);
+                        }
+                    }else{
+                        $new[] = $value['url'];
+                    }
+                }
+                BusinessOpportunityInfo::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ['file' => ['new' => $new, 'old' => $old]]];
+    }
+
+    public function BusinessOpportunityAdd($data,$user){
+        list($status,$msg) = $this->BusinessOpportunityRule($data, $user);
+        if(! $status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = new BusinessOpportunity();
+            $model->order_number = $data['order_number'];
+            $model->title = $data['title'] ?? "";
+            $model->relationship_level = $data['relationship_level'] ?? 0;
+            $model->is_company = $data['is_company'] ?? 0;
+            $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
+            $model->address2 = $data['address2'] ?? '';
+            $model->mark = $data['mark'] ?? '';
+            $model->intention = $data['intention'] ?? '';
+            $model->customer_type = $data['customer_type'] ?? '';
+            $model->crt_id = $user['id'];
+            $model->save();
+            $time = time();
+
+            if(! empty($data['customer_contact'])){
+                $insert = [];
+                foreach ($data['customer_contact'] as $value){
+                    $insert[] = [
+                        'business_opportunity_id' => $model->id,
+                        'contact_type' => $value['type'],
+                        'contact_info' => $value['info'],
+                        'type' => BusinessOpportunityInfo::type_one,
+                        'crt_time' => $time,
+                    ];
+                }
+                BusinessOpportunityInfo::insert($insert);
+            }
+
+            $new = [];
+            if(! empty($data['img'])){
+                $insert = [];
+                foreach ($data['img'] as $value){
+                    $insert[] = [
+                        'business_opportunity_id' => $model->id,
+                        'file' => $value['url'],
+                        'type' => BusinessOpportunityInfo::type_five,
+                        'name' => $value['name'],
+                        'crt_time' => $time,
+                    ];
+                    if(! empty($value['url'])) $new[] = $value['url'];
+                }
+                BusinessOpportunityInfo::insert($insert);
+            }
+            if(! empty($data['file'])){
+                $insert = [];
+                foreach ($data['file'] as $value){
+                    $insert[] = [
+                        'business_opportunity_id' => $model->id,
+                        'file' => $value['url'],
+                        'type' => BusinessOpportunityInfo::type_six,
+                        'name' => $value['name'],
+                        'crt_time' => $time,
+                    ];
+                    if(! empty($value['url'])) $new[] = $value['url'];
+                }
+                BusinessOpportunityInfo::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        (new OperationLogService())->setOperationList($data,$user);
+
+        return [true, ['file' => ['new' => $new]]];
+    }
+
+    public function BusinessOpportunityDel($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+
+        if($data['id'] < 0) return [false, '系统数据,操作失败!'];
+        try {
+            DB::beginTransaction();
+
+            BusinessOpportunity::where('id',$data['id'])->update([
+                'del_time'=> time()
+            ]);
+            $old = BusinessOpportunityInfo::where('del_time',0)
+                ->where('business_opportunity_id',$data['id'])
+                ->whereIn('type',[BusinessOpportunityInfo::type_five,BusinessOpportunityInfo::type_six])
+                ->select('file')
+                ->get()->toArray();
+            $old = array_column($old,'file');
+
+            BusinessOpportunityInfo::where('del_time',0)
+                ->where('business_opportunity_id',$data['id'])
+                ->update(['del_time' => time()]);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ['file' => ['old' => $old]]];
+    }
+
+    public function BusinessOpportunityDetail($data,$user){
+        if($this->isEmpty($data,'id')) return [false,'请选择商机资源'];
+
+        $customer = BusinessOpportunity::where('del_time',0)
+            ->where('id',$data['id'])
+            ->first();
+        if(empty($customer)) return [false,'商机不存在或已被删除'];
+        $customer = $customer->toArray();
+
+        //地址
+        $address_map = config('address');
+        $address_str = [];
+        if(! empty($customer['address1'])) {
+            $tmp = json_decode($customer['address1'],true);
+            $this->findLabelsByValue($address_map,$tmp,$address_str);
+            $customer['address1'] = $tmp;
+            $tmp = implode(' ',$address_str);
+            $tmp .= ' ' . $customer['address2'];
+            $address = $tmp;
+        }else{
+            $address = $customer['address2'];
+        }
+        $customer['address'] = $address;
+        $customer['customer_contact'] = $customer['img'] = $customer['file'] = [];
+
+        //详情
+        $customer_info = BusinessOpportunityInfo::where('del_time',0)
+            ->where('business_opportunity_id',$customer['id'])
+            ->select('id','business_opportunity_id','contact_type','contact_info','data_id','file','type','name')
+            ->get()->toArray();
+        $emp_id = [];
+        $emp_id[] = $customer['crt_id'];
+
+        $emp_map = Employee::whereIn('id',array_unique($emp_id))
+            ->pluck('emp_name','id')
+            ->toArray();
+
+        $fileUploadService = new FileUploadService();
+        foreach ($customer_info as $value){
+            if($value['type'] == BusinessOpportunityInfo::type_one){
+                $tmp = [
+                    'id' => $value['contact_type'],
+                    'type' => $value['contact_type'],
+                    'info' => $value['contact_info']
+                ];
+                $customer['customer_contact'][] = $tmp;
+            }elseif ($value['type'] == BusinessOpportunityInfo::type_five){
+                $tmp = [
+                    'url' => $value['file'],
+                    'name' => $value['name'],
+                    'show_url' => $fileUploadService->getFileShow($value['file']),
+                ];
+                $customer['img'][] = $tmp;
+            }elseif ($value['type'] == BusinessOpportunityInfo::type_six){
+                $tmp = [
+                    'url' => $value['file'],
+                    'name' => $value['name'],
+                    'show_url' => $fileUploadService->getFileShow($value['file']),
+                ];
+                $customer['file'][] = $tmp;
+            }
+        }
+        $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
+        $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
+
+        return [true, $customer];
+    }
+
+    public function BusinessOpportunityList($data,$user){
+        $model = BusinessOpportunity::where('del_time',0)
+            ->select('title','id','order_number','intention','customer_type','address1','address2','crt_id','crt_time','mark','relationship_level','is_company')
+            ->orderby('id', 'desc');
+
+        if(! empty($data['id'])) $model->where('id', $data['id']);
+        if(isset($data['relationship_level'])) $model->where('relationship_level', $data['relationship_level']);
+        if(isset($data['is_company'])) $model->where('is_company', $data['is_company']);
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+        if(! empty($data['time_type'])) {
+            if($data['time_type'] == 1) {
+                $start = strtotime('today');
+                $end = strtotime('tomorrow') - 1;
+            }elseif ($data['time_type'] == 2){
+                $start = strtotime('this week',strtotime('today'));
+                $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
+            }
+            if(! empty($start) && ! empty($end)) {
+                $model->where('crt_time','>=',$start);
+                $model->where('crt_time','<=',$end);
+            }
+        }
+        if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
+        if(! empty($data['intention'])) $model->where('intention',$data['intention']);
+        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
+            $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
+            $model->where('crt_time','>=',$return[0]);
+            $model->where('crt_time','<=',$return[1]);
+        }
+        if(! empty($data['contact_info'])){
+            $customer_info = BusinessOpportunityInfo::where('del_time',0)
+                ->where("type",BusinessOpportunityInfo::type_one)
+                ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
+                ->select('business_opportunity_id')
+                ->get()->toArray();
+            $model->whereIn('id',array_column($customer_info,'business_opportunity_id'));
+        }
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list,$data);
+
+        return [true, $list];
+    }
+
+    public function BusinessOpportunityRule(&$data, $user, $is_add = true){
+        if(empty($data['title'])) return [false,'商机名称不能为空'];
+
+        if($is_add){
+            $data['order_number'] = (new OrderNoService())->createOrderNumber(BusinessOpportunity::$prefix);
+        }else{
+            if(empty($data['id'])) return [false,'ID不能为空'];
+        }
+
+        return [true, ''];
+    }
+
+    public function fillData($data,$ergs){
+        if(empty($data['data'])) return $data;
+
+        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
+            ->pluck('emp_name','id')
+            ->toArray();
+
+        //跟进记录
+        $record_array = (new FollowUpRecordService())->getVisitDataOfTime(array_column($data['data'],'id'), FollowUpRecord::type_two);
+
+        $customer_info = BusinessOpportunityInfo::where('del_time',0)
+            ->whereIn('business_opportunity_id',array_column($data['data'],'id'))
+            ->whereIn('type',[BusinessOpportunityInfo::type_one])
+            ->select('type','contact_type','contact_info','customer_id','data_id')
+            ->get()->toArray();
+
+        //联系方式
+        $customer_info_map = [];
+        foreach ($customer_info as $value){
+            $customer_info_map[$value['customer_id']][] = $value;
+        }
+
+        $address_map = config('address');
+        foreach ($data['data'] as $key => $value){
+            $address_str = [];
+            if(! empty($value['address1'])) {
+                $tmp = json_decode($value['address1'],true);
+                $this->findLabelsByValue($address_map,$tmp,$address_str);
+                $tmp = implode(' ',$address_str);
+                $tmp .= ' ' . $value['address2'];
+                $address = $tmp;
+            }else{
+                $address = $value['address2'];
+            }
+            $data['data'][$key]['address'] = $address;
+            $data['data'][$key]['is_company_title'] = $value['customer_intention'] ? '是' : '否';
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+            $record_tmp = $record_array[$value['id']] ?? "";
+            $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
+            $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
+            $customer_tmp = $customer_info_map[$value['id']] ?? [];
+            $data['data'][$key]['customer_detail'] = $customer_tmp;
+        }
+
+        return $data;
+    }
+}

+ 1 - 1
app/Service/CustomerService.php

@@ -136,7 +136,7 @@ class CustomerService extends Service
             DB::beginTransaction();
 
             $model = new Customer();
-            $model->order_number = (new OrderNoService())->createOrderNumber(Customer::$prefix);
+            $model->order_number = $data['order_number'];
             $model->title = $data['title'] ?? "";
             $model->relationship_level = $data['relationship_level'] ?? 0;
             $model->is_company = $data['is_company'] ?? 0;

+ 3 - 0
app/Service/FollowUpRecordService.php

@@ -132,6 +132,7 @@ class FollowUpRecordService extends Service
         if(! empty($data['data_id'])) $model->where('data_id',$data['data_id']);
         if(! empty($data['crt_id'])) $model->where('crt_id',$data['crt_id']);
         if(! empty($data['type'])) $model->where('type',$data['type']);
+        if(! empty($data['level'])) $model->where('level',$data['level']);
 
         $list = $this->limit($model,'',$data);
         $list = $this->organizationData($list);
@@ -163,6 +164,8 @@ class FollowUpRecordService extends Service
         if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在'];
         if($data['type'] == FollowUpRecord::type_one){
             $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
+        }elseif($data['type'] == FollowUpRecord::type_two){
+            $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
         }
         if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
         if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];

+ 7 - 1
routes/api.php

@@ -66,7 +66,13 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('customerAdd', 'Api\CustomerController@customerAdd')->middleware('OssFileDeal');
     $route->any('customerDel', 'Api\CustomerController@customerDel')->middleware('OssFileDeal');
     $route->any('customerDetail', 'Api\CustomerController@customerDetail');
-    $route->any('customerGrabbing', 'Api\CustomerController@customerGrabbing');
+
+    //商机
+    $route->any('BusinessOpportunityList', 'Api\BusinessOpportunityController@BusinessOpportunityList');
+    $route->any('BusinessOpportunityEdit', 'Api\BusinessOpportunityController@BusinessOpportunityEdit')->middleware('OssFileDeal');
+    $route->any('BusinessOpportunityAdd', 'Api\BusinessOpportunityController@BusinessOpportunityAdd')->middleware('OssFileDeal');
+    $route->any('BusinessOpportunityDel', 'Api\BusinessOpportunityController@BusinessOpportunityDel')->middleware('OssFileDeal');
+    $route->any('BusinessOpportunityDetail', 'Api\BusinessOpportunityController@BusinessOpportunityDetail');
 
     //获取考勤的图片
     $route->get('/kq/image/{filename}', function ($filename) {