Browse Source

feat(customerResources): 添加客户资源编辑功能

- 新增客户资源编辑接口和相关逻辑
- 实现客户资源数据和六要素数据的更新
- 添加数据变更日志记录功能
- 优化数据验证和错误处理
master
liutong 10 months ago
parent
commit
a956a43db3
  1. 75
      niucloud/app/api/controller/apiController/CustomerResources.php
  2. 2
      niucloud/app/api/route/route.php
  3. 56
      niucloud/app/model/six_speed/SixSpeed.php
  4. 37
      niucloud/app/service/api/apiService/CommonService.php
  5. 122
      niucloud/app/service/api/apiService/CustomerResourcesService.php
  6. 7
      niucloud/app/service/api/apiService/ResourceSharingService.php

75
niucloud/app/api/controller/apiController/CustomerResources.php

@ -77,5 +77,80 @@ class CustomerResources extends BaseApiService
return success([]);
}
//客户资源-编辑
public function edit(Request $request){
$resource_sharing_id = $request->param('resource_sharing_id', '');//资源共享id
$customer_resources_id = $request->param('id', '');//客户资源表id
$promised_visit_time = $request->param('promised_visit_time', '');
if($promised_visit_time){
$promised_visit_time = date('Y-m-d H:i:s',strtotime($promised_visit_time));
}
$optional_class_time = $request->param('optional_class_time', '');
if($optional_class_time){
$optional_class_time = date('Y-m-d H:i:s',strtotime($optional_class_time));
}
if(empty($customer_resources_id)){
return fail("缺少客户id");
}
$where=[
'id'=>$customer_resources_id
];
//客户资源数据
$customer_resources_data = [
"source_channel" => $request->param('source_channel', ''),//来源渠道
"source" => $request->param('source', ''),//来源
"name" => $request->param('name', ''),//姓名
"age" => $request->param('age', ''),//年龄
"gender" => $request->param('gender', ''),//性别
"phone_number" => $request->param('phone_number', ''),//联系电话
"demand" => $request->param('demand', ''),//需求
"decision_maker" => $request->param('decision_maker', ''),//决策人
"initial_intent" => $request->param('initial_intent', ''),//客户初步意向度
"status" => $request->param('status', ''),//客户状态
"purchasing_power" => $request->param('purchasing_power', ''),//购买力
"cognitive_idea" => $request->param('cognitive_idea', ''),//认知理念
"optional_class_time" => $optional_class_time,//可选上课时间
"distance" => $request->param('distance', ''),//距离
];
//六要素相关数据
$six_speed_data = [
"purchase_power" => $request->param('purchasing_power', ''),//需求购买力
"concept_awareness" => $request->param('cognitive_idea', ''),//认知理念
"promised_visit_time" => $promised_visit_time,//承诺到访时间
"preferred_class_time" => $optional_class_time,//可选上课时间
"distance" => $request->param('distance', ''),//距离
"communication" => $request->param('communication', ''),//沟通备注
"staff_id" => $request->param('staff_id', ''),//人员ID
];
foreach($customer_resources_data as $k=>$v){
if(!isset($v) || $v === ''){
return fail("缺少必填项{$k}");
}
}
foreach($six_speed_data as $k=>$v){
if(!isset($v) || $v === ''){
return fail("缺少必填项{$k}");
}
}
$res = (new CustomerResourcesService())->editData($where,$customer_resources_data,$six_speed_data);
if(!$res['code']){
return fail($res['msg']);
}
return success([]);
}
}

2
niucloud/app/api/route/route.php

@ -206,6 +206,8 @@ Route::group(function () {
Route::get('personnel/getPersonnelAll', 'apiController.Personnel/getPersonnelAll');
//客户资源-添加
Route::post('customerResources/add', 'apiController.CustomerResources/add');
//客户资源-编辑
Route::post('customerResources/edit', 'apiController.CustomerResources/edit');
//资源共享-列表
Route::get('resourceSharing/index', 'apiController.ResourceSharing/index');

56
niucloud/app/model/six_speed/SixSpeed.php

@ -11,6 +11,7 @@
namespace app\model\six_speed;
use app\model\dict\Dict;
use core\base\BaseModel;
use think\model\concern\SoftDelete;
use think\model\relation\HasMany;
@ -67,4 +68,59 @@ class SixSpeed extends BaseModel
return $this->hasOne(CustomerResources::class, 'id', 'resource_id')->joinType('left')->withField('name,id')->bind(['resource_id_name'=>'name']);
}
/**
* 获取需求购买力类型名称
* @param $value
* @param $data
* @return array|mixed|string
*/
public function getPurchasePowerNameAttr($value, $data)
{
$key = 'customer_purchasing_power';
$val = (String)$data['purchase_power'];
if ((!empty($val) || isset($val)) && $val !== '') {
$dict = Dict::where('key',$key)->find();
$dictionary = $dict['dictionary'] ?? [];
// 查找匹配的 name
$res = '';
foreach ($dictionary as $item) {
if ($item['value'] == $val) {
$res = $item['name'];
break;
}
}
return $res;
} else {
return '';
}
}
/**
* 获取认知理念类型名称
* @param $value
* @param $data
* @return array|mixed|string
*/
public function getConceptAwarenessNameAttr($value, $data)
{
$key = 'customer_purchasing_power';
$val = (String)$data['concept_awareness'];
if ((!empty($val) || isset($val)) && $val !== '') {
$dict = Dict::where('key',$key)->find();
$dictionary = $dict['dictionary'] ?? [];
// 查找匹配的 name
$res = '';
foreach ($dictionary as $item) {
if ($item['value'] == $val) {
$res = $item['name'];
break;
}
}
return $res;
} else {
return '';
}
}
}

