package com.fs.erp.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fs.common.config.FSSysConfig;
import com.fs.erp.domain.ErpGoods;
import com.fs.erp.domain.ErpUser;
import com.fs.erp.dto.*;
import com.fs.erp.service.IErpGoodsService;
import com.fs.erp.service.IErpUserService;
import com.fs.erp.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 参数配置 服务层实现
 *

 */
@Service
public class ErpGoodsServiceImpl implements IErpGoodsService
{

    @Autowired
    FSSysConfig sysConfig;


    @Override
    public BaseResponse addGoods(ErpGoods goods) {
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.item.add");
        param.set("code", goods.getCode());
        param.set("name", goods.getName());
        param.set("simple_name", goods.getSimple_name());
        param.set("item_add_attribute", goods.getItem_add_attribute());
        param.set("skus", goods.getSkus());
        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        BaseResponse response=JSONUtil.toBean(result, BaseResponse.class);
        return response;
    }

    @Override
    public ErpGoodsQueryResponse getGoods(ErpGoodsQueryRequert requert) {
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.items.get");
        param.set("code", requert.getCode());
        param.set("page_no", requert.getPage_no());
        param.set("page_size", requert.getPage_size());

        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        System.out.println(result);
        ErpGoodsQueryResponse response=JSONUtil.toBean(result, ErpGoodsQueryResponse.class);
        return response;
    }

    @Override
    public ErpGoodsStockQueryResponse getGoodsStock(ErpGoodsStockQueryRequert requert) {
        JSONObject param = JSONUtil.createObj();
        param.set("appkey", sysConfig.getErpAppKey());
        param.set("sessionkey", sysConfig.getErpSessionKey());
        param.set("method", "gy.erp.new.stock.get");
        param.set("barcode", requert.getBarcode());
        param.set("page_no", requert.getPage_no());
        param.set("page_size", requert.getPage_size());

        param.set("sign",CommonUtils.sign(param.toString(),sysConfig.getErpSecret()));
        String result = HttpUtil.post(sysConfig.getErpUrl(), param.toString());
        ErpGoodsStockQueryResponse response=JSONUtil.toBean(result, ErpGoodsStockQueryResponse.class);
        return response;
    }
}
