package com.ruoyi.erp.request;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

/**
 * 编辑库存请求
 *
 * 对应接口：POST /api/open/product/edit/stock
 */
public class ProductEditStockRequest extends ERPRequestBase {
	public ProductEditStockRequest(IERPAccount erpAccount) {
		super("https://open.goofish.pro/api/open/product/edit/stock", erpAccount);
	}

	public void setProductId(long productId) { ensureBody(); this.requestBody.put("product_id", productId); }
	public void setPrice(Long price) { putIfNotNull("price", price); }
	public void setOriginalPrice(Long originalPrice) { putIfNotNull("original_price", originalPrice); }
	public void setStock(Integer stock) { putIfNotNull("stock", stock); }

	public void addSkuItem(long skuId, Integer stock, Long price, String outerId) {
		ensureBody();
		JSONArray arr = this.requestBody.getJSONArray("sku_items");
		if (arr == null) { arr = new JSONArray(); this.requestBody.put("sku_items", arr); }
		JSONObject sku = new JSONObject();
		sku.put("sku_id", skuId);
		if (stock != null) sku.put("stock", stock);
		if (price != null) sku.put("price", price);
		if (outerId != null) sku.put("outer_id", outerId);
		arr.add(sku);
	}

	private void ensureBody() { if (this.requestBody == null) this.requestBody = new JSONObject(); }
	private void putIfNotNull(String key, Object value) { if (value != null) { ensureBody(); this.requestBody.put(key, value); } }
}


