module_credit_account.go 6.8 KB
package allied_creation_cooperation

import (
	"encoding/json"
	"fmt"
	"strconv"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway"
)

// Credit-accountsPay 支付账期结算
func (gateway HttplibAlliedCreationCooperation) CreditAccountsPay(param ReqCreditAccountsPay) (*DataCreditAccountsPay, error) {
	url := gateway.baseUrL + "/credit-accounts/pay"
	method := "POST"
	req := gateway.CreateRequest(url, method)
	log.Logger.Debug("向业务模块请求数据:支付账期结算。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求支付账期结算失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取支付账期结算失败:%w", err)
	}
	log.Logger.Debug("获取业务模块请求数据:支付账期结算。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析支付账期结算:%w", err)
	}
	var data DataCreditAccountsPay
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

// Credit-accountsSearch 查询账期结算单
func (gateway HttplibAlliedCreationCooperation) CreditAccountsSearch(param ReqCreditAccountsSearch) (*DataCreditAccountsSearch, error) {
	url := gateway.baseUrL + "/credit-accounts/search"
	method := "POST"
	req := gateway.CreateRequest(url, method)
	log.Logger.Debug("向业务模块请求数据:查询账期结算单。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求查询账期结算单失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取查询账期结算单失败:%w", err)
	}
	log.Logger.Debug("获取业务模块请求数据:查询账期结算单。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析查询账期结算单:%w", err)
	}
	var data DataCreditAccountsSearch
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

// CreditAccountRemove 移除账期结算单
func (gateway HttplibAlliedCreationCooperation) CreditAccountRemove(param ReqCreditAccountRemove) (*DataCreditAccountRemove, error) {
	url := gateway.baseUrL + "/credit-accounts/" + strconv.Itoa(param.CreditAccountId)
	method := "DELETE"
	req := gateway.CreateRequest(url, method)
	log.Logger.Debug("向业务模块请求数据:移除账期结算单。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求移除账期结算单失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取移除账期结算单失败:%w", err)
	}
	log.Logger.Debug("获取业务模块请求数据:移除账期结算单。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析移除账期结算单:%w", err)
	}
	var data DataCreditAccountRemove
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

// CreditAccountList 返回账期结算单列表
func (gateway HttplibAlliedCreationCooperation) CreditAccountList(param ReqCreditAccountList) (*DataCreditAccountList, error) {
	url := gateway.baseUrL + "/credit-accounts"
	method := "GET"
	req := gateway.CreateRequest(url, method)
	log.Logger.Debug("向业务模块请求数据:返回账期结算单列表。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求返回账期结算单列表失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取返回账期结算单列表失败:%w", err)
	}
	log.Logger.Debug("获取业务模块请求数据:返回账期结算单列表。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析返回账期结算单列表:%w", err)
	}
	var data DataCreditAccountList
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

// CreditAccountGet 返回账期结算单详情
func (gateway HttplibAlliedCreationCooperation) CreditAccountGet(param ReqCreditAccountGet) (*DataCreditAccountGet, error) {
	url := gateway.baseUrL + "/credit-accounts/" + strconv.Itoa(param.CreditAccountId)
	method := "GET"
	req := gateway.CreateRequest(url, method)
	log.Logger.Debug("向业务模块请求数据:返回账期结算单详情。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求返回账期结算单详情失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取返回账期结算单详情失败:%w", err)
	}
	log.Logger.Debug("获取业务模块请求数据:返回账期结算单详情。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析返回账期结算单详情:%w", err)
	}
	var data DataCreditAccountGet
	err = gateway.GetResponseData(result, &data)
	return &data, err
}

// CreditAccountsAdd 创建账期结算
func (gateway HttplibAlliedCreationCooperation) CreditAccountsAdd(param ReqCreditAccountsAdd) (*DataCreditAccountsAdd, error) {
	url := gateway.baseUrL + "/credit-accounts"
	method := "POST"
	req := gateway.CreateRequest(url, method)
	log.Logger.Debug("向业务模块请求数据:创建账期结算。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("请求创建账期结算失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("获取创建账期结算失败:%w", err)
	}
	log.Logger.Debug("获取业务模块请求数据:创建账期结算。", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析创建账期结算:%w", err)
	}
	var data DataCreditAccountsAdd
	err = gateway.GetResponseData(result, &data)
	return &data, err
}