JavaWeb10(实现结算&查看订单&订单详情)(一)

简介: JavaWeb10(实现结算&查看订单&订单详情)

一.效果预览

二.实现思路

2.1 实现结算

①形成订单,并赋值

//从session中拿购物车
        List<OrderItem> items =(List<OrderItem>)session.getAttribute("mycart");
        //从session中拿用户
        User user = (User)session.getAttribute("user");
        //形成一个订单
        Order order = new Order();
        //给订单赋值 缺少oid!!!!!!!
        order.setItems(items);
        order.calcSum();//务必先给集合赋值
        order.setUser(user);
        order.setOaddress(user.getUaddress());
    //调用增加订单的方法
        int n = iob.addOrder(order);
        if(n>0) {
            //说明结算成功
            session.setAttribute("mycart", null);
            //跳转到我的订单页面
            resp.sendRedirect("orders.do");
        }
        else {
            resp.sendRedirect("cart.jsp?index=5");
        }

②将订单插入到数据表tb_order中,同时将该订单所对应的订单项插入到数据表tb_orderitem中

package com.zking.entity;
/**
 * 实体类订单类
 * @author PC
 *
 */
import java.io.Serializable;
import java.util.List;
public class Order implements Serializable{
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private int oid;//订单编号
  private double osumprice;//订单总价
  private User user;//用户对象
  private String oaddress;//订单地址
  //一个订单由多个订单项组成的√
  private List<OrderItem> items=null;
  //计算订单总价
  public void calcSum() {
    double sum = 0;
    for (OrderItem oi : items) {
      sum+=oi.getOiprice();//累加求小计的和
    }
    this.osumprice=sum;//再计算总价
  }
  public int getOid() {
    return oid;
  }
  public void setOid(int oid) {
    this.oid = oid;
  }
  public double getOsumprice() {
    return osumprice;
  }
  public void setOsumprice(double osumprice) {
    this.osumprice = osumprice;
  }
  public User getUser() {
    return user;
  }
  public void setUser(User user) {
    this.user = user;
  }
  public String getOaddress() {
    return oaddress;
  }
  public void setOaddress(String oaddress) {
    this.oaddress = oaddress;
  }
  public List<OrderItem> getItems() {
    return items;
  }
  public void setItems(List<OrderItem> items) {
    this.items = items;
  }
  public Order() {
    // TODO Auto-generated constructor stub
  }
  public Order(int oid, double osumprice, User user, String oaddress, List<OrderItem> items) {
    this.oid = oid;
    this.osumprice = osumprice;
    this.user = user;
    this.oaddress = oaddress;
    this.items = items;
  }
  public Order(double osumprice, User user, String oaddress, List<OrderItem> items) {
    this.osumprice = osumprice;
    this.user = user;
    this.oaddress = oaddress;
    this.items = items;
  }
  @Override
  public String toString() {
    return "Order [oid=" + oid + ", osumprice=" + osumprice + ", user=" + user + ", oaddress=" + oaddress
        + ", items=" + items + "]";
  }
}
package com.zking.entity;
import java.io.Serializable;
/**
 * 实体类:订单项类
 * @author PC
 *
 */
