package com.uas.platform.b2b.openapi.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.CollectionUtils;

public class TEMP_IO extends Data {

	private String date;

	private Double qty;

	// private String trader;

	private String end_item_fld;

	private String type;// in/out

	private String po;// order

	private String po_date;// order date

	/**
	 * 期初库存
	 */
	private Double beginStock;

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Double getQty() {
		return qty;
	}

	public void setQty(Double qty) {
		this.qty = qty;
	}

	// public String getTrader() {
	// return trader;
	// }
	//
	// public void setTrader(String trader) {
	// this.trader = trader;
	// }

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPo() {
		return po;
	}

	public void setPo(String po) {
		this.po = po;
	}

	public String getPo_date() {
		return po_date;
	}

	public void setPo_date(String po_date) {
		this.po_date = po_date;
	}

	/**
	 * @param temps
	 *            本期io
	 * @param datas
	 *            这个是指有期初，无本期io的部分
	 * @return
	 */
	public static List<IO> reduce(List<TEMP_IO> temps, List<TEMP_DATA> datas) {
		List<IO> ios = new ArrayList<IO>();
		if (!CollectionUtils.isEmpty(temps)) {
			Map<TEMP_DATA, List<IOItem>> imap = new HashMap<TEMP_DATA, List<IOItem>>();
			Map<TEMP_DATA, List<IOItem>> omap = new HashMap<TEMP_DATA, List<IOItem>>();
			Set<TEMP_DATA> keys = new HashSet<TEMP_DATA>();
			for (TEMP_IO temp : temps) {
				TEMP_DATA data = new TEMP_DATA(temp.getBrand(), temp.getOrg_item(), temp.getItem_desc(), temp.getItem_spec(),
						temp.getItem_unit());
				data.setBeginStock(temp.getBeginStock());
				keys.add(data);

				List<IOItem> items = null;
				if ("in".equals(temp.getType())) {
					if (imap.containsKey(data)) {
						items = imap.get(data);
					} else {
						items = new ArrayList<IOItem>();
					}
				} else {
					if (omap.containsKey(data)) {
						items = omap.get(data);
					} else {
						items = new ArrayList<IOItem>();
					}
				}

				items.add(new IOItem(temp.getDate(), temp.getEnd_item_fld(), temp.getQty(), temp.getPo(), temp.getPo_date()));
				if ("in".equals(temp.getType())) {
					imap.put(data, items);
				} else {
					omap.put(data, items);
				}
			}
			for (TEMP_DATA temp : keys) {
				IO io = new IO(temp.getBrand(), temp.getCode(), temp.getTitle(), temp.getSpec(), temp.getUnit(), imap.get(temp),
						omap.get(temp));
				io.setBeginStock(temp.getBeginStock());
				ios.add(io);
			}
		}
		if (!CollectionUtils.isEmpty(datas)) {
			for (TEMP_DATA temp : datas) {
				IO io = new IO(temp.getBrand(), temp.getCode(), temp.getTitle(), temp.getSpec(), temp.getUnit(), null, null);
				io.setBeginStock(temp.getBeginStock());
				ios.add(io);
			}
		}
		return ios;
	}

	public Double getBeginStock() {
		return beginStock;
	}

	public void setBeginStock(Double beginStock) {
		this.beginStock = beginStock;
	}

	public String getEnd_item_fld() {
		return end_item_fld;
	}

	public void setEnd_item_fld(String end_item_fld) {
		this.end_item_fld = end_item_fld;
	}

}
