This commit is contained in:
oneao committed 2025-12-26 21:35:12 +08:00
1 parent 2f8a815c62
commit c76581d8cb
34 files changed
+539 -277

No files matched your search

@@ -5,17 +5,32 @@ import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
public class AuthLoginVO {
private Long companyId;
private String companyLogo;
private String companyName;
private Long userId;
private Integer userRole;
private String userName;
private String userPosition;
private Company company;
private User user;
private String token;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Company {
private Long id;
private String logo;
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class User {
private Long id;
private Integer role;
private String name;
private String position;
private String avatar;
}
}
@@ -9,8 +9,8 @@ import cn.link56.auth.identity.model.vo.AuthLoginVO;
import cn.link56.auth.token.model.AuthToken;
import cn.link56.auth.token.model.TokenPayload;
import cn.link56.auth.token.service.AuthTokenService;
import cn.link56.company.model.po.CompanyPO;
import cn.link56.company.model.po.CompanyUserPO;
import cn.link56.company.model.po.Company;
import cn.link56.company.model.po.CompanyUser;
import cn.link56.auth.identity.model.dto.AuthRegisterDTO;
import cn.link56.company.service.CompanyService;
import cn.link56.company.service.CompanyUserService;
@@ -61,7 +61,7 @@ public class AuthIdentityService {
long companyId = IdGenerator.nextId();
companyService.save(
new CompanyPO()
new Company()
.setId(companyId)
.setName(company.getName())
.setIndustry(StringUtils.join(company.getIndustries(),","))
@@ -72,7 +72,7 @@ public class AuthIdentityService {
);
companyUserService.save(
new CompanyUserPO()
new CompanyUser()
.setId(IdGenerator.nextId())
.setCompanyId(companyId)
.setName(admin.getName())
@@ -85,7 +85,7 @@ public class AuthIdentityService {
}
public AuthLoginVO loginByPassword(AuthLoginPasswordDTO dto) {
CompanyUserPO user = companyUserService.loginByEmailAndPassword(dto.getEmail(), dto.getPassword());
CompanyUser user = companyUserService.loginByEmailAndPassword(dto.getEmail(), dto.getPassword());
if (user == null) {
throw new BusinessException("邮箱或密码错误");
}
@@ -107,7 +107,7 @@ public class AuthIdentityService {
throw new BusinessException("邮箱或验证码失效");
}
CompanyUserPO user = companyUserService.loginByEmail(email);
CompanyUser user = companyUserService.loginByEmail(email);
if (user == null) {
throw new BusinessException("邮箱不存在");
}
@@ -118,8 +118,8 @@ public class AuthIdentityService {
/**
* 公共逻辑:根据用户生成 LoginVO
*/
private AuthLoginVO buildLoginVO(CompanyUserPO user) {
CompanyPO company = companyService.getById(user.getCompanyId());
private AuthLoginVO buildLoginVO(CompanyUser user) {
Company company = companyService.getById(user.getCompanyId());
TokenPayload tokenPayload = new TokenPayload();
tokenPayload.setCompanyId(user.getCompanyId());
@@ -127,18 +127,22 @@ public class AuthIdentityService {
AuthToken authToken = tokenService.generateToken(tokenPayload);
return new AuthLoginVO(
AuthLoginVO.Company companyVO = new AuthLoginVO.Company(
company.getId(),
company.getLogo(),
company.getName(),
company.getName()
);
AuthLoginVO.User userVO = new AuthLoginVO.User(
user.getId(),
user.getRole(),
user.getName(),
user.getPosition(),
authToken.getToken()
user.getAvatar()
);
return new AuthLoginVO(companyVO, userVO, authToken.getToken());
}
}
@@ -1,13 +1,13 @@
package cn.link56.company.mapper;
import cn.link56.company.model.middle.RecommendCompanyRow;
import cn.link56.company.model.po.CompanyPO;
import cn.link56.company.model.po.Company;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CompanyMapper extends BaseMapper<CompanyPO> {
public interface CompanyMapper extends BaseMapper<Company> {
List<RecommendCompanyRow> listRecommend();
}
@@ -1,9 +1,9 @@
package cn.link56.company.mapper;
import cn.link56.company.model.po.CompanyUserPO;
import cn.link56.company.model.po.CompanyUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CompanyUserMapper extends BaseMapper<CompanyUserPO> {
public interface CompanyUserMapper extends BaseMapper<CompanyUser> {
}
@@ -24,4 +24,6 @@ public class UpdateCompanyUserDTO {
private String name;
private String phone;
private String canuse;
private String avatar;
}
@@ -17,7 +17,7 @@ import java.util.Date;
@NoArgsConstructor
@TableName("b_company")
@Accessors(chain = true)
public class CompanyPO {
public class Company {
@TableId(value = "b_id")
private Long id;
@@ -17,7 +17,7 @@ import java.util.Date;
@NoArgsConstructor
@TableName("b_company_user")
@Accessors(chain = true)
public class CompanyUserPO {
public class CompanyUser {
@TableId(value = "b_id")
private Long id;
@@ -42,6 +42,9 @@ public class CompanyUserPO {
@TableField("b_position")
private String position;
@TableField("b_avatar")
private String avatar;
@TableField("b_canuse")
private String canuse;
@@ -26,7 +26,7 @@ public class CompanyDetailVO {
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class User{
public static class User {
private Long id;
private Long companyId;
private String email;
@@ -34,5 +34,6 @@ public class CompanyDetailVO {
private Integer role;
private String position;
private String name;
private String avatar;
}
}
@@ -17,5 +17,7 @@ public class QueryCompanyUserVO {
private String email;
private String phone;
private String avatar;
private String canuse;
}
@@ -7,8 +7,8 @@ import cn.link56.company.mapper.CompanyMapper;
import cn.link56.company.model.dto.QueryCompanyDTO;
import cn.link56.company.model.dto.UpdateCompanyDTO;
import cn.link56.company.model.middle.RecommendCompanyRow;
import cn.link56.company.model.po.CompanyPO;
import cn.link56.company.model.po.CompanyUserPO;
import cn.link56.company.model.po.Company;
import cn.link56.company.model.po.CompanyUser;
import cn.link56.company.model.vo.CompanyDetailVO;
import cn.link56.company.model.vo.QueryCompanyVO;
import cn.link56.company.model.vo.RecommendCompanyVO;
@@ -29,7 +29,7 @@ import java.util.List;
import java.util.stream.Collectors;
@Service
public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
public class CompanyService extends ServiceImpl<CompanyMapper, Company> {
@Resource
private CompanyMapper companyMapper;
@Resource
@@ -67,9 +67,9 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
}
public List<RecommendHotCompanyVO> listHot() {
LambdaQueryWrapper<CompanyPO> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyPO::getHotRecommend, "1")
.select(CompanyPO::getId, CompanyPO::getName);
LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Company::getHotRecommend, "1")
.select(Company::getId, Company::getName);
return companyMapper.selectList(queryWrapper)
.stream()
@@ -86,8 +86,8 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
*/
public boolean isNameExists(String name) {
Long count = companyMapper.selectCount(
new LambdaQueryWrapper<CompanyPO>()
.eq(CompanyPO::getName, name)
new LambdaQueryWrapper<Company>()
.eq(Company::getName, name)
);
return count > 0;
}
@@ -97,8 +97,8 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
*/
public boolean isPhoneExists(String phone) {
Long count = companyMapper.selectCount(
new LambdaQueryWrapper<CompanyPO>()
.eq(CompanyPO::getPhone, phone)
new LambdaQueryWrapper<Company>()
.eq(Company::getPhone, phone)
);
return count > 0;
}
@@ -108,8 +108,8 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
*/
public boolean isEmailExists(String email) {
Long count = companyMapper.selectCount(
new LambdaQueryWrapper<CompanyPO>()
.eq(CompanyPO::getEmail, email)
new LambdaQueryWrapper<Company>()
.eq(Company::getEmail, email)
);
return count > 0;
}
@@ -123,19 +123,19 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
List<String> industries = queryCompanyDTO.getIndustries();
// 分页
Page<CompanyPO> companyPage = new Page<>(currentPage,pageSize);
LambdaQueryWrapper<CompanyPO> queryWrapper = new LambdaQueryWrapper<>();
Page<Company> companyPage = new Page<>(currentPage,pageSize);
LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
// 名称
if (StringUtils.isNotBlank(name)){
queryWrapper.like(CompanyPO::getName,name);
queryWrapper.like(Company::getName,name);
}
// 地区
if (areas != null && !areas.isEmpty()){
queryWrapper.and(wrapper -> {
for (String area : areas) {
wrapper.or(w -> w.like(CompanyPO::getArea, area));
wrapper.or(w -> w.like(Company::getArea, area));
}
});
}
@@ -144,12 +144,12 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
if (industries != null && !industries.isEmpty()) {
queryWrapper.and(wrapper -> {
for (String industry : industries) {
wrapper.or(w -> w.like(CompanyPO::getIndustry, industry));
wrapper.or(w -> w.like(Company::getIndustry, industry));
}
});
}
queryWrapper.eq(CompanyPO::getCanuse,"1");
queryWrapper.eq(Company::getCanuse,"1");
List<QueryCompanyVO> result = companyMapper.selectList(companyPage, queryWrapper).stream().map(item -> {
QueryCompanyVO queryCompanyVO = new QueryCompanyVO();
@@ -173,18 +173,18 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
}
public CompanyDetailVO getCompanyDetail(Long companyId) {
CompanyPO companyPO = companyMapper.selectById(companyId);
List<CompanyUserPO> companyUserPOS = companyUserService.listByCompanyId(companyId);
Company company = companyMapper.selectById(companyId);
List<CompanyUser> companyUsers = companyUserService.listByCompanyId(companyId);
CompanyDetailVO companyDetailVO = new CompanyDetailVO();
BeanUtils.copyProperties(companyPO,companyDetailVO);
companyDetailVO.setAreas(Arrays.stream(companyPO.getArea().split(","))
BeanUtils.copyProperties(company,companyDetailVO);
companyDetailVO.setAreas(Arrays.stream(company.getArea().split(","))
.collect(Collectors.toList()));
companyDetailVO.setIndustries(Arrays.stream(companyPO.getIndustry().split(","))
companyDetailVO.setIndustries(Arrays.stream(company.getIndustry().split(","))
.collect(Collectors.toList()));
companyDetailVO.setUsers(companyUserPOS.stream()
companyDetailVO.setUsers(companyUsers.stream()
.map(item -> {
CompanyDetailVO.User user = new CompanyDetailVO.User();
BeanUtils.copyProperties(item, user);
@@ -206,43 +206,42 @@ public class CompanyService extends ServiceImpl<CompanyMapper,CompanyPO> {
String name = updateCompanyDTO.getName();
String phone = updateCompanyDTO.getPhone();
String email = updateCompanyDTO.getEmail();
Date establishmentDate = updateCompanyDTO.getEstablishmentDate();
String profile = updateCompanyDTO.getProfile();
String description = updateCompanyDTO.getDescription();
String logo = updateCompanyDTO.getLogo();
String area = StringUtils.join(updateCompanyDTO.getAreas(), ",");
String industry = StringUtils.join(updateCompanyDTO.getIndustries(), ",");
// 检查
CompanyPO companyPO = companyMapper.selectById(id);
if (ObjectUtils.isEmpty(companyPO)){
Company company = companyMapper.selectById(id);
if (ObjectUtils.isEmpty(company)){
throw new BusinessException("更新失败,该公司不存在");
}
// 检查名称
if (!companyPO.getName().equals(name) && isNameExists(name)){
if (!company.getName().equals(name) && isNameExists(name)){
throw new BusinessException("公司名称已存在");
}
// 检查联系电话
if (!StringUtils.equals(phone, companyPO.getPhone())
if (!StringUtils.equals(phone, company.getPhone())
&& isPhoneExists(phone)) {
throw new BusinessException("公司联系电话已存在");
}
// 检查邮箱
if (!StringUtils.equals(email, companyPO.getEmail())
if (!StringUtils.equals(email, company.getEmail())
&& isEmailExists(email)) {
throw new BusinessException("公司联系邮箱已存在");
}
// 更新
BeanUtils.copyProperties(updateCompanyDTO,companyPO);
BeanUtils.copyProperties(updateCompanyDTO, company);
companyPO.setArea(area);
companyPO.setIndustry(industry);
company.setArea(area);
company.setIndustry(industry);
// 保存更新后的公司信息
companyMapper.updateById(companyPO);
companyMapper.updateById(company);
// 更新用户头像
companyUserService.updateCompanyAvatar(company.getId(),company.getLogo());
}
}
@@ -9,7 +9,7 @@ import cn.link56.company.model.dto.AddCompanyUserDTO;
import cn.link56.company.model.dto.DeleteCompanyUserDTO;
import cn.link56.company.model.dto.QueryCompanyUserDTO;
import cn.link56.company.model.dto.UpdateCompanyUserDTO;
import cn.link56.company.model.po.CompanyUserPO;
import cn.link56.company.model.po.CompanyUser;
import cn.link56.company.model.vo.QueryCompanyUserVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -26,7 +26,7 @@ import java.util.List;
import java.util.stream.Collectors;
@Service
public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUserPO> {
public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUser> {
@Resource
private CompanyUserMapper companyUserMapper;
@@ -41,8 +41,8 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
return false;
}
long count = companyUserMapper.selectCount(
new LambdaQueryWrapper<CompanyUserPO>()
.eq(CompanyUserPO::getEmail, email)
new LambdaQueryWrapper<CompanyUser>()
.eq(CompanyUser::getEmail, email)
);
return count > 0;
}
@@ -54,13 +54,29 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
*/
public boolean isPhoneExists(String phone) {
long count = companyUserMapper.selectCount(
new LambdaQueryWrapper<CompanyUserPO>()
.eq(CompanyUserPO::getPhone, phone)
new LambdaQueryWrapper<CompanyUser>()
.eq(CompanyUser::getPhone, phone)
);
return count > 0;
}
public void updateCompanyAvatar(Long companyId, String avatar) {
if (StringUtils.isBlank(avatar)) {
return;
}
LambdaUpdateWrapper<CompanyUser> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CompanyUser::getCompanyId, companyId)
.and(w -> w.isNull(CompanyUser::getAvatar)
.or()
.eq(CompanyUser::getAvatar, ""))
.set(CompanyUser::getAvatar, avatar);
companyUserMapper.update(wrapper);
}
/**
* 校验当前用户是否有权限操作指定的公司
* @param companyId 当前公司ID
@@ -69,10 +85,10 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
*/
public boolean hasPermission(Long companyId, Long userId) {
// 根据 userId 查询公司用户信息
CompanyUserPO companyUserPO = companyUserMapper.selectById(userId);
CompanyUser companyUser = companyUserMapper.selectById(userId);
// 如果 companyUserPO 为空,表示该用户不存在,直接返回无权限
if (ObjectUtils.isEmpty(companyUserPO)) {
if (ObjectUtils.isEmpty(companyUser)) {
return false;
}
@@ -82,35 +98,35 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
}
// 判断用户是否属于该公司
if (!companyUserPO.getCompanyId().equals(companyId)) {
if (!companyUser.getCompanyId().equals(companyId)) {
return false;
}
// 检查用户角色是否为 0,只有角色为 0 才有权限
return companyUserPO.getRole() == 0;
return companyUser.getRole() == 0;
// 如果角色不是 0,表示没有权限
}
public CompanyUserPO loginByEmailAndPassword(String email, String password){
LambdaQueryWrapper<CompanyUserPO> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUserPO::getEmail,email)
.eq(CompanyUserPO::getPassword,password);
public CompanyUser loginByEmailAndPassword(String email, String password){
LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUser::getEmail,email)
.eq(CompanyUser::getPassword,password);
return companyUserMapper.selectOne(queryWrapper);
}
public CompanyUserPO loginByEmail(String email){
LambdaQueryWrapper<CompanyUserPO> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUserPO::getEmail,email);
public CompanyUser loginByEmail(String email){
LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUser::getEmail,email);
return companyUserMapper.selectOne(queryWrapper);
}
public List<CompanyUserPO> listByCompanyId(Long companyId){
LambdaQueryWrapper<CompanyUserPO> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUserPO::getCompanyId,companyId);
public List<CompanyUser> listByCompanyId(Long companyId){
LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUser::getCompanyId,companyId);
return companyUserMapper.selectList(queryWrapper);
}
@@ -132,34 +148,34 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
String position = queryCompanyUserDTO.getPosition();
String canuse = queryCompanyUserDTO.getCanuse();
Page<CompanyUserPO> queryPage = new Page<>(currentPage,pageSize);
LambdaQueryWrapper<CompanyUserPO> queryWrapper = new LambdaQueryWrapper<>();
Page<CompanyUser> queryPage = new Page<>(currentPage,pageSize);
LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CompanyUserPO::getCompanyId,companyId);
queryWrapper.eq(CompanyUser::getCompanyId,companyId);
if (StringUtils.isNotBlank(name)){
queryWrapper.like(CompanyUserPO::getName,name);
queryWrapper.like(CompanyUser::getName,name);
}
if (StringUtils.isNotBlank(email)){
queryWrapper.like(CompanyUserPO::getEmail,email);
queryWrapper.like(CompanyUser::getEmail,email);
}
if (StringUtils.isNotBlank(phone)){
queryWrapper.like(CompanyUserPO::getPhone,phone);
queryWrapper.like(CompanyUser::getPhone,phone);
}
if (StringUtils.isNotBlank(position)){
queryWrapper.like(CompanyUserPO::getPosition,position);
queryWrapper.like(CompanyUser::getPosition,position);
}
if (StringUtils.isNotBlank(canuse)){
queryWrapper.eq(CompanyUserPO::getCanuse,canuse);
queryWrapper.eq(CompanyUser::getCanuse,canuse);
}
List<CompanyUserPO> companyUserPOS = companyUserMapper.selectList(queryPage, queryWrapper);
List<CompanyUser> companyUsers = companyUserMapper.selectList(queryPage, queryWrapper);
List<QueryCompanyUserVO> result = companyUserPOS.stream().map(item -> {
List<QueryCompanyUserVO> result = companyUsers.stream().map(item -> {
QueryCompanyUserVO queryCompanyUserVO = new QueryCompanyUserVO();
BeanUtils.copyProperties(item, queryCompanyUserVO);
return queryCompanyUserVO;
@@ -192,14 +208,14 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
canuse = "1";
}
CompanyUserPO companyUserPO = new CompanyUserPO();
CompanyUser companyUser = new CompanyUser();
BeanUtils.copyProperties(companyUserDTO,companyUserPO);
companyUserPO.setId(IdGenerator.nextId());
companyUserPO.setRole(1);
companyUserPO.setCanuse(canuse);
BeanUtils.copyProperties(companyUserDTO, companyUser);
companyUser.setId(IdGenerator.nextId());
companyUser.setRole(1);
companyUser.setCanuse(canuse);
companyUserMapper.insert(companyUserPO);
companyUserMapper.insert(companyUser);
}
@Transactional
@@ -217,37 +233,38 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
throw new BusinessException("您没有权限访问该公司的信息,操作被拒绝");
}
CompanyUserPO companyUserPO = companyUserMapper.selectById(id);
String oldPassword = companyUserPO.getPassword();
CompanyUser companyUser = companyUserMapper.selectById(id);
String oldPassword = companyUser.getPassword();
if (ObjectUtils.isEmpty(companyUserPO)) {
if (ObjectUtils.isEmpty(companyUser)) {
throw new BusinessException("更新失败,该用户不存在");
}
// 检查邮箱是否发生变化
boolean isEmailChanged = !StringUtils.equals(email, companyUserPO.getEmail());
boolean isEmailChanged = !StringUtils.equals(email, companyUser.getEmail());
if (isEmailChanged && isEmailExists(email)) {
throw new BusinessException("邮箱已被注册");
}
// 检查手机号是否发生变化
if (!StringUtils.equals(phone, companyUserPO.getPhone()) && isPhoneExists(phone)) {
if (!StringUtils.equals(phone, companyUser.getPhone()) && isPhoneExists(phone)) {
throw new BusinessException("手机号已被注册");
}
if (StringUtils.isNotBlank(password)) {
companyUserPO.setPassword(password);
companyUser.setPassword(password);
}
BeanUtils.copyProperties(companyUserDTO, companyUserPO);
if(StringUtils.isNotBlank(password)){
companyUserPO.setPassword(password);
}else{
companyUserPO.setPassword(oldPassword);
BeanUtils.copyProperties(companyUserDTO, companyUser);
if(StringUtils.isNotBlank(password)) {
companyUser.setPassword(password);
} else {
companyUser.setPassword(oldPassword);
}
// 更新用户信息
companyUserMapper.updateById(companyUserPO);
companyUserMapper.updateById(companyUser);
}
@@ -261,10 +278,10 @@ public class CompanyUserService extends ServiceImpl<CompanyUserMapper, CompanyUs
throw new BusinessException("您没有权限访问该公司的信息,操作被拒绝");
}
LambdaUpdateWrapper<CompanyUserPO> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.in(CompanyUserPO::getId, ids)
.eq(CompanyUserPO::getCompanyId, companyId)
.ne(CompanyUserPO::getRole, 0);
LambdaUpdateWrapper<CompanyUser> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.in(CompanyUser::getId, ids)
.eq(CompanyUser::getCompanyId, companyId)
.ne(CompanyUser::getRole, 0);
// 执行删除操作
companyUserMapper.delete(updateWrapper);
@@ -22,7 +22,7 @@ public class PostCommentVO {
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class User{
public static class User {
private Long id;
private Long companyId;
private String name;
@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
@Data
@@ -14,6 +15,7 @@ public class PostCompanyVO {
private String name;
private String logo;
private String profile;
private Date establishmentDate;
private List<String> areas;
private List<String> industries;
}
@@ -41,61 +41,68 @@ public class PostCommentService extends ServiceImpl<PostCommentMapper, PostComme
return null;
}
public List<PostCommentVO> listPostComments(Long id) {
public List<PostCommentVO> listPostComments(Long postId) {
// 1. 查询所有评论
List<PostCommentVO> list = postCommentMapper.selectComments(id);
List<PostCommentVO> list = postCommentMapper.selectComments(postId);
// 2. 构建扁平化树形结构(所有子评论提升到根评论层级)
List<PostCommentVO> rootComments = new ArrayList<>();
Map<Long, PostCommentVO> commentMap = new HashMap<>();
// 2.1 初始化映射并收集根评论
// 2. 构建 id -> comment 映射
Map<Long, PostCommentVO> commentMap = new HashMap<>(list.size());
for (PostCommentVO comment : list) {
comment.setChildren(new ArrayList<>()); // 统一初始化
commentMap.put(comment.getId(), comment);
if (ObjectUtils.isEmpty(comment.getFatherId())) {
rootComments.add(comment);
comment.setChildren(new ArrayList<>()); // 提前初始化children
}
}
// 2.2 遍历所有评论,将子评论挂到根祖先的children中
// 3. 收集一级评论
List<PostCommentVO> rootComments = new ArrayList<>();
for (PostCommentVO comment : list) {
if (ObjectUtils.isEmpty(comment.getFatherId())) {
continue; // 跳过根评论
}
// 查找当前评论的根祖先(追溯到没有fatherId的祖先)
PostCommentVO rootAncestor = null;
Long currentFatherId = comment.getFatherId();
while (true) {
PostCommentVO parent = commentMap.get(currentFatherId);
if (parent == null) break; // 父节点不存在,终止循环
if (ObjectUtils.isEmpty(comment.getFatherId())) {
rootAncestor = parent; // 找到根祖先
break;
} else {
currentFatherId = parent.getFatherId(); // 继续向上查找
}
}
// 挂载到根祖先的children中
if (rootAncestor != null && rootAncestor.getChildren() != null) {
rootAncestor.getChildren().add(comment);
if (comment.getFatherId() == null) {
rootComments.add(comment);
}
}
// 3. 按原始顺序排序(可选)
rootComments.forEach(root -> {
if (root.getChildren() != null) {
// 根据需求排序,例如按创建时间升序
root.getChildren().sort(Comparator.comparing(PostCommentVO::getCreateTime));
// 4. 将所有子评论挂到「根评论」的 children 中
for (PostCommentVO comment : list) {
// 跳过一级评论
if (comment.getFatherId() == null) {
continue;
}
});
PostCommentVO root = findRootComment(comment, commentMap);
if (root != null) {
root.getChildren().add(comment);
}
}
// 5. 子评论排序(可选)
rootComments.forEach(root ->
root.getChildren().sort(
Comparator.comparing(PostCommentVO::getCreateTime)
)
);
return rootComments;
}
private PostCommentVO findRootComment(
PostCommentVO comment,
Map<Long, PostCommentVO> commentMap) {
PostCommentVO current = comment;
while (current.getFatherId() != null) {
PostCommentVO parent = commentMap.get(current.getFatherId());
if (parent == null) {
return null; // 数据异常,防御
}
current = parent;
}
return current; // fatherId == null 的就是根评论
}
@Transactional
public CreatePostCommentVO createPostComment(CreatePostCommentDTO dto) {
PostComment postComment = new PostComment();
@@ -4,7 +4,7 @@ import cn.link56.common.BasePageVO;
import cn.link56.common.context.UserContext;
import cn.link56.common.exception.BusinessException;
import cn.link56.common.utils.snowflake.idgen.IdGenerator;
import cn.link56.company.model.po.CompanyPO;
import cn.link56.company.model.po.Company;
import cn.link56.company.service.CompanyService;
import cn.link56.post.mapper.PostMapper;
import cn.link56.post.model.dto.*;
@@ -16,13 +16,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
@@ -258,7 +256,6 @@ public class PostService extends ServiceImpl<PostMapper, Post> {
Long viewTotal = postViewService.getTotalByPostId(id);
Long commentTotal = postCommentService.getTotalByPostId(id);
postVO.setViewCount(viewTotal);
postVO.setCommentCount(commentTotal);
@@ -274,16 +271,16 @@ public class PostService extends ServiceImpl<PostMapper, Post> {
}
public PostCompanyVO getPostCompanyById(Long id) {
CompanyPO companyPO = companyService.getById(id);
if (companyPO == null){
Company company = companyService.getById(id);
if (company == null){
throw new BusinessException("该公司不存在或已注销");
}
PostCompanyVO postCompanyVO = new PostCompanyVO();
BeanUtils.copyProperties(companyPO,postCompanyVO);
BeanUtils.copyProperties(company,postCompanyVO);
postCompanyVO.setAreas(Arrays.stream(StringUtils.split(companyPO.getArea(),",")).collect(Collectors.toList()));
postCompanyVO.setIndustries(Arrays.stream(StringUtils.split(companyPO.getIndustry(),",")).collect(Collectors.toList()));
postCompanyVO.setAreas(Arrays.stream(StringUtils.split(company.getArea(),",")).collect(Collectors.toList()));
postCompanyVO.setIndustries(Arrays.stream(StringUtils.split(company.getIndustry(),",")).collect(Collectors.toList()));
return postCompanyVO;
}
@@ -28,8 +28,8 @@
<select id="selectComments" resultMap="CommentListMap">
SELECT c.*,
pc.b_id AS pub_user_id,pc.b_company_id AS pub_company_id,pc.b_name AS pub_user_name,
rc.b_id AS rep_user_id,rc.b_company_id AS rep_company_id,rc.b_name AS rep_company_id
pc.b_id AS pub_user_id,pc.b_company_id AS pub_company_id,pc.b_name AS pub_user_name,pc.b_avatar AS pub_user_avatar,
rc.b_id AS rep_user_id,rc.b_company_id AS rep_company_id,rc.b_name AS rep_user_name,rc.b_avatar AS rep_user_avatar
FROM b_post_comment c
LEFT JOIN b_company_user pc ON c.b_create_by = pc.b_id
LEFT JOIN b_company_user rc ON (