src/main/java/com/mzl/flower/constant/Constants.java
@@ -10,6 +10,8 @@ public static final String DEFAULT_PASSWORD = "1234562"; public static final String DEFAULT_MEMBER_ID = "1"; //默认会员等级 /** * 审核结果 src/main/java/com/mzl/flower/dto/request/menber/MemberGrowthRecordDTO.java
@@ -3,6 +3,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Date; @@ -13,6 +14,9 @@ @ApiModelProperty("记录日期") private Date recordDate; @ApiModelProperty("订单金额") private BigDecimal totalAmount; @ApiModelProperty("成长值") private int growth; @@ -31,4 +35,5 @@ @ApiModelProperty("备注") private String remarks; } src/main/java/com/mzl/flower/service/menber/MemberGrowthRecordService.java
@@ -7,15 +7,18 @@ import com.mzl.flower.dto.request.menber.MemberRecordQueryDTO; import com.mzl.flower.dto.request.menber.UserGrowthRecordDTO; import com.mzl.flower.dto.response.member.MemberGrowthRecordVO; import com.mzl.flower.entity.menber.Member; import com.mzl.flower.entity.menber.MemberGrowthRecord; public interface MemberGrowthRecordService extends IService<MemberGrowthRecord> { void saveMemberGrowthRecord(MemberGrowthRecord memberGrowthRecord); void saveMemberGrowthRecord(MemberGrowthRecordDTO memberGrowthRecordDTO); void updateMemberGrowthRecord(MemberGrowthRecordDTO memberGrowthRecordDTO); void deleteMemberGrowthRecord(String id); UserGrowthRecordDTO getInfoByUserId(String userId); Page<MemberGrowthRecordVO> queryPage(MemberRecordQueryDTO memberRecordQueryDTO, Page page); Member getMemberByUserId(String userId); } 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("成长记录信息不存在"); src/main/java/com/mzl/flower/service/menber/impl/MemberServiceImpl.java
@@ -17,6 +17,7 @@ import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.util.List; /** @@ -39,9 +40,23 @@ throw new ValidationException("会员等级名称不能为空"); } if (member.getStartPoint()>member.getEndPoint()) { if (member.getStartPoint() > member.getEndPoint()) { throw new ValidationException("成长点开始不能大于结束"); } int discountRatio1 = member.getDiscountRatio().compareTo(BigDecimal.ZERO); if (discountRatio1 == -1) { throw new ValidationException("会员折扣百分比不能小于0"); } int discountRatio2 = member.getDiscountRatio().compareTo(new BigDecimal(100)); if (discountRatio2 == 1) { throw new ValidationException("会员折扣百分比不能大于100"); } int discountAmount = member.getDiscountAmount().compareTo(BigDecimal.ZERO); if (discountAmount == -1) { throw new ValidationException("会员折扣固定金额不能小于0"); } //保存时判断是否有重复的名称 Member memberByName = memberMapper.getMemberByName(member.getName()); if (!ObjectUtils.isEmpty(memberByName)) { src/main/java/com/mzl/flower/web/member/MemberController.java
@@ -9,7 +9,6 @@ import com.mzl.flower.dto.response.member.MemberGrowthRecordVO; import com.mzl.flower.dto.response.member.MemberVO; import com.mzl.flower.entity.menber.Member; import com.mzl.flower.entity.menber.MemberGrowthRecord; import com.mzl.flower.service.menber.MemberGrowthRecordService; import com.mzl.flower.service.menber.MemberService; import io.swagger.annotations.Api; @@ -86,10 +85,7 @@ @PostMapping(value = "/memberGrowthRecord/new") @ApiOperation(value = "保存会员记录", httpMethod = "POST") public ResponseEntity insertGrowthRecord(@RequestBody MemberGrowthRecordDTO memberGrowthRecordDTO) { MemberGrowthRecord memberGrowthRecord = new MemberGrowthRecord(); BeanUtils.copyProperties(memberGrowthRecordDTO, memberGrowthRecord); memberGrowthRecord.create(SecurityUtils.getUserId()); memberGrowthRecordService.saveMemberGrowthRecord(memberGrowthRecord); memberGrowthRecordService.saveMemberGrowthRecord(memberGrowthRecordDTO); return returnData(R.SUCCESS.getCode(), null); }