37
niucloud/app/service/api/apiService/CommonService.php

@ -43,4 +43,41 @@ class CommonService extends BaseApiService
}
/**
* 对比新旧数据改变
* @param array $old_data 旧数据
* @param array $new_data 新数据
* @param array $ignoreFields 忽略验证的字段|默认[]
* @return array
*/
public function compareData(array $old_data, array $new_data, array $ignoreFields = ['updated_at'])
{
$changedFields = [];//改了那些字段
$oldChanges = [];//数据修改前的样子
$newChanges = [];//数据修改后的样子
foreach ($new_data as $key => $value) {
// 如果字段在忽略列表中,则跳过
if (in_array($key, $ignoreFields)) {
continue;
}
if (!isset($old_data[$key]) || $old_data[$key] != $value) {
$changedFields[] = $key;
$oldChanges[$key] = $old_data[$key] ?? null;
$newChanges[$key] = $value;
}
}
return [
'changed_fields' => $changedFields,
'old_values' => $oldChanges,
'new_values' => $newChanges,
'changed_fields_json' => json_encode($changedFields, JSON_UNESCAPED_UNICODE),
'old_values_json' => json_encode($oldChanges, JSON_UNESCAPED_UNICODE),
'new_values_json' => json_encode($newChanges, JSON_UNESCAPED_UNICODE)
];
}
}

122
niucloud/app/service/api/apiService/CustomerResourcesService.php

@ -11,10 +11,13 @@
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\personnel\Personnel;
use app\model\resource_sharing\ResourceSharing;
use app\model\six_speed\SixSpeed;
use app\model\six_speed_modification_log\SixSpeedModificationLog;
use core\base\BaseApiService;
use think\facade\Db;
@ -85,4 +88,123 @@ class CustomerResourcesService extends BaseApiService
}
}
//客户资源-编辑
public function editData(array $where, array $customer_resources_data, array $six_speed_data)
{
$operator_id = $this->member_id;//当前登录用户的id(日志操作人的id)
$campus_id = 0;//日志操作人校区的id
$campus_id_arr = CampusPersonRole::where('person_id', $operator_id)->column('campus_id');
if (count($campus_id_arr)) {
if (count($campus_id_arr) > 1) {
$campus_id = 0;
} else {
$campus_id = $campus_id_arr[0];
}
}
$res = [
'code' => 0,
'msg' => '操作失败',
'data' => []
];
if (!$where) {
$res['msg'] = '查询条件不能为空';
return $res;
}
$date = date('Y-m-d H:i:s');
$customer_resources_data['updated_at'] = $date;
$six_speed_data['updated_at'] = $date;
//开启事物
Db::startTrans();
try {
$customer_resources = CustomerResources::where('id', $where['id'])->find();
if ($customer_resources) {
$customer_resources = $customer_resources->toArray();
}
$update_1 = CustomerResources::where('id', $where['id'])->update($customer_resources_data);//客户资源表
if (!$update_1) {
Db::rollback();
return $res;
}
//更近客户资源日志表
$compareData = (new CommonService)->compareData($customer_resources, $customer_resources_data);
if ($compareData['changed_fields']) {
$data = [
"customer_resource_id" => $where['id'],//客户资源的ID
"operator_id" => $operator_id,//操作人的ID
"campus_id" => $campus_id,//操作人校区的ID|如果这人有2校区就填0
"modified_fields" => $compareData['changed_fields_json'],//修改的哪些字段
"old_values" => $compareData['old_values_json'],//修改前的值
"new_values" => $compareData['new_values_json'],//修改后的值
];
$id = CustomerResourceChanges::insertGetId($data);
if (!$id) {
Db::rollback();
return $res;
}
}
$six_speed_data['resource_id'] = $where['id'];
//查六要素是否存在
$six_speed = SixSpeed::where('resource_id', $where['id'])->find();
if ($six_speed) {
$six_speed = $six_speed->toArray();
//更新六要素
$sixSpeedUpdate = SixSpeed::where('id', $six_speed['id'])->update($six_speed_data);
if (!$sixSpeedUpdate) {
Db::rollback();
return $res;
}
//更近六要素日志表
$compareData = (new CommonService)->compareData($six_speed, $six_speed_data);
if ($compareData['changed_fields']) {
$data = [
"operator_id" => $operator_id,//操作人的ID
"campus_id" => $campus_id,//操作人校区的ID|如果这人有2校区就填0
"customer_resource_id" => $where['id'],//客户资源的ID
"modified_field" => $compareData['changed_fields_json'],//修改的哪些字段
"old_value" => $compareData['old_values_json'],//修改前的值
"new_value" => $compareData['new_values_json'],//修改后的值
];
$id = SixSpeedModificationLog::insertGetId($data);
if (!$id) {
Db::rollback();
return $res;
}
}
} else {
//创建六要素
$sixSpeedUpdate = SixSpeed::create($six_speed_data);
if (!$sixSpeedUpdate) {
Db::rollback();
return $res;
}
}
Db::commit();
$res = [
'code' => 1,
'msg' => '操作成功'
];
return $res;
} catch (\Exception $exception) {
Db::rollback();
return $res;
}
}
}

7
niucloud/app/service/api/apiService/ResourceSharingService.php

@ -97,7 +97,12 @@ class ResourceSharingService extends BaseApiService
$data = $model->with([
'customerResource'=>function($query){
$query->with([
'sixSpeed'=>function($query_2){},
'sixSpeed'=>function($query_2){
$query_2->append([
'purchase_power_name',//购买力
'concept_awareness_name',//认知理念
]);
},
'personnel'=>function($query_2){}
])->append([
'source_channel_name',//来源渠道

Loading…
Cancel
Save