优化mybatis方法去掉冗余方法

This commit is contained in:
wh
2026-04-23 17:00:02 +08:00
parent 9483bea005
commit 2beebbe469
7 changed files with 23 additions and 73 deletions

View File

@@ -73,7 +73,7 @@ public class AuthInterceptor implements HandlerInterceptor {
.orElseThrow(() -> new UnauthorizedException("未登录或登录已过期"));
SysUser user = sysUserMapper.findByIdAndCompanyId(loginUser.userId(), loginUser.companyId());
SysCompany company = sysCompanyMapper.findById(loginUser.companyId());
SysCompany company = sysCompanyMapper.selectById(loginUser.companyId());
if (user == null || company == null || user.getStatus() != UserStatus.ENABLED || company.getStatus() != CompanyStatus.ENABLED) {
throw new UnauthorizedException("未登录或登录已过期");
}

View File

@@ -8,17 +8,11 @@ import org.apache.ibatis.annotations.Param;
public interface SysCompanyMapper extends BaseMapper<SysCompany> {
int insert(SysCompany company);
SysCompany findById(@Param("id") Long id);
SysCompany findByCompanyCode(@Param("companyCode") String companyCode);
List<SysCompany> findEnabledCompaniesByPhone(@Param("phone") String phone);
List<SysCompany> listAll();
int updateStatus(@Param("id") Long id, @Param("status") CompanyStatus status);
void deleteAll();
}
}

View File

@@ -10,35 +10,21 @@ import org.apache.ibatis.annotations.Param;
public interface SysUserMapper extends BaseMapper<SysUser> {
int insert(SysUser user);
SysUser findById(@Param("id") Long id);
SysUser findByIdAndCompanyId(@Param("id") Long id, @Param("companyId") Long companyId);
SysUser findByCompanyIdAndPhone(@Param("companyId") Long companyId, @Param("phone") String phone);
List<SysUser> listByCompanyId(@Param("companyId") Long companyId);
List<SysUser> listCompanyAdmins(@Param("companyId") Long companyId);
int updatePassword(
@Param("id") Long id,
@Param("companyId") Long companyId,
@Param("passwordHash") String passwordHash,
@Param("mustChangePassword") boolean mustChangePassword
);
int updatePassword(@Param("id") Long id, @Param("companyId") Long companyId,
@Param("passwordHash") String passwordHash, @Param("mustChangePassword") boolean mustChangePassword);
int updateAssignment(
@Param("id") Long id,
@Param("companyId") Long companyId,
@Param("role") UserRole role,
@Param("position") UserPosition position
);
int updateAssignment(@Param("id") Long id, @Param("companyId") Long companyId, @Param("role") UserRole role,
@Param("position") UserPosition position);
int updateStatus(@Param("id") Long id, @Param("companyId") Long companyId, @Param("status") UserStatus status);
int bumpSessionVersion(@Param("id") Long id, @Param("companyId") Long companyId);
void deleteAll();
}
}

View File

@@ -21,7 +21,7 @@ public class CompanyService {
public List<SysCompany> listCompanies(LoginUser currentUser) {
assertPlatformAdmin(currentUser);
return sysCompanyMapper.listAll();
return sysCompanyMapper.selectList(null);
}
public SysCompany createCompany(LoginUser currentUser, CreateCompanyRequest request) {

View File

@@ -1,5 +1,12 @@
package com.labelsys.backend.service;
import java.util.List;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.labelsys.backend.common.ResultCode;
import com.labelsys.backend.common.exception.BusinessException;
import com.labelsys.backend.common.exception.ForbiddenException;
@@ -18,11 +25,8 @@ import com.labelsys.backend.mapper.SysCompanyMapper;
import com.labelsys.backend.mapper.SysUserMapper;
import com.labelsys.backend.service.session.TokenSessionRepository;
import com.labelsys.backend.util.IdGenerator;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
@@ -37,7 +41,9 @@ public class UserService {
public List<SysUser> listCompanyUsers(LoginUser currentUser) {
assertCompanyAdmin(currentUser);
return sysUserMapper.listByCompanyId(currentUser.companyId());
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getCompanyId, currentUser.companyId()).orderByAsc(SysUser::getId);
return sysUserMapper.selectList(wrapper);
}
public List<SysUser> listCompanyAdmins(LoginUser currentUser, Long companyId) {
@@ -114,7 +120,7 @@ public class UserService {
}
private void ensureEnabledCompany(Long companyId) {
SysCompany company = sysCompanyMapper.findById(companyId);
SysCompany company = sysCompanyMapper.selectById(companyId);
if (company == null || company.getStatus() != CompanyStatus.ENABLED) {
throw new BusinessException(ResultCode.NOT_FOUND, "公司不存在或已禁用");
}