TCommentUserServiceImpl.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.ylx.massage.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.ylx.common.core.domain.model.WxLoginUser;
  6. import com.ylx.common.exception.ServiceException;
  7. import com.ylx.common.utils.StringUtils;
  8. import com.ylx.lottery.domain.LotteryCountLog;
  9. import com.ylx.lottery.domain.vo.LocalActivityTableVO;
  10. import com.ylx.lottery.domain.vo.LotteryActivityVO;
  11. import com.ylx.lottery.service.LotteryCountLogService;
  12. import com.ylx.lottery.service.LotteryCountService;
  13. import com.ylx.massage.domain.CommentUserAudit;
  14. import com.ylx.massage.domain.TCommentUser;
  15. import com.ylx.massage.domain.TOrder;
  16. import com.ylx.massage.enums.OrderStatusEnum;
  17. import com.ylx.massage.mapper.TCommentUserMapper;
  18. import com.ylx.massage.service.TCommentUserAuditService;
  19. import com.ylx.massage.service.TCommentUserService;
  20. import com.ylx.massage.service.TOrderService;
  21. import com.ylx.usercenter.domain.dto.UnifiedUserCenterDTO;
  22. import com.ylx.usercenter.service.UnifiedUserCenterService;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.transaction.support.TransactionSynchronization;
  27. import org.springframework.transaction.support.TransactionSynchronizationManager;
  28. import javax.annotation.Resource;
  29. import java.util.Date;
  30. import java.util.List;
  31. /**
  32. * 用户评论表(TCommentUser)表服务实现类
  33. *
  34. * @author makejava
  35. * @since 2024-08-08 10:32:08
  36. */
  37. @Slf4j
  38. @Service("tCommentUserService")
  39. public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCommentUser> implements TCommentUserService {
  40. @Resource
  41. private TOrderService orderService;
  42. @Resource
  43. private TCommentUserAuditService commentUserAuditService;
  44. @Resource
  45. private TCommentUserMapper tCommentUserMapper;
  46. @Resource
  47. private LotteryCountService lotteryCountService;
  48. @Resource
  49. private LotteryCountLogService lotteryCountLogService;
  50. @Resource
  51. private UnifiedUserCenterService unifiedUserCenterService;
  52. @Override
  53. @Transactional(rollbackFor = Exception.class)
  54. public Boolean saveComment(TCommentUser comment, WxLoginUser wxLoginUser) {
  55. //订单ID不能为空
  56. if (StringUtils.isBlank(comment.getOrderId())) {
  57. throw new ServiceException("订单id不能为空");
  58. }
  59. //判断订单是否存在
  60. TOrder order1 = orderService.getById(comment.getOrderId());
  61. if (order1 == null) {
  62. throw new ServiceException("订单不存在");
  63. }
  64. if (StringUtils.isBlank(comment.getOrderNo())) {
  65. throw new ServiceException("订单号不能为空");
  66. }
  67. if (StringUtils.isBlank(comment.getText())) {
  68. throw new ServiceException("评论内容不能空");
  69. }
  70. if (StringUtils.isBlank(comment.getJsId())) {
  71. throw new ServiceException("技师id不能空");
  72. }
  73. if (StringUtils.isBlank(comment.getName())) {
  74. throw new ServiceException("技师姓名不能空");
  75. }
  76. comment.setOpenId(wxLoginUser.getCOpenid());
  77. //comment.setOpenId("o-HEJ6RAjBDjFvuZcAdHRX8mIZXw");
  78. comment.setCommentTime(new Date());
  79. this.save(comment);
  80. //向用户评论审核表中插入数据
  81. CommentUserAudit commentUserAudit = new CommentUserAudit();
  82. commentUserAudit.setCommentId(comment.getId());
  83. commentUserAudit.setOrderNo(comment.getOrderNo());
  84. commentUserAudit.setText(comment.getText());
  85. //默认审核状态为待审核
  86. commentUserAudit.setAuditStatus(0);
  87. commentUserAudit.setCreateTime(new Date());
  88. commentUserAudit.setUpdateTime(new Date());
  89. commentUserAudit.setIsDelete(0);
  90. commentUserAuditService.save(commentUserAudit);
  91. TOrder order = new TOrder();
  92. //修改订单状态(已完成)
  93. order.setnStatus(OrderStatusEnum.COMPLETE.getCode());
  94. order.setcId(comment.getOrderId());
  95. boolean updateOrderSuccess = orderService.updateById(order);
  96. if (!updateOrderSuccess) {
  97. throw new ServiceException("订单状态更新失败");
  98. }
  99. // 评价有礼 —— 发放抽奖次数日志
  100. grantLotteryCountForComment(wxLoginUser);
  101. // 已绑定一账通 → 异步同步抽奖次数
  102. syncLotteryCountIfNeeded(wxLoginUser);
  103. return true;
  104. }
  105. @Override
  106. public List<TCommentUser> selectAll(TCommentUser tCommentUser) {
  107. return tCommentUserMapper.selectAll(tCommentUser);
  108. }
  109. /**
  110. * 评价有礼发放抽奖次数
  111. */
  112. private void grantLotteryCountForComment(WxLoginUser wxLoginUser) {
  113. List<LotteryActivityVO> activityList = lotteryCountService.queryActivityRules();
  114. if (CollUtil.isEmpty(activityList)) {
  115. return;
  116. }
  117. for (LotteryActivityVO activity : activityList) {
  118. // 处理积分表
  119. List<LocalActivityTableVO> pointTables = activity.getGyyPointTables();
  120. if (CollUtil.isNotEmpty(pointTables)) {
  121. for (LocalActivityTableVO table : pointTables) {
  122. saveLotteryLog(wxLoginUser, table, table.getPointNumber());
  123. }
  124. continue;
  125. }
  126. // 非积分表、任务奖励类型(参与规则=3)
  127. if (ObjectUtil.notEqual(activity.getParticipationRules(), 3)) {
  128. continue;
  129. }
  130. List<LocalActivityTableVO> localTables = activity.getLocalActivityTables();
  131. if (CollUtil.isEmpty(localTables)) {
  132. continue;
  133. }
  134. // 类型=4 → 评价有礼
  135. for (LocalActivityTableVO table : localTables) {
  136. if (ObjectUtil.equals(table.getType(), 4)) {
  137. saveLotteryLog(wxLoginUser, table, 1);
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * 统一保存抽奖次数日志
  144. */
  145. private void saveLotteryLog(WxLoginUser wxLoginUser, LocalActivityTableVO table, Integer lotteryNum) {
  146. LotteryCountLog log = new LotteryCountLog();
  147. log.setOpenId(wxLoginUser.getCOpenid());
  148. log.setUserId(wxLoginUser.getId());
  149. log.setUserPhone(wxLoginUser.getCPhone());
  150. log.setActivityType(2); // 任务奖励
  151. log.setLocalActivityTableId(table.getId());
  152. log.setLotteryNum(lotteryNum);
  153. log.setReceiveTime(new Date());
  154. log.setIsDelete("0");
  155. log.setStatus(0); // 未同步
  156. log.setIsLottery(0);
  157. log.setType(3);
  158. lotteryCountLogService.save(log);
  159. }
  160. private void syncLotteryCountIfNeeded(WxLoginUser wxLoginUser) {
  161. if (ObjectUtil.equals(wxLoginUser.getIsBind(), 1)
  162. && StringUtils.isNotBlank(wxLoginUser.getLocalLiveUserId())) {
  163. UnifiedUserCenterDTO dto = new UnifiedUserCenterDTO();
  164. dto.setTargetUserId(wxLoginUser.getLocalLiveUserId());
  165. dto.setSourceUserId(wxLoginUser.getId());
  166. // 👇 事务提交后再执行异步
  167. TransactionSynchronizationManager.registerSynchronization(
  168. new TransactionSynchronization() {
  169. @Override
  170. public void afterCommit() {
  171. // 事务提交完成!现在调用异步,一定能查到数据
  172. unifiedUserCenterService.syncLotteryCount(dto);
  173. }
  174. }
  175. );
  176. }
  177. }
  178. }