作者 陈志颖

Merge branch 'dev-chenzhiying' into dev

... ... @@ -9,6 +9,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"strconv"
"time"
... ... @@ -82,9 +83,33 @@ func (cooperationModeService *CooperationModeService) CreateCooperationMode(crea
organization = data
}
//TODO 校验共创模式编码唯一性
//TODO 校验共创模式名称唯一性
// 共创模式DAO初始化
var cooperationModeDao *dao.CooperationModeDao
if value, err := factory.CreateCooperationModeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cooperationModeDao = value
}
// 校验共创模式编码唯一性
numberAvailable, _ := cooperationModeDao.CheckModeNumberAvailable(map[string]interface{}{
"companyId": createCooperationModeCommand.CompanyId,
"orgId": createCooperationModeCommand.OrgId,
"cooperationModeNumber": createCooperationModeCommand.CooperationModeNumber,
})
if !numberAvailable {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "模式编码已存在")
}
// 校验共创模式名称唯一性
nameAvailable, _ := cooperationModeDao.CheckModeNameAvailable(map[string]interface{}{
"companyId": createCooperationModeCommand.CompanyId,
"orgId": createCooperationModeCommand.OrgId,
"cooperationModeName": createCooperationModeCommand.CooperationModeName,
})
if !nameAvailable {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "模式名称已存在")
}
newCooperationMode := &domain.CooperationMode{
CooperationModeName: createCooperationModeCommand.CooperationModeName,
... ...
package factory
import (
"github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
)
func CreateCooperationModeDao(options map[string]interface{}) (*dao.CooperationModeDao, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return dao.NewCooperationModeDao(transactionContext)
}
... ...
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
)
type CooperationModeDao struct {
transactionContext *pgTransaction.TransactionContext
}
// CheckModeNameAvailable 校验模式名称唯一性
func (dao *CooperationModeDao) CheckModeNameAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationModeModels []*models.CooperationMode
query := tx.Model(&cooperationModeModels)
if cooperationModeName, ok := queryOptions["cooperationModeName"]; ok && cooperationModeName != "" {
query = query.Where("cooperation_mode_name = ?", cooperationModeName)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
// CheckModeNumberAvailable 校验模式编码唯一性
func (dao *CooperationModeDao) CheckModeNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationModeModels []*models.CooperationMode
query := tx.Model(&cooperationModeModels)
if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber != "" {
query = query.Where("cooperation_mode_number = ?", cooperationModeNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
func NewCooperationModeDao(transactionContext *pgTransaction.TransactionContext) (*CooperationModeDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationModeDao{
transactionContext: transactionContext,
}, nil
}
}
... ...