|
|
@@ -0,0 +1,94 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.entity.GiftMembershipProgress;
|
|
|
+import com.etotem.cfc.mapper.GiftMembershipProgressMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class GiftMembershipProgressService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private GiftMembershipProgressMapper progressMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MembershipService membershipService;
|
|
|
+
|
|
|
+ /** 查询家庭累计进度(无记录则返回初始进度) */
|
|
|
+ public GiftMembershipProgress getOrCreate(Long familyId) {
|
|
|
+ if (familyId == null) return null;
|
|
|
+ GiftMembershipProgress progress = progressMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<GiftMembershipProgress>()
|
|
|
+ .eq(GiftMembershipProgress::getFamilyId, familyId));
|
|
|
+ if (progress != null) return progress;
|
|
|
+ GiftMembershipProgress np = new GiftMembershipProgress();
|
|
|
+ np.setFamilyId(familyId);
|
|
|
+ np.setAccumulatedAmount(0);
|
|
|
+ np.setGranted(0);
|
|
|
+ np.setCreatedAt(new Date());
|
|
|
+ np.setUpdatedAt(new Date());
|
|
|
+ try {
|
|
|
+ progressMapper.insert(np);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 并发插入唯一键冲突,回查
|
|
|
+ progress = progressMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<GiftMembershipProgress>()
|
|
|
+ .eq(GiftMembershipProgress::getFamilyId, familyId));
|
|
|
+ }
|
|
|
+ return progress != null ? progress : np;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 累计送会员商品金额,达到阈值且未赠送时自动赠送会员。
|
|
|
+ * 阈值与赠送等级从 sys_config 读取(gift_membership_threshold / gift_membership_level)。
|
|
|
+ *
|
|
|
+ * @return 本次是否触发了赠送
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean accumulate(Long familyId, Long buyerId, int amount) {
|
|
|
+ if (familyId == null || amount <= 0) return false;
|
|
|
+ GiftMembershipProgress progress = getOrCreate(familyId);
|
|
|
+ if (progress == null) return false;
|
|
|
+ if (progress.getGranted() != null && progress.getGranted() == 1) {
|
|
|
+ return false; // 已赠送过,不再重复
|
|
|
+ }
|
|
|
+ int newAmount = (progress.getAccumulatedAmount() != null ? progress.getAccumulatedAmount() : 0) + amount;
|
|
|
+ progress.setAccumulatedAmount(newAmount);
|
|
|
+ progress.setUpdatedAt(new Date());
|
|
|
+
|
|
|
+ String thresholdStr = sysConfigService.getValue("gift_membership_threshold");
|
|
|
+ int threshold;
|
|
|
+ try {
|
|
|
+ threshold = thresholdStr != null ? Integer.parseInt(thresholdStr) : 0;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ threshold = 0;
|
|
|
+ }
|
|
|
+ if (threshold <= 0 || newAmount < threshold) {
|
|
|
+ progressMapper.updateById(progress);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String levelCode = sysConfigService.getValue("gift_membership_level");
|
|
|
+ if (levelCode == null || levelCode.isEmpty()) {
|
|
|
+ levelCode = "FAMILY";
|
|
|
+ }
|
|
|
+ // 达到阈值:赠送会员并标记已赠送
|
|
|
+ boolean granted = membershipService.grantMembershipByGift(
|
|
|
+ familyId, buyerId, levelCode, null, null);
|
|
|
+ if (granted) {
|
|
|
+ progress.setGranted(1);
|
|
|
+ progress.setGrantedLevel(levelCode);
|
|
|
+ progress.setGrantedAt(new Date());
|
|
|
+ }
|
|
|
+ progressMapper.updateById(progress);
|
|
|
+ return granted;
|
|
|
+ }
|
|
|
+}
|