作者 yangfu

增加:project项目信息

... ... @@ -7,22 +7,25 @@ require (
github.com/aliyun/alibaba-cloud-sdk-go v1.60.348
github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309
github.com/astaxie/beego v1.10.0
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/google/go-querystring v1.0.0 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/klauspost/cpuid v1.2.3 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/onsi/ginkgo v1.10.3
github.com/onsi/gomega v1.7.1
github.com/satori/go.uuid v1.2.0
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/valyala/fasthttp v1.13.1 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1
)
... ...
... ... @@ -15,7 +15,9 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
var (
repApp, _ = repository.NewAppInfoRepository(nil)
repDevice, _ = repository.NewPushDeviceRepository(nil)
repProject, _ = repository.NewProjectRepository()
appInfo *protocol.AppInfo
project *protocol.Project
receivers []string
receiverIds []int64
deviceList []*protocol.Device
... ... @@ -27,14 +29,19 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
}
)
rsp = &protocol.PushInfoResponse{}
if appInfo, err = repApp.FindOne(map[string]interface{}{"project_key": request.ProjectKey}); err != nil {
if project, err = repProject.FindOne(map[string]interface{}{"project_slave_key": request.ProjectKey}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey))
return
}
if appInfo, err = repApp.FindOne(map[string]interface{}{"project_id": project.Id}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey))
return
}
if deviceList, err = repDevice.Find(
map[string]interface{}{
"receivers": request.Receivers, "project_master_key": appInfo.ProjectMasterKey},
"receivers": request.Receivers, "project_master_key": project.ProjectMasterKey},
); err != nil {
log.Error(err)
err = nil
... ...
package domain
type Project struct {
Id int
ProjectName string
ProjectMasterKey string
ProjectSlaveKey string
}
func (d *Project) Identify() interface{} {
if d.Id == 0 {
return nil
}
return d.Id
}
... ...
... ... @@ -31,7 +31,7 @@ type PushInfoRequest struct {
/*UpdateDevice 更新设备*/
type UpdateDeviceRequest struct {
ProjectKey string `json:"projectKey"` //项目编号
ProjectKey string `json:"projectKey"` //项目编号
Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID
ClientId string `json:"clientId" valid:"Required"`
DeviceToken string `json:"deviceToken"`
... ... @@ -49,11 +49,9 @@ type Device struct {
//应用信息
type AppInfo struct {
Id int
AppKey string
AppMasterSecret string
AppId string
ProjectName string //子项目名称
ProjectKey string //包含在主项目内-子项目编码 worth
ProjectMasterKey string //主项目编码 ability
Id int
AppKey string
AppMasterSecret string
AppId string
ProjectId int //项目编号
}
... ...
package models
import (
"fmt"
"github.com/astaxie/beego/orm"
)
type Project struct {
Id int `orm:"column(id);auto" description:"编号"`
ProjectName string `orm:"column(project_name);size(255);null" description:"项目名称 能力展示"`
ProjectSlaveKey string `orm:"column(project_slave_key);size(255);null" description:"项目从键 ability"`
ProjectMasterKey string `orm:"column(project_master_key);size(50);null" description:"主项目主键编码 ability"`
}
func (t *Project) TableName() string {
return "project"
}
func init() {
orm.RegisterModel(new(Project))
}
// AddProject insert a new Project into database and returns
// last inserted Id on success.
func AddProject(m *Project) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetProjectById retrieves Project by Id. Returns error if
// Id doesn't exist
func GetProjectById(id int) (v *Project, err error) {
o := orm.NewOrm()
v = &Project{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateProject updates Project by Id and returns error if
// the record to be updated doesn't exist
func UpdateProjectById(m *Project) (err error) {
o := orm.NewOrm()
v := Project{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteProject deletes Project by Id and returns error if
// the record to be deleted doesn't exist
func DeleteProject(id int) (err error) {
o := orm.NewOrm()
v := Project{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Project{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
... ... @@ -10,13 +10,11 @@ import (
)
type PushAppInfo struct {
Id int `orm:"column(id);auto" description:"编号"`
AppKey string `orm:"column(app_key);size(255);null" description:"推送key "`
AppMasterSecret string `orm:"column(app_master_secret);size(255);null" description:"推送服务端密钥"`
AppId string `orm:"column(app_id);size(255);null" description:"推送应用编号"`
ProjectName string `orm:"column(project_name);size(255);null" description:"项目名称 能力展示"`
ProjectKey string `orm:"column(project_key);size(255);null" description:"项目唯一标识 ability"`
ProjectMasterKey string `orm:"column(project_master_key);size(50);null" description:"主项目唯一标识 ability"`
Id int `orm:"column(id);auto" description:"编号"`
AppKey string `orm:"column(app_key);size(255);null" description:"推送key "`
AppMasterSecret string `orm:"column(app_master_secret);size(255);null" description:"推送服务端密钥"`
AppId string `orm:"column(app_id);size(255);null" description:"推送应用编号"`
ProjectId int `orm:"column(project_id);size(255);null" description:"项目编号"`
}
func (t *PushAppInfo) TableName() string {
... ...
package repository
import (
"fmt"
"github.com/astaxie/beego/orm"
"openapi/pkg/domain"
"openapi/pkg/infrastructure/bgorm/model"
. "openapi/pkg/infrastructure/utils"
)
type ProjectRepository struct{}
func (repository *ProjectRepository) Save(dm *domain.Project) (*domain.Project, error) {
var (
err error
m = &models.Project{}
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if _, err = models.AddProject(m); err != nil {
return nil, err
}
return dm, nil
}
if err = models.UpdateProjectById(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *ProjectRepository) Remove(Project *domain.Project) (*domain.Project, error) {
if err := models.DeleteProject(Project.Id); err != nil {
return nil, err
}
return nil, nil
}
func (repository *ProjectRepository) FindOne(queryOptions map[string]interface{}) (*domain.Project, error) {
m := new(models.Project)
//key := queryOptions["project_slave_key"].(string)
//if v, ok := Cache.Load(key); ok {
// return repository.transformPgModelToDomainModel(v.(*models.Project))
//}
qb, _ := orm.NewQueryBuilder("mysql")
qb.Select("*")
qb.From("project")
if v, ok := queryOptions["project_slave_key"]; ok {
qb.Where(fmt.Sprintf("project_slave_key = '%v'", v))
}
sql := qb.String()
o := orm.NewOrm()
if err := o.Raw(sql).QueryRow(m); err != nil {
return nil, err
}
//Cache.Store(key, m)
return repository.transformPgModelToDomainModel(m)
}
func (repository *ProjectRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Project, error) {
var models []*models.Project
dms := make([]*domain.Project, 0)
qb, _ := orm.NewQueryBuilder("mysql")
qb.From("project")
if v, ok := queryOptions["project_key"]; ok {
qb.Where(fmt.Sprintf("project_key = '%v'", v))
}
sql := qb.String()
o := orm.NewOrm()
if _, err := o.Raw(sql).QueryRows(models); err != nil {
return 0, nil, err
}
for _, m := range models {
if dm, err := repository.transformPgModelToDomainModel(m); err != nil {
return 0, nil, err
} else {
dms = append(dms, dm)
}
}
return int64(len(dms)), dms, nil
}
func (repository *ProjectRepository) transformPgModelToDomainModel(model *models.Project) (*domain.Project, error) {
return &domain.Project{
Id: model.Id,
ProjectName: model.ProjectName,
ProjectSlaveKey: model.ProjectSlaveKey,
ProjectMasterKey: model.ProjectMasterKey,
}, nil
}
func NewProjectRepository() (*ProjectRepository, error) {
return &ProjectRepository{}, nil
}
... ...
... ... @@ -15,18 +15,21 @@ type AppInfoRepository struct {
func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) {
o := orm.NewOrm()
model := new(models.PushAppInfo)
qs := o.QueryTable(model).Filter("project_key", queryOptions["project_key"])
err := qs.One(model)
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
}
var appInfo *domain.AppInfo
var ok bool
if appInfo, ok = getCache(queryOptions["project_key"]); ok {
return appInfo, nil
}
appInfo, err = repository.transformBgormModelToDomainModel(model)
setCache(queryOptions["project_key"], appInfo)
//setCache(queryOptions["project_id"], appInfo)
return appInfo, err
}
... ... @@ -36,13 +39,11 @@ func (repository *AppInfoRepository) Find(queryOptions map[string]interface{}) (
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,
ProjectName: model.ProjectName,
ProjectKey: model.ProjectKey,
ProjectMasterKey: model.ProjectMasterKey,
Id: model.Id,
AppKey: model.AppKey,
AppMasterSecret: model.AppMasterSecret,
AppId: model.AppId,
ProjectId: model.ProjectId,
}, nil
}
... ...
... ... @@ -2,6 +2,7 @@ package utils
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"github.com/astaxie/beego/orm"
... ... @@ -242,3 +243,17 @@ func (s *SqlExcutor) WhereString() string {
}
return sql.String()
}
//GobModelTransform 模型转换
func GobModelTransform(dst interface{}, src interface{}) error {
var data bytes.Buffer
enc := gob.NewEncoder(&data)
if err := enc.Encode(src); err != nil {
return err
}
dec := gob.NewDecoder(&data)
if err := dec.Decode(dst); err != nil {
return err
}
return nil
}
... ...