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