From 0aee3da7de4666a5ed2322447ac801ef3b8f8943 Mon Sep 17 00:00:00 2001 From: liutong <836164388@qq.com> Date: Wed, 21 May 2025 18:01:43 +0800 Subject: [PATCH] =?UTF-8?q?(customerfeat-resources):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E8=B5=84=E6=BA=90=E4=BF=AE=E6=94=B9=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 getEditLogList 接口,用于获取客户资源和六要素的修改记录列表 - 实现 getCustomerResourceChangesEditLog 和 getSixSpeedModificationEditLog 方法,分别用于获取客户资源和六要素的修改记录 - 在模型中添加字段中文映射常量,用于展示修改记录时的字段名称 -优化修改记录的数据处理,将字典值和人员信息转换为中文名称 --- .../apiController/CustomerResources.php | 28 ++ niucloud/app/api/route/route.php | 3 + .../customer_resources/CustomerResources.php | 27 ++ niucloud/app/model/six_speed/SixSpeed.php | 21 +- .../apiService/CustomerResourcesService.php | 258 ++++++++++++++++++ 5 files changed, 336 insertions(+), 1 deletion(-) diff --git a/niucloud/app/api/controller/apiController/CustomerResources.php b/niucloud/app/api/controller/apiController/CustomerResources.php index 29f2cf13..7cfb3f3c 100644 --- a/niucloud/app/api/controller/apiController/CustomerResources.php +++ b/niucloud/app/api/controller/apiController/CustomerResources.php @@ -152,5 +152,33 @@ class CustomerResources extends BaseApiService return success([]); } + //客户资源-修改记录列表 + public function getEditLogList(Request $request){ + //前端要传递分页(page,limit)参数 + $customer_resource_id = $request->param('customer_resource_id',''); + $type = $request->param('type','resource');//查询类型|resource=客户资源,six_speed=六要素 + + if(empty($customer_resource_id) || empty($type)){ + return fail('缺少必要参数'); + } + + if(!in_array($type,['resource','six_speed'])){ + return fail('类型不正确'); + } + + $where = [ + 'customer_resource_id' =>$customer_resource_id + ]; + + if($type == 'resource'){ + //resource=客户资源 + $res =(new CustomerResourcesService())->getCustomerResourceChangesEditLog($where); + }else{ + $res =(new CustomerResourcesService())->getSixSpeedModificationEditLog($where); + } + + return success($res); + } + } diff --git a/niucloud/app/api/route/route.php b/niucloud/app/api/route/route.php index 9e03f657..1e0e64e6 100644 --- a/niucloud/app/api/route/route.php +++ b/niucloud/app/api/route/route.php @@ -208,6 +208,9 @@ Route::group(function () { Route::post('customerResources/add', 'apiController.CustomerResources/add'); //客户资源-编辑 Route::post('customerResources/edit', 'apiController.CustomerResources/edit'); + //客户资源-修改记录列表 + Route::get('customerResources/getEditLogList', 'apiController.CustomerResources/getEditLogList'); + //资源共享-列表 Route::get('resourceSharing/index', 'apiController.ResourceSharing/index'); diff --git a/niucloud/app/model/customer_resources/CustomerResources.php b/niucloud/app/model/customer_resources/CustomerResources.php index b3c0aae6..bfcc6515 100644 --- a/niucloud/app/model/customer_resources/CustomerResources.php +++ b/niucloud/app/model/customer_resources/CustomerResources.php @@ -57,6 +57,33 @@ class CustomerResources extends BaseModel */ protected $defaultSoftDelete = 0; + //定义一个常量数组-字段中文映射 + const FieldZh = [ + 'id' => '编号', + 'create_year_month' => '创建年月', + 'create_date' => '创建日期', + 'source_channel' => '来源渠道', + 'source' => '来源', + 'consultant' => '顾问', + 'name' => '姓名', + 'age' => '年龄', + 'gender' => '性别', + 'phone_number' => '联系电话', + 'demand' => '需求', + 'purchasing_power' => '购买力', + 'cognitive_idea' => '认知理念', + 'optional_class_time' => '可选上课时间', + 'distance' => '距离', + 'decision_maker' => '决策人', + 'initial_intent' => '客户初步意向度', + 'campus' => '所属校区', + 'trial_class_count' => '体验课次数', + 'created_at' => '创建时间', + 'updated_at' => '更新时间', + 'deleted_at' => '逻辑删除时间', + 'status' => '客户状态', + ]; + /** * 搜索器:客户资源姓名 * @param $value diff --git a/niucloud/app/model/six_speed/SixSpeed.php b/niucloud/app/model/six_speed/SixSpeed.php index b7fde1a2..0911fb2c 100644 --- a/niucloud/app/model/six_speed/SixSpeed.php +++ b/niucloud/app/model/six_speed/SixSpeed.php @@ -57,7 +57,26 @@ class SixSpeed extends BaseModel - + //定义一个常量数组-字段中文映射 + const FieldZh = [ + 'id' => '编号', + 'purchase_power' => '需求购买力', + 'concept_awareness' => '认知理念', + 'preferred_class_time' => '可选上课时间', + 'distance' => '距离', + 'communication' => '沟通备注', + 'promised_visit_time' => '承诺到访时间', + 'actual_visit_time' => '实际到访时间', + 'call_intent' => '电话后的意向程度', + 'first_visit_status' => '一访情况', + 'second_visit_status' => '二访情况', + 'is_closed' => '是否关单', + 'staff_id' => '人员ID', + 'resource_id' => '资源ID', + 'created_at' => '创建时间', + 'updated_at' => '更新时间', + 'deleted_at' => '逻辑删除时间' + ]; public function personnel(){ diff --git a/niucloud/app/service/api/apiService/CustomerResourcesService.php b/niucloud/app/service/api/apiService/CustomerResourcesService.php index 1cc0bb86..90e32d42 100644 --- a/niucloud/app/service/api/apiService/CustomerResourcesService.php +++ b/niucloud/app/service/api/apiService/CustomerResourcesService.php @@ -14,6 +14,7 @@ namespace app\service\api\apiService; use app\model\campus_person_role\CampusPersonRole; use app\model\customer_resource_changes\CustomerResourceChanges; use app\model\customer_resources\CustomerResources; +use app\model\dict\Dict; use app\model\personnel\Personnel; use app\model\resource_sharing\ResourceSharing; use app\model\six_speed\SixSpeed; @@ -205,6 +206,263 @@ class CustomerResourcesService extends BaseApiService } } + //客户资源-获取客户资源修改记录列表 + public function getCustomerResourceChangesEditLog(array $where) + { + $page_params = $this->getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + + //客户资源修改记录表 + $data = CustomerResourceChanges::where('customer_resource_id', $where['customer_resource_id']) + ->order('created_at', 'desc') + ->with([ + 'personnel' => function ($query) { + } + ]) + ->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ]) + ->toArray(); + + + $fieldZhArr = CustomerResources::FieldZh;//字段中文映射常量 + //需要获取字段与字典key的映射关系数组 + $fieldDictArr = [ + 'source_channel'=>'SourceChannel',//field=>dict_key(来源渠道) + 'source'=>'source',//field=>dict_key(来源) + 'gender'=>'zy_sex',//field=>dict_key(性别) + 'purchasing_power'=>'customer_purchasing_power',//field=>dict_key(购买力) + 'cognitive_idea'=>'cognitive_concept',//field=>dict_key(认知理念) + 'initial_intent'=>'preliminarycustomerintention',//field=>dict_key(客户初步意向度) + 'status'=>'kh_status',//field=>dict_key(客户状态) + ]; + $dict_arr = $this->getFieldDictionary($fieldDictArr);//字段与字典值列表的映射关系 + + $consultant_id_arr = []; + foreach ($data['data'] as $v) { + $modified_fields_arr = json_decode($v['modified_fields'], true); + $old_values_arr = json_decode($v['old_values'], true); + $new_values_arr = json_decode($v['new_values'], true); + + //判断有没有修改过"顾问"字段 + if(in_array('consultant',$modified_fields_arr)){ + $consultant_id_arr[] = $old_values_arr['consultant']; + $consultant_id_arr[] = $new_values_arr['consultant']; + } + } + $consultant_id_arr = array_unique($consultant_id_arr);//去重 + $dict_arr['consultant'] = [];//获取员工信息列表 + if($consultant_id_arr){ + $personnel = Personnel::whereIn('id',$consultant_id_arr)->field('id,name')->select()->toArray(); + foreach($personnel as $v){ + $dict_arr['consultant'][] = [ + "name" => $v['name'], + "value" => $v['id'], + "sort" => 0, + "memo" => "", + ]; + } + } + + + $fieldDictKeyArr = array_keys($fieldDictArr); + $list = []; + foreach ($data['data'] as $v) { + $modified_fields_arr = json_decode($v['modified_fields'], true); + $old_values_arr = json_decode($v['old_values'], true); + $new_values_arr = json_decode($v['new_values'], true); + + + $update_arr = []; + foreach ($modified_fields_arr as $m_v) { + $old_value = $old_values_arr[$m_v] ?? ''; + $new_value = $new_values_arr[$m_v] ?? ''; + + if(in_array($m_v,$fieldDictKeyArr)){ + $dict = $dict_arr[$m_v]; + foreach($dict as $d_v){ + if($d_v['value'] == $old_value){ + $old_value = $d_v['name']; + } + if($d_v['value'] == $new_value){ + $new_value = $d_v['name']; + } + } + } + + + //一些字典和外键相关的字段值处理 + $update_arr[] = [ + 'field_name_en' => $m_v, + 'field_name_zh' => $fieldZhArr[$m_v] ?? $m_v, + 'old_value' => $old_value, + 'new_value' => $new_value, + ]; + } + + $list[] = [ + 'id' => $v['id'],//日志id + 'modification_time' => $v['modification_time'],//修改时间 + 'staff_id_name' => $v['staff_id_name'],//修改人的名字 + 'update_arr' => $update_arr,//数据变更数组 + ]; + } + + + $data['data'] = $list; + return $data; + } + + //客户资源-获取六要素修改记录列表 + public function getSixSpeedModificationEditLog(array $where) + { + $page_params = $this->getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + + //六要素修改记录表 + $data = SixSpeedModificationLog::where('operator_id', $where['customer_resource_id']) + ->order('created_at', 'desc') + ->with([ + 'personnel' => function ($query) { + } + ]) + ->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ]) + ->toArray(); + + + $fieldZhArr = SixSpeed::FieldZh;//字段中文映射常量 + //需要获取字段与字典key的映射关系数组 + $fieldDictArr = [ + 'purchase_power'=>'customer_purchasing_power',//field=>dict_key(需求购买力) + 'concept_awareness'=>'cognitive_concept',//field=>dict_key(认知理念) + 'call_intent'=>'preliminarycustomerintention',//field=>dict_key(电话后的意向程度) + 'is_closed'=>'global_true_or_false',//field=>dict_key(是否关单: 1-是, 0-否) + ]; + $dict_arr = $this->getFieldDictionary($fieldDictArr);//字段与字典值列表的映射关系 + + $consultant_id_arr = [];//顾问(人员)id数组 + foreach ($data['data'] as $v) { + $modified_fields_arr = json_decode($v['modified_field'], true); + $old_values_arr = json_decode($v['old_value'], true); + $new_values_arr = json_decode($v['new_value'], true); + + //判断有没有修改过"顾问(人员)"字段 + if(in_array('staff_id',$modified_fields_arr)){ + $consultant_id_arr[] = $old_values_arr['staff_id']; + $consultant_id_arr[] = $new_values_arr['staff_id']; + } + } + $consultant_id_arr = array_unique($consultant_id_arr);//去重 + $dict_arr['consultant'] = [];//获取员工信息列表 + if($consultant_id_arr){ + $personnel = Personnel::whereIn('id',$consultant_id_arr)->field('id,name')->select()->toArray(); + foreach($personnel as $v){ + $dict_arr['consultant'][] = [ + "name" => $v['name'], + "value" => $v['id'], + "sort" => 0, + "memo" => "", + ]; + } + } + + + $fieldDictKeyArr = array_keys($fieldDictArr); + $list = []; + foreach ($data['data'] as $v) { + $modified_fields_arr = json_decode($v['modified_field'], true); + $old_values_arr = json_decode($v['old_value'], true); + $new_values_arr = json_decode($v['new_value'], true); + + + $update_arr = []; + foreach ($modified_fields_arr as $m_v) { + $old_value = $old_values_arr[$m_v] ?? ''; + $new_value = $new_values_arr[$m_v] ?? ''; + + if(in_array($m_v,$fieldDictKeyArr)){ + $dict = $dict_arr[$m_v]; + foreach($dict as $d_v){ + if($d_v['value'] == $old_value){ + $old_value = $d_v['name']; + } + if($d_v['value'] == $new_value){ + $new_value = $d_v['name']; + } + } + } + + + //一些字典和外键相关的字段值处理 + $update_arr[] = [ + 'field_name_en' => $m_v, + 'field_name_zh' => $fieldZhArr[$m_v] ?? $m_v, + 'old_value' => $old_value, + 'new_value' => $new_value, + ]; + } + + $list[] = [ + 'id' => $v['id'],//日志id + 'modification_time' => $v['updated_at'],//修改时间 + 'staff_id_name' => $v['staff_id_name'],//修改人的名字 + 'update_arr' => $update_arr,//数据变更数组 + ]; + } + + + $data['data'] = $list; + return $data; + } + + + /** + * 根据字段和字典键映射关系,获取字段对应字典项 + * + * @param array $fieldDictMapping 字段 => 字典 key 映射数组 + * @return array 字段 => 字典列表 + */ + public function getFieldDictionary(array $fieldDictMapping): array + { + // 获取所有需要的字典 key 列表 + $dictKeys = array_values($fieldDictMapping); + + if (empty($dictKeys)) { + return []; + } + + // 查询字典数据 + $dictData = Dict::whereIn('key', $dictKeys)->select()->toArray(); + + //使用 array_column 构建 key => dict 的映射 + $dictMap = array_column($dictData, null, 'key'); // 以 key 字段为索引 + + + // 构建字段 => 字典项的映射 + $result = []; + foreach ($fieldDictMapping as $field => $key) { + $result[$field] = $dictMap[$key]['dictionary'] ?? []; + //判断是不是获取的"来源渠道"的字典 + if($field == 'source_channel'){ + $append_arr = [ + "name" => "线下", + "value" => "0", + "sort" => 0, + "memo" => "", + ]; + //插入到数组头部 + array_unshift($result[$field],$append_arr); + } + } + + return $result; + } }