cloudroam
2024-08-30 7eba8b9dc7090c4ed366afa365026947252c1086
src/main/java/com/mzl/flower/service/menber/impl/MemberGrowthRecordServiceImpl.java
@@ -1,13 +1,20 @@
package com.mzl.flower.service.menber.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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;
@@ -16,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;
@@ -37,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
@@ -89,4 +162,11 @@
        return userGrowthRecordDTO;
    }
    @Override
    public Page<MemberGrowthRecordVO> queryPage(MemberRecordQueryDTO memberRecordQueryDTO, Page page) {
        List<MemberGrowthRecordVO> list = memberGrowthRecordMapper.queryPage(memberRecordQueryDTO, page);
        page.setRecords(list);
        return page;
    }
}