作者 yangfu

增加企业缓存

返回组织对应的企业信息
@@ -13,6 +13,8 @@ type GetOrgQuery struct { @@ -13,6 +13,8 @@ type GetOrgQuery struct {
13 OperateInfo *domain.OperateInfo `json:"-"` 13 OperateInfo *domain.OperateInfo `json:"-"`
14 // 组织ID 14 // 组织ID
15 OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"` 15 OrgId int64 `cname:"组织ID" json:"orgId" valid:"Required"`
  16 + // 获取标记 bit 0:获取企业数据
  17 + FetchFlag int `cname:"获取标记" json:"fetchFlag,omitempty"`
16 } 18 }
17 19
18 func (getOrgQuery *GetOrgQuery) Valid(validation *validation.Validation) { 20 func (getOrgQuery *GetOrgQuery) Valid(validation *validation.Validation) {
@@ -9,6 +9,10 @@ import ( @@ -9,6 +9,10 @@ import (
9 "github.com/beego/beego/v2/core/validation" 9 "github.com/beego/beego/v2/core/validation"
10 ) 10 )
11 11
  12 +const (
  13 + FetchCompanyInfo = 1 << iota
  14 +)
  15 +
12 type ListOrgQuery struct { 16 type ListOrgQuery struct {
13 OperateInfo *domain.OperateInfo `json:"-"` 17 OperateInfo *domain.OperateInfo `json:"-"`
14 // 查询偏离量 18 // 查询偏离量
@@ -154,12 +154,18 @@ func (orgService *OrgService) GetOrg(getOrgQuery *query.GetOrgQuery) (interface{ @@ -154,12 +154,18 @@ func (orgService *OrgService) GetOrg(getOrgQuery *query.GetOrgQuery) (interface{
154 } 154 }
155 if org == nil { 155 if org == nil {
156 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrgQuery.OrgId))) 156 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrgQuery.OrgId)))
157 - } else {  
158 - if err := transactionContext.CommitTransaction(); err != nil {  
159 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 157 + }
  158 + if getOrgQuery.FetchFlag&query.FetchCompanyInfo > 0 && org.CompanyId > 0 {
  159 + companyRepository, _, _ := factory.FastPgCompany(transactionContext, 0)
  160 + if company, err := companyRepository.FindOne(map[string]interface{}{"companyId": org.CompanyId}); err == nil && company != nil {
  161 + org.Company = company.ToCompanyVisible()
160 } 162 }
161 - return org, nil  
162 } 163 }
  164 + if err := transactionContext.CommitTransaction(); err != nil {
  165 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  166 + }
  167 + return org, nil
  168 +
163 } 169 }
164 170
165 // 获取组织的子部门(通用部门列表使用) 171 // 获取组织的子部门(通用部门列表使用)
@@ -215,25 +221,18 @@ func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interfa @@ -215,25 +221,18 @@ func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interfa
215 defer func() { 221 defer func() {
216 transactionContext.RollbackTransaction() 222 transactionContext.RollbackTransaction()
217 }() 223 }()
218 - var orgRepository domain.OrgRepository  
219 - if value, err := factory.CreateOrgRepository(map[string]interface{}{  
220 - "transactionContext": transactionContext,  
221 - }); err != nil { 224 + orgRepository, _, _ := factory.FastPgOrg(transactionContext, 0)
  225 + count, orgs, err := orgRepository.Find(utils.ObjectToMap(listOrgQuery))
  226 + if err != nil {
222 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 227 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
223 - } else {  
224 - orgRepository = value  
225 } 228 }
226 - if count, orgs, err := orgRepository.Find(utils.ObjectToMap(listOrgQuery)); err != nil {  
227 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
228 - } else {  
229 - if err := transactionContext.CommitTransaction(); err != nil {  
230 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
231 - }  
232 - return map[string]interface{}{  
233 - "count": count,  
234 - "orgs": orgs,  
235 - }, nil 229 + if err := transactionContext.CommitTransaction(); err != nil {
  230 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
236 } 231 }
  232 + return map[string]interface{}{
  233 + "count": count,
  234 + "orgs": orgs,
  235 + }, nil
237 } 236 }
238 237
239 // 移除组织 238 // 移除组织
1 package domain 1 package domain
2 2
3 -import "time" 3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
  6 + "time"
  7 +)
