作者 yangfu

合约查询修改

... ... @@ -2,8 +2,12 @@ package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
... ... @@ -146,6 +150,76 @@ func (dao *CooperationContractDao) SearchCooperationContractByUndertaker(queryOp
}
}
func (dao *CooperationContractDao) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContract, error) {
tx := dao.transactionContext.PgTx
var cooperationContractModels []*models.CooperationContract
cooperationContracts := make([]*domain.CooperationContract, 0)
query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractModels), queryOptions)
if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
query.Where("cooperation_contract_number ilike ?", fmt.Sprintf("%%%s%%", cooperationContractNumber))
}
if cooperationContractName, ok := queryOptions["cooperationContactName"]; ok && cooperationContractName != "" {
query.Where("cooperation_contract_name like ?", fmt.Sprintf("%%%s%%", cooperationContractName))
}
if departmentName, ok := queryOptions["departmentName"]; ok && departmentName != "" {
query.Where(`(cooperation_contract.department->>'departmentName')::test LIKE ?`, fmt.Sprintf("%%%s%%", departmentName))
}
if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" {
query.Where(`(cooperation_contract.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName))
}
if cooperationContractIds, ok := queryOptions["cooperationContractIds"]; ok && len(cooperationContractIds.([]int64)) != 0 {
query.Where("cooperation_contract_id in (?)", pg.In(cooperationContractIds))
}
if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company->>'companyId' = '?'", companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query.Where("org->>'orgId' = '?'", orgId)
}
if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
newOrgIds := utils.SliceItoa(orgIds.([]int64))
query.Where("org->>'orgId' in (?)", pg.In(newOrgIds))
}
if incentivesType, ok := queryOptions["incentivesType"]; ok && incentivesType.(int32) != 0 {
query.Where("incentives_type = ?", incentivesType)
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
}
if offsetLimitFlag {
query.SetOffsetAndLimit(20)
}
query.SetOrderDirect("cooperation_contract_id", "DESC")
var count int
var err error
if count, err = query.SelectAndCount(); err != nil {
return 0, cooperationContracts, err
}
for _, cooperationContractModel := range cooperationContractModels {
// 获取分红激励规则列表
var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
var moneyIncentivesRuleModels []*models.MoneyIncentivesRule
var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
var cooperationContractRelevantModels []*models.CooperationContractRelevant
if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(
cooperationContractModel,
&models.CooperationMode{},
dividendsIncentivesRuleModels,
moneyIncentivesRuleModels,
cooperationContractRelevantModels,
cooperationContractUndertakerModels); err != nil {
return 0, cooperationContracts, err
} else {
cooperationContracts = append(cooperationContracts, cooperationContract)
}
}
return int64(count), cooperationContracts, nil
}
func NewCooperationContractDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -76,6 +76,7 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
//企业
CompanyId int64 `json:"companyId"`
OrgId int64 `json:"orgId"`
UserId int64 `json:"userId"`
//个人
UserBaseId int64 `json:"userBaseId"`
Offset int `json:"offset"`
... ... @@ -90,10 +91,10 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
var contracts []*domain.CooperationContract
var err error
if request.UserBaseId > 0 {
contracts, err = ptr.getUserContracts(queryOptions)
contracts, err = ptr.getUserContracts(request.UserBaseId, queryOptions)
} else if request.OrgId > 0 {
queryOptions["orgId"] = request.OrgId
contracts, err = ptr.getCompanyContracts(queryOptions)
contracts, err = ptr.getCompanyContracts(request.UserId, queryOptions)
}
if err != nil {
return nil, err
... ... @@ -130,31 +131,60 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
// getUserContracts 获取用户的合约列表
//
// p1 p1_desc
func (ptr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
_, undertakers, err := undertakerRepository.Find(queryOptions)
var numbers []string
for i := range undertakers {
numbers = append(numbers, undertakers[i].CooperationContractNumber)
func (ptr *CooperationStatisticsService) getUserContracts(userBaseId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
var contracts = make([]*domain.CooperationContract, 0)
if userBaseId == 0 {
return contracts, nil
}
if len(numbers) == 0 {
return []*domain.CooperationContract{}, nil
contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userBaseId": userBaseId})
if len(contractNumbers) == 0 {
return contracts, nil
}
queryOptions["cooperationContractNumbers"] = numbers
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
_, contracts, err := contractRepository.Find(queryOptions)
if err != nil {
return contracts, err
}
queryOptions["cooperationContractNumbers"] = contractNumbers
contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext)
_, contracts, err = contractRepository.Find(queryOptions)
return contracts, err
}
// getCompanyContracts 获取组织合约列表
//
// p1 p1_desc
func (ptr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
_, contracts, err := contractRepository.Find(queryOptions)
func (ptr *CooperationStatisticsService) getCompanyContracts(userId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
var contracts = make([]*domain.CooperationContract, 0)
if userId == 0 {
return contracts, nil
}
contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userId": userId})
if len(contractNumbers) == 0 {
return contracts, nil
}
if err != nil {
return contracts, err
}
queryOptions["cooperationContractNumbers"] = contractNumbers
contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext)
_, contracts, err = contractRepository.Find(queryOptions)
return contracts, err
}
//getRelevantContracts 获取相关人的合约
func (ptr *CooperationStatisticsService) getRelevantContracts(queryOptions map[string]interface{}) ([]string, error) {
undertakerRepository, _ := repository.NewCooperationContractRelevantRepository(ptr.transactionContext)
_, undertakers, err := undertakerRepository.Find(queryOptions)
var numbers []string
for i := range undertakers {
numbers = append(numbers, undertakers[i].CooperationContractNumber)
}
if len(numbers) == 0 {
return []string{}, nil
}
queryOptions["cooperationContractNumbers"] = numbers
return numbers, err
}
// getContractsDividendsEstimate 合约分红预算
func (ptr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
var estimates []*domain.DividendsEstimate
... ...
... ... @@ -181,6 +181,9 @@ func (repository *CooperationContractRelevantRepository) Find(queryOptions map[s
if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
query.Where("user_base_id = ?", userBaseId)
}
if userId, ok := queryOptions["userId"]; ok && userId.(int64) != 0 {
query.Where("user_id = ?", userId)
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
... ...