Files
cunkebao_v3/Server/application/chukebao/controller/AiSettingsController.php

266 lines
9.7 KiB
PHP
Raw Normal View History

2025-09-17 16:51:11 +08:00
<?php
namespace app\chukebao\controller;
2025-09-26 15:37:15 +08:00
use app\chukebao\model\FriendSettings;
2025-09-17 16:51:11 +08:00
use app\chukebao\model\Questions;
use library\ResponseHelper;
use think\Db;
class AiSettingsController extends BaseController
{
const SETTING_DEFAULT = [
'audioSetting' => false,
'round' => 10,
'aiStopSetting' => [
'status' => true,
'key' => ['好', '不错', '好的', '下次', '可以']
],
'fileSetting' => [
'type' => 1,
'content' => ''
],
'modelSetting' => [
'model' => 'GPT-4',
'role' => '你是一名销售的AI助理同时也是一个工智能技术专家你的名字叫小灵你是单身女性出生于2003年10月10日喜欢听音乐和看电影有着丰富的人生阅历前成熟大方分享用幽默风趣的语言和客户交流顾客问起你的感情回复内容中不要使用号特别注意不要跟客户问题不要更多选择发送的信息。',
'businessBackground' => '灵销智能公司开发了多款AI营销智能技术产品以提升销售GPT AI大模型为核心接入打造的销售/营销/客服等AI智能应用为企业AI办公AI助理AI销售AI营销AI直播等大AI应用产品。',
'dialogueStyle' => '客户你们的AI解决方案具体是怎么收费的销售朋友我们的AI解决方案是根据项目需求来定的这样吧你能跟我说说你们的具体情况吗不过这样一分钱您看怎么样我们可以给您做个详细的方案对比。',
]
];
const TYPE_DATA = ['audioSetting', 'round', 'aiStopSetting', 'fileSetting', 'modelSetting'];
/**
* 获取配置信息
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getSetting()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$data = Db::name('ai_settings')->where(['userId' => $userId, 'companyId' => $companyId])->find();
if (empty($data)) {
$setting = self::SETTING_DEFAULT;
$data = [
'companyId' => $companyId,
'userId' => $userId,
'config' => json_encode($setting, 256),
'createTime' => time(),
'updateTime' => time()
];
Db::name('ai_settings')->insert($data);
} else {
$setting = json_decode($data['config'], true);
}
return ResponseHelper::success($setting, '获取成功');
}
/**
* 配置
* @return \think\response\Json
* @throws \Exception
*/
public function setSetting()
{
$key = $this->request->param('key', '');
$value = $this->request->param('value', '');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($key) || empty($value)) {
return ResponseHelper::error('参数缺失');
}
if (!in_array($key, self::TYPE_DATA)) {
return ResponseHelper::error('该类型不在配置项');
}
Db::startTrans();
try {
$data = Db::name('ai_settings')->where(['userId' => $userId, 'companyId' => $companyId])->find();
if (empty($data)) {
$setting = self::SETTING_DEFAULT;
} else {
$setting = json_decode($data['config'], true);
}
$setting[$key] = $value;
$setting = json_encode($setting, 256);
Db::name('ai_settings')->where(['id' => $data['id']])->update(['config' => $setting, 'updateTime' => time()]);
Db::commit();
return ResponseHelper::success(' ', '配置成功');
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error('配置失败:' . $e->getMessage());
}
}
2025-09-23 16:21:19 +08:00
public function getUserTokens()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$tokens = Db::name('users')
->where('id', $userId)
->where('companyId', $companyId)
->value('tokens');
return ResponseHelper::success($tokens, '获取成功');
}
public function getFriend()
{
$friendId = $this->request->param('friendId', '');
$wechatAccountId = $this->request->param('wechatAccountId', '');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
2025-09-26 15:37:15 +08:00
$aiType = FriendSettings::where(['userId' => $userId, 'companyId' => $companyId,'friendId' => $friendId,'wechatAccountId' => $wechatAccountId])->value('type');
2025-09-23 16:21:19 +08:00
if (empty($aiType)) {
$aiType = 0;
}
return ResponseHelper::success($aiType, '获取成功');
}
2025-09-17 16:51:11 +08:00
public function setFriend()
{
$friendId = $this->request->param('friendId', '');
$wechatAccountId = $this->request->param('wechatAccountId', '');
$type = $this->request->param('type', 0);
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($friendId) || empty($wechatAccountId)) {
return ResponseHelper::error('参数缺失');
}
$friend = Db::table('s2_wechat_friend')->where(['id' => $friendId,'wechatAccountId' => $wechatAccountId])->find();
if (empty($friend)) {
return ResponseHelper::error('该好友不存在');
}
2025-09-26 15:37:15 +08:00
$friendSettings = FriendSettings::where(['userId' => $userId, 'companyId' => $companyId,'friendId' => $friendId,'wechatAccountId' => $wechatAccountId])->find();
2025-09-17 16:51:11 +08:00
Db::startTrans();
try {
2025-09-26 15:37:15 +08:00
if (empty($friendSettings)) {
$friendSettings = new FriendSettings();
$friendSettings->companyId = $companyId;
$friendSettings->userId = $userId;
$friendSettings->type = $type;
$friendSettings->wechatAccountId = $wechatAccountId;
$friendSettings->friendId = $friendId;
$friendSettings->createTime = time();
$friendSettings->updateTime = time();
2025-09-17 16:51:11 +08:00
}else{
2025-09-26 15:37:15 +08:00
$friendSettings->type = $type;
$friendSettings->updateTime = time();
2025-09-17 16:51:11 +08:00
}
2025-09-26 15:37:15 +08:00
$friendSettings->save();
2025-09-17 16:51:11 +08:00
Db::commit();
return ResponseHelper::success(' ', '配置成功');
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error('配置失败:' . $e->getMessage());
}
}
2025-09-23 16:21:19 +08:00
public function setAllFriend()
{
$packageId = $this->request->param('packageId', []);
$type = $this->request->param('type', 0);
$isUpdata = $this->request->param('isUpdata', 0);
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($packageId)) {
return ResponseHelper::error('参数缺失');
}
//列出所有好友
$row = Db::name('traffic_source_package_item')->alias('a')
->join('wechat_friendship f','a.identifier = f.wechatId and f.companyId = '.$companyId)
->join(['s2_wechat_account' => 'wa'],'f.ownerWechatId = wa.wechatId')
->whereIn('a.packageId' , $packageId)
->field('f.id as friendId,wa.id as wechatAccountId')
->group('f.id')
->select();
if (empty($row)) {
return ResponseHelper::error('`好友不存在');
}
// 1000条为一组进行批量处理
$batchSize = 1000;
$totalRows = count($row);
for ($i = 0; $i < $totalRows; $i += $batchSize) {
$batchRows = array_slice($row, $i, $batchSize);
if (!empty($batchRows)) {
// 1. 提取当前批次的phone
$friendIds = array_column($batchRows, 'friendId');
// 2. 批量查询已存在的phone
$existingPhones = [];
if (!empty($friendIds)) {
//强制更新
if(!empty($isUpdata)){
Db::name('ai_friend_settings')->whereIn('friendId',$friendIds)->update(['type' => $type,'updateTime' => time()]);
}
2025-09-26 15:37:15 +08:00
$existing = FriendSettings::where('companyId', $companyId)->where('friendId', 'in', $friendIds)->field('friendId')->select();
2025-09-23 16:21:19 +08:00
$existingPhones = array_column($existing, 'friendId');
}
// 3. 过滤出新数据,批量插入
$newData = [];
foreach ($batchRows as $row) {
if (!empty($friendIds) && !in_array($row['friendId'], $existingPhones)) {
$newData[] = [
'companyId' => $companyId,
'userId' => $userId,
'type' => $type,
'wechatAccountId' => $row['wechatAccountId'],
'friendId' => $row['friendId'],
'createTime' => time(),
'updateTime' => time(),
];
}
}
// 4. 批量插入新数据
if (!empty($newData)) {
Db::name('ai_friend_settings')->insertAll($newData);
}
}
}
try {
return ResponseHelper::success(' ', '配置成功');
} catch (\Exception $e) {
return ResponseHelper::error('配置失败:' . $e->getMessage());
}
}
2025-09-17 16:51:11 +08:00
}