4 8
5 // 公司状态状态 1:已注册 2:待认证 3:已认证 9 // 公司状态状态 1:已注册 2:待认证 3:已认证
6 const ( 10 const (
@@ -76,9 +80,8 @@ func (company *Company) Update(data map[string]interface{}) error { @@ -76,9 +80,8 @@ func (company *Company) Update(data map[string]interface{}) error {
76 if createdAt, ok := data["createdAt"]; ok { 80 if createdAt, ok := data["createdAt"]; ok {
77 company.CreatedAt = createdAt.(time.Time) 81 company.CreatedAt = createdAt.(time.Time)
78 } 82 }
79 - if updatedAt, ok := data["updatedAt"]; ok {  
80 - company.UpdatedAt = updatedAt.(time.Time)  
81 - } 83 +
  84 + company.UpdatedAt = time.Now()
82 return nil 85 return nil
83 } 86 }
84 87
@@ -89,3 +92,34 @@ func (company *Company) CloneSample() *Company { @@ -89,3 +92,34 @@ func (company *Company) CloneSample() *Company {
89 CompanyInfo: company.CompanyInfo, 92 CompanyInfo: company.CompanyInfo,
90 } 93 }
91 } 94 }
  95 +
  96 +func (company *Company) ToCompanyVisible() *CompanyVisible {
  97 + return &CompanyVisible{
  98 + CompanyId: company.CompanyId,
  99 + CompanyInfo: *company.CompanyInfo,
  100 + Status: company.Status,
  101 + SystemName: company.CompanyConfig.SystemName,
  102 + }
  103 +}
  104 +
  105 +/***** 2.缓存模块 *****/
  106 +
  107 +func (company *Company) CacheKeyFunc() string {
  108 + if constant.DISABLE_REPOSITORY_CACHE {
  109 + return ""
  110 + }
  111 + if company.CompanyId == 0 {
  112 + return ""
  113 + }
  114 + return fmt.Sprintf("%v:cache:company:id:%v", constant.CACHE_PREFIX, company.CompanyId)
  115 +}
  116 +
  117 +type CompanyVisible struct {
  118 + // 企业id
  119 + CompanyId int64 `json:"companyId"`
  120 + // 企业基本信息
  121 + CompanyInfo
  122 + Status int `json:"status"`
  123 + // 系统名称
  124 + SystemName string `json:"systemName,omitempty"`
  125 +}
@@ -46,6 +46,9 @@ type Org struct { @@ -46,6 +46,9 @@ type Org struct {
46 ParentId int64 `json:"parentId,omitempty"` 46 ParentId int64 `json:"parentId,omitempty"`
47 // 父级节点路径("0,11,12,") 47 // 父级节点路径("0,11,12,")
48 ParentPath string `json:"parentPath,omitempty"` 48 ParentPath string `json:"parentPath,omitempty"`
  49 +
  50 + // 企业id
  51 + Company *CompanyVisible `json:"company,omitempty"`
49 } 52 }
50 53
51 type OrgRepository interface { 54 type OrgRepository interface {
@@ -3,6 +3,8 @@ package repository @@ -3,6 +3,8 @@ package repository
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/go-pg/pg/v10" 5 "github.com/go-pg/pg/v10"
  6 + "github.com/linmadan/egglib-go/persistent/cache"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
6 8
7 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" 9 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
8 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 10 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
@@ -13,6 +15,7 @@ import ( @@ -13,6 +15,7 @@ import (
13 ) 15 )
14 16
15 type CompanyRepository struct { 17 type CompanyRepository struct {
  18 + *cache.CachedRepository
16 transactionContext *pgTransaction.TransactionContext 19 transactionContext *pgTransaction.TransactionContext
17 } 20 }
18 21
@@ -66,23 +69,29 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp @@ -66,23 +69,29 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp
66 return company, err 69 return company, err
67 } 70 }
68 } else { 71 } else {
69 - if _, err := tx.QueryOne(  
70 - pg.Scan(  
71 - &company.CompanyId,  
72 - &company.CompanyConfig,  
73 - &company.CompanyInfo,  
74 - &company.Status,  
75 - &company.CreatedAt,  
76 - &company.UpdatedAt,  
77 - ),  
78 - fmt.Sprintf("UPDATE users.company SET %s WHERE company_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),  
79 - company.CompanyConfig,  
80 - company.CompanyInfo,  
81 - company.Status,  
82 - company.CreatedAt,  
83 - company.UpdatedAt,  
84 - company.Identify(),  
85 - ); err != nil { 72 + queryFunc := func() (interface{}, error) {
  73 + if _, err := tx.QueryOne(
  74 + pg.Scan(
  75 + &company.CompanyId,
  76 + &company.CompanyConfig,
  77 + &company.CompanyInfo,
  78 + &company.Status,
  79 + &company.CreatedAt,
  80 + &company.UpdatedAt,
  81 + ),
  82 + fmt.Sprintf("UPDATE users.company SET %s WHERE company_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  83 + company.CompanyConfig,
  84 + company.CompanyInfo,
  85 + company.Status,
  86 + company.CreatedAt,
  87 + company.UpdatedAt,
  88 + company.Identify(),
  89 + ); err != nil {
  90 + return company, err
  91 + }
  92 + return company, nil
  93 + }
  94 + if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
86 return company, err 95 return company, err
87 } 96 }
88 } 97 }
@@ -92,7 +101,13 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co @@ -92,7 +101,13 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co
92 tx := repository.transactionContext.PgTx 101 tx := repository.transactionContext.PgTx
93 companyModel := new(models.Company) 102 companyModel := new(models.Company)
94 companyModel.CompanyId = company.Identify().(int64) 103 companyModel.CompanyId = company.Identify().(int64)
95 - if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil { 104 + queryFunc := func() (interface{}, error) {
  105 + if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil {
  106 + return company, err
  107 + }
  108 + return company, nil
  109 + }
  110 + if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
96 return company, err 111 return company, err
97 } 112 }
98 return company, nil 113 return company, nil
@@ -100,18 +115,28 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co @@ -100,18 +115,28 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co
100 func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) { 115 func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
101 tx := repository.transactionContext.PgTx 116 tx := repository.transactionContext.PgTx
102 companyModel := new(models.Company) 117 companyModel := new(models.Company)
103 - query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)  
104 - query.SetWhereByQueryOption("company.company_id = ?", "companyId") 118 + queryFunc := func() (interface{}, error) {
  119 + query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)
  120 + query.SetWhereByQueryOption("company.company_id = ?", "companyId")
