作者 yangfu

合约查询修改

@@ -2,8 +2,12 @@ package dao @@ -2,8 +2,12 @@ package dao
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 7 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
8 "time" 12 "time"
9 ) 13 )
@@ -146,6 +150,76 @@ func (dao *CooperationContractDao) SearchCooperationContractByUndertaker(queryOp @@ -146,6 +150,76 @@ func (dao *CooperationContractDao) SearchCooperationContractByUndertaker(queryOp
146 } 150 }
147 } 151 }
148 152
  153 +func (dao *CooperationContractDao) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContract, error) {
  154 + tx := dao.transactionContext.PgTx
  155 + var cooperationContractModels []*models.CooperationContract
  156 + cooperationContracts := make([]*domain.CooperationContract, 0)
  157 + query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractModels), queryOptions)
  158 + if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
  159 + query.Where("cooperation_contract_number ilike ?", fmt.Sprintf("%%%s%%", cooperationContractNumber))
  160 + }
  161 + if cooperationContractName, ok := queryOptions["cooperationContactName"]; ok && cooperationContractName != "" {
  162 + query.Where("cooperation_contract_name like ?", fmt.Sprintf("%%%s%%", cooperationContractName))
  163 + }
  164 + if departmentName, ok := queryOptions["departmentName"]; ok && departmentName != "" {
  165 + query.Where(`(cooperation_contract.department->>'departmentName')::test LIKE ?`, fmt.Sprintf("%%%s%%", departmentName))
  166 + }
  167 + if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" {
  168 + query.Where(`(cooperation_contract.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName))
  169 + }
  170 + if cooperationContractIds, ok := queryOptions["cooperationContractIds"]; ok && len(cooperationContractIds.([]int64)) != 0 {
  171 + query.Where("cooperation_contract_id in (?)", pg.In(cooperationContractIds))
  172 + }
  173 + if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
  174 + query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
  175 + }
  176 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  177 + query.Where("company->>'companyId' = '?'", companyId)
  178 + }
  179 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  180 + query.Where("org->>'orgId' = '?'", orgId)
  181 + }
  182 + if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
  183 + newOrgIds := utils.SliceItoa(orgIds.([]int64))
  184 + query.Where("org->>'orgId' in (?)", pg.In(newOrgIds))
  185 + }
  186 + if incentivesType, ok := queryOptions["incentivesType"]; ok && incentivesType.(int32) != 0 {
  187 + query.Where("incentives_type = ?", incentivesType)
  188 + }
  189 + offsetLimitFlag := true
  190 + if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
  191 + offsetLimitFlag = offsetLimit.(bool)
  192 + }
  193 + if offsetLimitFlag {
  194 + query.SetOffsetAndLimit(20)
  195 + }
  196 + query.SetOrderDirect("cooperation_contract_id", "DESC")
  197 + var count int
  198 + var err error
  199 + if count, err = query.SelectAndCount(); err != nil {
  200 + return 0, cooperationContracts, err
  201 + }
  202 + for _, cooperationContractModel := range cooperationContractModels {
  203 + // 获取分红激励规则列表
  204 + var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
  205 + var moneyIncentivesRuleModels []*models.MoneyIncentivesRule
  206 + var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
  207 + var cooperationContractRelevantModels []*models.CooperationContractRelevant
  208 + if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(
  209 + cooperationContractModel,
  210 + &models.CooperationMode{},
  211 + dividendsIncentivesRuleModels,
  212 + moneyIncentivesRuleModels,
  213 + cooperationContractRelevantModels,
  214 + cooperationContractUndertakerModels); err != nil {
  215 + return 0, cooperationContracts, err
  216 + } else {
  217 + cooperationContracts = append(cooperationContracts, cooperationContract)
  218 + }
  219 + }
  220 + return int64(count), cooperationContracts, nil
  221 +}
  222 +
149 func NewCooperationContractDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractDao, error) { 223 func NewCooperationContractDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractDao, error) {
150 if transactionContext == nil { 224 if transactionContext == nil {
151 return nil, fmt.Errorf("transactionContext参数不能为nil") 225 return nil, fmt.Errorf("transactionContext参数不能为nil")
@@ -76,6 +76,7 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -76,6 +76,7 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
76 //企业 76 //企业
77 CompanyId int64 `json:"companyId"` 77 CompanyId int64 `json:"companyId"`
78 OrgId int64 `json:"orgId"` 78 OrgId int64 `json:"orgId"`
  79 + UserId int64 `json:"userId"`
79 //个人 80 //个人
80 UserBaseId int64 `json:"userBaseId"` 81 UserBaseId int64 `json:"userBaseId"`
81 Offset int `json:"offset"` 82 Offset int `json:"offset"`
@@ -90,10 +91,10 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -90,10 +91,10 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
90 var contracts []*domain.CooperationContract 91 var contracts []*domain.CooperationContract
91 var err error 92 var err error
92 if request.UserBaseId > 0 { 93 if request.UserBaseId > 0 {
93 - contracts, err = ptr.getUserContracts(queryOptions) 94 + contracts, err = ptr.getUserContracts(request.UserBaseId, queryOptions)
94 } else if request.OrgId > 0 { 95 } else if request.OrgId > 0 {
95 queryOptions["orgId"] = request.OrgId 96 queryOptions["orgId"] = request.OrgId
96 - contracts, err = ptr.getCompanyContracts(queryOptions) 97 + contracts, err = ptr.getCompanyContracts(request.UserId, queryOptions)
97 } 98 }
98 if err != nil { 99 if err != nil {
99 return nil, err 100 return nil, err
@@ -130,31 +131,60 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -130,31 +131,60 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
130 // getUserContracts 获取用户的合约列表 131 // getUserContracts 获取用户的合约列表
131 // 132 //
132 // p1 p1_desc 133 // p1 p1_desc
133 -func (ptr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {  
134 - undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)  
135 - _, undertakers, err := undertakerRepository.Find(queryOptions)  
136 - var numbers []string  
137 - for i := range undertakers {  
138 - numbers = append(numbers, undertakers[i].CooperationContractNumber) 134 +func (ptr *CooperationStatisticsService) getUserContracts(userBaseId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
  135 + var contracts = make([]*domain.CooperationContract, 0)
  136 + if userBaseId == 0 {
  137 + return contracts, nil
139 } 138 }
140 - if len(numbers) == 0 {  
141 - return []*domain.CooperationContract{}, nil 139 + contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userBaseId": userBaseId})
  140 + if len(contractNumbers) == 0 {
  141 + return contracts, nil
142 } 142 }
143 - queryOptions["cooperationContractNumbers"] = numbers  
144 - contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)  
145 - _, contracts, err := contractRepository.Find(queryOptions) 143 + if err != nil {
  144 + return contracts, err
  145 + }
  146 + queryOptions["cooperationContractNumbers"] = contractNumbers
  147 + contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext)
  148 + _, contracts, err = contractRepository.Find(queryOptions)
146 return contracts, err 149 return contracts, err
147 } 150 }
148 151
149 // getCompanyContracts 获取组织合约列表 152 // getCompanyContracts 获取组织合约列表
150 // 153 //
151 // p1 p1_desc 154 // p1 p1_desc
152 -func (ptr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {  
153 - contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)  
154 - _, contracts, err := contractRepository.Find(queryOptions) 155 +func (ptr *CooperationStatisticsService) getCompanyContracts(userId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
  156 + var contracts = make([]*domain.CooperationContract, 0)
  157 + if userId == 0 {
  158 + return contracts, nil
  159 + }
  160 + contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userId": userId})
  161 + if len(contractNumbers) == 0 {
  162 + return contracts, nil
  163 + }
  164 + if err != nil {
  165 + return contracts, err
  166 + }
  167 + queryOptions["cooperationContractNumbers"] = contractNumbers
  168 + contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext)
  169 + _, contracts, err = contractRepository.Find(queryOptions)
155 return contracts, err 170 return contracts, err
156 } 171 }
157 172
  173 +//getRelevantContracts 获取相关人的合约
  174 +func (ptr *CooperationStatisticsService) getRelevantContracts(queryOptions map[string]interface{}) ([]string, error) {
  175 + undertakerRepository, _ := repository.NewCooperationContractRelevantRepository(ptr.transactionContext)
  176 + _, undertakers, err := undertakerRepository.Find(queryOptions)
  177 + var numbers []string
  178 + for i := range undertakers {
  179 + numbers = append(numbers, undertakers[i].CooperationContractNumber)
  180 + }
  181 + if len(numbers) == 0 {
  182 + return []string{}, nil
  183 + }
  184 + queryOptions["cooperationContractNumbers"] = numbers
  185 + return numbers, err
  186 +}
  187 +
158 // getContractsDividendsEstimate 合约分红预算 188 // getContractsDividendsEstimate 合约分红预算
159 func (ptr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) { 189 func (ptr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
160 var estimates []*domain.DividendsEstimate 190 var estimates []*domain.DividendsEstimate
@@ -181,6 +181,9 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s @@ -181,6 +181,9 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s
181 if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 { 181 if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
182 query.Where("user_base_id = ?", userBaseId) 182 query.Where("user_base_id = ?", userBaseId)
183 } 183 }
  184 + if userId, ok := queryOptions["userId"]; ok && userId.(int64) != 0 {
  185 + query.Where("user_id = ?", userId)
  186 + }
184 offsetLimitFlag := true 187 offsetLimitFlag := true
185 if offsetLimit, ok := queryOptions["offsetLimit"]; ok { 188 if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
186 offsetLimitFlag = offsetLimit.(bool) 189 offsetLimitFlag = offsetLimit.(bool)