订单分箱问题

订单分箱需求,我把它简化为如下模型:

订单分箱问题_第1张图片

 一张表实现,实现分箱的效果,总结一下做个小demo。

package com.kaspar.order.model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "Orders")
public class Order implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(generator = "orderid")
    @GenericGenerator(name = "orderid", strategy = "native")
    private int orderid;
    private String ordernum;
    public String getOrdernum() {
        return ordernum;
    }

    public void setOrdernum(String ordernum) {
        this.ordernum = ordernum;
    }

    @Column(length = 100, name = "item")
    private String item;
    private String box;
    private String quality;


    public int getOrderid() {
        return orderid;
    }

    public void setOrderid(int orderid) {
        this.orderid = orderid;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getBox() {
        return box;
    }

    public void setBox(String box) {
        this.box = box;
    }

    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }
    public Order() {

    }

    public Order(String ordernum,String item, String box, String quality) {
        this.ordernum = ordernum;
        this.item = item;
        this.box = box;
        this.quality = quality;
    }
}
View Code
public class fenxiangbyorder {
    private ArrayList getList(){
        ArrayList list = new ArrayList();
        Order b1 = new Order();
        Order b2 = new Order();
        Order b3 = new Order();
        
        b1.setOrderid(1);
        b2.setOrderid(2);
        b3.setOrderid(3);
        
        b1.setBox("1");
        b2.setBox("4");
        b3.setBox("1");
        
        b1.setItem("1");
        b2.setItem("2");
        b3.setItem("3");
        
        b1.setOrdernum("WR20191213001");
        b2.setOrdernum("WR20191213001");
        b3.setOrdernum("WR20191213001");
        
        b1.setQuality("75");
        b2.setQuality("36");
        b3.setQuality("23");
        list .add(b1);
        list .add(b2);
        list .add(b3);
        
        return list;
    }
    
    public  Map getOrder2(){
        Map    map = new HashMap();
        List listresult =getList();//三个bean对象
        ArrayList listBox = new ArrayList();
         Integer  num1=1;
         for (int i = 0; i <  listresult.size(); i++) { //根据条目遍历,因为这个和条目一致直接拿来用了
             Order box = (Order) listresult.get(i);
             int boxNum = Integer.valueOf(box.getBox());
             for (int k = 0; k < boxNum; k++) {  //根据箱子数量遍历
                 Order Order1 = new Order();
                 Order1 = box;
                 System.out.println("dayin第一遍"+num1);
                 Order1.setOrderid(num1);
                 System.out.println(Order1);
                 num1=num1+1;
                 listBox.add(Order1);
            }
        }
        map.put("result", listBox);
        return map;
    }
    public static void main(String[] args) {
        fenxiangbyorder a = new fenxiangbyorder();
        Map ss = a.getOrder2();
        System.out.println("---------------------------------");
        System.out.println(ss);
    }
}
View Code

数据就直接在里面取,实际业务直接从持久层拿就欧克。

订单分箱问题_第2张图片

 

 到这里结束,一共六箱。关于设置箱号这里有点补充。

原因就是引用类型变量的赋值只复制对象的引用,而不复制对象本身;而将一个值类型赋给另一个值类型变量时, 将复制包含的值。我的实体bean自然是引用类型,所以我在for循环赋值的时候,实体类添加但对象一直都是相同的, bt.setValues将之 前的赋值更新成。新的赋值然后再添加一-次就会出现上面的结果。

你可能感兴趣的:(订单分箱问题)