Browse Source

修改客资获取

cqp 1 tháng trước cách đây
mục cha
commit
99a5bf1b9e

+ 274 - 0
app/Console/Commands/CustomerFromThreePlatForm.php

@@ -0,0 +1,274 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Model\BasicType;
+use App\Model\Customer;
+use App\Model\CustomerInfo;
+use App\Model\Depart;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class CustomerFromThreePlatForm extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'command:customer_from_three_plat_form';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        echo '任务--------start---------------';
+
+        $basic = BasicType::where('del_time',0)
+            ->where('type',BasicType::type_10)
+            ->select('id','title')
+            ->get()->toArray();
+        \App\Model\CustomerFromThreePlatForm::where("del_time",0)
+            ->where("customerName","<>","")
+            ->orderBy('id')
+            ->select('id','crt_time','city','actionType','acquireTime','clueType','product','wechat','clueId','transformType','clueSource','clueSourceId','customerName','platform','ipLocationCity','phone','isRepeatClue','flowType','fromUserId')
+            ->chunkById(20, function ($data) use($basic){
+                // 开启事务
+                DB::transaction(function () use ($data,$basic) {
+                    $dataArray = $data->toArray();
+                    $nowStamp = time();
+                    list($main, $main_detail, $fail) = $this->processingData($dataArray, $nowStamp,$basic);
+                    $c_third_platform_id = array_column($main,'c_third_platform_id');
+                    if (! empty($main)) {
+                        Customer::insert($main);
+
+                        $map = Customer::whereIn('c_third_platform_id',$c_third_platform_id)
+                            ->pluck('id','c_third_platform_id')
+                            ->toArray();
+                        $insert_detail = [];
+                        foreach ($main_detail as $c_third_platform_id => $value){
+                            if(isset($map[$c_third_platform_id])){
+                                foreach ($value as $v){
+                                    $v['customer_id'] = $map[$c_third_platform_id];
+                                    $insert_detail[] = $v;
+                                }
+                            }
+                        }
+                        if(! empty($insert_detail)) CustomerInfo::insert($insert_detail);
+                        if(! empty($fail)) {
+                            foreach ($fail as $update){
+                                \App\Model\CustomerFromThreePlatForm::where('id', $update['id'])
+                                    ->update(['del_time' => $update['result']]);
+                            }
+                        }
+                        echo '更新完成' . PHP_EOL;
+                    } else {
+                        echo '暂无数据写入' . PHP_EOL;
+                    }
+                });
+            });
+
+        echo '任务结束--------end---------------';
+    }
+
+    private function processingData2($data,$time, $basic,$top_depart_id){
+        $return = $return1 = $return2 = [];
+
+        $search = $info_phone = $info_wechat = [];
+        foreach ($data as $value){
+            //同样的客户信息只保留最后一次的id
+            if(! empty($value['phone'])) {
+                $info_phone[$value['phone']] = $value['id'];
+                $search[] = $value['phone'];
+            }
+            if(! empty($value['wechat'])) {
+                $info_wechat[$value['wechat']] = $value['id'];
+                $search[] = $value['wechat'];
+            }
+        }
+
+        $contact = CustomerInfo::from('customer_info as a')
+            ->join('customer as b','b.id','a.customer_id')
+            ->where('a.del_time',0)
+            ->where('b.del_time',0)
+            ->where('b.top_depart_id',$top_depart_id)
+            ->whereIn('a.contact_info', $search)
+            ->pluck('a.contact_info')
+            ->toArray();
+
+        //result 1 联系方式数据库中已存在失败 2 联系方式重复传递失败 3
+        foreach ($data as $value){
+            if(! empty($value['phone'])){
+                if(in_array($value['phone'], $contact)){
+                    $return2[] = [
+                        'id' => $value['id'],
+                        'result' => 1
+                    ];
+                }else{
+                    $tmp_id = $info_phone[$value['phone']] ?? 0;
+                    if($tmp_id != $value['id']){
+                        $return2[] = [
+                            'id' => $value['id'],
+                            'result' => 2
+                        ];
+                    }
+                }
+            }
+            if(! empty($value['wechat']) && in_array($value['wechat'], $contact)){
+                $return2[] = [
+                    'id' => $value['id'],
+                    'result' => 1
+                ];
+                continue;
+            }
+
+        }
+    }
+
+    private function processingData($data, $time,$basic)
+    {
+        $return = []; // 成功数据
+        $return_detail = []; // 成功数据
+        $fail   = []; // 失败数据(含 result 标记)
+        $basic_map = array_column($basic,'title','id');
+
+        $info_phone = [];
+        $info_wechat = [];
+        $search = [];
+
+        // 第一步:同批次去重(保留最后一次 ID)
+        foreach ($data as $value) {
+            if (!empty($value['phone'])) {
+                $info_phone[$value['phone']] = $value['id'];
+                $search[] = $value['phone'];
+            }
+            if (!empty($value['wechat'])) {
+                $info_wechat[$value['wechat']] = $value['id'];
+                $search[] = $value['wechat'];
+            }
+        }
+
+        // 第二步:数据库中已存在的联系方式
+        $contact = CustomerInfo::from('customer_info as a')
+            ->join('customer as b', 'b.id', 'a.customer_id')
+            ->where('a.del_time', 0)
+            ->where('b.del_time', 0)
+            ->where('b.top_depart_id', 2)
+            ->whereIn('a.contact_info', $search)
+            ->pluck('a.contact_info')
+            ->toArray();
+
+        $contact = array_flip($contact); // 方便 in_array 变成 isset 检查
+
+        // 第三步:逐条判断
+        foreach ($data as $value) {
+            $id = $value['id'];
+            $isFail = false;
+
+            // 检查手机号
+            if (!empty($value['phone'])) {
+                if (isset($contact[$value['phone']])) {
+                    // 数据库已存在
+                    $fail[] = ['id' => $id, 'result' => 1];
+                    $isFail = true;
+                } elseif (($info_phone[$value['phone']] ?? 0) !== $id) {
+                    // 同批重复
+                    $fail[] = ['id' => $id, 'result' => 2];
+                    $isFail = true;
+                }
+            }
+
+            // 检查微信号(如果之前手机号已 fail,这里也可以继续检查,因为两者可能不同)
+            if (!$isFail && !empty($value['wechat'])) {
+                if (isset($contact[$value['wechat']])) {
+                    $fail[] = ['id' => $id, 'result' => 1];
+                    $isFail = true;
+                } elseif (($info_wechat[$value['wechat']] ?? 0) !== $id) {
+                    $fail[] = ['id' => $id, 'result' => 2];
+                    $isFail = true;
+                }
+            }
+
+            // 成功数据
+            if (! $isFail) {
+                $fail[] = ['id' => $id, 'result' => 3];
+
+                $car_type = 0;
+                if(! empty($value['product'])){
+                    $t = explode('/', $value['product']);
+                    foreach ($t as $t_v){
+                        if($car_type) continue;
+                        if(isset($basic_map[$t_v])) $car_type = $basic_map[$t_v];
+                    }
+                }
+                $customer_from = 0;
+                if($value['platform'] == 1){
+                    $customer_from = 17190;//抖音DY
+                }elseif($value['platform'] == 2){
+                    $customer_from = 17202;//快手KS
+                }elseif ($value['platform'] == 5){
+                    $customer_from = 17195;//公众号GZH
+                }
+                $enter_time = $time;
+                if(! empty($value['acquireTime'])) $enter_time = bcdiv($value['acquireTime'],1000);
+                $return[] = [
+                    'c_third_platform_id' => $id,
+                    'title' => $value['customerName'],
+                    'model_type' => Customer::Model_type_one,
+                    'progress_stage' => 9315, //A后台私信
+                    'car_type' => $car_type,
+                    'customer_from' => $customer_from,
+                    'address2' => ! empty($value['city']) ? $value['city'] : $value['ipLocationCity'],
+                    'depart_id' => 2,
+                    'top_depart_id' => 2,
+                    'crt_id' => 1,
+                    'crt_time' => $time,
+                    'upd_time' => $time,
+                    'enter_time' => $enter_time,
+                ];
+
+                if(! empty($value['phone'])){
+                    $return_detail[$id][] = [
+                        'customer_id' => 0,
+                        'contact_type' => 133,
+                        'contact_info' => $value['phone'],
+                        'crt_time' => $time,
+                        'type' => CustomerInfo::type_one
+                    ];
+                }
+                if(! empty($value['wechat'])){
+                    $return_detail[$id][] = [
+                        'customer_id' => 0,
+                        'contact_type' => 135,
+                        'contact_info' => $value['wechat'],
+                        'crt_time' => $time,
+                        'type' => CustomerInfo::type_one
+                    ];
+                }
+            }
+        }
+
+        return [$return, $return_detail, $fail];
+    }
+
+}

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

