|
|
package byte_bank
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
gatewayLib "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway"
|
|
|
translator2 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/byte_bank/translator"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type HttpLibByteBankServiceGateway struct {
|
|
|
gatewayLib.BaseServiceGateway
|
|
|
baseURL string
|
|
|
}
|
|
|
|
|
|
// ListFormulas 列出所有公式
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) ListFormulas(pageSize int64, pageNumber int64) (*translator2.Formula, error) {
|
|
|
url := strings.Join([]string{serviceGateway.baseURL, "formulas"}, "/")
|
|
|
request := serviceGateway.CreateRequest(url, "get")
|
|
|
request.Param("pageSize", strconv.FormatInt(pageSize, 10))
|
|
|
request.Param("pageNumber", strconv.FormatInt(pageNumber, 10))
|
|
|
byteResult, err := request.Bytes()
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("获取列表列表失败:%w", err)
|
|
|
}
|
|
|
var result gatewayLib.Response
|
|
|
err = json.Unmarshal(byteResult, &result)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("解析公式列表失败:%w", err)
|
|
|
}
|
|
|
var data translator2.Formula
|
|
|
err = serviceGateway.GetResponseData(result, &data)
|
|
|
return &data, err
|
|
|
}
|
|
|
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) GetFormula(formulaId string) (*translator2.FormulaDetail, error) {
|
|
|
url := strings.Join([]string{serviceGateway.baseURL, "/formulas/" + formulaId}, "/")
|
|
|
request := serviceGateway.CreateRequest(url, "get")
|
|
|
byteResult, err := request.Bytes()
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("获取列表列表失败:%w", err)
|
|
|
}
|
|
|
var result gatewayLib.Response
|
|
|
err = json.Unmarshal(byteResult, &result)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("解析公式列表失败:%w", err)
|
|
|
}
|
|
|
var data translator2.FormulaDetail
|
|
|
err = serviceGateway.GetResponseData(result, &data)
|
|
|
return &data, err
|
|
|
}
|
|
|
|
|
|
// AnalysisFormula 方案解析
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) AnalysisFormula(formulaId int64) (*translator2.AnalysisFormulaDetail, error) {
|
|
|
url := strings.Join([]string{serviceGateway.baseURL, "sql/ad-hoc/for-formula"}, "/")
|
|
|
request := serviceGateway.CreateRequest(url, "post")
|
|
|
options := make(map[string]interface{})
|
|
|
options["formulaId"] = strconv.FormatInt(formulaId, 10)
|
|
|
_, err := request.JSONBody(options)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
byteResult, err := request.Bytes()
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("方案解析失败失败:%w", err)
|
|
|
}
|
|
|
var result gatewayLib.Response
|
|
|
err = json.Unmarshal(byteResult, &result)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("解析方案解析结果失败:%w", err)
|
|
|
}
|
|
|
var data translator2.AnalysisFormulaDetail
|
|
|
err = serviceGateway.GetResponseData(result, &data)
|
|
|
return &data, err
|
|
|
}
|
|
|
|
|
|
// AnalysisFormula 方案解析
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) AnalysisFormulaByte(formulaId string) ([]byte, error) {
|
|
|
url := strings.Join([]string{serviceGateway.baseURL, "sql/ad-hoc/for-formula"}, "/")
|
|
|
request := serviceGateway.CreateRequest(url, "post")
|
|
|
options := make(map[string]interface{})
|
|
|
options["formulaId"] = formulaId
|
|
|
_, err := request.JSONBody(options)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
byteResult, err := request.Bytes()
|
|
|
if err != nil {
|
|
|
return byteResult, fmt.Errorf("方案解析失败失败:%w", err)
|
|
|
}
|
|
|
return byteResult, err
|
|
|
}
|
|
|
|
|
|
// ListDataApplications 列出所有数据应用
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) ListDataApplications(pageSize int64, pageNumber int64) (*translator2.DataApplication, error) {
|
|
|
url := strings.Join([]string{serviceGateway.baseURL, "data-applications"}, "/")
|
|
|
request := serviceGateway.CreateRequest(url, "get")
|
|
|
request.Param("pageSize", strconv.FormatInt(pageSize, 10))
|
|
|
request.Param("pageNumber", strconv.FormatInt(pageNumber, 10))
|
|
|
byteResult, err := request.Bytes()
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("获取应用列表失败:%w", err)
|
|
|
}
|
|
|
var result gatewayLib.Response
|
|
|
err = json.Unmarshal(byteResult, &result)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("解析应用列表失败:%w", err)
|
|
|
}
|
|
|
var data translator2.DataApplication
|
|
|
err = serviceGateway.GetResponseData(result, &data)
|
|
|
return &data, err
|
|
|
}
|
|
|
|
|
|
// StartDataApplication 启动数据应用
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) StartDataApplication(dataApplicationId int64) (map[string]interface{}, error) {
|
|
|
dataApplicationIdString := strconv.FormatInt(dataApplicationId, 10)
|
|
|
url := strings.Join([]string{serviceGateway.baseURL, "data-applications/" + dataApplicationIdString + "/data-job/launch"}, "/")
|
|
|
request := serviceGateway.CreateRequest(url, "post")
|
|
|
response := make(map[string]interface{})
|
|
|
err := request.ToJSON(&response)
|
|
|
if err != nil {
|
|
|
//log.Logger.Error("Service Gateway Fail")
|
|
|
return nil, err
|
|
|
}
|
|
|
data, err := serviceGateway.responseHandle(response)
|
|
|
return data, err
|
|
|
}
|
|
|
|
|
|
func (serviceGateway *HttpLibByteBankServiceGateway) responseHandle(response map[string]interface{}) (map[string]interface{}, error) {
|
|
|
data := make(map[string]interface{})
|
|
|
var err error
|
|
|
if code, ok := response["code"]; ok {
|
|
|
code := code.(float64)
|
|
|
if code == 0 {
|
|
|
data = response["data"].(map[string]interface{})
|
|
|
} else {
|
|
|
msg := response["msg"].(string)
|
|
|
err = fmt.Errorf(strings.Join([]string{strconv.FormatFloat(code, 'f', -1, 64), msg}, " "))
|
|
|
}
|
|
|
} else {
|
|
|
jsonBytes, marshalErr := json.Marshal(response)
|
|
|
if marshalErr != nil {
|
|
|
err = marshalErr
|
|
|
}
|
|
|
err = fmt.Errorf("无法解析的网关服务数据返回格式:%s", string(jsonBytes))
|
|
|
}
|
|
|
return data, err
|
|
|
}
|
|
|
|
|
|
func NewHttpLibByteBankServiceGateway(host string) *HttpLibByteBankServiceGateway {
|
|
|
gt := gatewayLib.NewBaseServiceGateway(host)
|
|
|
gt.ConnectTimeout = 10 * time.Second
|
|
|
gt.ReadWriteTimeout = 10 * time.Second
|
|
|
return &HttpLibByteBankServiceGateway{
|
|
|
BaseServiceGateway: gt,
|
|
|
}
|
|
|
} |
...
|
...
|
|