作者 陈志颖

feat:共创合约增加批量删除仓储

@@ -10,6 +10,8 @@ import ( @@ -10,6 +10,8 @@ import (
10 type BatchOperateCooperationContractCommand struct { 10 type BatchOperateCooperationContractCommand struct {
11 // 共创合约id列表 11 // 共创合约id列表
12 CooperationContractIds []string `cname:"共创合约id" json:"cooperationContractIds" valid:"Required"` 12 CooperationContractIds []string `cname:"共创合约id" json:"cooperationContractIds" valid:"Required"`
  13 + // 动作
  14 + Action int32 `cname:"操作" json:"action" valid:"Required"`
13 // 公司ID,通过集成REST上下文获取 15 // 公司ID,通过集成REST上下文获取
14 CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"` 16 CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"`
15 // 组织机构ID 17 // 组织机构ID
@@ -11,6 +11,7 @@ import ( @@ -11,6 +11,7 @@ import (
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
  14 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
14 "strconv" 15 "strconv"
15 "time" 16 "time"
16 ) 17 )
@@ -479,7 +480,7 @@ func (cooperationContractService *CooperationContractService) OperateCooperation @@ -479,7 +480,7 @@ func (cooperationContractService *CooperationContractService) OperateCooperation
479 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operateCooperationContractCommand.CooperationContractId))) 480 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operateCooperationContractCommand.CooperationContractId)))
480 } 481 }
481 if err := cooperationContract.Update(map[string]interface{}{ 482 if err := cooperationContract.Update(map[string]interface{}{
482 - "action": operateCooperationContractCommand, 483 + "action": operateCooperationContractCommand.Action,
483 }); err != nil { 484 }); err != nil {
484 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 485 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
485 } 486 }
@@ -493,7 +494,7 @@ func (cooperationContractService *CooperationContractService) OperateCooperation @@ -493,7 +494,7 @@ func (cooperationContractService *CooperationContractService) OperateCooperation
493 } 494 }
494 } 495 }
495 496
496 -// BatchOperateCooperationContract TODO 批量暂停或恢复共创合约 497 +// BatchOperateCooperationContract 批量暂停或恢复共创合约
497 func (cooperationContractService *CooperationContractService) BatchOperateCooperationContract(batchOperateCooperationContractCommand *command.BatchOperateCooperationContractCommand) (interface{}, error) { 498 func (cooperationContractService *CooperationContractService) BatchOperateCooperationContract(batchOperateCooperationContractCommand *command.BatchOperateCooperationContractCommand) (interface{}, error) {
498 if err := batchOperateCooperationContractCommand.ValidateCommand(); err != nil { 499 if err := batchOperateCooperationContractCommand.ValidateCommand(); err != nil {
499 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 500 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -508,8 +509,33 @@ func (cooperationContractService *CooperationContractService) BatchOperateCooper @@ -508,8 +509,33 @@ func (cooperationContractService *CooperationContractService) BatchOperateCooper
508 defer func() { 509 defer func() {
509 _ = transactionContext.RollbackTransaction() 510 _ = transactionContext.RollbackTransaction()
510 }() 511 }()
511 - if err := transactionContext.CommitTransaction(); err != nil {  
512 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 512 + var cooperationContractRepository domain.CooperationContractRepository
  513 + if value, err := factory.CreateCooperationContractRepository(map[string]interface{}{
  514 + "transactionContext": transactionContext,
  515 + }); err != nil {
  516 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  517 + } else {
  518 + cooperationContractRepository = value
  519 + }
  520 + cooperationContractIds, _ := utils.SliceAtoi(batchOperateCooperationContractCommand.CooperationContractIds)
  521 + if count, cooperationContracts, err := cooperationContractRepository.Find(map[string]interface{}{
  522 + "cooperationContractIds": cooperationContractIds,
  523 + }); err != nil {
  524 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  525 + } else {
  526 + if count > 0 {
  527 + for i, _ := range cooperationContracts {
  528 + cooperationContracts[i].Status = batchOperateCooperationContractCommand.Action
  529 + }
  530 + if cooperationContractsReturn, err := cooperationContractRepository.UpdateMany(cooperationContracts); err != nil {
  531 + if err := transactionContext.CommitTransaction(); err != nil {
  532 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  533 + }
  534 + return cooperationContractsReturn, nil
  535 + }
  536 + } else {
  537 + return map[string]interface{}{}, nil
  538 + }
513 } 539 }
514 return nil, nil 540 return nil, nil
515 } 541 }
@@ -630,7 +630,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom @@ -630,7 +630,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom
630 // 获取分红激励规则列表 630 // 获取分红激励规则列表
631 var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule 631 var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
632 dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels) 632 dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels)
633 - if err := dividendsIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).Select(); err != nil { 633 + if err := dividendsIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
634 return nil, err 634 return nil, err
635 } else { 635 } else {
636 if _, err := tx.Model(&dividendsIncentivesRuleModels).WherePK().Delete(); err != nil { 636 if _, err := tx.Model(&dividendsIncentivesRuleModels).WherePK().Delete(); err != nil {
@@ -640,7 +640,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom @@ -640,7 +640,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom
640 // 获取金额激励规则列表 640 // 获取金额激励规则列表
641 var moneyIncentivesRuleModels []*models.MoneyIncentivesRule 641 var moneyIncentivesRuleModels []*models.MoneyIncentivesRule
642 moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRuleModels) 642 moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRuleModels)
643 - if err := moneyIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).Select(); err != nil { 643 + if err := moneyIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
644 return nil, err 644 return nil, err
645 } else { 645 } else {
646 if _, err := tx.Model(&moneyIncentivesRuleModels).WherePK().Delete(); err != nil { 646 if _, err := tx.Model(&moneyIncentivesRuleModels).WherePK().Delete(); err != nil {
@@ -650,7 +650,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom @@ -650,7 +650,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom
650 // 获取承接人列表 650 // 获取承接人列表
651 var cooperationContractUndertakerModels []*models.CooperationContractUndertaker 651 var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
652 cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakerModels) 652 cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakerModels)
653 - if err := cooperationContractUndertakerQuery.Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).Select(); err != nil { 653 + if err := cooperationContractUndertakerQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
654 return nil, err 654 return nil, err
655 } else { 655 } else {
656 if _, err := tx.Model(&cooperationContractUndertakerModels).WherePK().Delete(); err != nil { 656 if _, err := tx.Model(&cooperationContractUndertakerModels).WherePK().Delete(); err != nil {
@@ -660,7 +660,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom @@ -660,7 +660,7 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom
660 // 获取相关人列表 660 // 获取相关人列表
661 var cooperationContractRelevantModels []*models.CooperationContractRelevant 661 var cooperationContractRelevantModels []*models.CooperationContractRelevant
662 cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModels) 662 cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModels)
663 - if err := cooperationContractRelevantQuery.Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).Select(); err != nil { 663 + if err := cooperationContractRelevantQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
664 return nil, err 664 return nil, err
665 } else { 665 } else {
666 if _, err := tx.Model(&cooperationContractRelevantModels).WherePK().Delete(); err != nil { 666 if _, err := tx.Model(&cooperationContractRelevantModels).WherePK().Delete(); err != nil {
@@ -671,6 +671,64 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom @@ -671,6 +671,64 @@ func (repository *CooperationContractRepository) Remove(cooperationContract *dom
671 return cooperationContract, nil 671 return cooperationContract, nil
672 } 672 }
673 673
  674 +// BatchRemove 批量删除
  675 +func (repository *CooperationContractRepository) BatchRemove(cooperationContracts []*domain.CooperationContract) ([]*domain.CooperationContract, error) {
  676 + tx := repository.transactionContext.PgTx
  677 + var cooperationContractModels []*models.CooperationContract
  678 + for _, cooperationContract := range cooperationContracts {
  679 + cooperationContractModels = append(cooperationContractModels, &models.CooperationContract{
  680 + CooperationContractId: cooperationContract.Identify().(int64),
  681 + })
  682 + }
  683 + if _, err := tx.Model(&cooperationContractModels).WherePK().Delete(); err != nil {
  684 + return cooperationContracts, err
  685 + } else {
  686 + for _, cooperationContract := range cooperationContracts {
  687 + // 获取分红激励规则列表
  688 + var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
  689 + dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels)
  690 + if err := dividendsIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
  691 + return nil, err
  692 + } else {
  693 + if _, err := tx.Model(&dividendsIncentivesRuleModels).WherePK().Delete(); err != nil {
  694 + return nil, err
  695 + }
  696 + }
  697 + // 获取金额激励规则列表
  698 + var moneyIncentivesRuleModels []*models.MoneyIncentivesRule
  699 + moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRuleModels)
  700 + if err := moneyIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
  701 + return nil, err
  702 + } else {
  703 + if _, err := tx.Model(&moneyIncentivesRuleModels).WherePK().Delete(); err != nil {
  704 + return nil, err
  705 + }
  706 + }
  707 + // 获取承接人列表
  708 + var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
  709 + cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakerModels)
  710 + if err := cooperationContractUndertakerQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
  711 + return nil, err
  712 + } else {
  713 + if _, err := tx.Model(&cooperationContractUndertakerModels).WherePK().Delete(); err != nil {
  714 + return nil, err
  715 + }
  716 + }
  717 + // 获取相关人列表
  718 + var cooperationContractRelevantModels []*models.CooperationContractRelevant
  719 + cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModels)
  720 + if err := cooperationContractRelevantQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
  721 + return nil, err
  722 + } else {
  723 + if _, err := tx.Model(&cooperationContractRelevantModels).WherePK().Delete(); err != nil {
  724 + return nil, err
  725 + }
  726 + }
  727 + }
  728 + }
  729 + return cooperationContracts, nil
  730 +}
  731 +
674 func (repository *CooperationContractRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContract, error) { 732 func (repository *CooperationContractRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContract, error) {
675 tx := repository.transactionContext.PgTx 733 tx := repository.transactionContext.PgTx
676 cooperationContractModel := new(models.CooperationContract) 734 cooperationContractModel := new(models.CooperationContract)
@@ -738,6 +796,9 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in @@ -738,6 +796,9 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in
738 if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" { 796 if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" {
739 query.Where(`(cooperation_contract.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName)) 797 query.Where(`(cooperation_contract.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName))
740 } 798 }
  799 + if cooperationContractIds, ok := queryOptions["cooperationContractIds"]; ok && len(cooperationContractIds.([]int64)) != 0 {
  800 + query.Where("cooperation_contract_id in (?)", pg.In(cooperationContractIds))
  801 + }
741 offsetLimitFlag := true 802 offsetLimitFlag := true
742 if limit, ok := queryOptions["limit"]; ok { 803 if limit, ok := queryOptions["limit"]; ok {
743 offsetLimitFlag = limit.(bool) 804 offsetLimitFlag = limit.(bool)