代码提交

This commit is contained in:
wong
2025-10-16 10:59:08 +08:00
parent db22100846
commit 1083822513
3 changed files with 161 additions and 232 deletions

View File

@@ -62,7 +62,9 @@ class AccountController extends BaseController
// 保存数据到数据库
if (!empty($response['results'])) {
$this->saveAccount($response['results']);
foreach ($response['results'] as $item) {
$this->saveAccount($item);
}
}
if ($isInner) {
@@ -701,60 +703,42 @@ class AccountController extends BaseController
* 保存账号数据到数据库
* @param array $item 账号数据
*/
private function saveAccount($data)
private function saveAccount($item)
{
// 将日期时间字符串转换为时间戳
$createTime = isset($item['createTime']) ? strtotime($item['createTime']) : null;
$deleteTime = isset($item['deleteTime']) ? strtotime($item['deleteTime']) : null;
$sqlData = [];
foreach ($data as $item) {
$rows[] = [
'id' => $item['id'],
'accountType' => isset($item['accountType']) ? $item['accountType'] : 0,
'status' => isset($item['status']) ? $item['status'] : 0,
'tenantId' => isset($item['tenantId']) ? $item['tenantId'] : 0,
'userName' => isset($item['userName']) ? $item['userName'] : '',
'realName' => isset($item['realName']) ? $item['realName'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'phone' => isset($item['phone']) ? $item['phone'] : '',
'memo' => isset($item['memo']) ? $item['memo'] : '',
'createTime' => $createTime,
'creator' => isset($item['creator']) ? $item['creator'] : 0,
'creatorUserName' => isset($item['creatorUserName']) ? $item['creatorUserName'] : '',
'creatorRealName' => isset($item['creatorRealName']) ? $item['creatorRealName'] : '',
'departmentId' => isset($item['departmentId']) ? $item['departmentId'] : 0,
'departmentName' => isset($item['departmentName']) ? $item['departmentName'] : '',
'privilegeIds' => isset($item['privilegeIds']) ? json_encode($item['privilegeIds']) : json_encode([]),
'alive' => isset($item['alive']) ? $item['alive'] : false,
'hasXiakeAccount' => isset($item['hasXiakeAccount']) ? $item['hasXiakeAccount'] : false,
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : false,
'deleteTime' => $deleteTime
];
}
// 拆分插入/更新:按主键 id 判断
$ids = array_column($rows, 'id');
$existingIds = CompanyAccountModel::whereIn('id', $ids)->column('id');
$existSet = array_flip($existingIds);
$data = [
'id' => $item['id'],
'accountType' => isset($item['accountType']) ? $item['accountType'] : 0,
'status' => isset($item['status']) ? $item['status'] : 0,
'tenantId' => isset($item['tenantId']) ? $item['tenantId'] : 0,
'userName' => isset($item['userName']) ? $item['userName'] : '',
'realName' => isset($item['realName']) ? $item['realName'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'phone' => isset($item['phone']) ? $item['phone'] : '',
'memo' => isset($item['memo']) ? $item['memo'] : '',
'createTime' => $createTime,
'creator' => isset($item['creator']) ? $item['creator'] : 0,
'creatorUserName' => isset($item['creatorUserName']) ? $item['creatorUserName'] : '',
'creatorRealName' => isset($item['creatorRealName']) ? $item['creatorRealName'] : '',
'departmentId' => isset($item['departmentId']) ? $item['departmentId'] : 0,
'departmentName' => isset($item['departmentName']) ? $item['departmentName'] : '',
'privilegeIds' => isset($item['privilegeIds']) ? json_encode($item['privilegeIds']) : json_encode([]),
'alive' => isset($item['alive']) ? $item['alive'] : false,
'hasXiakeAccount' => isset($item['hasXiakeAccount']) ? $item['hasXiakeAccount'] : false,
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : false,
'deleteTime' => $deleteTime
];
$toInsert = [];
$toUpdate = [];
foreach ($rows as $r) {
if (isset($existSet[$r['id']])) $toUpdate[] = $r; else $toInsert[] = $r;
// 使用tenantId作为唯一性判断
$account = CompanyAccountModel::where('id', $item['id'])->find();
if ($account) {
$account->save($data);
} else {
CompanyAccountModel::create($data);
}
$changed = false;
if (!empty($toInsert)) {
$m = new CompanyAccountModel();
$m->insertAll($toInsert, true); // 允许外部主键
$changed = true;
}
if (!empty($toUpdate)) {
$m = new CompanyAccountModel();
$res = $m->saveAll($toUpdate);
if ($res) $changed = true;
}
return $changed;
}
}

View File

@@ -54,9 +54,11 @@ class WechatChatroomController extends BaseController
// 保存数据到数据库
if (!empty($response['results'])) {
$isUpdate = false;
$updated = $this->saveChatroom($response['results']);
if($updated && $isDel == 0){
$isUpdate = true;
foreach ($response['results'] as $item) {
$updated = $this->saveChatroom($item);
if($updated && $isDel == 0){
$isUpdate = true;
}
}
}
@@ -78,71 +80,53 @@ class WechatChatroomController extends BaseController
* 保存群聊数据到数据库
* @param array $item 群聊数据
*/
private function saveChatroom($data)
private function saveChatroom($item)
{
$sqlData = [];
foreach ($data as $item) {
$sqlData[] = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'wechatAccountAlias' => $item['wechatAccountAlias'],
'wechatAccountWechatId' => $item['wechatAccountWechatId'],
'wechatAccountAvatar' => $item['wechatAccountAvatar'],
'wechatAccountNickname' => $item['wechatAccountNickname'],
'chatroomId' => $item['chatroomId'],
'hasMe' => $item['hasMe'],
'chatroomOwnerNickname' => isset($item['chatroomOwnerNickname']) ? $item['chatroomOwnerNickname'] : '',
'chatroomOwnerAvatar' => isset($item['chatroomOwnerAvatar']) ? $item['chatroomOwnerAvatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'pyInitial' => isset($item['pyInitial']) ? $item['pyInitial'] : '',
'quanPin' => isset($item['quanPin']) ? $item['quanPin'] : '',
'chatroomAvatar' => isset($item['chatroomAvatar']) ? $item['chatroomAvatar'] : '',
'members' => is_array($item['members']) ? json_encode($item['members']) : json_encode([]),
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : 0,
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'accountId' => isset($item['accountId']) ? $item['accountId'] : 0,
'accountUserName' => isset($item['accountUserName']) ? $item['accountUserName'] : '',
'accountRealName' => isset($item['accountRealName']) ? $item['accountRealName'] : '',
'accountNickname' => isset($item['accountNickname']) ? $item['accountNickname'] : '',
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'updateTime' => time()
];
}
$data = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'wechatAccountAlias' => $item['wechatAccountAlias'],
'wechatAccountWechatId' => $item['wechatAccountWechatId'],
'wechatAccountAvatar' => $item['wechatAccountAvatar'],
'wechatAccountNickname' => $item['wechatAccountNickname'],
'chatroomId' => $item['chatroomId'],
'hasMe' => $item['hasMe'],
'chatroomOwnerNickname' => isset($item['chatroomOwnerNickname']) ? $item['chatroomOwnerNickname'] : '',
'chatroomOwnerAvatar' => isset($item['chatroomOwnerAvatar']) ? $item['chatroomOwnerAvatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'pyInitial' => isset($item['pyInitial']) ? $item['pyInitial'] : '',
'quanPin' => isset($item['quanPin']) ? $item['quanPin'] : '',
'chatroomAvatar' => isset($item['chatroomAvatar']) ? $item['chatroomAvatar'] : '',
'members' => is_array($item['members']) ? json_encode($item['members']) : json_encode([]),
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : 0,
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'accountId' => isset($item['accountId']) ? $item['accountId'] : 0,
'accountUserName' => isset($item['accountUserName']) ? $item['accountUserName'] : '',
'accountRealName' => isset($item['accountRealName']) ? $item['accountRealName'] : '',
'accountNickname' => isset($item['accountNickname']) ? $item['accountNickname'] : '',
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'updateTime' => time()
];
if (empty($sqlData)) {
// 使用chatroomId和wechatAccountId的组合作为唯一性判断
$chatroom = WechatChatroomModel::where('id',$item['id'])->find();
if ($chatroom) {
$chatroom->save($data);
return true;
} else {
WechatChatroomModel::create($data);
return false;
}
// 先查已存在ID拆分插入与更新两批参考 WechatFriendController
$ids = array_column($sqlData, 'id');
$existingIds = WechatChatroomModel::whereIn('id', $ids)->column('id');
$existingSet = array_flip($existingIds);
$toInsert = [];
$toUpdate = [];
foreach ($sqlData as $row) {
if (isset($existingSet[$row['id']])) {
$toUpdate[] = $row;
} else {
$toInsert[] = $row;
}
}
$isUpdate = false;
if (!empty($toInsert)) {
$m = new WechatChatroomModel();
// 批量插入(允许外部主键)
$m->insertAll($toInsert, true);
$isUpdate = false;
}
if (!empty($toUpdate)) {
$m = new WechatChatroomModel();
$m->saveAll($toUpdate);
$isUpdate = true;
}
return $isUpdate;
// // 同时保存群成员数据
// if (!empty($item['members'])) {
// foreach ($item['members'] as $member) {
// $this->saveChatroomMember($member, $item['chatroomId']);
// }
// }
}
/**
@@ -190,7 +174,9 @@ class WechatChatroomController extends BaseController
// 保存数据到数据库
if (!empty($response)) {
$this->saveChatroomMember($response, $chatroomId);
foreach ($response as $item) {
$this->saveChatroomMember($item, $chatroomId);
}
}
if($isInner){
@@ -212,50 +198,30 @@ class WechatChatroomController extends BaseController
* @param array $item 群成员数据
* @param string $wechatChatroomId 微信群ID
*/
private function saveChatroomMember($data, $wechatChatroomId)
private function saveChatroomMember($item, $wechatChatroomId)
{
$sqlData = [];
foreach ($data as $item) {
$sqlData[] = [
'chatroomId' => $wechatChatroomId,
'wechatId' => isset($item['wechatId']) ? $item['wechatId'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'alias' => isset($item['alias']) ? $item['alias'] : '',
'friendType' => isset($item['friendType']) ? $item['friendType'] : false,
'updateTime' => time()
];
}
$data = [
'chatroomId' => $wechatChatroomId,
'wechatId' => isset($item['wechatId']) ? $item['wechatId'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'alias' => isset($item['alias']) ? $item['alias'] : '',
'friendType' => isset($item['friendType']) ? $item['friendType'] : false,
'updateTime' => time()
];
if (empty($sqlData)) {
return false;
}
// 使用chatroomId和wechatId的组合作为唯一性判断
$member = WechatChatroomMemberModel::where([
['chatroomId', '=', $wechatChatroomId],
['wechatId', '=', $item['wechatId']]
])->find();
// 使用 (chatroomId, wechatId) 组合作为唯一性判断,拆分插入与更新
$existing = WechatChatroomMemberModel::where('chatroomId', $wechatChatroomId)
->column('id,wechatId', 'wechatId');
$toInsert = [];
$toUpdate = [];
foreach ($sqlData as $row) {
$wid = $row['wechatId'];
if ($wid !== '' && isset($existing[$wid])) {
// 带上主键以便 saveAll 根据主键更新
$row['id'] = $existing[$wid]['id'] ?? $existing[$wid];
$toUpdate[] = $row;
} else {
$toInsert[] = $row;
}
}
if (!empty($toInsert)) {
$m = new WechatChatroomMemberModel();
$m->insertAll($toInsert, true);
}
if (!empty($toUpdate)) {
$m = new WechatChatroomMemberModel();
$m->saveAll($toUpdate);
if ($member) {
$member->savea($data);
} else {
$data['createTime'] = time();
WechatChatroomMemberModel::create($data);
}
}

View File

@@ -3,7 +3,6 @@
namespace app\api\controller;
use app\api\model\WechatFriendModel;
use think\Db;
use think\facade\Request;
use think\facade\Log;
@@ -76,12 +75,15 @@ class WechatFriendController extends BaseController
// 发送请求获取好友列表
$result = requestCurl($this->baseUrl . 'api/WechatFriend/friendlistData', $params, 'POST', $header, 'json');
$response = handleApiResponse($result);
// 保存数据到数据库
if (is_array($response)) {
$isUpdate = false;
$updated = $this->saveFriend($response);
if($updated && $isDel == 0){
$isUpdate = true;
foreach ($response as $item) {
$updated = $this->saveFriend($item);
if($updated && $isDel == 0){
$isUpdate = true;
}
}
}
@@ -105,82 +107,59 @@ class WechatFriendController extends BaseController
* @param array $item 微信好友数据
* @return bool 是否创建或更新了记录
*/
private function saveFriend($data)
private function saveFriend($item)
{
$sqlData = [];
foreach ($data as $item) {
$sqlData[] = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'alias' => $item['alias'],
'wechatId' => $item['wechatId'],
'conRemark' => $item['conRemark'],
'nickname' => $item['nickname'],
'pyInitial' => $item['pyInitial'],
'quanPin' => $item['quanPin'],
'avatar' => $item['avatar'],
'gender' => $item['gender'],
'region' => $item['region'],
'addFrom' => $item['addFrom'],
'labels' => is_array($item['labels']) ? json_encode($item['labels']) : json_encode([]),
'siteLabels' => json_encode([]),
'signature' => $item['signature'],
'isDeleted' => $item['isDeleted'],
'isPassed' => $item['isPassed'],
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'accountId' => $item['accountId'],
'extendFields' => is_array($item['extendFields']) ? json_encode($item['extendFields']) : json_encode([]),
'accountUserName' => $item['accountUserName'],
'accountRealName' => $item['accountRealName'],
'accountNickname' => $item['accountNickname'],
'ownerAlias' => $item['ownerAlias'],
'ownerWechatId' => $item['ownerWechatId'],
'ownerNickname' => $item['ownerNickname'],
'ownerAvatar' => $item['ownerAvatar'],
'phone' => $item['phone'],
'thirdParty' => is_array($item['thirdParty']) ? json_encode($item['thirdParty']) : json_encode([]),
'groupId' => $item['groupId'],
'passTime' => !empty($item['isPassed']) && $item['passTime'] != '0001-01-01T00:00:00' ? strtotime($item['passTime']) : 0,
'additionalPicture' => $item['additionalPicture'],
'desc' => $item['desc'],
'country' => $item['country'],
'privince' => isset($item['privince']) ? $item['privince'] : '',
'city' => isset($item['city']) ? $item['city'] : '',
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'updateTime' => time()
];
}
if (empty($sqlData)) {
$data = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'alias' => $item['alias'],
'wechatId' => $item['wechatId'],
'conRemark' => $item['conRemark'],
'nickname' => $item['nickname'],
'pyInitial' => $item['pyInitial'],
'quanPin' => $item['quanPin'],
'avatar' => $item['avatar'],
'gender' => $item['gender'],
'region' => $item['region'],
'addFrom' => $item['addFrom'],
'labels' => is_array($item['labels']) ? json_encode($item['labels']) : json_encode([]),
'siteLabels' => json_encode([]),
'signature' => $item['signature'],
'isDeleted' => $item['isDeleted'],
'isPassed' => $item['isPassed'],
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'accountId' => $item['accountId'],
'extendFields' => is_array($item['extendFields']) ? json_encode($item['extendFields']) : json_encode([]),
'accountUserName' => $item['accountUserName'],
'accountRealName' => $item['accountRealName'],
'accountNickname' => $item['accountNickname'],
'ownerAlias' => $item['ownerAlias'],
'ownerWechatId' => $item['ownerWechatId'],
'ownerNickname' => $item['ownerNickname'],
'ownerAvatar' => $item['ownerAvatar'],
'phone' => $item['phone'],
'thirdParty' => is_array($item['thirdParty']) ? json_encode($item['thirdParty']) : json_encode([]),
'groupId' => $item['groupId'],
'passTime' => !empty($item['isPassed']) && $item['passTime'] != '0001-01-01T00:00:00' ? strtotime($item['passTime']) : 0,
'additionalPicture' => $item['additionalPicture'],
'desc' => $item['desc'],
'country' => $item['country'],
'privince' => isset($item['privince']) ? $item['privince'] : '',
'city' => isset($item['city']) ? $item['city'] : '',
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'updateTime' => time()
];
// 使用ID作为唯一性判断
$friend = WechatFriendModel::where('id', $item['id'])->find();
if ($friend) {
unset($data['siteLabels']);
$friend->save($data);
return true;
} else {
WechatFriendModel::create($data);
return false;
}
// 先查已存在ID拆分插入与更新两批
$ids = array_column($sqlData, 'id');
$existingIds = WechatFriendModel::whereIn('id', $ids)->column('id');
$existingSet = array_flip($existingIds);
$toInsert = [];
$toUpdate = [];
foreach ($sqlData as $row) {
if (isset($existingSet[$row['id']])) {
$toUpdate[] = $row;
} else {
$toInsert[] = $row;
}
}
$isUpdate = false;
if (!empty($toInsert)) {
$m = new WechatFriendModel();
$m->insertAll($toInsert,true);
$isUpdate = false;
}
if (!empty($toUpdate)) {
// 批量更新使用 saveAll按主键更新
$m = new WechatFriendModel();
$m->saveAll($toUpdate);
$isUpdate = true;
}
return $isUpdate;
}
}