作者 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 { 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()
  162 + }
  163 + }
158 if err := transactionContext.CommitTransaction(); err != nil { 164 if err := transactionContext.CommitTransaction(); err != nil {
159 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 165 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
160 } 166 }
161 return org, nil 167 return org, nil
162 - } 168 +
163 } 169 }
164 170
165 // 获取组织的子部门(通用部门列表使用) 171 // 获取组织的子部门(通用部门列表使用)
@@ -215,17 +221,11 @@ func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interfa @@ -215,17 +221,11 @@ 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 { 229 if err := transactionContext.CommitTransaction(); err != nil {
230 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 230 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
231 } 231 }
@@ -233,7 +233,6 @@ func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interfa @@ -233,7 +233,6 @@ func (orgService *OrgService) ListOrg(listOrgQuery *query.ListOrgQuery) (interfa
233 "count": count, 233 "count": count,
234 "orgs": orgs, 234 "orgs": orgs,
235 }, nil 235 }, nil
236 - }  
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,6 +69,7 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp @@ -66,6 +69,7 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp
66 return company, err 69 return company, err
67 } 70 }
68 } else { 71 } else {
  72 + queryFunc := func() (interface{}, error) {
69 if _, err := tx.QueryOne( 73 if _, err := tx.QueryOne(
70 pg.Scan( 74 pg.Scan(
71 &company.CompanyId, 75 &company.CompanyId,
@@ -85,6 +89,11 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp @@ -85,6 +89,11 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp
85 ); err != nil { 89 ); err != nil {
86 return company, err 90 return company, err
87 } 91 }
  92 + return company, nil
  93 + }
  94 + if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
  95 + return company, err
  96 + }
88 } 97 }
89 return company, nil 98 return company, nil
90 } 99 }
@@ -92,14 +101,21 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co @@ -92,14 +101,21 @@ 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)
  104 + queryFunc := func() (interface{}, error) {
95 if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil { 105 if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil {
96 return company, err 106 return company, err
97 } 107 }
98 return company, nil 108 return company, nil
  109 + }
  110 + if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
  111 + return company, err
  112 + }
  113 + return company, nil
99 } 114 }
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)
  118 + queryFunc := func() (interface{}, error) {
103 query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions) 119 query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)
104 query.SetWhereByQueryOption("company.company_id = ?", "companyId") 120 query.SetWhereByQueryOption("company.company_id = ?", "companyId")
105 121
@@ -113,6 +129,15 @@ func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{} @@ -113,6 +129,15 @@ func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}
113 return nil, err 129 return nil, err
114 } 130 }
115 } 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
  140 + }
116 if companyModel.CompanyId == 0 { 141 if companyModel.CompanyId == 0 {
117 return nil, nil 142 return nil, nil
118 } else { 143 } else {
@@ -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)