正在显示
9 个修改的文件
包含
225 行增加
和
22 行删除
@@ -7,22 +7,25 @@ require ( | @@ -7,22 +7,25 @@ require ( | ||
7 | github.com/aliyun/alibaba-cloud-sdk-go v1.60.348 | 7 | github.com/aliyun/alibaba-cloud-sdk-go v1.60.348 |
8 | github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309 | 8 | github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309 |
9 | github.com/astaxie/beego v1.10.0 | 9 | github.com/astaxie/beego v1.10.0 |
10 | + github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect | ||
10 | github.com/fatih/structs v1.1.0 // indirect | 11 | github.com/fatih/structs v1.1.0 // indirect |
11 | github.com/gavv/httpexpect v2.0.0+incompatible | 12 | github.com/gavv/httpexpect v2.0.0+incompatible |
12 | github.com/go-sql-driver/mysql v1.4.1 | 13 | github.com/go-sql-driver/mysql v1.4.1 |
13 | github.com/google/go-querystring v1.0.0 // indirect | 14 | github.com/google/go-querystring v1.0.0 // indirect |
14 | github.com/imkira/go-interpol v1.1.0 // indirect | 15 | github.com/imkira/go-interpol v1.1.0 // indirect |
15 | - github.com/klauspost/cpuid v1.2.3 // indirect | 16 | + github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect |
17 | + github.com/mattn/go-colorable v0.1.7 // indirect | ||
16 | github.com/moul/http2curl v1.0.0 // indirect | 18 | github.com/moul/http2curl v1.0.0 // indirect |
17 | github.com/onsi/ginkgo v1.10.3 | 19 | github.com/onsi/ginkgo v1.10.3 |
18 | github.com/onsi/gomega v1.7.1 | 20 | github.com/onsi/gomega v1.7.1 |
19 | - github.com/satori/go.uuid v1.2.0 | 21 | + github.com/satori/go.uuid v1.2.0 // indirect |
20 | github.com/sergi/go-diff v1.1.0 // indirect | 22 | github.com/sergi/go-diff v1.1.0 // indirect |
21 | github.com/valyala/fasthttp v1.13.1 // indirect | 23 | github.com/valyala/fasthttp v1.13.1 // indirect |
22 | github.com/xeipuuv/gojsonschema v1.2.0 // indirect | 24 | github.com/xeipuuv/gojsonschema v1.2.0 // indirect |
23 | github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect | 25 | github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect |
24 | github.com/yudai/gojsondiff v1.0.0 // indirect | 26 | github.com/yudai/gojsondiff v1.0.0 // indirect |
25 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect | 27 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect |
28 | + github.com/yudai/pp v2.0.1+incompatible // indirect | ||
26 | gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 | 29 | gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 |
27 | ) | 30 | ) |
28 | 31 |
@@ -15,7 +15,9 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | @@ -15,7 +15,9 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | ||
15 | var ( | 15 | var ( |
16 | repApp, _ = repository.NewAppInfoRepository(nil) | 16 | repApp, _ = repository.NewAppInfoRepository(nil) |
17 | repDevice, _ = repository.NewPushDeviceRepository(nil) | 17 | repDevice, _ = repository.NewPushDeviceRepository(nil) |
18 | + repProject, _ = repository.NewProjectRepository() | ||
18 | appInfo *protocol.AppInfo | 19 | appInfo *protocol.AppInfo |
20 | + project *protocol.Project | ||
19 | receivers []string | 21 | receivers []string |
20 | receiverIds []int64 | 22 | receiverIds []int64 |
21 | deviceList []*protocol.Device | 23 | deviceList []*protocol.Device |
@@ -27,14 +29,19 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | @@ -27,14 +29,19 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | ||
27 | } | 29 | } |
28 | ) | 30 | ) |
29 | rsp = &protocol.PushInfoResponse{} | 31 | rsp = &protocol.PushInfoResponse{} |
30 | - if appInfo, err = repApp.FindOne(map[string]interface{}{"project_key": request.ProjectKey}); err != nil { | 32 | + if project, err = repProject.FindOne(map[string]interface{}{"project_slave_key": request.ProjectKey}); err != nil { |
33 | + log.Error(err) | ||
34 | + err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey)) | ||
35 | + return | ||
36 | + } | ||
37 | + if appInfo, err = repApp.FindOne(map[string]interface{}{"project_id": project.Id}); err != nil { | ||
31 | log.Error(err) | 38 | log.Error(err) |
32 | err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey)) | 39 | err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey)) |
33 | return | 40 | return |
34 | } | 41 | } |
35 | if deviceList, err = repDevice.Find( | 42 | if deviceList, err = repDevice.Find( |
36 | map[string]interface{}{ | 43 | map[string]interface{}{ |
37 | - "receivers": request.Receivers, "project_master_key": appInfo.ProjectMasterKey}, | 44 | + "receivers": request.Receivers, "project_master_key": project.ProjectMasterKey}, |
38 | ); err != nil { | 45 | ); err != nil { |
39 | log.Error(err) | 46 | log.Error(err) |
40 | err = nil | 47 | err = nil |
pkg/domain/project.go
0 → 100644
@@ -31,7 +31,7 @@ type PushInfoRequest struct { | @@ -31,7 +31,7 @@ type PushInfoRequest struct { | ||
31 | 31 | ||
32 | /*UpdateDevice 更新设备*/ | 32 | /*UpdateDevice 更新设备*/ |
33 | type UpdateDeviceRequest struct { | 33 | type UpdateDeviceRequest struct { |
34 | - ProjectKey string `json:"projectKey"` //项目编号 | 34 | + ProjectKey string `json:"projectKey"` //项目主编号 |
35 | Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID | 35 | Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID |
36 | ClientId string `json:"clientId" valid:"Required"` | 36 | ClientId string `json:"clientId" valid:"Required"` |
37 | DeviceToken string `json:"deviceToken"` | 37 | DeviceToken string `json:"deviceToken"` |
@@ -53,7 +53,5 @@ type AppInfo struct { | @@ -53,7 +53,5 @@ type AppInfo struct { | ||
53 | AppKey string | 53 | AppKey string |
54 | AppMasterSecret string | 54 | AppMasterSecret string |
55 | AppId string | 55 | AppId string |
56 | - ProjectName string //子项目名称 | ||
57 | - ProjectKey string //包含在主项目内-子项目编码 worth | ||
58 | - ProjectMasterKey string //主项目编码 ability | 56 | + ProjectId int //项目编号 |
59 | } | 57 | } |
pkg/infrastructure/bgorm/model/project.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/astaxie/beego/orm" | ||
6 | +) | ||
7 | + | ||
8 | +type Project struct { | ||
9 | + Id int `orm:"column(id);auto" description:"编号"` | ||
10 | + ProjectName string `orm:"column(project_name);size(255);null" description:"项目名称 能力展示"` | ||
11 | + ProjectSlaveKey string `orm:"column(project_slave_key);size(255);null" description:"项目从键 ability"` | ||
12 | + ProjectMasterKey string `orm:"column(project_master_key);size(50);null" description:"主项目主键编码 ability"` | ||
13 | +} | ||
14 | + | ||
15 | +func (t *Project) TableName() string { | ||
16 | + return "project" | ||
17 | +} | ||
18 | + | ||
19 | +func init() { | ||
20 | + orm.RegisterModel(new(Project)) | ||
21 | +} | ||
22 | + | ||
23 | +// AddProject insert a new Project into database and returns | ||
24 | +// last inserted Id on success. | ||
25 | +func AddProject(m *Project) (id int64, err error) { | ||
26 | + o := orm.NewOrm() | ||
27 | + id, err = o.Insert(m) | ||
28 | + return | ||
29 | +} | ||
30 | + | ||
31 | +// GetProjectById retrieves Project by Id. Returns error if | ||
32 | +// Id doesn't exist | ||
33 | +func GetProjectById(id int) (v *Project, err error) { | ||
34 | + o := orm.NewOrm() | ||
35 | + v = &Project{Id: id} | ||
36 | + if err = o.Read(v); err == nil { | ||
37 | + return v, nil | ||
38 | + } | ||
39 | + return nil, err | ||
40 | +} | ||
41 | + | ||
42 | +// UpdateProject updates Project by Id and returns error if | ||
43 | +// the record to be updated doesn't exist | ||
44 | +func UpdateProjectById(m *Project) (err error) { | ||
45 | + o := orm.NewOrm() | ||
46 | + v := Project{Id: m.Id} | ||
47 | + // ascertain id exists in the database | ||
48 | + if err = o.Read(&v); err == nil { | ||
49 | + var num int64 | ||
50 | + if num, err = o.Update(m); err == nil { | ||
51 | + fmt.Println("Number of records updated in database:", num) | ||
52 | + } | ||
53 | + } | ||
54 | + return | ||
55 | +} | ||
56 | + | ||
57 | +// DeleteProject deletes Project by Id and returns error if | ||
58 | +// the record to be deleted doesn't exist | ||
59 | +func DeleteProject(id int) (err error) { | ||
60 | + o := orm.NewOrm() | ||
61 | + v := Project{Id: id} | ||
62 | + // ascertain id exists in the database | ||
63 | + if err = o.Read(&v); err == nil { | ||
64 | + var num int64 | ||
65 | + if num, err = o.Delete(&Project{Id: id}); err == nil { | ||
66 | + fmt.Println("Number of records deleted in database:", num) | ||
67 | + } | ||
68 | + } | ||
69 | + return | ||
70 | +} |
@@ -14,9 +14,7 @@ type PushAppInfo struct { | @@ -14,9 +14,7 @@ type PushAppInfo struct { | ||
14 | AppKey string `orm:"column(app_key);size(255);null" description:"推送key "` | 14 | AppKey string `orm:"column(app_key);size(255);null" description:"推送key "` |
15 | AppMasterSecret string `orm:"column(app_master_secret);size(255);null" description:"推送服务端密钥"` | 15 | AppMasterSecret string `orm:"column(app_master_secret);size(255);null" description:"推送服务端密钥"` |
16 | AppId string `orm:"column(app_id);size(255);null" description:"推送应用编号"` | 16 | AppId string `orm:"column(app_id);size(255);null" description:"推送应用编号"` |
17 | - ProjectName string `orm:"column(project_name);size(255);null" description:"项目名称 能力展示"` | ||
18 | - ProjectKey string `orm:"column(project_key);size(255);null" description:"项目唯一标识 ability"` | ||
19 | - ProjectMasterKey string `orm:"column(project_master_key);size(50);null" description:"主项目唯一标识 ability"` | 17 | + ProjectId int `orm:"column(project_id);size(255);null" description:"项目编号"` |
20 | } | 18 | } |
21 | 19 | ||
22 | func (t *PushAppInfo) TableName() string { | 20 | func (t *PushAppInfo) TableName() string { |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/astaxie/beego/orm" | ||
6 | + "openapi/pkg/domain" | ||
7 | + "openapi/pkg/infrastructure/bgorm/model" | ||
8 | + . "openapi/pkg/infrastructure/utils" | ||
9 | +) | ||
10 | + | ||
11 | +type ProjectRepository struct{} | ||
12 | + | ||
13 | +func (repository *ProjectRepository) Save(dm *domain.Project) (*domain.Project, error) { | ||
14 | + var ( | ||
15 | + err error | ||
16 | + m = &models.Project{} | ||
17 | + ) | ||
18 | + if err = GobModelTransform(m, dm); err != nil { | ||
19 | + return nil, err | ||
20 | + } | ||
21 | + if dm.Identify() == nil { | ||
22 | + if _, err = models.AddProject(m); err != nil { | ||
23 | + return nil, err | ||
24 | + } | ||
25 | + return dm, nil | ||
26 | + } | ||
27 | + if err = models.UpdateProjectById(m); err != nil { | ||
28 | + return nil, err | ||
29 | + } | ||
30 | + return dm, nil | ||
31 | +} | ||
32 | + | ||
33 | +func (repository *ProjectRepository) Remove(Project *domain.Project) (*domain.Project, error) { | ||
34 | + if err := models.DeleteProject(Project.Id); err != nil { | ||
35 | + return nil, err | ||
36 | + } | ||
37 | + return nil, nil | ||
38 | +} | ||
39 | + | ||
40 | +func (repository *ProjectRepository) FindOne(queryOptions map[string]interface{}) (*domain.Project, error) { | ||
41 | + m := new(models.Project) | ||
42 | + //key := queryOptions["project_slave_key"].(string) | ||
43 | + //if v, ok := Cache.Load(key); ok { | ||
44 | + // return repository.transformPgModelToDomainModel(v.(*models.Project)) | ||
45 | + //} | ||
46 | + | ||
47 | + qb, _ := orm.NewQueryBuilder("mysql") | ||
48 | + qb.Select("*") | ||
49 | + qb.From("project") | ||
50 | + if v, ok := queryOptions["project_slave_key"]; ok { | ||
51 | + qb.Where(fmt.Sprintf("project_slave_key = '%v'", v)) | ||
52 | + } | ||
53 | + sql := qb.String() | ||
54 | + o := orm.NewOrm() | ||
55 | + if err := o.Raw(sql).QueryRow(m); err != nil { | ||
56 | + return nil, err | ||
57 | + } | ||
58 | + //Cache.Store(key, m) | ||
59 | + return repository.transformPgModelToDomainModel(m) | ||
60 | +} | ||
61 | + | ||
62 | +func (repository *ProjectRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Project, error) { | ||
63 | + var models []*models.Project | ||
64 | + dms := make([]*domain.Project, 0) | ||
65 | + qb, _ := orm.NewQueryBuilder("mysql") | ||
66 | + qb.From("project") | ||
67 | + if v, ok := queryOptions["project_key"]; ok { | ||
68 | + qb.Where(fmt.Sprintf("project_key = '%v'", v)) | ||
69 | + } | ||
70 | + sql := qb.String() | ||
71 | + o := orm.NewOrm() | ||
72 | + if _, err := o.Raw(sql).QueryRows(models); err != nil { | ||
73 | + return 0, nil, err | ||
74 | + } | ||
75 | + for _, m := range models { | ||
76 | + if dm, err := repository.transformPgModelToDomainModel(m); err != nil { | ||
77 | + return 0, nil, err | ||
78 | + } else { | ||
79 | + dms = append(dms, dm) | ||
80 | + } | ||
81 | + } | ||
82 | + return int64(len(dms)), dms, nil | ||
83 | +} | ||
84 | + | ||
85 | +func (repository *ProjectRepository) transformPgModelToDomainModel(model *models.Project) (*domain.Project, error) { | ||
86 | + return &domain.Project{ | ||
87 | + Id: model.Id, | ||
88 | + ProjectName: model.ProjectName, | ||
89 | + ProjectSlaveKey: model.ProjectSlaveKey, | ||
90 | + ProjectMasterKey: model.ProjectMasterKey, | ||
91 | + }, nil | ||
92 | +} | ||
93 | + | ||
94 | +func NewProjectRepository() (*ProjectRepository, error) { | ||
95 | + return &ProjectRepository{}, nil | ||
96 | +} |
@@ -15,18 +15,21 @@ type AppInfoRepository struct { | @@ -15,18 +15,21 @@ type AppInfoRepository struct { | ||
15 | func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) { | 15 | func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) { |
16 | o := orm.NewOrm() | 16 | o := orm.NewOrm() |
17 | model := new(models.PushAppInfo) | 17 | model := new(models.PushAppInfo) |
18 | - qs := o.QueryTable(model).Filter("project_key", queryOptions["project_key"]) | ||
19 | - err := qs.One(model) | 18 | + var appInfo *domain.AppInfo |
19 | + | ||
20 | + //var ok bool | ||
21 | + var err error | ||
22 | + //if appInfo, ok = getCache(queryOptions["project_id"]); ok { | ||
23 | + // return appInfo, nil | ||
24 | + //} | ||
25 | + | ||
26 | + qs := o.QueryTable(model).Filter("project_id", queryOptions["project_id"]) | ||
27 | + err = qs.One(model) | ||
20 | if err != nil { | 28 | if err != nil { |
21 | return nil, err | 29 | return nil, err |
22 | } | 30 | } |
23 | - var appInfo *domain.AppInfo | ||
24 | - var ok bool | ||
25 | - if appInfo, ok = getCache(queryOptions["project_key"]); ok { | ||
26 | - return appInfo, nil | ||
27 | - } | ||
28 | appInfo, err = repository.transformBgormModelToDomainModel(model) | 31 | appInfo, err = repository.transformBgormModelToDomainModel(model) |
29 | - setCache(queryOptions["project_key"], appInfo) | 32 | + //setCache(queryOptions["project_id"], appInfo) |
30 | return appInfo, err | 33 | return appInfo, err |
31 | } | 34 | } |
32 | 35 | ||
@@ -40,9 +43,7 @@ func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *mod | @@ -40,9 +43,7 @@ func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *mod | ||
40 | AppKey: model.AppKey, | 43 | AppKey: model.AppKey, |
41 | AppMasterSecret: model.AppMasterSecret, | 44 | AppMasterSecret: model.AppMasterSecret, |
42 | AppId: model.AppId, | 45 | AppId: model.AppId, |
43 | - ProjectName: model.ProjectName, | ||
44 | - ProjectKey: model.ProjectKey, | ||
45 | - ProjectMasterKey: model.ProjectMasterKey, | 46 | + ProjectId: model.ProjectId, |
46 | }, nil | 47 | }, nil |
47 | } | 48 | } |
48 | 49 |
@@ -2,6 +2,7 @@ package utils | @@ -2,6 +2,7 @@ package utils | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "bytes" | 4 | "bytes" |
5 | + "encoding/gob" | ||
5 | "errors" | 6 | "errors" |
6 | "fmt" | 7 | "fmt" |
7 | "github.com/astaxie/beego/orm" | 8 | "github.com/astaxie/beego/orm" |
@@ -242,3 +243,17 @@ func (s *SqlExcutor) WhereString() string { | @@ -242,3 +243,17 @@ func (s *SqlExcutor) WhereString() string { | ||
242 | } | 243 | } |
243 | return sql.String() | 244 | return sql.String() |
244 | } | 245 | } |
246 | + | ||
247 | +//GobModelTransform 模型转换 | ||
248 | +func GobModelTransform(dst interface{}, src interface{}) error { | ||
249 | + var data bytes.Buffer | ||
250 | + enc := gob.NewEncoder(&data) | ||
251 | + if err := enc.Encode(src); err != nil { | ||
252 | + return err | ||
253 | + } | ||
254 | + dec := gob.NewDecoder(&data) | ||
255 | + if err := dec.Decode(dst); err != nil { | ||
256 | + return err | ||
257 | + } | ||
258 | + return nil | ||
259 | +} |
-
请 注册 或 登录 后发表评论