push_app_info_repository.go 1.8 KB
package repository

import (
	"github.com/astaxie/beego/orm"
	"openapi/pkg/domain"
	"openapi/pkg/infrastructure/bgorm/models"
	"sync"
)

var Cache sync.Map

type AppInfoRepository struct {
}

func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) {
	o := orm.NewOrm()
	model := new(models.PushAppInfo)
	var appInfo *domain.AppInfo

	//var ok bool
	var err error
	//if appInfo, ok = getCache(queryOptions["project_id"]); ok {
	//	return appInfo, nil
	//}

	qs := o.QueryTable(model).Filter("project_id", queryOptions["project_id"])
	err = qs.One(model)
	if err != nil {
		return nil, err
	}
	appInfo, err = repository.transformBgormModelToDomainModel(model)
	//setCache(queryOptions["project_id"], appInfo)
	return appInfo, err
}

func (repository *AppInfoRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.AppInfo, err error) {
	return
}

func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *models.PushAppInfo) (*domain.AppInfo, error) {
	return &domain.AppInfo{
		Id:              model.Id,
		AppKey:          model.AppKey,
		AppMasterSecret: model.AppMasterSecret,
		AppId:           model.AppId,
		ProjectId:       model.ProjectId,
		ExtInfo:         model.ExtInfo,
	}, nil
}

func NewAppInfoRepository(transactionContext interface{}) (*AppInfoRepository, error) {
	if transactionContext == nil {
		return &AppInfoRepository{}, nil
	} else {
		return &AppInfoRepository{}, nil
	}
}

//设置缓存
func setCache(key interface{}, value interface{}) {
	Cache.Store(key, value)
}

//设置缓存
func getCache(key interface{}) (*domain.AppInfo, bool) {
	v, ok := Cache.Load(key)
	if !ok {
		return nil, false
	}
	if appInfo, ok := v.(*domain.AppInfo); ok {
		return appInfo, true
	}
	return nil, false
}