Skip to content

Commit edc1055

Browse files
committed
update
1 parent c92e510 commit edc1055

File tree

17 files changed

+613
-25
lines changed

17 files changed

+613
-25
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.atguigu.dao;
2+
3+
import com.atguigu.pojo.Order;
4+
5+
import java.util.List;
6+
7+
public interface OrderDao {
8+
9+
public int saveOrder(Order order);//用户 保存订单
10+
public List<Order> queryOrders();//管理员 查询全部订单
11+
public int changeOrderStatus(String orderId,Integer status);//管理员 根据订单号 改变订单状态
12+
public List<Order> queryOrdersByUserId(Integer userId);//根据 用户编号 查询其订单信息
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.atguigu.dao;
2+
3+
import com.atguigu.pojo.OrderItem;
4+
5+
import java.util.List;
6+
7+
public interface OrderItemDao {
8+
9+
public int saveOrderItem(OrderItem orderItem);//用户 保存订单里的 商品项信息
10+
public List<OrderItem> queryOrderItemByOrderId(String orderId);//用户或者管理员 根据订单号查询 订单里所有商品项信息
11+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.atguigu.dao.impl;
2+
3+
import com.atguigu.dao.OrderDao;
4+
import com.atguigu.pojo.Order;
5+
6+
import java.util.List;
7+
8+
public class OrderDaoImpl extends BaseDao implements OrderDao {
9+
10+
/** 保存订单(往数据库添加一条订单)
11+
*
12+
* @param order
13+
* @return
14+
*/
15+
@Override
16+
public int saveOrder(Order order) {
17+
String sql="insert into t_order(`order_id`,`create_time`,`price`,`status`,`user_id`)values(?,?,?,?,?);";
18+
return update(sql,order.getOrderId(),order.getCreateTime(),order.getPrice(),order.getStatus(),order.getUserId());
19+
}
20+
21+
/** 管理员 查看所有 订单
22+
*
23+
* @return
24+
*/
25+
@Override
26+
public List<Order> queryOrders() {
27+
String sql="select `order_id` as `orderId`,`create_time` as `createTime`,`price` ,`status`,`user_id` as `userId` from t_order;";
28+
return queryForList(Order.class,sql);
29+
}
30+
31+
/** 管理员 改变订单状态 (0 未发货 , 1 已发货 , 2 已签收)
32+
*
33+
* @param orderId
34+
* @param status
35+
* @return
36+
*/
37+
@Override
38+
public int changeOrderStatus(String orderId, Integer status) {
39+
String sql="update t_order set status=? where order_id=?";
40+
return update(sql,status,orderId);
41+
}
42+
43+
/** 根据用户编号 查询 用户的订单信息
44+
*
45+
* @param userId
46+
* @return
47+
*/
48+
@Override
49+
public List<Order> queryOrdersByUserId(Integer userId) {
50+
String sql="select `order_id` as `orderId`,`create_time` as `createTime`,`price` ,`status` from t_order where user_id=?";
51+
return queryForList(Order.class,sql,userId);
52+
}
53+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.atguigu.dao.impl;
2+
3+
import com.atguigu.dao.OrderItemDao;
4+
import com.atguigu.pojo.OrderItem;
5+
6+
import java.util.List;
7+
8+
public class OrderItemDaoImpl extends BaseDao implements OrderItemDao {
9+
10+
/** 保存订单里的 商品项信息 (将订单里的商品项插入进数据库保存起来),1对多的关系
11+
*
12+
* @param orderItem
13+
* @return
14+
*/
15+
@Override
16+
public int saveOrderItem(OrderItem orderItem) {
17+
String sql ="insert into t_order_item(`name`,`count`,`price`,`total_price` ,`order_id` )values(?,?,?,?,?) ";
18+
19+
return update(sql,
20+
orderItem.getName(),orderItem.getCount(),orderItem.getPrice(),orderItem.getTotalPrice(),orderItem.getOrderId());
21+
}
22+
23+
/** 用户或者管理员 根据订单号查询 订单里所有商品项信息
24+
*
25+
* @param orderId
26+
* @return
27+
*/
28+
@Override
29+
public List<OrderItem> queryOrderItemByOrderId(String orderId) {
30+
String sql="select id,`name`,`count`,`price`,`total_price` as `totalPrice` from t_order_item where order_Id=?";
31+
return queryForList(OrderItem.class,sql,orderId);
32+
}
33+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.atguigu.pojo;
2+
3+
import java.math.BigDecimal;
4+
import java.sql.Timestamp;
5+
import java.util.Date;
6+
7+
public class Order {
8+
9+
private String orderId;
10+
private Timestamp createTime;// mysql中的datetime 对应 java中的TimeStamp
11+
private BigDecimal price;
12+
// 0 表示未发货,1 表示已发货,2 表示已签收
13+
private Integer status=0;
14+
private Integer userId;
15+
16+
public Order() {
17+
}
18+
19+
public Order(String orderId, Timestamp createTime, BigDecimal price, Integer status, Integer userId) {
20+
this.orderId = orderId;
21+
this.createTime = createTime;
22+
this.price = price;
23+
this.status = status;
24+
this.userId = userId;
25+
}
26+
27+
public String getOrderId() {
28+
return orderId;
29+
}
30+
31+
public void setOrderId(String orderId) {
32+
this.orderId = orderId;
33+
}
34+
35+
public Timestamp getCreateTime() {
36+
return createTime;
37+
}
38+
39+
public void setCreateTime(Timestamp createTime) {
40+
this.createTime = createTime;
41+
}
42+
43+
public BigDecimal getPrice() {
44+
return price;
45+
}
46+
47+
public void setPrice(BigDecimal price) {
48+
this.price = price;
49+
}
50+
51+
public Integer getStatus() {
52+
return status;
53+
}
54+
55+
public void setStatus(Integer status) {
56+
this.status = status;
57+
}
58+
59+
public Integer getUserId() {
60+
return userId;
61+
}
62+
63+
public void setUserId(Integer userId) {
64+
this.userId = userId;
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return "Order{" +
70+
"orderId='" + orderId + '\'' +
71+
", createTime=" + createTime +
72+
", price=" + price +
73+
", status=" + status +
74+
", userId=" + userId +
75+
'}';
76+
}
77+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.atguigu.pojo;
2+
3+
import java.math.BigDecimal;
4+
5+
public class OrderItem {
6+
7+
private Integer id;
8+
private String name;
9+
private Integer count;
10+
private BigDecimal price;
11+
private BigDecimal totalPrice;
12+
private String orderId;
13+
14+
public OrderItem() {
15+
}
16+
17+
public OrderItem(Integer id, String name, Integer count, BigDecimal price, BigDecimal totalPrice, String orderId) {
18+
this.id = id;
19+
this.name = name;
20+
this.count = count;
21+
this.price = price;
22+
this.totalPrice = totalPrice;
23+
this.orderId = orderId;
24+
}
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public Integer getCount() {
43+
return count;
44+
}
45+
46+
public void setCount(Integer count) {
47+
this.count = count;
48+
}
49+
50+
public BigDecimal getPrice() {
51+
return price;
52+
}
53+
54+
public void setPrice(BigDecimal price) {
55+
this.price = price;
56+
}
57+
58+
public BigDecimal getTotalPrice() {
59+
return totalPrice;
60+
}
61+
62+
public void setTotalPrice(BigDecimal totalPrice) {
63+
this.totalPrice = totalPrice;
64+
}
65+
66+
public String getOrderId() {
67+
return orderId;
68+
}
69+
70+
public void setOrderId(String orderId) {
71+
this.orderId = orderId;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return "OrderItem{" +
77+
"id=" + id +
78+
", name='" + name + '\'' +
79+
", count=" + count +
80+
", price=" + price +
81+
", totalPrice=" + totalPrice +
82+
", orderId='" + orderId + '\'' +
83+
'}';
84+
}
85+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.atguigu.service;
2+
3+
import com.atguigu.pojo.Cart;
4+
import com.atguigu.pojo.Order;
5+
import com.atguigu.pojo.OrderItem;
6+
7+
import java.util.List;
8+
9+
public interface OrderService {
10+
public String createOrder(Cart cart,Integer userId);//生成订单 = 保存order订单 + 保存orderItem订单项
11+
public List<Order> showAllOrders();//展示所有订单 -> queryOrders()
12+
public void sendOrder(String orderId);//发货
13+
public List<OrderItem> showOrderDetail(String orderId);//管理员或用户 查看订单详情
14+
public List<Order> showMyOrder(Integer userId);//用户 查看我的订单
15+
public void receiveOrder(String orderId);//签收
16+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.atguigu.service.impl;
2+
3+
import com.atguigu.dao.BookDao;
4+
import com.atguigu.dao.OrderDao;
5+
import com.atguigu.dao.OrderItemDao;
6+
import com.atguigu.dao.impl.BookDaoImpl;
7+
import com.atguigu.dao.impl.OrderDaoImpl;
8+
import com.atguigu.dao.impl.OrderItemDaoImpl;
9+
import com.atguigu.pojo.*;
10+
import com.atguigu.service.OrderService;
11+
12+
import java.sql.Timestamp;
13+
import java.util.Date;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class OrderServiceImpl implements OrderService {
18+
19+
private OrderDao orderDao=new OrderDaoImpl();
20+
private OrderItemDao orderItemDao =new OrderItemDaoImpl();
21+
private BookDao bookDao=new BookDaoImpl();
22+
23+
/** 生成订单 = 保存order订单 + 保存orderItem订单项
24+
*
25+
* @param cart
26+
* @param userId
27+
* @return 返回orderId ,因为orderId要唯一 ,不返回没人知道
28+
*/
29+
@Override
30+
public String createOrder(Cart cart, Integer userId) {
31+
//订单号 要唯一 ,使用时间戳加用户id 就实现唯一性
32+
String orderId=System.currentTimeMillis()+""+userId;
33+
//创建订单对象
34+
Order order=new Order(orderId,new Timestamp(new Date().getTime()),cart.getTotalPrice(),0,userId);
35+
//保存订单
36+
orderDao.saveOrder(order);
37+
38+
//遍历购物车中每一个商品项,转换成订单项保存到数据库
39+
for(Map.Entry<Integer, CartItem> entry:cart.getItems().entrySet()){
40+
//取出每一个商品项
41+
CartItem cartItem=entry.getValue();
42+
//将商品项转为订单项
43+
OrderItem orderItem= new OrderItem(
44+
null,cartItem.getName(),cartItem.getCount(),cartItem.getPrice(),cartItem.getTotalPrice(), orderId);
45+
//保存订单项到数据库
46+
orderItemDao.saveOrderItem(orderItem);
47+
48+
//更新 图书库存 和 销量
49+
Book book = bookDao.queryBookById(cartItem.getId());
50+
book.setSales(book.getSales()+cartItem.getCount());
51+
book.setStock(book.getStock()-cartItem.getCount());
52+
bookDao.updateBook(book);//保存更新
53+
}
54+
55+
cart.clear();
56+
57+
return orderId;
58+
}
59+
60+
/** 管理员 查看所有订单
61+
*
62+
* @return
63+
*/
64+
@Override
65+
public List<Order> showAllOrders() {
66+
return orderDao.queryOrders();
67+
}
68+
69+
/** 发货
70+
*
71+
* @param orderId
72+
*/
73+
@Override
74+
public void sendOrder(String orderId) {
75+
orderDao.changeOrderStatus(orderId,1);
76+
}
77+
78+
/** 查看订单详情
79+
*
80+
* @param orderId
81+
*/
82+
@Override
83+
public List<OrderItem> showOrderDetail(String orderId) {
84+
return orderItemDao.queryOrderItemByOrderId(orderId);
85+
}
86+
87+
/** 查看我的订单
88+
*
89+
* @param userId
90+
*/
91+
@Override
92+
public List<Order> showMyOrder(Integer userId ) {
93+
return orderDao.queryOrdersByUserId(userId);
94+
}
95+
96+
/** 签收
97+
*
98+
* @param orderId
99+
*/
100+
@Override
101+
public void receiveOrder(String orderId) {
102+
orderDao.changeOrderStatus(orderId,2);
103+
}
104+
}

0 commit comments

Comments
 (0)