package com.ruoyi.erp.request;

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

import java.util.List;

/**
 * 编辑商品请求（按需传入要修改的字段）
 *
 * 对应接口：POST /api/open/product/edit
 */
public class ProductEditRequest extends ERPRequestBase {
	private final JSONArray channelPv = new JSONArray();
	private final JSONArray publishShop = new JSONArray();
	private final JSONArray skuItems = new JSONArray();

	public ProductEditRequest(IERPAccount erpAccount) {
		super("https://open.goofish.pro/api/open/product/edit", erpAccount);
	}

	public void setProductId(long productId) { ensureBody(); this.requestBody.put("product_id", productId); }

	public void setItemBizType(Integer itemBizType) { putIfNotNull("item_biz_type", itemBizType); }
	public void setSpBizType(Integer spBizType) { putIfNotNull("sp_biz_type", spBizType); }
	public void setCategoryId(Integer categoryId) { putIfNotNull("category_id", categoryId); }
	public void setChannelCatId(String channelCatId) { putIfNotNull("channel_cat_id", channelCatId); }
	public void addChannelPv(String propertyId, String propertyName, String valueId, String valueName) {
		JSONObject pv = new JSONObject();
		pv.put("property_id", propertyId);
		pv.put("property_name", propertyName);
		pv.put("value_id", valueId);
		pv.put("value_name", valueName);
		channelPv.add(pv);
		ensureBody();
		this.requestBody.put("channel_pv", channelPv);
	}
	public void setTitle(String title) { putIfNotNull("title", title); }
	public void setPrice(Long price) { putIfNotNull("price", price); }
	public void setOriginalPrice(Long originalPrice) { putIfNotNull("original_price", originalPrice); }
	public void setExpressFee(Long expressFee) { putIfNotNull("express_fee", expressFee); }
	public void setStock(Integer stock) { putIfNotNull("stock", stock); }
	public void setOuterId(String outerId) { putIfNotNull("outer_id", outerId); }
	public void setStuffStatus(Integer stuffStatus) { putIfNotNull("stuff_status", stuffStatus); }
	public void addPublishShop(String userName, Integer province, Integer city, Integer district, String title, String content, List<String> images, String whiteImages, String serviceSupport) {
		JSONObject shop = new JSONObject();
		if (userName != null) shop.put("user_name", userName);
		if (province != null) shop.put("province", province);
		if (city != null) shop.put("city", city);
		if (district != null) shop.put("district", district);
		if (title != null) shop.put("title", title);
		if (content != null) shop.put("content", content);
		if (images != null && !images.isEmpty()) {
			JSONArray imgs = new JSONArray();
			imgs.addAll(images);
			shop.put("images", imgs);
		}
		if (whiteImages != null) shop.put("white_images", whiteImages);
		if (serviceSupport != null) shop.put("service_support", serviceSupport);
		publishShop.add(shop);
		ensureBody();
		this.requestBody.put("publish_shop", publishShop);
	}
	public void addSkuItem(Long price, Integer stock, String skuText, String outerId) {
		JSONObject sku = new JSONObject();
		if (price != null) sku.put("price", price);
		if (stock != null) sku.put("stock", stock);
		if (skuText != null) sku.put("sku_text", skuText);
		if (outerId != null) sku.put("outer_id", outerId);
		skuItems.add(sku);
		ensureBody();
		this.requestBody.put("sku_items", skuItems);
	}
	public void setBookData(JSONObject bookData) { putIfNotNull("book_data", bookData); }
	public void setFoodData(JSONObject foodData) { putIfNotNull("food_data", foodData); }
	public void setReportData(JSONObject reportData) { putIfNotNull("report_data", reportData); }
	public void setNotifyUrl(String notifyUrl) { putIfNotNull("notify_url", notifyUrl); }
	public void setFlashSaleType(Integer flashSaleType) { putIfNotNull("flash_sale_type", flashSaleType); }
	public void setAdventData(JSONObject adventData) { putIfNotNull("advent_data", adventData); }
	public void setInspectData(JSONObject inspectData) { putIfNotNull("inspect_data", inspectData); }
	public void setBrandData(JSONObject brandData) { putIfNotNull("brand_data", brandData); }
	public void setDetailImages(JSONArray images) { putIfNotNull("detail_images", images); }
	public void setSkuImages(JSONArray images) { putIfNotNull("sku_images", images); }

	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); } }
}


