作者 陈志颖

refactor:优化仓储

... ... @@ -31,3 +31,10 @@ type Relevant struct {
// 公司
Company *Company `json:"company"`
}
func (relevant *Relevant) Identify() interface{} {
if relevant.RelevantId == 0 {
return nil
}
return relevant.RelevantId
}
... ...
... ... @@ -37,3 +37,10 @@ type Undertaker struct {
// 合同附件
ContractAttachment []*Attachment `json:"contractAttachment"`
}
func (undertaker *Undertaker) Identify() interface{} {
if undertaker.UndertakerId == 0 {
return nil
}
return undertaker.UndertakerId
}
... ...
... ... @@ -15,14 +15,11 @@ import (
type ContractUndertakerFeedbackRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *ContractUndertakerFeedbackRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -202,8 +199,13 @@ func NewContractUndertakerFeedbackRepository(transactionContext *pgTransaction.T
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &ContractUndertakerFeedbackRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -17,14 +17,11 @@ import (
type CooperationApplicationRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationApplicationRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -322,8 +319,13 @@ func NewCooperationApplicationRepository(transactionContext *pgTransaction.Trans
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationApplicationRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -15,14 +15,11 @@ import (
type CooperationContractChangeLogRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationContractChangeLogRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -198,8 +195,13 @@ func NewCooperationContractChangeLogRepository(transactionContext *pgTransaction
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationContractChangeLogRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -15,14 +15,11 @@ import (
type CooperationContractRelevantRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationContractRelevantRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -210,8 +207,13 @@ func NewCooperationContractRelevantRepository(transactionContext *pgTransaction.
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationContractRelevantRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -16,14 +16,11 @@ import (
type CooperationContractRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationContractRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -108,21 +105,30 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 新增相关人
var relevantPeopleModel []*models.CooperationContractRelevant
for _, relevant := range cooperationContract.RelevantPeople {
if relevant.Identify() == nil {
orderGoodId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
relevant.RelevantId = orderGoodId
}
}
relevantPeopleModel = append(relevantPeopleModel, &models.CooperationContractRelevant{
CooperationContractNumber: cooperationContract.CooperationContractNumber,
UserId: relevant.UserId,
UserBaseId: relevant.UserBaseId,
Org: relevant.Org,
Orgs: relevant.Orgs,
Department: relevant.Department,
Roles: relevant.Roles,
UserInfo: relevant.UserInfo,
UserType: relevant.UserType,
Status: relevant.Status,
Company: relevant.Company,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
CooperationContractRelevantId: relevant.RelevantId,
CooperationContractNumber: cooperationContract.CooperationContractNumber,
UserId: relevant.UserId,
UserBaseId: relevant.UserBaseId,
Org: relevant.Org,
Orgs: relevant.Orgs,
Department: relevant.Department,
Roles: relevant.Roles,
UserInfo: relevant.UserInfo,
UserType: relevant.UserType,
Status: relevant.Status,
Company: relevant.Company,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
})
}
if len(relevantPeopleModel) > 0 {
... ... @@ -169,7 +175,16 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 新增分红激励规则
var dividendsIncentivesRulesModel []*models.DividendsIncentivesRule
for _, rule := range cooperationContract.DividendsIncentivesRules {
if rule.Identify() == nil {
ruleId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
rule.DividendsIncentivesRuleId = ruleId
}
}
dividendsIncentivesRulesModel = append(dividendsIncentivesRulesModel, &models.DividendsIncentivesRule{
DividendsIncentivesRuleId: rule.DividendsIncentivesRuleId,
CooperationContractNumber: cooperationContract.CooperationContractNumber,
ReferrerPercentage: rule.ReferrerPercentage,
SalesmanPercentage: rule.SalesmanPercentage,
... ... @@ -197,7 +212,16 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 新增金额激励规则
var moneyIncentivesRulesModel []*models.MoneyIncentivesRule
for _, rule := range cooperationContract.MoneyIncentivesRules {
if rule.Identify() == nil {
ruleId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
rule.MoneyIncentivesRuleId = ruleId
}
}
moneyIncentivesRulesModel = append(moneyIncentivesRulesModel, &models.MoneyIncentivesRule{
MoneyIncentivesRuleId: rule.MoneyIncentivesRuleId,
CooperationContractNumber: cooperationContract.CooperationContractNumber,
MoneyIncentivesAmount: rule.MoneyIncentivesAmount,
MoneyIncentivesStage: rule.MoneyIncentivesStage,
... ... @@ -302,21 +326,30 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 将待添加的相关人领域模型转换为数据模型
var cooperationContractRelevantPeopleToAddModels []*models.CooperationContractRelevant
for _, relevantDomain := range cooperationContractRelevantPeopleToAdd {
if relevantDomain.Identify() == nil {
relevantId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
relevantDomain.RelevantId = relevantId
}
}
cooperationContractRelevantPeopleToAddModels = append(cooperationContractRelevantPeopleToAddModels, &models.CooperationContractRelevant{
CooperationContractNumber: relevantDomain.CooperationContractNumber,
UserId: relevantDomain.UserId,
UserBaseId: relevantDomain.UserBaseId,
Org: relevantDomain.Org,
Orgs: relevantDomain.Orgs,
Department: relevantDomain.Department,
Roles: relevantDomain.Roles,
UserInfo: relevantDomain.UserInfo,
UserType: relevantDomain.UserType,
Status: relevantDomain.Status,
Company: relevantDomain.Company,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
CooperationContractRelevantId: relevantDomain.RelevantId,
CooperationContractNumber: relevantDomain.CooperationContractNumber,
UserId: relevantDomain.UserId,
UserBaseId: relevantDomain.UserBaseId,
Org: relevantDomain.Org,
Orgs: relevantDomain.Orgs,
Department: relevantDomain.Department,
Roles: relevantDomain.Roles,
UserInfo: relevantDomain.UserInfo,
UserType: relevantDomain.UserType,
Status: relevantDomain.Status,
Company: relevantDomain.Company,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
})
}
... ... @@ -401,24 +434,33 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 将待添加的相关人领域模型转换为数据模型
var cooperationContractUndertakersToAddModels []*models.CooperationContractUndertaker
for _, undertakerDomain := range cooperationContractUndertakersToAdd {
if undertakerDomain.Identify() == nil {
undertakerId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
undertakerDomain.UndertakerId = undertakerId
}
}
cooperationContractUndertakersToAddModels = append(cooperationContractUndertakersToAddModels, &models.CooperationContractUndertaker{
CooperationContractNumber: undertakerDomain.CooperationContractNumber,
UserId: undertakerDomain.UserId,
UserBaseId: undertakerDomain.UserBaseId,
Org: undertakerDomain.Org,
Orgs: undertakerDomain.Orgs,
Department: undertakerDomain.Department,
Roles: undertakerDomain.Roles,
UserInfo: undertakerDomain.UserInfo,
UserType: undertakerDomain.UserType,
Referrer: undertakerDomain.Referrer,
Salesman: undertakerDomain.Salesman,
Status: undertakerDomain.Status,
Company: undertakerDomain.Company,
ContractAttachment: undertakerDomain.ContractAttachment,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
CooperationContractUndertakerId: undertakerDomain.UndertakerId,
CooperationContractNumber: undertakerDomain.CooperationContractNumber,
UserId: undertakerDomain.UserId,
UserBaseId: undertakerDomain.UserBaseId,
Org: undertakerDomain.Org,
Orgs: undertakerDomain.Orgs,
Department: undertakerDomain.Department,
Roles: undertakerDomain.Roles,
UserInfo: undertakerDomain.UserInfo,
UserType: undertakerDomain.UserType,
Referrer: undertakerDomain.Referrer,
Salesman: undertakerDomain.Salesman,
Status: undertakerDomain.Status,
Company: undertakerDomain.Company,
ContractAttachment: undertakerDomain.ContractAttachment,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
})
}
log.Logger.Info("待添加的承接人", map[string]interface{}{
... ... @@ -534,7 +576,16 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 将待添加的分红激励规则领域模型转换为数据模型
var dividendsIncentivesRulesToAddModels []*models.DividendsIncentivesRule
for _, dividendsIncentivesRuleDomain := range dividendsIncentivesRulesToAdd {
if dividendsIncentivesRuleDomain.Identify() == nil {
ruleId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
dividendsIncentivesRuleDomain.DividendsIncentivesRuleId = ruleId
}
}
dividendsIncentivesRulesToAddModels = append(dividendsIncentivesRulesToAddModels, &models.DividendsIncentivesRule{
DividendsIncentivesRuleId: dividendsIncentivesRuleDomain.DividendsIncentivesRuleId,
CooperationContractNumber: dividendsIncentivesRuleDomain.CooperationContractNumber,
ReferrerPercentage: dividendsIncentivesRuleDomain.ReferrerPercentage,
SalesmanPercentage: dividendsIncentivesRuleDomain.SalesmanPercentage,
... ... @@ -657,7 +708,16 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
// 将待添加的金额激励规则领域模型转换为数据模型
var moneyIncentivesRulesToAddModels []*models.MoneyIncentivesRule
for _, moneyIncentivesRuleDomain := range moneyIncentivesRulesToAdd {
if moneyIncentivesRuleDomain.Identify() == nil {
ruleId, err := repository.nextIdentify()
if err != nil {
return nil, err
} else {
moneyIncentivesRuleDomain.MoneyIncentivesRuleId = ruleId
}
}
moneyIncentivesRulesToAddModels = append(moneyIncentivesRulesToAddModels, &models.MoneyIncentivesRule{
MoneyIncentivesRuleId: moneyIncentivesRuleDomain.MoneyIncentivesRuleId,
CooperationContractNumber: moneyIncentivesRuleDomain.CooperationContractNumber,
MoneyIncentivesAmount: moneyIncentivesRuleDomain.MoneyIncentivesAmount,
MoneyIncentivesStage: moneyIncentivesRuleDomain.MoneyIncentivesStage,
... ... @@ -1175,8 +1235,13 @@ func NewCooperationContractRepository(transactionContext *pgTransaction.Transact
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationContractRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -15,14 +15,11 @@ import (
type CooperationContractUndertakerRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationContractUndertakerRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -225,8 +222,13 @@ func NewCooperationContractUndertakerRepository(transactionContext *pgTransactio
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationContractUndertakerRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -14,14 +14,11 @@ import (
type CooperationModeRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationModeRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -221,8 +218,13 @@ func NewCooperationModeRepository(transactionContext *pgTransaction.TransactionC
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationModeRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -17,14 +17,11 @@ import (
type CooperationProjectRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CooperationProjectRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -317,8 +314,13 @@ func NewCooperationProjectRepository(transactionContext *pgTransaction.Transacti
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CooperationProjectRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -15,14 +15,11 @@ import (
type CreditAccountRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *CreditAccountRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -249,8 +246,13 @@ func NewCreditAccountRepository(transactionContext *pgTransaction.TransactionCon
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &CreditAccountRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -16,14 +16,11 @@ import (
type DividendsEstimateRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *DividendsEstimateRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -332,8 +329,13 @@ func NewDividendsEstimateRepository(transactionContext *pgTransaction.Transactio
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &DividendsEstimateRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...
... ... @@ -15,14 +15,11 @@ import (
type DividendsIncentivesRuleRepository struct {
transactionContext *pgTransaction.TransactionContext
IdWorker *snowflake.IdWorker
}
func (repository *DividendsIncentivesRuleRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
id, err := repository.IdWorker.NextId()
return id, err
}
... ... @@ -202,8 +199,13 @@ func NewDividendsIncentivesRuleRepository(transactionContext *pgTransaction.Tran
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
idWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return nil, err
}
return &DividendsIncentivesRuleRepository{
transactionContext: transactionContext,
IdWorker: idWorker,
}, nil
}
}
... ...