|  |  | 
 |  |  | 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.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.constant.Constants; | 
 |  |  | import com.mzl.flower.dto.request.menber.*; | 
 |  |  | import com.mzl.flower.dto.response.customer.CustomerDTO; | 
 |  |  | import com.mzl.flower.dto.response.member.MemberGrowthRecordVO; | 
 |  |  | import com.mzl.flower.dto.response.member.UserGrowthRecordVO; | 
 |  |  | 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.entity.payment.Order; | 
 |  |  | 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; | 
 |  |  | 
 |  |  | 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; | 
 |  |  | 
 |  |  |  | 
 |  |  |     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())) { | 
 |  |  |             //消费:成长值=消费金额/消费金额(元)*已消费产生的成长值C | 
 |  |  |             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 | 
 |  |  | 
 |  |  |         Integer sumGrowth = memberGrowthRecordMapper.getSumGrowthByUsertId(userId); | 
 |  |  |         userGrowthRecordDTO.setCurrentGrowthValue(sumGrowth); | 
 |  |  |         //查询当前会员等级 | 
 |  |  |         Member member = memberMapper.getMemberByGrowthValue(sumGrowth); | 
 |  |  |         Member member = new Member(); | 
 |  |  |         member = memberMapper.getMemberByGrowthValue(sumGrowth); | 
 |  |  |         if (ObjectUtils.isEmpty(member)) { | 
 |  |  |             member = memberMapper.selectById(Constants.DEFAULT_MEMBER_ID); | 
 |  |  |         } | 
 |  |  |         userGrowthRecordDTO.setCurrentMemberLevel(member.getName()); | 
 |  |  |         userGrowthRecordDTO.setCurrentDiscountType(member.getDiscountType()); | 
 |  |  |         switch (member.getDiscountType()) { | 
 |  |  |             case "ratio": | 
 |  |  |                 userGrowthRecordDTO.setCurrentDiscountTypeStr(Constants.DISCOUNT_TYPE.ratio.getDesc()); | 
 |  |  |                 break; | 
 |  |  |             case "amount": | 
 |  |  |                 userGrowthRecordDTO.setCurrentDiscountTypeStr(Constants.DISCOUNT_TYPE.amount.getDesc()); | 
 |  |  |                 break; | 
 |  |  |         } | 
 |  |  |         userGrowthRecordDTO.setCurrentDiscountRatio(StringUtils.isEmpty(member.getDiscountRatio()) ? BigDecimal.valueOf(100) : member.getDiscountRatio()); | 
 |  |  |         userGrowthRecordDTO.setCurrentDiscountAmount(StringUtils.isEmpty(member.getDiscountAmount()) ? BigDecimal.ZERO  : member.getDiscountAmount()); | 
 |  |  |         userGrowthRecordDTO.setCurrentGrowthValueDesc(member.getGrowthValueDesc()); | 
 |  |  |         //查询比当前等级高的会员等级信息 | 
 |  |  |         List<Member> memberList = memberMapper.getgtMembersByGrowthValue(sumGrowth); | 
 |  |  |         if (!CollectionUtils.isEmpty(memberList)) { | 
 |  |  | 
 |  |  |                 targetMemberDTO.setTargetMemberLevel(m.getName()); | 
 |  |  |                 targetMemberDTO.setTargetStartPoint(m.getStartPoint()); | 
 |  |  |                 targetMemberDTO.setTargetGap(m.getStartPoint() - sumGrowth); | 
 |  |  |                 targetMemberDTO.setTargetDiscountAmount(m.getDiscountAmount()); | 
 |  |  |                 targetMemberDTO.setTargetDiscountAmount(StringUtils.isEmpty(m.getDiscountAmount()) ? BigDecimal.ZERO : m.getDiscountAmount()); | 
 |  |  |                 targetMemberDTO.setTargetDiscountType(m.getDiscountType()); | 
 |  |  |                 switch (m.getDiscountType()) { | 
 |  |  |                     case "ratio": | 
 |  |  |                         targetMemberDTO.setTargetDiscountTypeStr(Constants.DISCOUNT_TYPE.ratio.getDesc()); | 
 |  |  |                         break; | 
 |  |  |                     case "amount": | 
 |  |  |                         targetMemberDTO.setTargetDiscountTypeStr(Constants.DISCOUNT_TYPE.amount.getDesc()); | 
 |  |  |                         break; | 
 |  |  |                 } | 
 |  |  |                 targetMemberDTO.setTargetDiscountRatio(StringUtils.isEmpty(m.getDiscountRatio()) ? BigDecimal.valueOf(100) : m.getDiscountRatio()); | 
 |  |  |                 targetMemberDTO.setTargetGrowthValueDesc(m.getGrowthValueDesc()); | 
 |  |  |                 targetMemberDTOList.add(targetMemberDTO); | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  | 
 |  |  |         page.setRecords(list); | 
 |  |  |         return page; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void growthValueDeduct(Order order) { | 
 |  |  |         //超过30天不到90天,每天处理当前会员等级的成长值 | 
 |  |  |         CustomerDTO customerDTO = customerMapper.getCurrentCustomer(order.getCreateBy()); | 
 |  |  |         Customer customer = customerMapper.selectById(customerDTO.getId()); | 
 |  |  |         Member member = memberMapper.selectById(customer.getLevelId()); | 
 |  |  |         if(ObjectUtils.isEmpty(member)){ | 
 |  |  |             throw new ValidationException("用户会员等级未维护"); | 
 |  |  |         } | 
 |  |  |         int deductGrowthValue = member.getDowngradeValue(); | 
 |  |  |         Integer sumGrowthByUserId = memberGrowthRecordMapper.getSumGrowthByUsertId(order.getCreateBy()); | 
 |  |  |  | 
 |  |  |         //当前成长值如果是等于0不需要走扣除逻辑 | 
 |  |  |         if (sumGrowthByUserId != 0) { | 
 |  |  |         int waitDeductGrowthValue = 0; | 
 |  |  |  | 
 |  |  |         //判断当前用户的成长值是够扣除,如果够扣除直接减去成长值,如果不够扣除则全部减去 | 
 |  |  |         if (sumGrowthByUserId - deductGrowthValue > 0) { | 
 |  |  |             waitDeductGrowthValue = deductGrowthValue; | 
 |  |  |         } else { | 
 |  |  |             waitDeductGrowthValue = sumGrowthByUserId; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //保存会员成长记录到记录表 | 
 |  |  |         MemberGrowthRecordDTO memberGrowthRecordDTO = new MemberGrowthRecordDTO(); | 
 |  |  |         memberGrowthRecordDTO.setUserId(order.getCreateBy()); | 
 |  |  |         memberGrowthRecordDTO.setRecordDate(new Date()); | 
 |  |  |         memberGrowthRecordDTO.setGrowth(-waitDeductGrowthValue); | 
 |  |  |         memberGrowthRecordDTO.setSource(Constants.GROWTH_SOURCE.downgrading.name()); | 
 |  |  |         memberGrowthRecordDTO.setType(Constants.GROWTH_TYPE.reduce.name()); | 
 |  |  |         memberGrowthRecordDTO.setRemarks("自动扣除"); | 
 |  |  |         saveMemberGrowthRecord(memberGrowthRecordDTO); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Page<UserGrowthRecordVO> queryUserPage(UserMemberRecordQueryDTO userMemberRecordQueryDTO, Page page) { | 
 |  |  |         List<UserGrowthRecordVO> list = memberGrowthRecordMapper.queryUserPage(userMemberRecordQueryDTO, page); | 
 |  |  |         page.setRecords(list); | 
 |  |  |         return page; | 
 |  |  |     } | 
 |  |  | } |