|
@@ -0,0 +1,102 @@
|
|
|
|
|
+package com.sf.test;
|
|
|
|
|
+
|
|
|
|
|
+import com.sf.dao.*;
|
|
|
|
|
+import com.sf.dao.impl.*;
|
|
|
|
|
+import com.sf.domain.*;
|
|
|
|
|
+import com.sun.org.apache.xpath.internal.operations.Or;
|
|
|
|
|
+
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.util.Scanner;
|
|
|
|
|
+
|
|
|
|
|
+public class ShopTest {
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ // 先创建Scanner 录入账号和密码
|
|
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
|
|
+ // 根据输入账号和密码上数据库当中查询数据
|
|
|
|
|
+ IUserDAO userDAO = new UserDAOImpl();
|
|
|
|
|
+ User user;
|
|
|
|
|
+ while (true){
|
|
|
|
|
+ System.out.println("请输入账号");
|
|
|
|
|
+ String username = scanner.next();
|
|
|
|
|
+ System.out.println("请输入密码");
|
|
|
|
|
+ String password = scanner.next();
|
|
|
|
|
+ user = userDAO.login(username, password);
|
|
|
|
|
+ if(user !=null){
|
|
|
|
|
+ System.out.println("登录成功");
|
|
|
|
|
+ break;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ System.out.println("登录失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ System.out.println("请输入购买商品id");
|
|
|
|
|
+ // 用户输入的商品id
|
|
|
|
|
+ long inputProductId = scanner.nextLong();
|
|
|
|
|
+ System.out.println("请输入购买商品数量");
|
|
|
|
|
+ // 用户输入的库存
|
|
|
|
|
+ int inputStock = scanner.nextInt();
|
|
|
|
|
+ IProductDAO productDAO = new ProductDAOImpl();
|
|
|
|
|
+ // 根据商品id 查询商品信息
|
|
|
|
|
+ Product product = productDAO.get(inputProductId);
|
|
|
|
|
+ // 判断商品是否存在
|
|
|
|
|
+ if(product == null){
|
|
|
|
|
+ System.out.println("商品不存在");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 判断商品库存是否足够
|
|
|
|
|
+ if(product.getStocket() < inputStock){
|
|
|
|
|
+ System.out.println("商品库存不足");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 根据用户id 查询用户余额信息表 account 表
|
|
|
|
|
+ IAccountDAO accountDAO = new IAccountDAOImpl();
|
|
|
|
|
+ // 根据当前登录用户id查询账户信息
|
|
|
|
|
+ Account account = accountDAO.get(user.getId());
|
|
|
|
|
+ System.out.println("账户还剩:"+ account.getAmount()+"元");
|
|
|
|
|
+ // 判断账户余额是否足够
|
|
|
|
|
+ int totalPrice = product.getPrice().intValue() * inputStock;
|
|
|
|
|
+ System.out.println("此次一共需要花费:"+ totalPrice+"元");
|
|
|
|
|
+ // bigdecmial compare bigdecmail-> int 比较
|
|
|
|
|
+ if(account.getAmount().intValue() < totalPrice){
|
|
|
|
|
+ System.out.println("账户余额不足");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 如果走到这里说用户账户余额是充足可以进行扣减
|
|
|
|
|
+ // 根据用户id 减少余额
|
|
|
|
|
+ accountDAO.updateAmountByUserId(user.getId(),new BigDecimal(totalPrice));
|
|
|
|
|
+ System.out.println("账号余额已更新");
|
|
|
|
|
+ // 保存订单信息
|
|
|
|
|
+ IOrderDAO orderDAO = new OrderDAOImpl();
|
|
|
|
|
+ // 封装订单信息
|
|
|
|
|
+ Order order = new Order();
|
|
|
|
|
+ order.setOrderName(product.getProductName());
|
|
|
|
|
+ order.setTitle(product.getTitle());
|
|
|
|
|
+ order.setUserId(user.getId());
|
|
|
|
|
+ order.setBuyCount(inputStock);
|
|
|
|
|
+ order.setPrice(new BigDecimal(totalPrice));
|
|
|
|
|
+ // 获取当前系统时间的毫秒值
|
|
|
|
|
+ long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
|
+ // 用毫秒值创建 java.sql.Date
|
|
|
|
|
+ java.sql.Date sqlDate = new java.sql.Date(currentTimeMillis);
|
|
|
|
|
+ order.setCreatTime(sqlDate);
|
|
|
|
|
+ // 把订单信息保存到数据库中
|
|
|
|
|
+ orderDAO.insert(order);
|
|
|
|
|
+ System.out.println("订单保存成功");
|
|
|
|
|
+ // 减少product 表当中库存信息
|
|
|
|
|
+ productDAO.updateStockById(product.getId(),inputStock);
|
|
|
|
|
+ // 根据当前用户id 查询积分表中是否有积分信息
|
|
|
|
|
+ IntegralDAO integralDAO = new IntegralDAOImpl();
|
|
|
|
|
+ Integral integral = integralDAO.get(user.getId());
|
|
|
|
|
+ // 如果没有 就进行新增操作
|
|
|
|
|
+ if(integral == null){
|
|
|
|
|
+ integral = new Integral();
|
|
|
|
|
+ integral.setUserId(user.getId());
|
|
|
|
|
+ integral.setBalance(totalPrice/10);
|
|
|
|
|
+ integralDAO.add(integral);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ // 如果有的话我们进行更新的操作
|
|
|
|
|
+ integral.setBalance(integral.getBalance() + totalPrice/10);
|
|
|
|
|
+ integralDAO.update(integral);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|