package com.mcwl.resource.consumer; import com.mcwl.common.constant.QueueConstants; import com.mcwl.common.core.domain.entity.SysUser; import com.mcwl.common.exception.ServiceException; import com.mcwl.common.utils.SecurityUtils; import com.mcwl.common.utils.StringUtils; import com.mcwl.resource.domain.*; import com.mcwl.resource.mapper.*; import com.mcwl.resource.service.ISysAdviceService; import com.mcwl.system.domain.enums.AdviceEnum; import com.mcwl.system.service.ISysUserService; import com.rabbitmq.client.Channel; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionSynchronization; import org.springframework.transaction.support.TransactionSynchronizationManager; import java.io.IOException; import java.util.Map; import java.util.Objects; @Slf4j @Component @RequiredArgsConstructor public class LikeConsumer { private final ModelMapper modelMapper; private final ModelLikeMapper modelLikeMapper; private final ISysAdviceService sysAdviceService; private final ISysUserService sysUserService; private final ModelCommentMapper modelCommentMapper; private final ModelCommentLikeMapper modelCommentLikeMapper; private final ModelImageCommentMapper modelImageCommentMapper; private final ModelImageCommentLikeMapper modelImageCommentLikeMapper; private final ModelImageMapper modelImageMapper; private final ModelImageLikeMapper modelImageLikeMapper; private final WorkFlowMapper workFlowMapper; private final WorkFlowLikeMapper workFlowLikeMapper; private final WorkFlowCommentMapper workFlowCommentMapper; private final WorkFlowCommentLikeMapper workFlowCommentLikeMapper; /** * 模型点赞消息消费 */ @RabbitListener(queues = QueueConstants.MODEL_LIKE_QUEUE, ackMode = "MANUAL") @Transactional(rollbackFor = Exception.class) public void modelLikeMsg(Map map, Channel channel, Message message) { try { Long userId = map.get("userId"); Long productId = map.get("commentId"); this.modelLike(userId, productId); // 注册事务同步回调 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { // 事务提交后发送ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { log.error("ACK失败: {}", e.getMessage()); } } @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { try { // 事务回滚后发送NACK channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { log.error("NACK失败: {}", e.getMessage()); } } } }); } catch (Exception e) { log.error("模型点赞消息出错: {}", e.getMessage(), e); throw new ServiceException("模型点赞消息出错"); } } /** * 模型评论点赞消息消费 */ @RabbitListener(queues = QueueConstants.MODEL_COMMENT_LIKE_QUEUE, ackMode = "MANUAL") @Transactional(rollbackFor = Exception.class) public void modelCommentLikeMsg(Map map, Channel channel, Message message) { try { Long userId = map.get("userId"); Long productId = map.get("commentId"); this.modelCommentLike(userId, productId); // 注册事务同步回调 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { // 事务提交后发送ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { log.error("ACK失败: {}", e.getMessage()); } } @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { try { // 事务回滚后发送NACK channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { log.error("NACK失败: {}", e.getMessage()); } } } }); } catch (Exception e) { log.error("模型评论点赞消息出错: {}", e.getMessage(), e); throw new ServiceException("模型评论点赞消息出错"); } } /** * 图片点赞消息消费 */ @RabbitListener(queues = QueueConstants.IMAGE_LIKE_QUEUE, ackMode = "MANUAL") @Transactional(rollbackFor = Exception.class) public void imageLikeMsg(Map map, Channel channel, Message message) { try { Long userId = map.get("userId"); Long productId = map.get("commentId"); this.imageLike(userId, productId); // 注册事务同步回调 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { // 事务提交后发送ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { log.error("ACK失败: {}", e.getMessage()); } } @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { try { // 事务回滚后发送NACK channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { log.error("NACK失败: {}", e.getMessage()); } } } }); } catch (Exception e) { log.error("图片点赞消息出错: {}", e.getMessage(), e); throw new ServiceException("图片点赞消息出错"); } } /**s * 图片评论点赞消息消费 */ @RabbitListener(queues = QueueConstants.IMAGE_COMMENT_LIKE_QUEUE, ackMode = "MANUAL") @Transactional(rollbackFor = Exception.class) public void imageCommentLikeMsg(Map map, Channel channel, Message message) { try { Long userId = map.get("userId"); Long productId = map.get("commentId"); this.imageCommentLike(userId, productId); // 注册事务同步回调 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { // 事务提交后发送ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { log.error("ACK失败: {}", e.getMessage()); } } @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { try { // 事务回滚后发送NACK channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { log.error("NACK失败: {}", e.getMessage()); } } } }); } catch (Exception e) { log.error("图片评论点赞消息出错: {}", e.getMessage(), e); throw new ServiceException("图片评论点赞消息出错"); } } /** * 工作流点赞消息消费 */ @RabbitListener(queues = QueueConstants.WORK_FLOW_LIKE_QUEUE, ackMode = "MANUAL") @Transactional(rollbackFor = Exception.class) public void workFlowLikeMsg(Map map, Channel channel, Message message) { try { Long userId = map.get("userId"); Long productId = map.get("commentId"); this.workFLowLike(userId, productId); // 注册事务同步回调 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { // 事务提交后发送ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { log.error("ACK失败: {}", e.getMessage()); } } @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { try { // 事务回滚后发送NACK channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { log.error("NACK失败: {}", e.getMessage()); } } } }); } catch (Exception e) { log.error("工作流点赞消息出错: {}", e.getMessage(), e); throw new ServiceException("工作流点赞消息出错"); } } /** * 工作流评论点赞消息消费 */ @RabbitListener(queues = QueueConstants.WORK_FLOW_COMMENT_LIKE_QUEUE, ackMode = "MANUAL") @Transactional(rollbackFor = Exception.class) public void workFlowCommentLikeMsg(Map map, Channel channel, Message message) { try { Long userId = map.get("userId"); Long productId = map.get("commentId"); this.workFlowCommentLike(userId, productId); // 注册事务同步回调 TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { @Override public void afterCommit() { try { // 事务提交后发送ACK channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { log.error("ACK失败: {}", e.getMessage()); } } @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { try { // 事务回滚后发送NACK channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { log.error("NACK失败: {}", e.getMessage()); } } } }); } catch (Exception e) { log.error("工作流评论点赞消息出错: {}", e.getMessage(), e); throw new ServiceException("工作流评论点赞消息出错"); } } private void modelLike(Long userId, Long modelId) { ModelProduct model = modelMapper.selectById(modelId); if (Objects.isNull(model)) { throw new ServiceException("该模型不存在或已下架"); } ModelLike modelLike = modelLikeMapper.getLike(userId, modelId); if (Objects.nonNull(modelLike)) { if (Objects.equals(modelLike.getDelFlag(), "0")) { modelLike.setDelFlag("2"); int likeNum = model.getLikeNum() - 1; likeNum = Math.max(likeNum, 0); model.setLikeNum(likeNum); } else { modelLike.setDelFlag("0"); model.setLikeNum(model.getLikeNum() + 1); this.addLikeAdvice(model, userId); } // 更新点赞记录 modelLikeMapper.updateDelFlagById(modelLike); // 更新图片点赞数 modelMapper.updateById(model); return; } // 添加点赞记录 modelLike = new ModelLike(); modelLike.setUserId(userId); modelLike.setModelId(modelId); modelLikeMapper.insert(modelLike); // 更新图片点赞数 model.setNumbers(model.getLikeNum() + 1); modelMapper.updateById(model); this.addLikeAdvice(model, userId); } private void addLikeAdvice(ModelProduct model, Long userId) { Long receiverUserId = model.getUserId(); if (Objects.equals(userId, receiverUserId)) { return; } SysUser sysUser = sysUserService.selectUserById(userId); String content = StringUtils.format("恭喜!{}点赞了您的模型:{}", sysUser.getNickName(), model.getModelName()); SysAdvice sysAdvice = new SysAdvice(); sysAdvice.setSenderId(userId); sysAdvice.setReceiverId(receiverUserId); sysAdvice.setContent(content); sysAdvice.setProductId(model.getId()); sysAdvice.setProductType(0); sysAdvice.setIsRead(0); sysAdvice.setType(AdviceEnum.LIKE_REMIND); sysAdviceService.save(sysAdvice); } private void modelCommentLike(Long userId, Long commentId) { ModelComment modelComment = modelCommentMapper.selectById(commentId); if (Objects.isNull(modelComment)) { throw new ServiceException("该评论不存在"); } ModelCommentLike modelCommentLike = modelCommentLikeMapper.getLikeComment(userId, commentId); if (Objects.nonNull(modelCommentLike)) { if (Objects.equals(modelCommentLike.getDelFlag(), "0")) { modelCommentLike.setDelFlag("2"); int likeNum = modelComment.getLikeNum() - 1; likeNum = Math.max(likeNum, 0); modelComment.setLikeNum(likeNum); } else { modelCommentLike.setDelFlag("0"); modelComment.setLikeNum(modelComment.getLikeNum() + 1); this.addLikeAdvice(modelComment, userId); } // 更新点赞记录 modelCommentLikeMapper.updateDelFlagById(modelCommentLike); // 更新图片评论点赞数 modelCommentMapper.updateById(modelComment); return; } // 添加点赞记录 modelCommentLike = new ModelCommentLike(); modelCommentLike.setUserId(userId); modelCommentLike.setModelCommentId(commentId); modelCommentLikeMapper.insert(modelCommentLike); // 更新模型点赞数 modelComment.setLikeNum(modelComment.getLikeNum() + 1); modelCommentMapper.updateById(modelComment); this.addLikeAdvice(modelComment, userId); } private void addLikeAdvice(ModelComment modelComment, Long userId) { Long receiverUserId = modelComment.getUserId(); if (Objects.equals(userId, receiverUserId)) { return; } SysUser sysUser = sysUserService.selectUserById(userId); String content = StringUtils.format("恭喜!{}点赞了您的评论", sysUser.getNickName()); SysAdvice sysAdvice = new SysAdvice(); sysAdvice.setSenderId(userId); sysAdvice.setReceiverId(receiverUserId); sysAdvice.setContent(content); sysAdvice.setProductId(modelComment.getModelId()); sysAdvice.setProductType(0); sysAdvice.setCommentId(modelComment.getId()); sysAdvice.setIsRead(0); sysAdvice.setType(AdviceEnum.LIKE_REMIND); sysAdviceService.save(sysAdvice); } private void imageLike(Long userId, Long imageId) { ModelImage modelImage = modelImageMapper.selectById(imageId); if (Objects.isNull(modelImage)) { throw new ServiceException("该图片不存在或已下架"); } ModelImageLike modelImageLike = modelImageLikeMapper.getLikeImage(userId, imageId); if (Objects.nonNull(modelImageLike)) { if (Objects.equals(modelImageLike.getDelFlag(), "0")) { modelImageLike.setDelFlag("2"); int likeNum = modelImage.getLikeNum() - 1; likeNum = Math.max(likeNum, 0); modelImage.setLikeNum(likeNum); } else { modelImageLike.setDelFlag("0"); modelImage.setLikeNum(modelImage.getLikeNum() + 1); this.addLikeAdvice(modelImage, userId); } // 更新点赞记录 modelImageLikeMapper.updateDelFlagById(modelImageLike); // 更新图片点赞数 modelImageMapper.updateById(modelImage); return; } // 添加点赞记录 modelImageLike = new ModelImageLike(); modelImageLike.setUserId(userId); modelImageLike.setModelImageId(imageId); modelImageLikeMapper.insert(modelImageLike); // 更新图片点赞数 modelImage.setLikeNum(modelImage.getLikeNum() + 1); modelImageMapper.updateById(modelImage); this.addLikeAdvice(modelImage, userId); } private void addLikeAdvice(ModelImage modelImage, Long userId) { Long receiverUserId = modelImage.getUserId(); if (Objects.equals(userId, receiverUserId)) { return; } SysUser sysUser = sysUserService.selectUserById(userId); String content = StringUtils.format("恭喜!{}点赞了您的图片:{}", sysUser.getNickName(), modelImage.getTitle()); SysAdvice sysAdvice = new SysAdvice(); sysAdvice.setSenderId(userId); sysAdvice.setReceiverId(receiverUserId); sysAdvice.setContent(content); sysAdvice.setProductId(modelImage.getId()); sysAdvice.setProductType(2); sysAdvice.setIsRead(0); sysAdvice.setType(AdviceEnum.LIKE_REMIND); sysAdviceService.save(sysAdvice); } private void imageCommentLike(Long userId, Long commentId) { ModelImageComment modelImageComment = modelImageCommentMapper.selectById(commentId); if (Objects.isNull(modelImageComment)) { throw new ServiceException("该评论不存在"); } ModelImageCommentLike modelImageCommentLike = modelImageCommentLikeMapper.getLikeImageComment(userId, commentId); if (Objects.nonNull(modelImageCommentLike)) { if (Objects.equals(modelImageCommentLike.getDelFlag(), "0")) { modelImageCommentLike.setDelFlag("2"); int likeNum = modelImageComment.getLikeNum() - 1; likeNum = Math.max(likeNum, 0); modelImageComment.setLikeNum(likeNum); } else { modelImageCommentLike.setDelFlag("0"); modelImageComment.setLikeNum(modelImageComment.getLikeNum() + 1); this.addLikeAdvice(modelImageComment, userId); } // 更新点赞记录 modelImageCommentLikeMapper.updateDelFlagById(modelImageCommentLike); // 更新图片评论点赞数 modelImageCommentMapper.updateById(modelImageComment); return; } // 添加点赞记录 modelImageCommentLike = new ModelImageCommentLike(); modelImageCommentLike.setUserId(userId); modelImageCommentLike.setModelImageCommentId(commentId); modelImageCommentLikeMapper.insert(modelImageCommentLike); // 更新图片点赞数 modelImageComment.setLikeNum(modelImageComment.getLikeNum() + 1); modelImageCommentMapper.updateById(modelImageComment); this.addLikeAdvice(modelImageComment, userId); } private void addLikeAdvice(ModelImageComment modelImageComment, Long userId) { Long receiverUserId = modelImageComment.getUserId(); if (Objects.equals(userId, receiverUserId)) { return; } SysUser sysUser = sysUserService.selectUserById(userId); String content = StringUtils.format("恭喜!{}点赞了您的评论", sysUser.getNickName()); SysAdvice sysAdvice = new SysAdvice(); sysAdvice.setSenderId(userId); sysAdvice.setReceiverId(receiverUserId); sysAdvice.setContent(content); sysAdvice.setProductId(modelImageComment.getModelImageId()); sysAdvice.setProductType(2); sysAdvice.setCommentId(modelImageComment.getId()); sysAdvice.setIsRead(0); sysAdvice.setType(AdviceEnum.LIKE_REMIND); sysAdviceService.save(sysAdvice); } private void workFLowLike(Long userId, Long modelId) { WorkFlow workFlow = workFlowMapper.selectById(modelId); if (Objects.isNull(workFlow)) { throw new ServiceException("该工作流不存在或已下架"); } WorkFlowLike workFlowLike = workFlowLikeMapper.getLike(userId, modelId); if (Objects.nonNull(workFlowLike)) { if (Objects.equals(workFlowLike.getDelFlag(), "0")) { workFlowLike.setDelFlag("2"); int likeCount = workFlow.getLikeNum() - 1; likeCount = Math.max(likeCount, 0); workFlow.setLikeNum(likeCount); } else { workFlowLike.setDelFlag("0"); workFlow.setLikeNum(workFlow.getLikeNum() + 1); this.addLikeAdvice(workFlow, userId); } // 更新点赞记录 workFlowLikeMapper.updateStatus(workFlowLike); // 更新图片点赞数 workFlowMapper.updateById(workFlow); return; } // 添加点赞记录 workFlowLike = new WorkFlowLike(); workFlowLike.setUserId(userId); workFlowLike.setWorkFlowId(modelId); workFlowLikeMapper.insert(workFlowLike); // 更新图片点赞数 workFlow.setLikeNum(workFlow.getLikeNum() + 1); workFlowMapper.updateById(workFlow); this.addLikeAdvice(workFlow, userId); } private void addLikeAdvice(WorkFlow workFlow, Long userId) { Long receiverUserId = workFlow.getUserId(); if (Objects.equals(userId, receiverUserId)) { return; } SysUser sysUser = sysUserService.selectUserById(userId); String content = StringUtils.format("恭喜!{}点赞了您的工作流:{}", sysUser.getNickName(), workFlow.getWorkflowName()); SysAdvice sysAdvice = new SysAdvice(); sysAdvice.setSenderId(userId); sysAdvice.setReceiverId(receiverUserId); sysAdvice.setContent(content); sysAdvice.setProductId(workFlow.getId()); sysAdvice.setProductType(1); sysAdvice.setIsRead(0); sysAdvice.setType(AdviceEnum.LIKE_REMIND); sysAdviceService.save(sysAdvice); } private void workFlowCommentLike(Long userId, Long commentId) { WorkFlowComment workFlowComment = workFlowCommentMapper.selectById(commentId); if (Objects.isNull(workFlowComment)) { throw new ServiceException("该评论不存在"); } WorkFlowCommentLike workFlowCommentLike = workFlowCommentLikeMapper.getLikeComment(userId, commentId); if (Objects.nonNull(workFlowCommentLike)) { if (Objects.equals(workFlowCommentLike.getDelFlag(), "0")) { workFlowCommentLike.setDelFlag("2"); int likeNum = workFlowComment.getLikeNum() - 1; likeNum = Math.max(likeNum, 0); workFlowComment.setLikeNum(likeNum); } else { workFlowCommentLike.setDelFlag("0"); workFlowComment.setLikeNum(workFlowComment.getLikeNum() + 1); this.addLikeAdvice(workFlowComment, userId); } // 更新点赞记录 workFlowCommentLikeMapper.updateDelFlagById(workFlowCommentLike); // 更新图片评论点赞数 workFlowCommentMapper.updateById(workFlowComment); return; } // 添加点赞记录 workFlowCommentLike = new WorkFlowCommentLike(); workFlowCommentLike.setUserId(userId); workFlowCommentLike.setWorkFlowCommentId(commentId); workFlowCommentLikeMapper.insert(workFlowCommentLike); // 更新模型点赞数 workFlowComment.setLikeNum(workFlowComment.getLikeNum() + 1); workFlowCommentMapper.updateById(workFlowComment); this.addLikeAdvice(workFlowComment, userId); } private void addLikeAdvice(WorkFlowComment workFlowComment, Long userId) { Long receiverUserId = workFlowComment.getUserId(); if (Objects.equals(userId, receiverUserId)) { return; } SysUser sysUser = sysUserService.selectUserById(userId); String content = StringUtils.format("恭喜!{}点赞了您的评论", sysUser.getNickName()); SysAdvice sysAdvice = new SysAdvice(); sysAdvice.setSenderId(userId); sysAdvice.setReceiverId(receiverUserId); sysAdvice.setContent(content); sysAdvice.setProductId(workFlowComment.getWorkFlowId()); sysAdvice.setProductType(1); sysAdvice.setCommentId(workFlowComment.getId()); sysAdvice.setIsRead(0); sysAdvice.setType(AdviceEnum.LIKE_REMIND); sysAdviceService.save(sysAdvice); } }