105 121
106 - if v, ok := queryOptions["companyName"]; ok {  
107 - query.Where(fmt.Sprintf(`company_info @>'{"companyName":"%v"}'`, v))  
108 - }  
109 - if err := query.First(); err != nil {  
110 - if err.Error() == "pg: no rows in result set" {  
111 - return nil, domain.ErrorNotFound  
112 - } else {  
113 - return nil, err 122 + if v, ok := queryOptions["companyName"]; ok {
  123 + query.Where(fmt.Sprintf(`company_info @>'{"companyName":"%v"}'`, v))
  124 + }
  125 + if err := query.First(); err != nil {
  126 + if err.Error() == "pg: no rows in result set" {
  127 + return nil, domain.ErrorNotFound
  128 + } else {
  129 + return nil, err
  130 + }
114 } 131 }
  132 + return companyModel, nil
  133 + }
  134 + var cacheModel = &domain.Company{}
  135 + if _, ok := queryOptions["companyId"]; ok {
  136 + cacheModel.CompanyId = queryOptions["companyId"].(int64)
  137 + }
  138 + if err := repository.QueryCache(cacheModel.CacheKeyFunc, companyModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
  139 + return nil, err
115 } 140 }
116 if companyModel.CompanyId == 0 { 141 if companyModel.CompanyId == 0 {
117 return nil, nil 142 return nil, nil
@@ -145,6 +170,7 @@ func NewCompanyRepository(transactionContext *pgTransaction.TransactionContext) @@ -145,6 +170,7 @@ func NewCompanyRepository(transactionContext *pgTransaction.TransactionContext)
145 } else { 170 } else {
146 return &CompanyRepository{ 171 return &CompanyRepository{
147 transactionContext: transactionContext, 172 transactionContext: transactionContext,
  173 + CachedRepository: cache.NewDefaultCachedRepository(),
148 }, nil 174 }, nil
149 } 175 }
150 } 176 }
@@ -36,6 +36,7 @@ func (controller *OrgController) GetOrg() { @@ -36,6 +36,7 @@ func (controller *OrgController) GetOrg() {
36 getOrgQuery := &query.GetOrgQuery{} 36 getOrgQuery := &query.GetOrgQuery{}
37 orgId, _ := controller.GetInt64(":orgId") 37 orgId, _ := controller.GetInt64(":orgId")
38 getOrgQuery.OrgId = orgId 38 getOrgQuery.OrgId = orgId
  39 + getOrgQuery.FetchFlag, _ = controller.GetInt("fetchFlag")
39 getOrgQuery.OperateInfo = ParseOperateInfo(controller.BaseController) 40 getOrgQuery.OperateInfo = ParseOperateInfo(controller.BaseController)
40 data, err := orgService.GetOrg(getOrgQuery) 41 data, err := orgService.GetOrg(getOrgQuery)
41 controller.Response(data, err) 42 controller.Response(data, err)