作者 陈志颖

feat:共创模式增加批量操作

package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type BatchOperateCooperationModeCommand struct {
// 共创模式ID
CooperationModeIds []string `cname:"共创模式id" json:"cooperationModeIds" valid:"Required"`
// 共创模式状态,1启用,2禁用
Status int32 `cname:"状态" json:"status" valid:"Required"`
// 公司ID,通过集成REST上下文获取
CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"`
// 组织机构ID
OrgId int64 `cname:"组织机构ID" json:"orgId,string" valid:"Required"`
// 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
// 用户基础数据id
UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string"`
}
func (batchOperateCooperationModeCommand *BatchOperateCooperationModeCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (batchOperateCooperationModeCommand *BatchOperateCooperationModeCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(batchOperateCooperationModeCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(batchOperateCooperationModeCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type BatchRemoveCooperationModeCommand struct {
// 共创模式ID数组
CooperationModeIds []string `cname:"共创模式ID数组" json:"cooperationModeIds,string" valid:"Required"`
// 公司ID,通过集成REST上下文获取
CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"`
// 组织机构ID
OrgId int64 `cname:"组织机构ID" json:"orgId,string" valid:"Required"`
// 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
// 用户基础数据id
UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string"`
}
func (batchRemoveCooperationModeCommand *BatchRemoveCooperationModeCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (batchRemoveCooperationModeCommand *BatchRemoveCooperationModeCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(batchRemoveCooperationModeCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(batchRemoveCooperationModeCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type OperateCooperationModeCommand struct {
// 共创模式ID
CooperationModeId string `cname:"共创模式id" json:"cooperationModeId" valid:"Required"`
// 共创模式状态,1启用,2禁用
Status int32 `cname:"状态" json:"status" valid:"Required"`
// 公司ID,通过集成REST上下文获取
CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"`
// 组织机构ID
OrgId int64 `cname:"组织机构ID" json:"orgId,string" valid:"Required"`
// 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
// 用户基础数据id
UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string"`
}
func (operateCooperationModeCommand *OperateCooperationModeCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (operateCooperationModeCommand *OperateCooperationModeCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(operateCooperationModeCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(operateCooperationModeCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -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/utils"
"strconv"
"time"
)
... ... @@ -231,6 +232,49 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo
}
}
// BatchRemoveCooperationMode 移除共创模式服务
func (cooperationModeService *CooperationModeService) BatchRemoveCooperationMode(batchRemoveCooperationModeCommand *command.BatchRemoveCooperationModeCommand) (interface{}, error) {
if err := batchRemoveCooperationModeCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var cooperationModeRepository domain.CooperationModeRepository
if value, err := factory.CreateCooperationModeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cooperationModeRepository = value
}
cooperationModeIds, _ := utils.SliceAtoi(batchRemoveCooperationModeCommand.CooperationModeIds)
if count, cooperationModes, err := cooperationModeRepository.Find(map[string]interface{}{
"cooperationModeIds": cooperationModeIds,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
if cooperationMode, err := cooperationModeRepository.BatchRemove(cooperationModes); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return cooperationMode, nil
}
}
}
return map[string]interface{}{}, nil
}
// SearchCooperationMode 查询共创模式
func (cooperationModeService *CooperationModeService) SearchCooperationMode(searchCooperationModeQuery *query.SearchCooperationModeQuery) (interface{}, error) {
if err := searchCooperationModeQuery.ValidateQuery(); err != nil {
... ... @@ -312,6 +356,72 @@ func (cooperationModeService *CooperationModeService) UpdateCooperationMode(upda
}
}
// OperateCooperationMode 更新共创模式服务
func (cooperationModeService *CooperationModeService) OperateCooperationMode(operateCooperationModeCommand *command.OperateCooperationModeCommand) (interface{}, error) {
if err := operateCooperationModeCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var cooperationModeRepository domain.CooperationModeRepository
if value, err := factory.CreateCooperationModeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cooperationModeRepository = value
}
cooperationModeId, _ := strconv.ParseInt(operateCooperationModeCommand.CooperationModeId, 10, 64)
cooperationMode, err := cooperationModeRepository.FindOne(map[string]interface{}{"cooperationModeId": cooperationModeId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cooperationMode == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(cooperationModeId, 10)))
}
if err := cooperationMode.Update(tool_funs.SimpleStructToMap(operateCooperationModeCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if cooperationMode, err := cooperationModeRepository.Save(cooperationMode); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return cooperationMode, nil
}
}
// BatchOperateCooperationMode 更新共创模式服务
func (cooperationModeService *CooperationModeService) BatchOperateCooperationMode(batchOperateCooperationModeCommand *command.BatchOperateCooperationModeCommand) (interface{}, error) {
if err := batchOperateCooperationModeCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
func NewCooperationModeService(options map[string]interface{}) *CooperationModeService {
newCooperationModeService := &CooperationModeService{}
return newCooperationModeService
... ...
... ... @@ -33,6 +33,7 @@ type CooperationMode struct {
type CooperationModeRepository interface {
Save(cooperationMode *CooperationMode) (*CooperationMode, error)
Remove(cooperationMode *CooperationMode) (*CooperationMode, error)
BatchRemove(cooperationModes []*CooperationMode) ([]*CooperationMode, error)
FindOne(queryOptions map[string]interface{}) (*CooperationMode, error)
Find(queryOptions map[string]interface{}) (int64, []*CooperationMode, error)
}
... ...
... ... @@ -3,7 +3,6 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
... ... @@ -131,6 +130,21 @@ func (repository *CooperationModeRepository) Remove(cooperationMode *domain.Coop
return cooperationMode, nil
}
// BatchRemove 批量删除
func (repository *CooperationModeRepository) BatchRemove(cooperationModes []*domain.CooperationMode) ([]*domain.CooperationMode, error) {
tx := repository.transactionContext.PgTx
var cooperationModeModels []*models.CooperationMode
for _, cooperationMode := range cooperationModes {
cooperationModeModels = append(cooperationModeModels, &models.CooperationMode{
CooperationModeId: cooperationMode.Identify().(int64),
})
}
if _, err := tx.Model(&cooperationModeModels).WherePK().Delete(); err != nil {
return cooperationModes, err
}
return cooperationModes, nil
}
func (repository *CooperationModeRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationMode, error) {
tx := repository.transactionContext.PgTx
cooperationModeModel := new(models.CooperationMode)
... ...
package utils
import "strconv"
// Intersect 返回两个数组的交集
func Intersect(nums1 []int64, nums2 []int64) []int64 {
if len(nums1) > len(nums2) {
... ... @@ -54,3 +56,15 @@ func Difference(slice1, slice2 []int64) []int64 {
}
return nn
}
func SliceAtoi(sa []string) ([]int64, error) {
si := make([]int64, 0, len(sa))
for _, a := range sa {
i, err := strconv.ParseInt(a, 10, 64)
if err != nil {
return si, err
}
si = append(si, i)
}
return si, nil
}
... ...
... ... @@ -71,6 +71,20 @@ func (controller *CooperationModeController) RemoveCooperationMode() {
controller.Response(data, err)
}
func (controller *CooperationModeController) BatchRemoveCooperationMode() {
cooperationModeService := service.NewCooperationModeService(nil)
batchRemoveCooperationModeCommand := &command.BatchRemoveCooperationModeCommand{}
_ = controller.Unmarshal(batchRemoveCooperationModeCommand)
// 解析头部信息
header := controller.GetRequestHeader(controller.Ctx)
batchRemoveCooperationModeCommand.CompanyId = header.CompanyId
batchRemoveCooperationModeCommand.OrgId = header.OrgId
batchRemoveCooperationModeCommand.UserId = header.UserId
batchRemoveCooperationModeCommand.UserBaseId = header.UserBaseId
data, err := cooperationModeService.BatchRemoveCooperationMode(batchRemoveCooperationModeCommand)
controller.Response(data, err)
}
func (controller *CooperationModeController) SearchCooperationMode() {
cooperationModeService := service.NewCooperationModeService(nil)
searchCooperationModeQuery := &query.SearchCooperationModeQuery{}
... ... @@ -101,3 +115,31 @@ func (controller *CooperationModeController) ListCooperationMode() {
data, err := cooperationModeService.ListCooperationMode(listCooperationModeQuery)
controller.Response(data, err)
}
func (controller *CooperationModeController) OperateCooperationMode() {
cooperationModeService := service.NewCooperationModeService(nil)
operateCooperationModeCommand := &command.OperateCooperationModeCommand{}
_ = controller.Unmarshal(operateCooperationModeCommand)
// 解析头部信息
header := controller.GetRequestHeader(controller.Ctx)
operateCooperationModeCommand.CompanyId = header.CompanyId
operateCooperationModeCommand.OrgId = header.OrgId
operateCooperationModeCommand.UserId = header.UserId
operateCooperationModeCommand.UserBaseId = header.UserBaseId
data, err := cooperationModeService.OperateCooperationMode(operateCooperationModeCommand)
controller.Response(data, err)
}
func (controller *CooperationModeController) BatchOperateCooperationMode() {
cooperationModeService := service.NewCooperationModeService(nil)
batchOperateCooperationModeCommand := &command.BatchOperateCooperationModeCommand{}
_ = controller.Unmarshal(batchOperateCooperationModeCommand)
// 解析头部信息
header := controller.GetRequestHeader(controller.Ctx)
batchOperateCooperationModeCommand.CompanyId = header.CompanyId
batchOperateCooperationModeCommand.OrgId = header.OrgId
batchOperateCooperationModeCommand.UserId = header.UserId
batchOperateCooperationModeCommand.UserBaseId = header.UserBaseId
data, err := cooperationModeService.BatchOperateCooperationMode(batchOperateCooperationModeCommand)
controller.Response(data, err)
}
... ...
... ... @@ -6,10 +6,13 @@ import (
)
func init() {
web.Router("/cooperation-modes/", &controllers.CooperationModeController{}, "Post:CreateCooperationMode")
web.Router("/cooperation-modes/:cooperationModeId", &controllers.CooperationModeController{}, "Put:UpdateCooperationMode")
web.Router("/cooperation-modes/:cooperationModeId", &controllers.CooperationModeController{}, "Get:GetCooperationMode")
web.Router("/cooperation-modes/:cooperationModeId", &controllers.CooperationModeController{}, "Delete:RemoveCooperationMode")
web.Router("/cooperation-modes/search", &controllers.CooperationModeController{}, "Post:SearchCooperationMode")
web.Router("/cooperation-modes/", &controllers.CooperationModeController{}, "Get:ListCooperationMode")
web.Router("/cooperation-modes/", &controllers.CooperationModeController{}, "Post:CreateCooperationMode") // 创建共创模式
web.Router("/cooperation-modes/:cooperationModeId", &controllers.CooperationModeController{}, "Put:UpdateCooperationMode") // 编辑共创模式
web.Router("/cooperation-modes/:cooperationModeId", &controllers.CooperationModeController{}, "Get:GetCooperationMode") // 获取共创模式详情
web.Router("/cooperation-modes/:cooperationModeId", &controllers.CooperationModeController{}, "Delete:RemoveCooperationMode") // 删除共创模式
web.Router("/cooperation-modes/batch-remove", &controllers.CooperationModeController{}, "Post:BatchRemoveCooperationMode") // 批量删除共创模式
web.Router("/cooperation-modes/operate", &controllers.CooperationModeController{}, "Post:OperateCooperationMode") // 启用或禁用共创模式
web.Router("/cooperation-modes/batch-operate", &controllers.CooperationModeController{}, "Post:BatchOperateCooperationMode") // 批量启用或禁用共创模式
web.Router("/cooperation-modes/search", &controllers.CooperationModeController{}, "Post:SearchCooperationMode") // 查找共创模式
web.Router("/cooperation-modes/", &controllers.CooperationModeController{}, "Get:ListCooperationMode") // 返回共创模式列表
}
... ...