public class OrderItem implements Serializable{
  private static final long serialVersionUID = 1L;
  private int oiid;//订单项编号
  private int oid;//订单编号
// private int gid;//商品编号
  private Goods goods;//商品对象 可以间接拿到其所有数据
  private int oinum;//购买数量
    private double oiprice;//小计
    public void calcxj() { 
      //小计=单价*数量
      this.oiprice=this.goods.getGprice()*this.oinum;
      String ss = String.format("%.1f",oiprice);
      double s = Double.parseDouble(ss);  
      this.oiprice=s;
    }
  public int getOiid() {
    return oiid;
  }
  public void setOiid(int oiid) {
    this.oiid = oiid;
  }
  public int getOid() {
    return oid;
  }
  public void setOid(int oid) {
    this.oid = oid;
  }
  public Goods getGoods() {
    return goods;
  }
  public void setGoods(Goods goods) {
    this.goods = goods;
  }
  public int getOinum() {
    return oinum;
  }
  public void setOinum(int oinum) {
    this.oinum = oinum;
  }
  public double getOiprice() {
    return oiprice;
  }
  public void setOiprice(double oiprice) {
    this.oiprice = oiprice;
  }
    public OrderItem() {
    // TODO Auto-generated constructor stub
  }
  public OrderItem(int oiid, int oid, Goods goods, int oinum, double oiprice) {
    this.oiid = oiid;
    this.oid = oid;
    this.goods = goods;
    this.oinum = oinum;
    this.oiprice = oiprice;
  }
  public OrderItem(int oid, Goods goods, int oinum, double oiprice) {
    this.oid = oid;
    this.goods = goods;
    this.oinum = oinum;
    this.oiprice = oiprice;
  }
  @Override
  public String toString() {
    return "OrderItem [oiid=" + oiid + ", oid=" + oid + ", goods=" + goods + ", oinum=" + oinum + ", oiprice="
        + oiprice + "]";
  }
}
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.entity.Order;
import com.zking.entity.OrderItem;
import com.zking.entity.User;
import com.zking.util.DBHelper;
/**
 * 数据访问层 tb_order
 * @author PC
 *
 */
