From e16c0077d51cdf4a6fb2bb6000de4497bcdc30e3 Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期五, 30 八月 2024 14:54:23 +0800
Subject: [PATCH] add:成长值兑换的方法;会员等级计算的方法;等级同步到用户信息表;

---
 src/main/java/com/mzl/flower/service/menber/impl/MemberGrowthRecordServiceImpl.java |   75 +++++++++++++++++++++++++++++++++++++
 1 files changed, 74 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/menber/impl/MemberGrowthRecordServiceImpl.java b/src/main/java/com/mzl/flower/service/menber/impl/MemberGrowthRecordServiceImpl.java
index c418589..719d57a 100644
--- a/src/main/java/com/mzl/flower/service/menber/impl/MemberGrowthRecordServiceImpl.java
+++ b/src/main/java/com/mzl/flower/service/menber/impl/MemberGrowthRecordServiceImpl.java
@@ -4,13 +4,17 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.mzl.flower.config.exception.ValidationException;
 import com.mzl.flower.config.security.SecurityUtils;
+import com.mzl.flower.constant.Constants;
 import com.mzl.flower.dto.request.menber.MemberGrowthRecordDTO;
 import com.mzl.flower.dto.request.menber.MemberRecordQueryDTO;
 import com.mzl.flower.dto.request.menber.TargetMemberDTO;
 import com.mzl.flower.dto.request.menber.UserGrowthRecordDTO;
+import com.mzl.flower.dto.response.customer.CustomerDTO;
 import com.mzl.flower.dto.response.member.MemberGrowthRecordVO;
+import com.mzl.flower.entity.customer.Customer;
 import com.mzl.flower.entity.menber.Member;
 import com.mzl.flower.entity.menber.MemberGrowthRecord;
+import com.mzl.flower.mapper.customer.CustomerMapper;
 import com.mzl.flower.mapper.member.MemberGrowthRecordMapper;
 import com.mzl.flower.mapper.member.MemberMapper;
 import com.mzl.flower.service.menber.MemberGrowthRecordService;
@@ -19,7 +23,10 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
+import org.springframework.util.StringUtils;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -40,10 +47,73 @@
 
     private final MemberMapper memberMapper;
 
+    private final CustomerMapper customerMapper;
+
+
     @Override
-    public void saveMemberGrowthRecord(MemberGrowthRecord memberGrowthRecord) {
+    public void saveMemberGrowthRecord(MemberGrowthRecordDTO memberGrowthRecordDTO) {
+        if (StringUtils.isEmpty(memberGrowthRecordDTO.getUserId())) {
+            throw new ValidationException("用户ID不能为空");
+        }
+
+        MemberGrowthRecord memberGrowthRecord = new MemberGrowthRecord();
+        BeanUtils.copyProperties(memberGrowthRecordDTO, memberGrowthRecord);
+        memberGrowthRecord.create(SecurityUtils.getUserId());
         memberGrowthRecord.setRecordDate(new Date());
+
+        CustomerDTO customerDTO = customerMapper.getCurrentCustomer(memberGrowthRecord.getUserId());
+        if (ObjectUtils.isEmpty(customerDTO)) {
+            throw new ValidationException("人员信息为空");
+        }
+
+        Customer customer = customerMapper.selectById(customerDTO.getId());
+        if (ObjectUtils.isEmpty(customer)) {
+            throw new ValidationException("人员信息为空");
+        }
+        Member memberBefore = memberMapper.selectById(customer.getLevelId());
+        if(ObjectUtils.isEmpty(memberBefore)){
+            throw new ValidationException("人员对应的会员等级不存在,请联系管理员");
+        }
+
+        //保存会员记录逻辑:
+        if (Constants.GROWTH_SOURCE.consume.name().equals(memberGrowthRecordDTO.getSource())) {
+            //消费:成长值=消费金额/消费金额(元)*已消费产生的成长值
+            BigDecimal totalAmount = memberGrowthRecordDTO.getTotalAmount();
+            int consumptionAmount = memberBefore.getConsumptionAmount();
+            int growthValue = memberBefore.getGrowthValue();
+            BigDecimal actualGrowthValue = totalAmount.divide(new BigDecimal(consumptionAmount), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(growthValue));
+            int growth = actualGrowthValue.setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); // 四舍五入取整
+            memberGrowthRecord.setGrowth(growth);
+            memberGrowthRecord.setType(Constants.GROWTH_TYPE.add.name());
+        }
         memberGrowthRecordMapper.insert(memberGrowthRecord);
+
+        //会员等级同步逻辑:
+        Member memberAfter = getMemberByUserId(memberGrowthRecord.getUserId());
+        customer.setLevelId(memberAfter.getId());
+        customerMapper.updateById(customer);
+    }
+
+
+    /**
+     * 查询当前人员的成长值 最少是0
+     *
+     * @param userId
+     * @return
+     */
+    @Override
+    public Member getMemberByUserId(String userId) {
+        if (StringUtils.isEmpty(userId)) {
+            throw new ValidationException("用户ID不能为空");
+        }
+        Integer sumGrowth = memberGrowthRecordMapper.getSumGrowthByUsertId(userId);
+
+        //查询当前会员等级
+        Member member = memberMapper.getMemberByGrowthValue(sumGrowth);
+        if (ObjectUtils.isEmpty(member)) {
+            throw new ValidationException("会员信息为空");
+        }
+        return member;
     }
 
     @Override
@@ -59,6 +129,9 @@
 
     @Override
     public void deleteMemberGrowthRecord(String id) {
+        if(id.equals(Constants.DEFAULT_MEMBER_ID)){
+            throw new ValidationException("默认普通会员只能编辑,不能删除");
+        }
         MemberGrowthRecord memberGrowthRecord = memberGrowthRecordMapper.selectById(id);
         if (memberGrowthRecord == null) {
             throw new ValidationException("成长记录信息不存在");

--
Gitblit v1.9.3