| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 | <?phpnamespace App\Service;use App\Model\BasicType;use App\Model\Customer;use App\Model\CustomerInfo;use App\Model\Depart;use App\Model\Employee;use App\Model\FollowUpRecord;use App\Model\Product;use App\Model\SeeRange;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];        $params = $this->getDataFile($data);        (new OperationLogService())->setOperationList($params,$user,2);        try {            DB::beginTransaction();            $model = Customer::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 = 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'])                ->where('type','<>',CustomerInfo::type_two)                ->update(['del_time' => $time]);            if(! empty($data['customer_contact'])){                $insert = [];                foreach ($data['customer_contact'] as $value){                    $insert[] = [                        'customer_id' => $model->id,                        'contact_type' => $value['type'],                        'contact_info' => $value['info'],                        'type' => CustomerInfo::type_one,                        '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();            $model = new Customer();            $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'] ?? 0;            $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[] = [                        'customer_id' => $model->id,                        'contact_type' => $value['type'],                        'contact_info' => $value['info'],                        'type' => CustomerInfo::type_one,                        '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()];        }        (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()]);            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();        //地址        $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 = 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'];        $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'] == CustomerInfo::type_one){                $tmp = [                    'id' => $value['contact_type'],                    'type' => $value['contact_type'],                    'type_title' => CustomerInfo::$contact_type[$value['contact_type']] ?? "",                    'info' => $value['contact_info']                ];                $customer['customer_contact'][] = $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;            }        }        $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];    }    /**     * 客户列表     * @param $data     * @param $user     * @return array     */    public function customerList($data,$user){        $model = Customer::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(isset($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 = 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'));        }        $list = $this->limit($model,'',$data);        $list = $this->fillData($list,$data);        return [true, $list];    }    /**     * 客户参数规则     * @param $data     * @param $is_add     * @return array     */    public function customerRule(&$data, $user, $is_add = true){        if(empty($data['title'])) return [false,'客户名称不能为空'];        if($is_add){            $data['order_number'] = (new OrderNoService())->createOrderNumber(Customer::$prefix);            $bool = Customer::where('del_time',0)                ->where('title',$data['title'])                ->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)                    ->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('title',$data['title'])                ->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)                    ->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){        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_one);        $customer_info = CustomerInfo::where('del_time',0)            ->whereIn('customer_id',array_column($data['data'],'id'))            ->whereIn('type',[CustomerInfo::type_one])            ->select('contact_type as type','contact_info as info','customer_id','data_id')            ->get()->toArray();        //客户的联系方式        $customer_info_map = [];        foreach ($customer_info as $value){            $value['type_title'] = CustomerInfo::$contact_type[$value['type']] ?? "";            $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['is_company'] ? '是' : '否';            $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;    }}
 |