代码同步
This commit is contained in:
@@ -708,7 +708,7 @@ class AccountController extends BaseController
|
||||
$deleteTime = isset($item['deleteTime']) ? strtotime($item['deleteTime']) : null;
|
||||
$sqlData = [];
|
||||
foreach ($data as $item) {
|
||||
$sqlData[] = [
|
||||
$rows[] = [
|
||||
'id' => $item['id'],
|
||||
'accountType' => isset($item['accountType']) ? $item['accountType'] : 0,
|
||||
'status' => isset($item['status']) ? $item['status'] : 0,
|
||||
@@ -733,9 +733,28 @@ class AccountController extends BaseController
|
||||
];
|
||||
}
|
||||
|
||||
// 拆分插入/更新:按主键 id 判断
|
||||
$ids = array_column($rows, 'id');
|
||||
$existingIds = CompanyAccountModel::whereIn('id', $ids)->column('id');
|
||||
$existSet = array_flip($existingIds);
|
||||
|
||||
// 使用tenantId作为唯一性判断
|
||||
$account = new CompanyAccountModel();
|
||||
$account->saveAll($account);
|
||||
$toInsert = [];
|
||||
$toUpdate = [];
|
||||
foreach ($rows as $r) {
|
||||
if (isset($existSet[$r['id']])) $toUpdate[] = $r; else $toInsert[] = $r;
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,6 @@ class WechatChatroomController extends BaseController
|
||||
*/
|
||||
private function saveChatroom($data)
|
||||
{
|
||||
|
||||
$sqlData = [];
|
||||
foreach ($data as $item) {
|
||||
$sqlData[] = [
|
||||
@@ -112,19 +111,38 @@ class WechatChatroomController extends BaseController
|
||||
];
|
||||
}
|
||||
|
||||
// 使用chatroomId和wechatAccountId的组合作为唯一性判断
|
||||
$chatroom = new WechatChatroomModel();
|
||||
$chatroom->saveAll($sqlData);
|
||||
if ($chatroom) {
|
||||
if ($chatroom->isUpdate()){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
|
||||
}else{
|
||||
if (empty($sqlData)) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,8 +228,35 @@ class WechatChatroomController extends BaseController
|
||||
];
|
||||
}
|
||||
|
||||
$member = new WechatChatroomMemberModel();
|
||||
$member->saveAll($sqlData);
|
||||
if (empty($sqlData)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 使用 (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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\api\model\WechatFriendModel;
|
||||
use think\Db;
|
||||
use think\facade\Request;
|
||||
use think\facade\Log;
|
||||
|
||||
@@ -75,15 +76,12 @@ class WechatFriendController extends BaseController
|
||||
// 发送请求获取好友列表
|
||||
$result = requestCurl($this->baseUrl . 'api/WechatFriend/friendlistData', $params, 'POST', $header, 'json');
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
// 保存数据到数据库
|
||||
if (is_array($response)) {
|
||||
$isUpdate = false;
|
||||
foreach ($response as $item) {
|
||||
$updated = $this->saveFriend($item);
|
||||
if($updated && $isDel == 0){
|
||||
$isUpdate = true;
|
||||
}
|
||||
$updated = $this->saveFriend($response);
|
||||
if($updated && $isDel == 0){
|
||||
$isUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,59 +105,82 @@ class WechatFriendController extends BaseController
|
||||
* @param array $item 微信好友数据
|
||||
* @return bool 是否创建或更新了记录
|
||||
*/
|
||||
private function saveFriend($item)
|
||||
private function saveFriend($data)
|
||||
{
|
||||
$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);
|
||||
$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)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user