| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package service
- import (
- "errors"
- "xps/cache"
- "xps/datasource"
- "xps/repositories"
- )
- type ApiGatewayService interface {
- Get(apiCode string, m map[string]interface{})([]map[string]interface{}, error)
- Create(apiCode string, m map[string]interface{})error
- Update(apiCode string, m map[string]interface{})error
- Patch(apiCode string, m map[string]interface{})error
- Delete(apiCode string, m map[string]interface{})error
- }
- type apiGatewayService struct {
- dataBus *repositories.DataBusRepo
- }
- func NewApiGatewayService() ApiGatewayService {
- return &apiGatewayService{
- dataBus: repositories.NewDataBusRepo(datasource.InstanceSlave()),
- }
- }
- func (a *apiGatewayService) Get(apiCode string, m map[string]interface{}) ([]map[string]interface{}, error) {
- apiInfo, err1 := cache.GetApiData(apiCode)
- if err1 != nil{
- return nil, err1
- }
-
- transUnits := apiInfo.TransUnits
- if len(transUnits) > 0 {
- trans := transUnits[0]
- oamType := trans.OamType
- sqlStatement := trans.SqlStatement
- sqlSelect := trans.SqlSelect
- sqlFrom := trans.SqlFrom
- sqlWhere := trans.SqlWhere
- sqlOrder := trans.SqlOrder
-
- if oamType == 1 {
- return a.dataBus.GetById(sqlStatement, m)
- }
-
- if oamType == 2 {
- return a.dataBus.GetList(sqlSelect, sqlFrom, sqlWhere, sqlOrder, m)
- }
-
- return a.dataBus.GetList(sqlSelect, sqlFrom, sqlWhere, sqlOrder, m)
- }
-
- return nil, errors.New("API交易数据不完整")
- }
- func (a *apiGatewayService) Handle(apiCode string, action int, m map[string]interface{}) error{
- apiInfo, err1 := cache.GetApiData(apiCode)
- if err1 == nil{
- return err1
- }
-
- transUnits := apiInfo.TransUnits
- for _, transUnit := range transUnits {
- objectCode := transUnit.ObjectCode
-
- objectInfo, err2 := cache.GetObjectData(objectCode)
- if err2 == nil {
- return err2
- }
-
- if action == 1 {
- err5 := a.dataBus.Create(objectInfo, m)
- if err5 == nil {
- return err5
- }
- }
- if action == 2 {
- err5 := a.dataBus.Update(objectInfo, m)
- if err5 == nil {
- return err5
- }
- }
- if action == 3 {
- err5 := a.dataBus.Patch(objectInfo, m)
- if err5 == nil {
- return err5
- }
- }
- if action == 4 {
- err5 := a.dataBus.Delete(objectInfo, m)
- if err5 == nil {
- return err5
- }
- }
- }
-
- return nil
- }
- func (a *apiGatewayService) Create(apiCode string, m map[string]interface{}) error {
- return a.Handle(apiCode, 1, m)
- }
- func (a *apiGatewayService) Update(apiCode string, m map[string]interface{}) error {
- return a.Handle(apiCode, 2, m)
- }
- func (a *apiGatewayService) Patch(apiCode string, m map[string]interface{}) error {
- return a.Handle(apiCode, 3, m)
- }
- func (a *apiGatewayService) Delete(apiCode string, m map[string]interface{}) error {
- return a.Handle(apiCode, 4, m)
- }
|