diff --git a/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java b/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java index 294ab48..339ecb5 100644 --- a/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java +++ b/src/main/java/com/greenorange/promotion/service/user/impl/UserInfoServiceImpl.java @@ -234,6 +234,13 @@ public class UserInfoServiceImpl extends ServiceImpl @Override public String getVerificationCode(String phoneNumber) { ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phoneNumber), ErrorCode.PARAMS_ERROR, "手机号格式错误"); + + // 判断手机号是否已注册 + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.eq(UserInfo::getPhoneNumber, phoneNumber); + UserInfo userInfo = this.getOne(lambdaQueryWrapper); + ThrowUtils.throwIf(userInfo != null, ErrorCode.OPERATION_ERROR, "手机号未注册"); + String verificationCode = SendSmsUtil.getVerificationCode(phoneNumber); ThrowUtils.throwIf(verificationCode == null, ErrorCode.OPERATION_ERROR, "验证码获取失败"); redisTemplate.opsForValue().set(SystemConstant.VERIFICATION_CODE + ":" + verificationCode, verificationCode, 5, TimeUnit.MINUTES); diff --git a/src/test/java/com/greenorange/promotion/draft/PricingTest.java b/src/test/java/com/greenorange/promotion/draft/PricingTest.java new file mode 100644 index 0000000..d5760f9 --- /dev/null +++ b/src/test/java/com/greenorange/promotion/draft/PricingTest.java @@ -0,0 +1,84 @@ +package com.greenorange.promotion.draft; + +// 价格接口 +interface Price { + double getPrice(double price); // 获取最终报价 +} + +// 策略抽象类 +abstract class Strategy { + protected Price priceStrategy; + + public Strategy(Price priceStrategy) { + this.priceStrategy = priceStrategy; + } + + public abstract double calculatePrice(double price); +} + +// 普通用户(全价) +class NormalCustomer extends Strategy { + public NormalCustomer() { + super(price -> price); // 全价策略 + } + + @Override + public double calculatePrice(double price) { + return priceStrategy.getPrice(price); // 返回全价 + } +} + +// 老客户(5%折扣) +class OldCustomer extends Strategy { + public OldCustomer() { + super(price -> price * 0.95); // 5%折扣 + } + + @Override + public double calculatePrice(double price) { + return priceStrategy.getPrice(price); // 应用折扣 + } +} + +class BigCustomer extends Strategy { + public BigCustomer() { + super(price -> price * 0.90); // 10%折扣 + } + + @Override + public double calculatePrice(double price) { + return priceStrategy.getPrice(price); // 应用折扣 + } +} + +// 报价管理类 +class PricingManager { + private Strategy strategy; + + // 设置不同客户的策略 + public void setStrategy(Strategy strategy) { + this.strategy = strategy; + } + + // 计算报价 + public double getFinalPrice(double price) { + return strategy.calculatePrice(price); + } +} + +// 测试类 +public class PricingTest { + public static void main(String[] args) { + PricingManager pricingManager = new PricingManager(); + + // 测试不同客户的价格 + pricingManager.setStrategy(new NormalCustomer()); // 普通用户 + System.out.println("普通用户报价: " + pricingManager.getFinalPrice(1000)); + + pricingManager.setStrategy(new OldCustomer()); // 老客户 + System.out.println("老客户报价: " + pricingManager.getFinalPrice(1000)); + + pricingManager.setStrategy(new BigCustomer()); // 大客户 + System.out.println("大客户报价: " + pricingManager.getFinalPrice(1000)); + } +} diff --git a/src/test/java/com/greenorange/promotion/light/CommandPatternExample.java b/src/test/java/com/greenorange/promotion/light/CommandPatternExample.java new file mode 100644 index 0000000..8e53dce --- /dev/null +++ b/src/test/java/com/greenorange/promotion/light/CommandPatternExample.java @@ -0,0 +1,94 @@ +package com.greenorange.promotion.light; + +public class CommandPatternExample { + + // 命令接口 + public interface Command { + void execute(); + } + + // 接收者类:开灯 + public static class Light { + public void on() { + System.out.println("灯已开启"); + } + + public void off() { + System.out.println("灯已关闭"); + } + } + + // 接收者类:开电视 + public static class TV { + public void on() { + System.out.println("电视已开启"); + } + + public void off() { + System.out.println("电视已关闭"); + } + } + + // 具体命令类:开灯命令 + public static class LightOnCommand implements Command { + private Light light; + + public LightOnCommand(Light light) { + this.light = light; + } + + @Override + public void execute() { + light.on(); // 执行开灯操作 + } + } + + // 具体命令类:开电视命令 + public static class TVOnCommand implements Command { + private TV tv; + + public TVOnCommand(TV tv) { + this.tv = tv; + } + + @Override + public void execute() { + tv.on(); // 执行开电视操作 + } + } + + // 调用者类:遥控器 + public static class RemoteControl { + private Command command; + + public void setCommand(Command command) { + this.command = command; + } + + public void pressButton() { + command.execute(); // 执行命令 + } + } + + // 测试类:命令模式的客户端 + public static void main(String[] args) { + // 创建接收者 + Light light = new Light(); + TV tv = new TV(); + + // 创建命令 + Command lightOn = new LightOnCommand(light); + Command tvOn = new TVOnCommand(tv); + + // 创建遥控器 + RemoteControl remote = new RemoteControl(); + + // 开灯 + remote.setCommand(lightOn); + remote.pressButton(); // 输出: 灯已开启 + + // 开电视 + remote.setCommand(tvOn); + remote.pressButton(); // 输出: 电视已开启 + } +}