作者 陈志颖

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,7 +105,16 @@ 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{
CooperationContractRelevantId: relevant.RelevantId,
CooperationContractNumber: cooperationContract.CooperationContractNumber,
UserId: relevant.UserId,
UserBaseId: relevant.UserBaseId,
... ... @@ -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,7 +326,16 @@ 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{
CooperationContractRelevantId: relevantDomain.RelevantId,
CooperationContractNumber: relevantDomain.CooperationContractNumber,
UserId: relevantDomain.UserId,
UserBaseId: relevantDomain.UserBaseId,
... ... @@ -401,7 +434,16 @@ 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{
CooperationContractUndertakerId: undertakerDomain.UndertakerId,
CooperationContractNumber: undertakerDomain.CooperationContractNumber,
UserId: undertakerDomain.UserId,
UserBaseId: undertakerDomain.UserBaseId,
... ... @@ -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
}
}
... ...