作者 陈志颖

feat:增加新建共创模式编码和名称校验

@@ -9,6 +9,7 @@ import ( @@ -9,6 +9,7 @@ import (
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
13 "strconv" 14 "strconv"
14 "time" 15 "time"
@@ -82,9 +83,33 @@ func (cooperationModeService *CooperationModeService) CreateCooperationMode(crea @@ -82,9 +83,33 @@ func (cooperationModeService *CooperationModeService) CreateCooperationMode(crea
82 organization = data 83 organization = data
83 } 84 }
84 85
85 - //TODO 校验共创模式编码唯一性  
86 -  
87 - //TODO 校验共创模式名称唯一性 86 + // 共创模式DAO初始化
  87 + var cooperationModeDao *dao.CooperationModeDao
  88 + if value, err := factory.CreateCooperationModeDao(map[string]interface{}{
  89 + "transactionContext": transactionContext,
  90 + }); err != nil {
  91 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  92 + } else {
  93 + cooperationModeDao = value
  94 + }
  95 + // 校验共创模式编码唯一性
  96 + numberAvailable, _ := cooperationModeDao.CheckModeNumberAvailable(map[string]interface{}{
  97 + "companyId": createCooperationModeCommand.CompanyId,
  98 + "orgId": createCooperationModeCommand.OrgId,
  99 + "cooperationModeNumber": createCooperationModeCommand.CooperationModeNumber,
  100 + })
  101 + if !numberAvailable {
  102 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "模式编码已存在")
  103 + }
  104 + // 校验共创模式名称唯一性
  105 + nameAvailable, _ := cooperationModeDao.CheckModeNameAvailable(map[string]interface{}{
  106 + "companyId": createCooperationModeCommand.CompanyId,
  107 + "orgId": createCooperationModeCommand.OrgId,
  108 + "cooperationModeName": createCooperationModeCommand.CooperationModeName,
  109 + })
  110 + if !nameAvailable {
  111 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "模式名称已存在")
  112 + }
88 113
89 newCooperationMode := &domain.CooperationMode{ 114 newCooperationMode := &domain.CooperationMode{
90 CooperationModeName: createCooperationModeCommand.CooperationModeName, 115 CooperationModeName: createCooperationModeCommand.CooperationModeName,
1 package factory 1 package factory
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/transaction/pg"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
  6 +)
  7 +
  8 +func CreateCooperationModeDao(options map[string]interface{}) (*dao.CooperationModeDao, error) {
  9 + var transactionContext *pg.TransactionContext
  10 + if value, ok := options["transactionContext"]; ok {
  11 + transactionContext = value.(*pg.TransactionContext)
  12 + }
  13 + return dao.NewCooperationModeDao(transactionContext)
  14 +}
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
  7 +)
  8 +
  9 +type CooperationModeDao struct {
  10 + transactionContext *pgTransaction.TransactionContext
  11 +}
  12 +
  13 +// CheckModeNameAvailable 校验模式名称唯一性
  14 +func (dao *CooperationModeDao) CheckModeNameAvailable(queryOptions map[string]interface{}) (bool, error) {
  15 + tx := dao.transactionContext.PgTx
  16 + var cooperationModeModels []*models.CooperationMode
  17 + query := tx.Model(&cooperationModeModels)
  18 + if cooperationModeName, ok := queryOptions["cooperationModeName"]; ok && cooperationModeName != "" {
  19 + query = query.Where("cooperation_mode_name = ?", cooperationModeName)
  20 + }
  21 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  22 + query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
  23 + }
  24 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  25 + query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
  26 + }
  27 + ok, err := query.Exists()
  28 + return !ok, err
  29 +}
  30 +
  31 +// CheckModeNumberAvailable 校验模式编码唯一性
  32 +func (dao *CooperationModeDao) CheckModeNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
  33 + tx := dao.transactionContext.PgTx
  34 + var cooperationModeModels []*models.CooperationMode
  35 + query := tx.Model(&cooperationModeModels)
  36 + if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber != "" {
  37 + query = query.Where("cooperation_mode_number = ?", cooperationModeNumber)
  38 + }
  39 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  40 + query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
  41 + }
  42 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  43 + query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
  44 + }
  45 + ok, err := query.Exists()
  46 + return !ok, err
  47 +}
  48 +
  49 +func NewCooperationModeDao(transactionContext *pgTransaction.TransactionContext) (*CooperationModeDao, error) {
  50 + if transactionContext == nil {
  51 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  52 + } else {
  53 + return &CooperationModeDao{
  54 + transactionContext: transactionContext,
  55 + }, nil
  56 + }
  57 +}