@@ -2,11 +2,22 @@
 
 namespace App\Http\Controllers\Api;
 
+use App\Jobs\CustomerReceivingThirdPlatFormJob;
+use App\Model\CustomerFromThreePlatForm;
 use App\Service\CustomerService;
 use Illuminate\Http\Request;
 
 class CustomerController extends BaseController
 {
+    public function receivingCustomer(Request $request)
+    {
+        $data = $request->all();
+
+        CustomerReceivingThirdPlatFormJob::dispatch($data)->onQueue(CustomerFromThreePlatForm::job);
+
+        return ["code"=> "200", "msg"=> "SUCCESS"];
+    }
+
     public function customerAdd(Request $request)
     {
         $service = new CustomerService();

+ 75 - 0
app/Jobs/CustomerReceivingThirdPlatFormJob.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Model\CustomerFromThreePlatForm;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\Log;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class CustomerReceivingThirdPlatFormJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $data;
+
+    public function __construct($data)
+    {
+        $this->data = $data;
+    }
+
+    public function handle()
+    {
+        try {
+            if(empty($this->data) || empty($this->data["data"])) {
+                $this->delete();
+                return;
+            }
+            $data = $this->data["data"];
+            $array = json_decode($data,true);
+            $content = $array["content"];
+            $adv_id = "";
+            if(! empty($content['adInfo'])){
+                $adInfo = json_decode($content['adInfo'],true);
+                $adv_id = $adInfo['advId'] ?? "";
+            }
+
+            $insert = [
+                'crt_time' => time(),
+                'city' => $content['city'] ?? "",
+                'actionType' => $content["actionType"] ?? 0,
+                'acquireTime' => $content["acquireTime"] ?? "",
+                'clueType' => $content["clueType"] ?? 0,
+                'product' => $content["product"] ?? "",
+                'wechat' => $content["wechat"] ?? "",
+                'clueId' => $content["clueId"] ?? "",
+                'transformType' => $content["transformType"] ?? "",
+                'clueSource' => $content["clueSource"] ?? "",
+                'clueSourceId' => $content["clueSourceId"] ?? "",
+                'customerName' => $content["customerName"] ?? "",
+                'platform' => $content["platform"] ?? 0,
+                'ipLocationCity' => $content["ipLocationCity"] ?? "",
+                'phone' => $content["phone"] ?? "",
+                'isRepeatClue' => $content["isRepeatClue"] ?? 0,
+                'flowType' => $content["flowType"] ?? 0,
+                'fromUserId' => $array["fromUserId"] ?? "",
+                'adv_id' => $adv_id,
+            ];
+            CustomerFromThreePlatForm::insert($insert);
+
+        } catch (\Throwable $e) {
+            Log::error('Queue error:', ['exception' => $e->getMessage() . '|' . $e->getLine()]);
+            $this->delete();
+        }
+    }
+
+    protected function echoMessage(OutputInterface $output)
+    {
+        //输出消息
+        $output->writeln(json_encode($this->data));
+    }
+}

+ 3 - 0
app/Model/BasicType.php

@@ -11,6 +11,9 @@ class BasicType extends UseScopeBaseModel
 
     const type_22 = 22;
     const type_20 = 20;
+    const type_4 = 4;
+    const type_2 = 2;
+    const type_10 = 10;
     public static $type = [
         1 => '客户意向类型',
         2 => '客户来源类型',

+ 16 - 0
app/Model/CustomerFromThreePlatForm.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+
+class CustomerFromThreePlatForm extends Model
+{
+    protected $table = "customer_from_three_platform"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+    const job = 't9_customer_from_online';
+}

+ 1 - 1
app/Service/CustomerService.php

@@ -1001,7 +1001,7 @@ class CustomerService extends Service
         if(empty($data['car_type']) && ! empty($data['car_type_title'])){
             $bool = BasicType::where('title',$data['car_type_title'])
                 ->where('top_depart_id',$data['top_depart_id'])
-                ->where('type',10)
+                ->where('type',BasicType::type_10)
                 ->where('del_time',0)
                 ->exists();
             if($bool) return [false,'车型名称已存在'];