public class OrderDao implements IOrderDao {
  private Connection con = null;
  private PreparedStatement ps = null;
  private ResultSet rs = null;
  boolean flag;
  /**
   * 根据用户拿到订单
   * @param u 用户对象
   * @return 订单集合
   */
  public List<Order> getAllByUser(User u){
    List<Order> ls = new ArrayList<Order>();
    try {
      con=DBHelper.getCon();
      String sql="select * from (select * from tb_order order by oid desc) where uuid="+u.getUuid();
      ps=con.prepareStatement(sql);
      rs=ps.executeQuery();
      while(rs.next()) {
        Order o =new Order();
        o.setOid(rs.getInt(1));
        String ss = String.format("%.1f",rs.getDouble(2));
          double s = Double.parseDouble(ss);
        o.setOsumprice(s);
        o.setUser(u);
        o.setOaddress(rs.getString(4));
        ls.add(o);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      DBHelper.myClose(con, ps, rs);
    }
    return ls;
  } 
  /**
   * 单个查询
   * @param oid
   * @return
   */
  public Order getOrder(String oid) {
    Order o = new Order();
    OrderItemDao odd = new OrderItemDao(); 
    try {
      con=DBHelper.getCon();
      String sql="select * from tb_orderitem where oid="+oid;
      ps=con.prepareStatement(sql);
      rs=ps.executeQuery();
      if(rs.next()) {
        o.setOid(rs.getInt(1));
        o.setOsumprice(rs.getDouble(2));
        o.setOaddress(rs.getString(4));
        //拿的该订单所对应的订单项集合
        List<OrderItem> items = odd.getAllByOid(oid);
        o.setItems(items);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      DBHelper.myClose(con, ps, rs);
    }
    return o;
  }
  /**
   * 增加订单
   * @param order 订单对象
   * @return 影响行数
   */
  public int addOrder(Order order) {
    int n =0;
    int oid = getID("tb_order", "oid");///!!!赋值  订单编号
    OrderItemDao odao = new OrderItemDao();
    try {
      con=DBHelper.getCon();
      String sql="insert into tb_order(oid,osumprice,uuid,oaddress) values(?,?,?,?)";
        ps=con.prepareStatement(sql);
        //给占位符赋值
        ps.setInt(1, oid);
        ps.setDouble(2, order.getOsumprice());
        ps.setInt(3, order.getUser().getUuid());
        ps.setString(4, order.getOaddress());
        n=ps.executeUpdate();
        if(n>0) {//说明订单下单成功
          //那么就把该订单所对应的订单项全部加入到数据库中
          //拿到该订单所对应的订单项集合
          List<OrderItem> items = order.getItems();
          //循环遍历
          for (OrderItem oi : items) {
          //接下来我需要将所有的oi都要进tb_orderitem表
            oi.setOid(oid);//关键
            odao.addOrderItem(oi);
            //把对应商品的销量+购买数量 库存-购买数量
            new GoodsDao().updateXL(oi.getGoods().getGid(),oi.getOinum());
        }
        }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      DBHelper.myClose(con, ps, rs);
    }
    return n;
  }
  /**
   * 删除订单
   * @param oid
   * @return
   */
  public boolean deleteOrderByOid(int oid) {
    try {
      con=DBHelper.getCon();
      String sql="delete from tb_order where oid="+oid;
      ps=con.prepareStatement(sql);
      if(ps.executeUpdate()>0) {
        flag = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      DBHelper.myClose(con, ps, rs);
    }
    return flag;
  }
  /**
   * 标识列实现  ②取表的最大序号+1
   * @param tabName 表名
   * @param colName 列名
   * @return
   */
  public int getID(String tabName,String colName) {//表名 列名
    int n = 0;
    try {
      con=DBHelper.getCon();
        String sql="select nvl(max("+colName+"),0)+1 from "+tabName;  
        ps=con.prepareStatement(sql);
        rs=ps.executeQuery();
        if(rs.next()) {
          n=rs.getInt(1);
        }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      DBHelper.myClose(con, ps, rs);
    }
    return n;
  }
}


相关文章
|
前端开发
JavaWeb11(查看订单详情&优化结算)
JavaWeb11(查看订单详情&优化结算)
|
SQL 前端开发
创建订单【项目 商城】
创建订单【项目 商城】
66 0
|
前端开发
JavaWeb10(实现结算&查看订单&订单详情)(二)
JavaWeb10(实现结算&查看订单&订单详情)
|
数据可视化 搜索推荐 API
如何通过拼多多订单API接口获取订单详情
拼多多订单接口获取到订单详情之后,你可以根据需求对订单信息进行处理和利用,例如: 将订单信息导入到 ERP 系统中实现自动化订单处理; 根据订单信息生成发货单、物流面单等相关文档; 对订单的商品信息进行分析,例如常购买的商品、下单时间、下单地点等,为业务决策提供参考; 根据买家信息进行个性化营销,为买家提供更好的购物体验; 对订单信息进行可视化展示,例如订单量趋势图、订单来源分析等,为业务和数据分析提供支持。 以上只是一些简单的示例,具体使用方式应根据实际业务需求和场景而定。需要注意的是,对于涉及用户隐私数据的处理,需要遵守相关的法律法规和隐私保护政策。
ERP,仓储系统官方订单详情接口trade.fullinfo.get( 获取单笔交易的详细信息 )
ERP,仓储系统官方订单详情接口trade.fullinfo.get( 获取单笔交易的详细信息 )
|
弹性计算
阿里云存在未支付订单导致无法下单解决方法
解决阿里云存在未支付订单请支付或作废后再下单,阿里云服务器或其他云资源无法立即购买,提示“您选择的资源存在未支付订单,请支付或作废后再下单!”什么原因?这是由于你的阿里云账号之前已经创建了该订单,只是订单没有支付,所以无法再次创建订单。解决方法是,要么取消之前的订单,要么支付之前的订单。阿里云百科来详细说下阿里云账号下存在未支付订单的解决方法:
1037 0
阿里云存在未支付订单导致无法下单解决方法
|
消息中间件 算法 Java
创建支付订单实现|学习笔记
快速学习创建支付订单实现
131 0
创建支付订单实现|学习笔记
|
消息中间件 数据库 RocketMQ
创建支付订单流程|学习笔记
快速学习创建支付订单流程
371 0
创建支付订单流程|学习笔记
|
数据库 开发者 微服务
查询订单支付状态接口(2) | 学习笔记
快速学习查询订单支付状态接口(2)
查询订单支付状态接口(2) | 学习笔记