优化mybatis方法去掉冗余方法
This commit is contained in:
@@ -73,7 +73,7 @@ public class AuthInterceptor implements HandlerInterceptor {
|
|||||||
.orElseThrow(() -> new UnauthorizedException("未登录或登录已过期"));
|
.orElseThrow(() -> new UnauthorizedException("未登录或登录已过期"));
|
||||||
|
|
||||||
SysUser user = sysUserMapper.findByIdAndCompanyId(loginUser.userId(), loginUser.companyId());
|
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) {
|
if (user == null || company == null || user.getStatus() != UserStatus.ENABLED || company.getStatus() != CompanyStatus.ENABLED) {
|
||||||
throw new UnauthorizedException("未登录或登录已过期");
|
throw new UnauthorizedException("未登录或登录已过期");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,17 +8,11 @@ import org.apache.ibatis.annotations.Param;
|
|||||||
|
|
||||||
public interface SysCompanyMapper extends BaseMapper<SysCompany> {
|
public interface SysCompanyMapper extends BaseMapper<SysCompany> {
|
||||||
|
|
||||||
int insert(SysCompany company);
|
|
||||||
|
|
||||||
SysCompany findById(@Param("id") Long id);
|
|
||||||
|
|
||||||
SysCompany findByCompanyCode(@Param("companyCode") String companyCode);
|
SysCompany findByCompanyCode(@Param("companyCode") String companyCode);
|
||||||
|
|
||||||
List<SysCompany> findEnabledCompaniesByPhone(@Param("phone") String phone);
|
List<SysCompany> findEnabledCompaniesByPhone(@Param("phone") String phone);
|
||||||
|
|
||||||
List<SysCompany> listAll();
|
|
||||||
|
|
||||||
int updateStatus(@Param("id") Long id, @Param("status") CompanyStatus status);
|
int updateStatus(@Param("id") Long id, @Param("status") CompanyStatus status);
|
||||||
|
|
||||||
void deleteAll();
|
void deleteAll();
|
||||||
}
|
}
|
||||||
@@ -10,35 +10,21 @@ import org.apache.ibatis.annotations.Param;
|
|||||||
|
|
||||||
public interface SysUserMapper extends BaseMapper<SysUser> {
|
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 findByIdAndCompanyId(@Param("id") Long id, @Param("companyId") Long companyId);
|
||||||
|
|
||||||
SysUser findByCompanyIdAndPhone(@Param("companyId") Long companyId, @Param("phone") String phone);
|
SysUser findByCompanyIdAndPhone(@Param("companyId") Long companyId, @Param("phone") String phone);
|
||||||
|
|
||||||
List<SysUser> listByCompanyId(@Param("companyId") Long companyId);
|
|
||||||
|
|
||||||
List<SysUser> listCompanyAdmins(@Param("companyId") Long companyId);
|
List<SysUser> listCompanyAdmins(@Param("companyId") Long companyId);
|
||||||
|
|
||||||
int updatePassword(
|
int updatePassword(@Param("id") Long id, @Param("companyId") Long companyId,
|
||||||
@Param("id") Long id,
|
@Param("passwordHash") String passwordHash, @Param("mustChangePassword") boolean mustChangePassword);
|
||||||
@Param("companyId") Long companyId,
|
|
||||||
@Param("passwordHash") String passwordHash,
|
|
||||||
@Param("mustChangePassword") boolean mustChangePassword
|
|
||||||
);
|
|
||||||
|
|
||||||
int updateAssignment(
|
int updateAssignment(@Param("id") Long id, @Param("companyId") Long companyId, @Param("role") UserRole role,
|
||||||
@Param("id") Long id,
|
@Param("position") UserPosition position);
|
||||||
@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 updateStatus(@Param("id") Long id, @Param("companyId") Long companyId, @Param("status") UserStatus status);
|
||||||
|
|
||||||
int bumpSessionVersion(@Param("id") Long id, @Param("companyId") Long companyId);
|
int bumpSessionVersion(@Param("id") Long id, @Param("companyId") Long companyId);
|
||||||
|
|
||||||
void deleteAll();
|
void deleteAll();
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ public class CompanyService {
|
|||||||
|
|
||||||
public List<SysCompany> listCompanies(LoginUser currentUser) {
|
public List<SysCompany> listCompanies(LoginUser currentUser) {
|
||||||
assertPlatformAdmin(currentUser);
|
assertPlatformAdmin(currentUser);
|
||||||
return sysCompanyMapper.listAll();
|
return sysCompanyMapper.selectList(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SysCompany createCompany(LoginUser currentUser, CreateCompanyRequest request) {
|
public SysCompany createCompany(LoginUser currentUser, CreateCompanyRequest request) {
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
package com.labelsys.backend.service;
|
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.ResultCode;
|
||||||
import com.labelsys.backend.common.exception.BusinessException;
|
import com.labelsys.backend.common.exception.BusinessException;
|
||||||
import com.labelsys.backend.common.exception.ForbiddenException;
|
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.mapper.SysUserMapper;
|
||||||
import com.labelsys.backend.service.session.TokenSessionRepository;
|
import com.labelsys.backend.service.session.TokenSessionRepository;
|
||||||
import com.labelsys.backend.util.IdGenerator;
|
import com.labelsys.backend.util.IdGenerator;
|
||||||
import java.util.List;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -37,7 +41,9 @@ public class UserService {
|
|||||||
|
|
||||||
public List<SysUser> listCompanyUsers(LoginUser currentUser) {
|
public List<SysUser> listCompanyUsers(LoginUser currentUser) {
|
||||||
assertCompanyAdmin(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) {
|
public List<SysUser> listCompanyAdmins(LoginUser currentUser, Long companyId) {
|
||||||
@@ -114,7 +120,7 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void ensureEnabledCompany(Long companyId) {
|
private void ensureEnabledCompany(Long companyId) {
|
||||||
SysCompany company = sysCompanyMapper.findById(companyId);
|
SysCompany company = sysCompanyMapper.selectById(companyId);
|
||||||
if (company == null || company.getStatus() != CompanyStatus.ENABLED) {
|
if (company == null || company.getStatus() != CompanyStatus.ENABLED) {
|
||||||
throw new BusinessException(ResultCode.NOT_FOUND, "公司不存在或已禁用");
|
throw new BusinessException(ResultCode.NOT_FOUND, "公司不存在或已禁用");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,17 +14,6 @@
|
|||||||
id, company_code, company_name, status, created_at, updated_at
|
id, company_code, company_name, status, created_at, updated_at
|
||||||
</sql>
|
</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 id="findByCompanyCode" resultMap="SysCompanyResultMap">
|
||||||
select <include refid="CompanyColumns"/>
|
select <include refid="CompanyColumns"/>
|
||||||
from sys_company
|
from sys_company
|
||||||
@@ -41,19 +30,13 @@
|
|||||||
order by c.id
|
order by c.id
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="listAll" resultMap="SysCompanyResultMap">
|
|
||||||
select <include refid="CompanyColumns"/>
|
|
||||||
from sys_company
|
|
||||||
order by id
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<update id="updateStatus">
|
<update id="updateStatus">
|
||||||
update sys_company
|
update sys_company
|
||||||
set status = #{status}, updated_at = current_timestamp
|
set status = #{status}, updated_at = current_timestamp
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<delete id="deleteAll">
|
<delete id="deleteAll">
|
||||||
delete from sys_company
|
delete from sys_company
|
||||||
</delete>
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -22,21 +22,6 @@
|
|||||||
status, session_version, created_at, updated_at
|
status, session_version, created_at, updated_at
|
||||||
</sql>
|
</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 id="findByIdAndCompanyId" resultMap="SysUserResultMap">
|
||||||
select <include refid="UserColumns"/> from sys_user where id = #{id} and company_id = #{companyId}
|
select <include refid="UserColumns"/> from sys_user where id = #{id} and company_id = #{companyId}
|
||||||
</select>
|
</select>
|
||||||
@@ -45,10 +30,6 @@
|
|||||||
select <include refid="UserColumns"/> from sys_user where company_id = #{companyId} and phone = #{phone}
|
select <include refid="UserColumns"/> from sys_user where company_id = #{companyId} and phone = #{phone}
|
||||||
</select>
|
</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 id="listCompanyAdmins" resultMap="SysUserResultMap">
|
||||||
select <include refid="UserColumns"/>
|
select <include refid="UserColumns"/>
|
||||||
from sys_user
|
from sys_user
|
||||||
@@ -91,4 +72,4 @@
|
|||||||
<delete id="deleteAll">
|
<delete id="deleteAll">
|
||||||
delete from sys_user
|
delete from sys_user
|
||||||
</delete>
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user