package com.yn.bftl.thirdparty.modules.erp.util;

import com.alibaba.fastjson.JSONObject;
import com.yn.bftl.common.modules.company.entity.ErpAccountSet;
import com.yn.bftl.thirdparty.common.util.HttpClientUtil;
import com.yn.bftl.thirdparty.modules.erp.vo.ErpResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;


/**
 * @author huabiao
 * @create 2023/7/7  12:08
 **/
@Slf4j
@Component
public class ErpUtil {


    /**
     * @param erpAccountSet ERP账套
     * @param json          请求参数
     * @param busType       请求类型
     * @return ERP响应VO
     * @author huabiao
     * @create 2023/7/7  14:36
     */
    public ErpResponseVO postErpMethod(ErpAccountSet erpAccountSet, String json, int busType) {
        // 拼接请求参数
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        // 业务参数JSON
        form.add("Json", json);
        // 请求类型
        form.add("BusType", busType);
        // 组织编码
        form.add("Org", erpAccountSet.getOrgCode());
        JSONObject jsonObject = HttpClientUtil.getInstance().setContentType(MediaType.parseMediaType(APPLICATION_FORM_URLENCODED_VALUE)).sendPost(erpAccountSet.getAddress(), form);
        return jsonObject.toJavaObject(ErpResponseVO.class);
    }
}
