优化mybatis方法去掉冗余方法
This commit is contained in:
@@ -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("未登录或登录已过期");
|
||||
}
|
||||
|
||||
@@ -8,16 +8,10 @@ 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();
|
||||
|
||||
@@ -10,31 +10,17 @@ 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);
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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, "公司不存在或已禁用");
|
||||
}
|
||||
|
||||
@@ -14,17 +14,6 @@
|
||||
id, company_code, company_name, status, created_at, updated_at
|
||||
</sql>
|
||||
|
||||
<insert id="insert" parameterType="com.labelsys.backend.entity.SysCompany">
|
||||
insert into sys_company (id, company_code, company_name, status, created_at, updated_at)
|
||||
values (#{id}, #{companyCode}, #{companyName}, #{status}, current_timestamp, current_timestamp)
|
||||
</insert>
|
||||
|
||||
<select id="findById" resultMap="SysCompanyResultMap">
|
||||
select <include refid="CompanyColumns"/>
|
||||
from sys_company
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="findByCompanyCode" resultMap="SysCompanyResultMap">
|
||||
select <include refid="CompanyColumns"/>
|
||||
from sys_company
|
||||
@@ -41,12 +30,6 @@
|
||||
order by c.id
|
||||
</select>
|
||||
|
||||
<select id="listAll" resultMap="SysCompanyResultMap">
|
||||
select <include refid="CompanyColumns"/>
|
||||
from sys_company
|
||||
order by id
|
||||
</select>
|
||||
|
||||
<update id="updateStatus">
|
||||
update sys_company
|
||||
set status = #{status}, updated_at = current_timestamp
|
||||
|
||||
@@ -22,21 +22,6 @@
|
||||
status, session_version, created_at, updated_at
|
||||
</sql>
|
||||
|
||||
<insert id="insert" parameterType="com.labelsys.backend.entity.SysUser">
|
||||
insert into sys_user (
|
||||
id, company_id, phone, username, role, position, real_name, password_hash, must_change_password,
|
||||
status, session_version, created_at, updated_at
|
||||
)
|
||||
values (
|
||||
#{id}, #{companyId}, #{phone}, #{username}, #{role}, #{position}, #{realName}, #{passwordHash}, #{mustChangePassword},
|
||||
#{status}, #{sessionVersion}, current_timestamp, current_timestamp
|
||||
)
|
||||
</insert>
|
||||
|
||||
<select id="findById" resultMap="SysUserResultMap">
|
||||
select <include refid="UserColumns"/> from sys_user where id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="findByIdAndCompanyId" resultMap="SysUserResultMap">
|
||||
select <include refid="UserColumns"/> from sys_user where id = #{id} and company_id = #{companyId}
|
||||
</select>
|
||||
@@ -45,10 +30,6 @@
|
||||
select <include refid="UserColumns"/> from sys_user where company_id = #{companyId} and phone = #{phone}
|
||||
</select>
|
||||
|
||||
<select id="listByCompanyId" resultMap="SysUserResultMap">
|
||||
select <include refid="UserColumns"/> from sys_user where company_id = #{companyId} order by id
|
||||
</select>
|
||||
|
||||
<select id="listCompanyAdmins" resultMap="SysUserResultMap">
|
||||
select <include refid="UserColumns"/>
|
||||
from sys_user
|
||||
|
||||
Reference in New Issue
Block a user