belongsTo(TrafficPoolGroup::class, 'groupId', 'id'); } /** * 关联公司流量详情 */ public function poolCompany() { return $this->belongsTo(TrafficPoolCompany::class, 'poolCompanyId', 'id'); } /** * 批量添加成员到分组 * @param int $groupId * @param array $poolCompanyIds * @param int $companyId * @param int $operatorId * @param int $addType * @return int 成功添加的数量 */ public static function batchAddMembers(int $groupId, array $poolCompanyIds, int $companyId, int $operatorId = null, int $addType = self::ADD_TYPE_MANUAL) { $count = 0; $time = time(); // 获取已存在的成员 $existIds = self::where('groupId', $groupId) ->whereIn('poolCompanyId', $poolCompanyIds) ->where('isDel', 0) ->column('poolCompanyId'); // 获取要添加的流量详情 $poolCompanies = TrafficPoolCompany::whereIn('id', $poolCompanyIds) ->where('companyId', $companyId) ->where('isDel', 0) ->column('identifier', 'id'); $insertData = []; foreach ($poolCompanyIds as $poolCompanyId) { if (in_array($poolCompanyId, $existIds)) { continue; // 跳过已存在的 } if (!isset($poolCompanies[$poolCompanyId])) { continue; // 跳过不存在的 } $insertData[] = [ 'groupId' => $groupId, 'poolCompanyId' => $poolCompanyId, 'identifier' => $poolCompanies[$poolCompanyId], 'companyId' => $companyId, 'addType' => $addType, 'operatorId' => $operatorId, 'createTime' => $time, 'isDel' => 0, ]; } if (!empty($insertData)) { (new self())->saveAll($insertData); $count = count($insertData); // 更新分组成员数量缓存 TrafficPoolGroup::where('id', $groupId)->setInc('memberCount', $count); } return $count; } /** * 批量移除成员 * @param int $groupId * @param array $poolCompanyIds * @return int */ public static function batchRemoveMembers(int $groupId, array $poolCompanyIds) { $count = self::where('groupId', $groupId) ->whereIn('poolCompanyId', $poolCompanyIds) ->where('isDel', 0) ->update([ 'isDel' => 1, 'deleteTime' => time() ]); if ($count > 0) { // 更新分组成员数量缓存 TrafficPoolGroup::where('id', $groupId)->setDec('memberCount', $count); } return $count; } }