package com.fs.erp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.erp.domain.ErpGoods;
import com.fs.erp.dto.*;
import com.fs.erp.dto.sdk.HzOMS.api.HzOMSClient;
import com.fs.erp.service.IErpGoodsService;
import com.fs.his.domain.FsStoreProduct;
import com.fs.his.domain.FsStoreProductAttr;
import com.fs.his.domain.FsStoreProductAttrValue;
import com.fs.his.service.IFsStoreProductAttrService;
import com.fs.his.service.IFsStoreProductAttrValueService;
import com.fs.his.service.IFsStoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author MixLiu
 * @date 2025/6/26 下午5:55)
 */
@Service
public class HzOMSErpGoodsServiceImpl implements IErpGoodsService {


    @Autowired
    HzOMSClient hzOMSClient;

    @Autowired
    private IFsStoreProductService fsStoreProductService;

    @Autowired
    private IFsStoreProductAttrService fsStoreProductAttrService;

    @Autowired
    private IFsStoreProductAttrValueService fsStoreProductAttrValueService;

    @Override
    public BaseResponse addGoods(ErpGoods goods) {

        Long storeProductId = goods.getStoreProductId();
        JSONObject hzGoods = buildGoods(storeProductId);

        hzOMSClient.send("oms_open_ptshopgoods_sync", hzGoods);

        return null;
    }

    @Override
    public BaseResponse addGoodsScrm(ErpGoods goods) {
        return null;
    }

    @Override
    public ErpGoodsQueryResponse getGoods(ErpGoodsQueryRequert param) {
        return null;
    }

    @Override
    public ErpGoodsQueryResponse getGoodsScrm(ErpGoodsQueryRequert param) {
        return null;
    }

    @Override
    public ErpGoodsStockQueryResponse getGoodsStock(ErpGoodsStockQueryRequert param) {
        return null;
    }

    @Override
    public ErpGoodsStockQueryResponse getGoodsStockScrm(ErpGoodsStockQueryRequert param) {
        return null;
    }

    /**
     * 构建店铺商品上传的参数
     *
     * @param storeProductId
     * @return
     */
    public JSONObject buildGoods(Long storeProductId) {

        JSONObject res = new JSONObject();
        //商品
        FsStoreProduct fsStoreProduct = fsStoreProductService.selectFsStoreProductByProductId(storeProductId);

        //平台店铺编码 todo 测试环境只能写死 110111 用于联调测试 fsStoreProduct.getStoreId()
        res.put("cptshopcode", 110111);
        JSONArray goodsList = new JSONArray();
        //添加上传的商品
        JSONObject goods = new JSONObject();
        //平台商品编码
        goods.put("cptgoodsid", fsStoreProduct.getProductId());
        //平台商品名称
        goods.put("cptgoodsname", fsStoreProduct.getProductName());
        //平台商品价格 f
        goods.put("fptprice", fsStoreProduct.getPrice());
        //平台商品库存 f
        goods.put("fptstock", fsStoreProduct.getStock());
        //上架状态0 下架；1 上架 f
        goods.put("isalestatus", fsStoreProduct.getIsShow());
        //平台分类编码 f
//        goods.put("cptgroupid", );
        //平台分类名称 f
//        goods.put("cptgroupname", );
        goods.put("cptgoodsid", fsStoreProduct.getProductId());

        //商品规格
        JSONArray attrList = new JSONArray();
        //商品规格List
        List<FsStoreProductAttrValue> fsStoreProductAttrValues = fsStoreProductAttrValueService.selectFsStoreProductAttrValueListByProductId(fsStoreProduct.getProductId());
        fsStoreProductAttrValues.forEach(attr->{
            JSONObject attrObj = new JSONObject();
            //平台规格名称
            attrObj.put("cptspecname", attr.getSku());
            //平台规格编码
            attrObj.put("cptspeccode", attr.getId());
            //自定义规格编码 f
//            attrObj.put("ccustid", attr.getAttrName());
            //规格库存
            attrObj.put("fptstock", attr.getStock());
            //规格价格
            attrObj.put("fptprice", attr.getPrice());
            //规格上架状态 0 下架;1 上架
            attrObj.put("isalestatus", fsStoreProduct.getIsShow());
            attrList.add(attrObj);
        });

        //规格信息 f
        goods.put("skus",attrList);
        goodsList.add(goods);
        res.put("goodsList", goodsList);

        return res;
    }
}
