智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

542 lines
20 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
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\member\Member;
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;
use think\facade\Event;
/**
* 客户资源服务层
* Class MemberService
* @package app\service\api\member
*/
class CustomerResourcesService extends BaseApiService
{
public function __construct()
{
parent::__construct();
}
//获取全部客户资源数据
public function getAll(array $where,string $field = '*')
{
$res = [
'code' => 0,
'msg' => '操作失败',
'data' => []
];
if(!$where){
$res['msg'] = '查询条件不能为空';
return $res;
}
$model = new CustomerResources();
if(!empty($where['name'])){
$model = $model->where('name', 'like', "%{$where['name']}%");
}
$data = $model->field($field)
->with([
'resourceSharingHasMany'
])
->append([
'initial_intent_name'
])
->select()->toArray();
if (!$data) {
$res['msg'] = '暂无数据';
return $res;
}
$res['code'] = 1;
$res['msg'] = '操作成功';
$res['data'] = $data;
return $res;
}
//添加数据
public function addData(array $customer_resources_data, array $six_speed_data)
{
$date = date('Y-m-d H:i:s');
$customer_resources_data['updated_at'] = $date;
$six_speed_data['updated_at'] = $date;
$res = [
'code' => 0,
'msg' => '操作失败'
];
//开启事物
Db::startTrans();
try {
$resource_id = CustomerResources::insertGetId($customer_resources_data);//客户资源表
if (!$resource_id) {
Db::rollback();
return $res;
}
$six_speed_data['resource_id'] = $resource_id;
$sixSpeedAdd = SixSpeed::create($six_speed_data);
if (!$sixSpeedAdd) {
Db::rollback();
return $res;
}
// 资源共享表新增记录
$personnel = new Personnel();
$role_id = $personnel->alias("a")
->join(['school_campus_person_role' => 'b'], 'a.id = b.person_id', 'left')
->where(['a.id' => $customer_resources_data['consultant']])
->value('b.role_id');
$resourceSharing = new ResourceSharing();
$resourceSharing->insert([
'resource_id' => $resource_id,
'user_id' => $customer_resources_data['consultant'],
'role_id' => $role_id
]);
Db::commit();
$res = [
'code' => 1,
'msg' => '操作成功'
];
$event_data = [
'customer_resources_id' => $resource_id,//客户资源表id
'event_type' => 'add'//事件类型"add=添加,edit=修改
];//事件类型"add=添加,edit=修改
Event::trigger('CalculatePerformance', $event_data)->queue();
return $res;
} catch (\Exception $exception) {
Db::rollback();
return $res;
}
}
//客户资源-编辑
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();
$six_speed = SixSpeed::where('resource_id', $where['id'])->find();
if ($customer_resources) {
$customer_resources = $customer_resources->toArray();
if(!$customer_resources['member_id'] && $six_speed){
//新数据存在一访问 或者旧数据存在一访的情况 && 这用户没注册过member账号的情况下才给他创建member账号
if(!empty($six_speed_data['first_visit_status']) || $six_speed['first_visit_status']){
$sex = 0;
switch ($customer_resources_data['gender']) {
case 'male'://
$sex = 1;
break;
case 'female'://
$sex = 2;
break;
default://其他
$sex = 0;
break;
}
$password = create_password($customer_resources_data['phone_number']);//创建密码
//给用户创建member账号
$member_id = Member::insertGetId([
'username'=>$customer_resources_data['phone_number'],//会员用户名
'mobile'=>$customer_resources_data['phone_number'],//手机号
'password'=>$password,//会员密码
'nickname'=>$customer_resources_data['name'],//会员昵称
'sex'=>$sex,//性别 0保密 1男 2女
'member_time'=>time(),//成为会员时间
]);
if($member_id){
$customer_resources_data['member_id'] = $member_id;
}else{
Db::rollback();
$res['msg'] = '创建用户账号失败';
return $res;
}
}
}
}
$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'];
//查六要素是否存在
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;
}
}
//客户资源-获取客户资源修改记录列表
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;
}
}