| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 | <?phpnamespace App\Service;use App\Model\BasicType;use App\Model\BasicTypeAllUse;use App\Model\Customer;use App\Model\CustomerInfo;use App\Model\CustomerRepeat;use App\Model\Depart;use App\Model\Employee;use App\Model\FollowUpRecord;use App\Model\Product;use App\Model\RoleMenuButton;use App\Model\SeeRange;use App\Model\WxEmployeeOfficial;use Carbon\Carbon;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\Facades\DB;/** * 客户管理相关 */class CustomerService extends Service{    /**     * 客户编辑     * @param $data     * @param $user     * @return array     */    public function customerEdit($data,$user){        list($status,$msg) = $this->customerRule($data,$user, false);        if(!$status) return [$status,$msg];        $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $data['title'];        $params = $this->getDataFile($data);        (new OperationLogService())->setOperationList($params,$user,2);        try {            DB::beginTransaction();            //车型            if(empty($data['car_type']) && ! empty($data['car_type_title'])){                $model_2 = new BasicType();                $model_2->title = $data['car_type_title'];                $model_2->type = 10;                $model_2->depart_id = $data['depart_id'] ?? 0;                $model_2->top_depart_id = $data['top_depart_id'] ?? 0;                $model_2->crt_id = $user['id'];                $model_2->save();                $data['car_type'] = $model_2->id;            }            $model = Customer::where('id',$data['id'])->first();            $model->title = $data['title'];            $model->code = $data['code'] ?? "";            $model->model_type = $data['model_type'];            $model->customer_intention = $data['customer_intention'] ?? 0;            $model->customer_from = $data['customer_from'] ?? 0;            $model->customer_type = $data['customer_type'] ?? 0;            $model->car_type = $data['car_type'] ?? 0;            $model->consulting_product = $data['consulting_product'] ?? '';            $model->consulting_product_new = $data['consulting_product_new'] ?? 0;            $model->intention_product = $data['intention_product'] ?? 0;            $model->progress_stage = $data['progress_stage'] ?? 0;            $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';            $model->address2 = $data['address2'] ?? '';            $model->mark = $data['mark'] ?? '';            $model->importance = $data['importance'] ?? '';            $model->company = $data['company'] ?? '';//            $model->company_short_name = $data['company_short_name'] ?? '';            $model->state_type = $data['state_type'] ?? 0;            $model->customer_state = $data['customer_state'] ?? 0;            $model->enter_time = $data['enter_time'] ?? 0;            $model->save();            $time = time();            $old = CustomerInfo::where('del_time',0)                ->where('customer_id',$data['id'])                ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])                ->select('file')                ->get()->toArray();            $old = array_column($old,'file');            CustomerInfo::where('del_time',0)                ->where('customer_id',$data['id'])                ->whereNotIn('type',CustomerInfo::$no_edit)                ->update(['del_time' => $time]);            if(! empty($data['customer_contact'])){                $insert = [];                foreach ($data['customer_contact'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'contact_type' => $value['id'],                        'contact_info' => $value['info'],                        'type' => CustomerInfo::type_one,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);            }//            if(! empty($data['employee_one'])){//                $insert = [];//                foreach ($data['employee_one'] as $value){//                    $insert[] = [//                        'customer_id' => $model->id,//                        'data_id' => $value,//                        'type' => CustomerInfo::type_two,//                        'crt_time' => $time,//                    ];//                }//                CustomerInfo::insert($insert);//            }            if(! empty($data['employee_two'])){                $insert = [];                foreach ($data['employee_two'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'data_id' => $value,                        'type' => CustomerInfo::type_three,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);            }            if(! empty($data['employee_three'])){                $insert = [];                foreach ($data['employee_three'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'data_id' => $value,                        'type' => CustomerInfo::type_four,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);            }            $new = [];            if(! empty($data['img'])){                $insert = [];                foreach ($data['img'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'file' => $value['url'],                        'type' => CustomerInfo::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'];                    }                }                CustomerInfo::insert($insert);            }            if(! empty($data['file'])){                $insert = [];                foreach ($data['file'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'file' => $value['url'],                        'type' => CustomerInfo::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'];                    }                }                CustomerInfo::insert($insert);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true, ['file' => ['new' => $new, 'old' => $old]]];    }    /**     * 客户新增     * @param $data     * @param $user     * @return array     */    public function customerAdd($data,$user){        list($status,$msg) = $this->customerRule($data,$user);        if(!$status) return [$status,$msg];        try {            DB::beginTransaction();            //车型            if(empty($data['car_type']) && ! empty($data['car_type_title'])){                $model_2 = new BasicType();                $model_2->title = $data['car_type_title'];                $model_2->type = 10;                $model_2->depart_id = $data['depart_id'] ?? 0;                $model_2->top_depart_id = $data['top_depart_id'] ?? 0;                $model_2->crt_id = $user['id'];                $model_2->save();                $data['car_type'] = $model_2->id;            }            $model = new Customer();            $model->title = $data['title'];            $model->code = $data['code'] ?? "";            $model->model_type = $data['model_type'];            $model->customer_intention = $data['customer_intention'] ?? 0;            $model->customer_from = $data['customer_from'] ?? 0;            $model->customer_type = $data['customer_type'] ?? 0;            $model->car_type = $data['car_type'] ?? 0;            $model->consulting_product = $data['consulting_product'] ?? '';            $model->consulting_product_new = $data['consulting_product_new'] ?? 0;            $model->intention_product = $data['intention_product'] ?? 0;            $model->progress_stage = $data['progress_stage'] ?? 0;            $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';            $model->address2 = $data['address2'] ?? '';            $model->crt_id = $user['id'];            $model->mark = $data['mark'] ?? '';            $model->importance = $data['importance'] ?? '';            $model->company = $data['company'] ?? '';//            $model->company_short_name = $data['company_short_name'] ?? '';            $model->depart_id = $data['depart_id'] ?? 0;            $model->top_depart_id = $data['top_depart_id'] ?? 0;            $model->state_type = $data['state_type'] ?? 0;            $model->customer_state = $data['customer_state'] ?? 0;            $model->enter_time = $data['enter_time'] ?? 0;            $model->save();            $time = time();            if(! empty($data['customer_contact'])){                $insert = [];                foreach ($data['customer_contact'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'contact_type' => $value['id'],                        'contact_info' => $value['info'],                        'type' => CustomerInfo::type_one,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);            }            if(! empty($data['employee_one'])){                $insert = [];                foreach ($data['employee_one'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'data_id' => $value,                        'type' => CustomerInfo::type_two,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);//                Customer::where('id',$model->id)->update([//                    'fp_top_depart_id' => $data['fp_top_depart_id'] ?? 0,//                    'fp_time' => $time,//                ]);            }            if(! empty($data['employee_two'])){                $insert = [];                foreach ($data['employee_two'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'data_id' => $value,                        'type' => CustomerInfo::type_three,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);            }            if(! empty($data['employee_three'])){                $insert = [];                foreach ($data['employee_three'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'data_id' => $value,                        'type' => CustomerInfo::type_four,                        'crt_time' => $time,                    ];                }                CustomerInfo::insert($insert);            }            $new = [];            if(! empty($data['img'])){                $insert = [];                foreach ($data['img'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'file' => $value['url'],                        'type' => CustomerInfo::type_five,                        'name' => $value['name'],                        'crt_time' => $time,                    ];                    if(! empty($value['url'])) $new[] = $value['url'];                }                CustomerInfo::insert($insert);            }            if(! empty($data['file'])){                $insert = [];                foreach ($data['file'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'file' => $value['url'],                        'type' => CustomerInfo::type_six,                        'name' => $value['name'],                        'crt_time' => $time,                    ];                    if(! empty($value['url'])) $new[] = $value['url'];                }                CustomerInfo::insert($insert);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        $data['order_number'] = Customer::$order_number . "|" . $model->id . "|" . $data['title'];        (new OperationLogService())->setOperationList($data,$user);        return [true, ['file' => ['new' => $new]]];    }    /**     * 客户删除     * @param $data     * @return array     */    public function customerDel($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        if($data['id'] < 0) return [false, '系统数据,操作失败!'];        try {            DB::beginTransaction();            Customer::where('id',$data['id'])->update([                'del_time'=> time()            ]);            $old = CustomerInfo::where('del_time',0)                ->where('customer_id',$data['id'])                ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])                ->select('file')                ->get()->toArray();            $old = array_column($old,'file');            CustomerInfo::where('del_time',0)                ->where('customer_id',$data['id'])                ->update(['del_time' => time()]);            (new RangeService())->RangeDelete($data['id'],SeeRange::type_one);            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true, ['file' => ['old' => $old]]];    }    /**     * 客户详情     * @param $data     * @return array     */    public function customerDetail($data,$user){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        $customer = Customer::where('del_time',0)            ->where('id',$data['id'])            ->first();        if(empty($customer)) return [false,'客户不存在或已被删除'];        $customer = $customer->toArray();        //是否是总社的客户        $is_main = 0;        if($customer['top_depart_id'] == $user['head']['id']) $is_main = 1;        $customer['is_belong_to_main'] = $is_main;        $customer['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $customer['title'];        $customer['intention_product_title'] = Product::where('id',$customer['intention_product'])->value('title') ?? "";        $customer['consulting_product_new_title'] = BasicTypeAllUse::where('id',$customer['consulting_product_new'])                ->where('type', BasicTypeAllUse::type_one)                ->value('title') ?? "";        $customer['product_category'] = [];        if(! empty($customer['intention_product'])){            $pro = (new ProductService())->getProductDetail([$customer['intention_product']]);            $product_category = $pro[$customer['intention_product']]['product_category'] ?? '';            $customer['product_category'] = $product_category ? json_decode($product_category,true) : [];        }        $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['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];        $array = [            $customer['customer_intention'],            $customer['customer_from'],            $customer['customer_type'],            $customer['car_type'],            $customer['progress_stage'],            $customer['state_type'],            $customer['customer_state'],        ];        $basic_map = BasicType::whereIn('id',$array)            ->pluck('title','id')            ->toArray();        $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');        $customer = [$customer];        foreach ($customer as $key => $value){            $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';            $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';            $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';            $this->isTopDispatch($user, $customer[$key]);            $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';            $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';            $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';            $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';            $customer[$key]['depart_title'] = $depart_title ?? '';        }        $customer = $customer[0];        $customer_info = CustomerInfo::where('del_time',0)            ->where('customer_id',$customer['id'])            ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')            ->get()->toArray();        $emp_id = [];        $emp_id[] = $customer['crt_id'];        foreach ($customer_info as $value){            if(in_array($value['type'], CustomerInfo::$man)){                $emp_id[] = $value['data_id'];            }        }        $emp_map = Employee::whereIn('id',array_unique($emp_id))            ->pluck('emp_name','id')            ->toArray();        $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))            ->pluck('title','id')            ->toArray();        $fileUploadService = new FileUploadService();        foreach ($customer_info as $value){            if($value['type'] == CustomerInfo::type_one){                $tmp = [                    'id' => $value['contact_type'],                    'title' => $basic_map2[$value['contact_type']] ?? '',                    'info' => $value['contact_info']                ];                $customer['customer_contact'][] = $tmp;            }elseif ($value['type'] == CustomerInfo::type_two){                $tmp = [                    'id' => $value['data_id'],                    'name' => $emp_map[$value['data_id']] ?? '',                ];                $customer['employee_one'][] = $tmp;            }elseif ($value['type'] == CustomerInfo::type_three){                $tmp = [                    'id' => $value['data_id'],                    'name' => $emp_map[$value['data_id']] ?? '',                ];                $customer['employee_two'][] = $tmp;            }elseif ($value['type'] == CustomerInfo::type_four){                $tmp = [                    'id' => $value['data_id'],                    'name' => $emp_map[$value['data_id']] ?? '',                ];                $customer['employee_three'][] = $tmp;            }elseif ($value['type'] == CustomerInfo::type_five){                $tmp = [                    'url' => $value['file'],                    'name' => $value['name'],                    'show_url' => $fileUploadService->getFileShow($value['file']),                ];                $customer['img'][] = $tmp;            }elseif ($value['type'] == CustomerInfo::type_six){                $tmp = [                    'url' => $value['file'],                    'name' => $value['name'],                    'show_url' => $fileUploadService->getFileShow($value['file']),                ];                $customer['file'][] = $tmp;            }elseif ($value['type'] == CustomerInfo::type_nine){                $tmp = [                    'id' => $value['data_id'],                    'name' => $emp_map[$value['data_id']] ?? '',                ];                $customer['old_employee_one'][] = $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 = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);        $customer['depart'] = $return[0] ?? [];        $customer['employee'] = $return[1] ?? [];        return [true, $customer];    }    public function customerCommonSearch($data,$user, $field = []){        if(empty($field)){            $field = ['title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','consulting_product_new','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id','enter_time'];        }        $model = Customer::Clear($user,$data);        $model = $model->where('del_time',0)            ->select($field)            ->orderby('id', 'desc');        if(! empty($data['title_t'])) {            // 清理用户输入,去除前后空白并替换多个连续空格为单个空格            $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));            $id = (new RangeService())->crtContactSearch($data);            // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符            $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])                ->orWhere('consulting_product', 'LIKE', '%'.$data['title_t'].'%')                ->orWhereIn('id', $id);        }        if(! empty($data['id'])) $model->where('id', $data['id']);        if(! empty($data['customer_id'])){            $customer_id = explode(',',$data['customer_id']);            $model->whereIn('id', $customer_id);        }        if(! empty($data['my_fz']) || ! empty($data['my_xt'])){            $id = $this->getCustomerId($data,$user);            $model->whereIn('id',$id);        }        if(! empty($data['pond_state'])) {            $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司            if(empty($search_depart_id)){                $top_depart_id = $user['depart_top'][0] ?? [];                $top_depart_id = $top_depart_id['depart_id'] ?? 0;            }else{                //查询 顶级公司                $top_depart_id = $search_depart_id;            }            // 进入公海池的客户 公司下所有人可见            $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);        }        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');        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['model_type'])) $model->where('model_type',$data['model_type']);        if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');        if(! empty($data['consulting_product_new'])) $model->where('consulting_product_new',$data['consulting_product_new']);        if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');        if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);        if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);        if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);        if(! empty($data['customer_intention_title'])){            $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);            $model->whereIn('customer_intention', $id);        }        if(! empty($data['consulting_product_new_title'])) {            $id = (new RangeService())->customerBasicTypeAllUseSearch($data['customer_from_title'],[BasicTypeAllUse::type_one]);            $model->whereIn('consulting_product_new', $id);        }        if(! empty($data['customer_from_title'])){            $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);            $model->whereIn('customer_from', $id);        }        if(! empty($data['customer_type_title'])) {            $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);            $model->whereIn('customer_type', $id);        }        if(! empty($data['progress_stage_title'])){            $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);            $model->whereIn('progress_stage', $id);        }        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['enter_time'][0]) && ! empty($data['enter_time'][1])) {            $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);            $model->where('enter_time','>=',$return[0]);            $model->where('enter_time','<=',$return[1]);        }        if(! empty($data['crt_name'])){            $id = (new RangeService())->crtNameSearch($data);            $model->whereIn('crt_id',$id);        }        if(! empty($data['fz'])){            $id = (new RangeService())->customerSearch($data);            $model->whereIn('id',$id);        }        if(! empty($data['last_visit_time'])){            $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);            $model->whereIn('id',$id);        }        if(! empty($data['contact_info'])){            $customer_id = $this->contactSearch($data['contact_info']);            $model->whereIn('id', $customer_id);        }        if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);        $is_fp = -1;        if(isset($data['is_fp'])) {            if($data['is_fp']){                $model->where('fp_time', '>', 0);                $is_fp = 1;            } else{                $model->where('fp_time', 0);                $is_fp = 0;            }        }        if(! empty($data['belong_top_depart_title'])){            list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);            if($is_fp < 0){                $model->whereIn('id', $id)                    ->orWhereIn('top_depart_id', $depart_id);            }elseif ($is_fp > 0){                $model->whereIn('id', $id);            }else{                $model->where('top_depart_id', $depart_id);            }        }        if(! empty($data['smart_search'])){            $customer_id = $this->contactSearch($data['smart_search']);            $progress_stage_id = (new RangeService())->customerBasicTypeSearch($data['smart_search'],[5]);            $model->where(function (Builder $query) use ($data, $customer_id,$progress_stage_id) {                $query->where('title', 'LIKE', '%'.$data['smart_search'].'%')                    ->orWhereIn('id', $customer_id)                    ->orWhereIn('progress_stage', $progress_stage_id);            });        }        if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {            $model->where('crt_time','>=',$data['wx_crt_time'][0]);            $model->where('crt_time','<=',$data['wx_crt_time'][1]);        }        return $model;    }    public function contactSearch($contact_info){        $customer_info = CustomerInfo::where('del_time',0)            ->where("type",CustomerInfo::type_one)            ->where('contact_info','LIKE', '%'.$contact_info.'%')            ->select('customer_id')            ->get()->toArray();        return array_column($customer_info,'customer_id');    }    public function titleSearch($user, $data, $message){        $model2 = Customer::Clear($user,$data);        $customer = $model2->where('del_time',0)            ->where('title', 'LIKE', '%'. $message .'%')            ->select('id')            ->get()->toArray();        return array_column($customer,'id');    }    /**     * 客户列表     * @param $data     * @param $user     * @return array     */    public function customerWxList($data,$user){        $model = $this->customerCommonSearch($data,$user);        $list = $this->limit($model,'',$data);        $list = $this->fillData($list,$data, $user);        //微信数据        $count = $this->countData("crt_time", $data, $user);        $list['today'] = $count[0] ?? 0;        $list['yesterday'] = $count[1] ?? 0;        return [true, $list];    }    /**     * 客户列表     * @param $data     * @param $user     * @return array     */    public function customerList($data,$user){        $model = $this->customerCommonSearch($data,$user);        $list = $this->limit($model,'',$data);        $list = $this->fillData($list,$data, $user);        return [true, $list];    }    public function customer2CommonSearch($data,$user){        $model = Customer::Clear($user,$data);        $model = $model->where('del_time',0)            ->where('fp_time','>',0)            ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','consulting_product_new','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id','enter_time')            ->orderby('fp_time', 'desc');        if(! empty($data['customer_id'])){            $customer_id = explode(',',$data['customer_id']);            $model->whereIn('id', $customer_id);        }        if(! empty($data['my_fz']) || ! empty($data['my_xt'])){            $id = $this->getCustomerId($data,$user);            $model->whereIn('id',$id);        }        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['model_type'])) $model->where('model_type',$data['model_type']);        if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');        if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');        if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);        if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);        if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);        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['crt_name'])){            $id = (new RangeService())->crtNameSearch($data);            $model->whereIn('crt_id',$id);        }        if(! empty($data['fz'])){            $id = (new RangeService())->customerSearch($data);            $model->whereIn('id',$id);        }        if(! empty($data['last_visit_time'])){            $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);            $model->whereIn('id',$id);        }        if(! empty($data['contact_info'])){            $customer_info = CustomerInfo::where('del_time',0)                ->where("type",CustomerInfo::type_one)                ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')                ->select('customer_id')                ->get()->toArray();            $model->whereIn('id',array_column($customer_info,'customer_id'));        }        if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){            $return = $this->changeDateToTimeStampAboutRange($data['fp_time']);            $model->where('fp_time','>=',$return[0]);            $model->where('fp_time','<=',$return[1]);        }        if(! empty($data['smart_search'])){            $customer_id = $this->contactSearch($data['smart_search']);            $progress_stage_id = (new RangeService())->customerBasicTypeSearch($data['smart_search'],[5]);            $model->where('title', 'LIKE', '%'.$data['smart_search'].'%')                ->orWhereIn('id', $customer_id)                ->orWhereIn('progress_stage', $progress_stage_id);        }        if(! empty($data['belong_top_depart_title'])){            list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);            $model->whereIn('id', $id);        }        if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {            $model->where('fp_time','>=',$data['wx_fp_time'][0]);            $model->where('fp_time','<=',$data['wx_fp_time'][1]);        }        return $model;    }    public function customerList2($data,$user){        $model = $this->customer2CommonSearch($data, $user);        $list = $this->limit($model,'',$data);        $list = $this->fillData($list,$data,$user);        //微信数据        $count = $this->countData2("fp_time", $data, $user);        $list['today'] = $count[0] ?? 0;        $list['yesterday'] = $count[1] ?? 0;        return [true, $list];    }    /**     * 客户重复     * @param $data     * @param $user     * @return array     */    public function customerRepeatList($data,$user){        $model = $this->customerRepeatCommonSearch($data,$user);        $list = $this->limit($model,'',$data);        $list = $this->fillRepeatData($list,$data, $user);        return [true, $list];    }    public function customerRepeatCommonSearch($data,$user, $field = []){        if(empty($field)){            $field = ['title','id','model_type','customer_from','car_type','address2','crt_id','crt_time','enter_time','contact_type','contact','mark'];        }        $model = CustomerRepeat::Clear($user,$data);        $model = $model->where('del_time',0)            ->select($field)            ->orderby('id', 'desc');        if(! empty($data['title_t'])) {            // 清理用户输入,去除前后空白并替换多个连续空格为单个空格            $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));            // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符            $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])                ->orWhere('consulting_product', 'LIKE', '%'.$data['title_t'].'%')                ->orWhere('contact', 'LIKE', '%'.$data['title_t'].'%');        }        if(! empty($data['customer_id'])){            $customer_id = explode(',',$data['customer_id']);            $model->whereIn('id', $customer_id);        }        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');        if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);        if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');        if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');        if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);        if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);        if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);        if(! empty($data['customer_intention_title'])){            $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);            $model->whereIn('customer_intention', $id);        }        if(! empty($data['customer_from_title'])){            $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);            $model->whereIn('customer_from', $id);        }        if(! empty($data['customer_type_title'])) {            $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);            $model->whereIn('customer_type', $id);        }        if(! empty($data['progress_stage_title'])){            $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);            $model->whereIn('progress_stage', $id);        }        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['enter_time'][0]) && ! empty($data['enter_time'][1])) {            $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);            $model->where('enter_time','>=',$return[0]);            $model->where('enter_time','<=',$return[1]);        }        if(! empty($data['crt_name'])){            $id = (new RangeService())->crtNameSearch($data);            $model->whereIn('crt_id',$id);        }        if(! empty($data['fz'])){            $emp_id = Employee::where('del_time',0)                ->where('emp_name','LIKE', '%'.$data['fz'].'%')                ->select('id')->get()->toArray();            $emp_id = array_column($emp_id,'id');            $model->whereIn('fz_man', $emp_id);        }        if(! empty($data['contact_info'])) $model->where('contact', 'LIKE', '%'.$data['contact_info'].'%');        return $model;    }    /**     * 拼接数据     * @param $data     * @return array     */    public function fillRepeatData($data,$ergs,$user){        if(empty($data['data'])) return $data;        $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_from'),array_column($data['data'],'car_type')));        $basic_map = BasicType::whereIn('id',$array)            ->pluck('title','id')            ->toArray();        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))            ->pluck('emp_name','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['address'] = $value['address2'];            //客户来源            $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';//            $this->isTopDispatch($user, $data['data'][$key]);            $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';            $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';            $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']] ?? '';        }        return $data;    }    public function getCustomerId($data,$user){        $return = [];        if(! empty($data['my_fz'])){            $info = CustomerInfo::where('del_time',0)                ->where('data_id',$user['id'])                ->where('type',CustomerInfo::type_two)                ->select('customer_id')                ->get()->toArray();            $return = array_unique(array_column($info,'customer_id'));        }        if(! empty($data['my_xt'])){            $info = CustomerInfo::where('del_time',0)                ->where('data_id',$user['id'])                ->where('type',CustomerInfo::type_three)                ->select('customer_id')                ->get()->toArray();            $return = array_unique(array_column($info,'customer_id'));        }        return $return;    }    /**     * 客户参数规则     * @param $data     * @param $is_add     * @return array     */    public function customerRule(&$data, $user, $is_add = true){        if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];        if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];        if(empty($data['title'])) return [false,'客户名称不能为空'];        if(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];        $data['enter_time'] = $this->changeDateToDateMin($data['enter_time']);        //所属部门 以及  顶级部门        if(empty($data['depart_id'])) {            $data['depart_id'] = $this->getDepart($user);            $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;        }        if($data['model_type'] == Customer::Model_type_one){//            if(empty($data['customer_from'])) return [false,'客户来源不能为空'];//            if(empty($data['customer_type'])) return [false,'客户类别不能为空'];//            if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];//            if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];//            if(empty($data['employee_one'])) return [false,'销售SA不能为空'];        }else{//            if(empty($data['car_type'])) return [false,'车型不能为空'];//            if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];        }        list($status,$msg) = $this->limitingSendRequestBackgExpire($data['top_depart_id'] . $data['title'],3);        if(! $status) return [false, $msg];        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('del_time',0)                ->exists();            if($bool) return [false,'车型名称已存在'];        }        if($is_add){            $bool = Customer::where('del_time',0)                ->where('top_depart_id',$data['top_depart_id'])                ->where('title',$data['title'])//                ->where('model_type',$data['model_type'])                ->exists();            if(! empty($data['customer_contact'])) {                $search = [];                foreach ($data['customer_contact'] as $value){                    $search[] = $value['info'];                }                $boolean = 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',$data['top_depart_id'])                    ->whereIn('a.contact_info', $search)                    ->exists();                if($boolean) return [false,'客户联系内容已存在'];            }        }else{            if(empty($data['id'])) return [false,'ID不能为空'];            $bool = Customer::where('del_time',0)                ->where('id','<>',$data['id'])                ->where('top_depart_id',$data['top_depart_id'])                ->where('title',$data['title'])//                ->where('model_type',$data['model_type'])                ->exists();            if(! empty($data['customer_contact'])) {                $search = [];                foreach ($data['customer_contact'] as $value){                    $search[] = $value['info'];                }                $boolean = CustomerInfo::from('customer_info as a')                    ->join('customer as b','b.id','a.customer_id')                    ->where('b.id','<>',$data['id'])                    ->where('a.del_time',0)                    ->where('b.del_time',0)                    ->where('b.top_depart_id',$data['top_depart_id'])                    ->whereIn('a.contact_info', $search)                    ->exists();                if($boolean) return [false,'客户联系内容已存在'];            }        }        if($bool) return [false,'客户名称不能重复'];        return [true, ''];    }    /**     * 拼接数据     * @param $data     * @return array     */    public function fillData($data,$ergs,$user){        if(empty($data['data'])) return $data;        $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_intention'),array_column($data['data'],'customer_from'),array_column($data['data'],'customer_type'),array_column($data['data'],'car_type'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state')));        $basic_map = BasicType::whereIn('id',$array)            ->pluck('title','id')            ->toArray();        $basic_all_map = BasicTypeAllUse::where('type', BasicTypeAllUse::type_one)            ->whereIn('id',array_unique(array_column($data['data'],'consulting_product_new')))            ->pluck('title','id')            ->toArray();        $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))            ->pluck('title','id')            ->toArray();        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))            ->pluck('emp_name','id')            ->toArray();        $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))            ->pluck('title','id')            ->toArray();        $customer_id = array_column($data['data'],'id');        //跟进记录        $record_array = (new FollowUpRecordService())->getVisitDataOfTime($customer_id, FollowUpRecord::type_one);        $customer_info = CustomerInfo::where('del_time',0)            ->whereIn('customer_id',$customer_id)            ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three])            ->select('type','contact_type','contact_info','customer_id','data_id')            ->get()->toArray();        //客户的联系方式  客户的负责人  协同人        $customer_info_map = $fz = $customer_info_map2 = $xt = [];        $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))            ->pluck('emp_name','id')            ->toArray();        $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();        foreach ($customer_info as $value){            if($value['type'] == CustomerInfo::type_one){                if(! $value['contact_info']) continue;                $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";                $customer_info_map[$value['customer_id']][] = $value;//                if(! isset($customer_info_map2[$value['customer_id']])){//                    $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];//                }else{//                    $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];//                }                if(! isset($customer_info_map2[$value['customer_id']])){                    $customer_info_map2[$value['customer_id']] = $value['contact_info'];                }else{                    $customer_info_map2[$value['customer_id']] .= '|' . $value['contact_info'];                }            }elseif($value['type'] == CustomerInfo::type_two){                $tmp = $emp_map[$value['data_id']] ?? "";                if(isset($fz[$value['customer_id']])){                    $fz[$value['customer_id']] .= ',' . $tmp;                }else{                    $fz[$value['customer_id']] = $tmp;                }            }else{                $tmp = $emp_map[$value['data_id']] ?? "";                if(isset($xt[$value['customer_id']])){                    $xt[$value['customer_id']] .= ',' . $tmp;                }else{                    $xt[$value['customer_id']] = $tmp;                }            }        }        $array2 = array_unique(array_column($data['data'],'top_depart_id'));        $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();        $depart2_map = $this->getCustomerDepart($customer_id);        //地址        $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]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';            //客户来源            $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';            $this->isTopDispatch($user, $data['data'][$key]);            $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';            $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';            $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';            $data['data'][$key]['consulting_product_new_title'] = $basic_all_map[$value['consulting_product_new']] ?? '';            $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';            $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';            $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';            $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';            $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';            $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';            $customer_tmp = $customer_info_map[$value['id']] ?? [];            $data['data'][$key]['customer_detail'] = $customer_tmp;            $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";            $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];            $record_tmp = $record_array[$value['id']] ?? "";            $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";            $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";            $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];            $top_depart_title = $depart[$value['top_depart_id']] ?? "";            $data['data'][$key]['top_depart_title'] = $top_depart_title;            //分配门店            $fp_top_depart_title = $depart2_map[$value['id']] ?? "";            if($value['fp_time'] > 0) {                //分配过所属门店是分配门店                $data['data'][$key]['belong_top_depart_title'] = $fp_top_depart_title;            }else{                //没分配过所属门店是创建门店                $data['data'][$key]['belong_top_depart_title'] = $top_depart_title;            }            $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";            $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";        }        return $data;    }    private function isTopDispatch($user, &$value){        //特殊功能按钮        $special_button = $user['special_button'] ?? [];        //总社        $head = $user['head']['id'] ?? 0;        //当前门店        $top_depart_id = $user['depart_top'][0] ?? [];        $top_depart_id = $top_depart_id['depart_id'] ?? 0;        //分社账号        if($top_depart_id != $head){            //总社创建分配给分社的客户            if($value['top_depart_id'] == $head && $value['fp_time'] > 0 && $value['fp_top_depart_id'] != $head){                if(! in_array(RoleMenuButton::special_nine,$special_button)) $value['customer_from_title'] = "";            }        }    }    public function countData($column = "", $data, $user){        if(empty($column) || empty($data['from_wx'])) return [0, 0];        // 获取今天的开始和结束时间戳        $todayStart = Carbon::today()->startOfDay()->timestamp;        $todayEnd = Carbon::today()->endOfDay()->timestamp;        // 获取昨天的开始和结束时间戳        $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;        $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;        $data['wx_' . $column] = [$todayStart, $todayEnd];        $model = $this->customerCommonSearch($data, $user);        // 查询今天的数据条数        $todayCount = $model->count();        $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];        $model = $this->customerCommonSearch($data, $user);        // 查询昨天的数据条数        $yesterdayCount = $model->count();        return [$todayCount, $yesterdayCount];    }    public function countData2($column = "", $data, $user){        if(empty($column) || empty($data['from_wx'])) return [0, 0];        // 获取今天的开始和结束时间戳        $todayStart = Carbon::today()->startOfDay()->timestamp;        $todayEnd = Carbon::today()->endOfDay()->timestamp;        // 获取昨天的开始和结束时间戳        $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;        $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;        $data['wx_' . $column] = [$todayStart, $todayEnd];        $model = $this->customer2CommonSearch($data, $user);        // 查询今天的数据条数        $todayCount = $model->count();        $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];        $model = $this->customer2CommonSearch($data, $user);        // 查询昨天的数据条数        $yesterdayCount = $model->count();        return [$todayCount, $yesterdayCount];    }    //获取客资门店    public function getCustomerDepart($customer_id = []){        if(empty($customer_id)) return [];        $result = SeeRange::where('del_time',0)            ->whereIn('data_id', $customer_id)            ->where('data_type',SeeRange::type_one)            ->where('type',SeeRange::data_three)            ->select('data_id as customer_id','param_id as top_depart_id')            ->get()->toArray();        $depart_map = Depart::whereIn('id',array_unique(array_column($result,'top_depart_id')))->pluck('title','id')->toArray();        $return = [];        foreach ($result as $value){            $tmp = $depart_map[$value['top_depart_id']] ?? "";            if(! $tmp) continue;            if(isset($return[$value['customer_id']])){                $return[$value['customer_id']] .= ',' . $tmp;            }else{                $return[$value['customer_id']] = $tmp;            }        }        return $return;    }    //客资门店搜索    public function searchCustomerDepart($depart_title = ""){        $customer_id = [];        if(empty($depart_title)) return $customer_id;        $depart_id = Depart::where('del_time', 0)            ->where('parent_id', 0)            ->where('title', 'LIKE', '%'. $depart_title .'%')            ->select('id')->get()->toArray();        $depart_id = array_column($depart_id, 'id');        if(empty($depart_id)) return [$customer_id, $depart_id];        $customer_id = SeeRange::where('del_time',0)            ->whereIn('param_id', $depart_id)            ->where('data_type',SeeRange::type_one)            ->where('type',SeeRange::data_three)            ->select('data_id as customer_id')            ->get()->toArray();        $customer_id = array_column($customer_id, 'customer_id');        return [$customer_id, $depart_id];    }    //抢客户    public function customerGrabbing($data, $user){        $key = Customer::$limitKey . $data['customer_id'];        list($status,$msg) = $this->customerGrabbingRule($data,$key);        if(! $status) {            //释放锁            $this->dellimitingSendRequestBackg($key);            return [false,$msg];        }        try {            DB::beginTransaction();            $time = time();            $insert = [];            //负责人获取 改为前负责人            $man = CustomerInfo::where('del_time',0)                ->where('customer_id',$data['customer_id'])                ->where('type', CustomerInfo::type_two)                ->get()->toArray();            foreach ($man as $value){                $insert[] = [                    'customer_id' => $data['customer_id'],                    'data_id' => $value['data_id'],                    'type' => CustomerInfo::type_nine,                    'crt_time' => $time                ];            }            //增加自己为负责人            $insert[] = [                'customer_id' => $data['customer_id'],                'data_id' => $user['id'],                'type' => CustomerInfo::type_two,                'crt_time' => $time            ];            //人员清空            CustomerInfo::where('del_time',0)                ->where('customer_id',$data['customer_id'])                ->whereIn('type', CustomerInfo::$man2)                ->update(['del_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,''];    }    public function searchBy($data,$user){        $model = BasicType::TopClear($user,$data);        $result = $model->where('del_time',0)            ->where('type', 2)            ->where('title', 'LIKE', '%'.$data['customer_from'].'%')            ->select('id')            ->get()->toArray();        $model2 = Customer::Clear($user,$data);        $customer = $model2->where('del_time',0)            ->whereIn('customer_from', array_column($result,'id'))            ->select('id')            ->get()->toArray();        return array_column($customer,'id');    }    public function customerImportanceEdit($data,$user){        if(empty($data['id'])) return [false, '请选择客户'];        $model = Customer::where('id',$data['id'])->first();        if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];        if(! isset($data['importance'])) return [false, '客户重要程度不存在'];        $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;        $params = $this->getDataFile($data);        (new OperationLogService())->setOperationList($params,$user,2);        try {            DB::beginTransaction();            $model = Customer::where('id',$data['id'])->first();            $model->importance = $data['importance'] ?? '';            $model->save();            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true, ''];    }    //发送客户消息    public function customerSendWx($data, $user){        list($status,$msg) = $this->customerSendWxRule($data);        if(! $status) return [false,$msg];        //发送消息        $send_data = $msg;        (new OaService())->sendWxOaCheckMessage($send_data);        return [true, ''];    }    public function customerSendWxRule($data){        if(empty($data['id'])) return [false,'请选择客户'];        $customer = Customer::where('del_time',0)            ->whereIn('id',$data['id'])            ->get()->toArray();        if(empty($customer)) return [false,'客户不存在或已被删除'];        $man = CustomerInfo::where('del_time',0)            ->whereIn('customer_id',$data['id'])            ->where('type', CustomerInfo::type_two)            ->get()->toArray();        if(empty($man)) return [false, '客户暂无负责人信息,公众号消息发送结束'];        $customer_map = [];        foreach ($man as $value){            $customer_map[$value['data_id']][] = $value['customer_id'];        }        $employee_id = array_unique(array_column($man,'data_id'));        $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)            ->pluck('openid','employee_id')            ->toArray();        $emp = Employee::whereIn('id', $employee_id)            ->pluck('emp_name','id')            ->toArray();        $send_data = [];        foreach ($employee_id as $value){            $tmp = $emp[$value] ?? "";            $open_id = $wx_map[$value] ?? "";//            if(empty($open_id)) return [false, $tmp . "暂未关注微信公众号,公众号消息发送失败"];            if(empty($open_id)) continue;            $customer_id = $customer_map[$value] ?? [];            $customer_id = implode(',', $customer_id);            //提醒创建人            $send_data[] = [                'employee_id' => $value,                'type' => 2,                'state' => 0,                'menu_id' => "16|list",                'openid' => $open_id,                'order_number' => $customer_id,                'tmp_data' => [                    time(),                    "客户消息提醒",                    "已发送",                    $tmp,                    date('Y-m-d H:i:s'),                ],            ];        }        return [true, $send_data];    }}
 |