作者 tangxuhui

实现:消息配置

... ... @@ -7,7 +7,7 @@ import (
)
type ListDictionaryQuery struct {
// 查询偏离量
// 查询页码
Pageindex int `json:"pageIndex"`
// 查询限制
PageSize int `json:"pageSize" valid:"Required"`
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)
type AddNoticeSettingCommand struct {
// 公司id
CompanyId int64 `json:"companyId"`
// 内容模板
Content string `json:"content"`
// 是否推送 【是:1】【否:2】
IsPush int `json:"isPush"`
// 消息对应的业务模块
Module string `json:"module"`
// 业务环节
ModuleAction string `json:"moduleAction"`
// 组织id
OrgId int64 `json:"orgId"`
// 消息对应的编码
SysCode string `json:"sysCode"`
}
func (addNoticeSettingCommand *AddNoticeSettingCommand) Valid(v *validation.Validation) {
ok := domain.ValidNoticeModule(addNoticeSettingCommand.Module)
if ok {
v.SetError("Module", "不是规定的值")
}
//检查消息编码
ok = domain.ValidNoticeModuleAction(addNoticeSettingCommand.Module, addNoticeSettingCommand.ModuleAction)
if !ok {
v.SetError("ModuleAction", "不是规定的值")
}
}
func (addNoticeSettingCommand *AddNoticeSettingCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(addNoticeSettingCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -4,9 +4,11 @@ import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)
type UpdateNoticeSettingCommand struct {
//id
NoticeSettingId int64 `json:"noticeSettingId"`
// 公司id
CompanyId int64 `json:"companyId"`
... ... @@ -19,13 +21,22 @@ type UpdateNoticeSettingCommand struct {
// 业务环节
ModuleAction string `json:"moduleAction"`
// 组织id
OrganizationId int64 `json:"organizationId"`
OrgId int64 `json:"orgId"`
// 消息对应的编码
SysCode string `json:"sysCode"`
}
func (updateNoticeSettingCommand *UpdateNoticeSettingCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
func (updateNoticeSettingCommand *UpdateNoticeSettingCommand) Valid(v *validation.Validation) {
ok := domain.ValidNoticeModule(updateNoticeSettingCommand.Module)
if ok {
v.SetError("Module", "不是规定的值")
}
//检查消息编码
ok = domain.ValidNoticeModuleAction(updateNoticeSettingCommand.Module, updateNoticeSettingCommand.ModuleAction)
if !ok {
v.SetError("ModuleAction", "不是规定的值")
}
}
func (updateNoticeSettingCommand *UpdateNoticeSettingCommand) ValidateCommand() error {
... ...
... ... @@ -8,13 +8,15 @@ import (
type ListNoticeSettingQuery struct {
// 查询偏离量
Offset int `json:"offset" valid:"Required"`
PageIndex int `json:"offset" valid:"Required"`
// 查询限制
Limit int `json:"limit" valid:"Required"`
PageSize int `json:"limit" valid:"Required"`
CompanyId int64 `json:"companyId" valid:"Required"`
OrgId int64 `json:"orgId"`
}
func (listNoticeSettingQuery *ListNoticeSettingQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listNoticeSettingQuery *ListNoticeSettingQuery) ValidateQuery() error {
... ...
... ... @@ -2,6 +2,7 @@ package service
import (
"fmt"
"time"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
... ... @@ -38,12 +39,14 @@ func (noticeSettingService *NoticeSettingService) GetNoticeSetting(getNoticeSett
} else {
noticeSettingRepository = value
}
noticeSetting, err := noticeSettingRepository.FindOne(map[string]interface{}{"noticeSettingId": getNoticeSettingQuery.NoticeSettingId})
noticeSetting, err := noticeSettingRepository.FindOne(map[string]interface{}{
"noticeSettingId": getNoticeSettingQuery.NoticeSettingId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if noticeSetting == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getNoticeSettingQuery.NoticeSettingId)))
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%d", getNoticeSettingQuery.NoticeSettingId))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ... @@ -53,6 +56,7 @@ func (noticeSettingService *NoticeSettingService) GetNoticeSetting(getNoticeSett
}
// 返回编排消息通知内容列表
// 检查初始化消息列表
func (noticeSettingService *NoticeSettingService) ListNoticeSetting(listNoticeSettingQuery *query.ListNoticeSettingQuery) (interface{}, error) {
if err := listNoticeSettingQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -111,15 +115,42 @@ func (noticeSettingService *NoticeSettingService) UpdateNoticeSetting(updateNoti
} else {
noticeSettingRepository = value
}
noticeSetting, err := noticeSettingRepository.FindOne(map[string]interface{}{"noticeSettingId": updateNoticeSettingCommand.NoticeSettingId})
var (
noticeSettings []*domain.NoticeSetting
noticeSetting *domain.NoticeSetting
)
_, noticeSettings, err = noticeSettingRepository.Find(map[string]interface{}{
"companyId": updateNoticeSettingCommand.CompanyId,
"orgId": updateNoticeSettingCommand.OrgId,
"moduleAction": updateNoticeSettingCommand.ModuleAction,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if noticeSetting == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateNoticeSettingCommand.NoticeSettingId)))
if len(noticeSettings) > 1 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "配置信息重复")
}
if len(noticeSettings) > 0 {
//存在旧的数据
noticeSetting = noticeSettings[0]
if noticeSetting.NoticeSettingId != updateNoticeSettingCommand.NoticeSettingId {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "配置信息已存在")
}
}
noticeSetting, err = noticeSettingRepository.FindOne(map[string]interface{}{
"noticeSettingId": updateNoticeSettingCommand.NoticeSettingId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := noticeSetting.Update(tool_funs.SimpleStructToMap(updateNoticeSettingCommand)); err != nil {
err = noticeSetting.Update(map[string]interface{}{
"content": updateNoticeSettingCommand.Content,
"isPush": updateNoticeSettingCommand.IsPush,
"module": updateNoticeSettingCommand.Module,
"moduleAction": updateNoticeSettingCommand.ModuleAction,
"updatedAt": time.Now(),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if noticeSetting, err := noticeSettingRepository.Save(noticeSetting); err != nil {
... ... @@ -132,6 +163,126 @@ func (noticeSettingService *NoticeSettingService) UpdateNoticeSetting(updateNoti
}
}
// 更新编排消息通知内容
func (noticeSettingService *NoticeSettingService) AddNoticeSetting(addNoticeSettingCommand *command.AddNoticeSettingCommand) (interface{}, error) {
if err := addNoticeSettingCommand.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 noticeSettingRepository domain.NoticeSettingRepository
if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
noticeSettingRepository = value
}
var (
noticeSettings []*domain.NoticeSetting
noticeSetting *domain.NoticeSetting
)
_, noticeSettings, err = noticeSettingRepository.Find(map[string]interface{}{
"companyId": addNoticeSettingCommand.CompanyId,
"orgId": addNoticeSettingCommand.OrgId,
"moduleAction": addNoticeSettingCommand.ModuleAction,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(noticeSettings) > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该环节配置已存在")
}
noticeSetting = &domain.NoticeSetting{
CompanyId: addNoticeSettingCommand.CompanyId,
Content: addNoticeSettingCommand.Content,
IsPush: addNoticeSettingCommand.IsPush,
Module: addNoticeSettingCommand.Module,
ModuleAction: addNoticeSettingCommand.ModuleAction,
OrgId: addNoticeSettingCommand.OrgId,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if noticeSetting, err := noticeSettingRepository.Save(noticeSetting); 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 noticeSetting, nil
}
}
// //InitNoticeSetting 为企业初始化消息列表,填充空白的消息模板
// func (noticeSettingService *NoticeSettingService) InitNoticeSetting(initCommand *command.InitNoticeSettingCommand) error {
// if err := initCommand.ValidateCommand(); err != nil {
// return application.ThrowError(application.ARG_ERROR, err.Error())
// }
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
// var noticeSettingRepository domain.NoticeSettingRepository
// if value, err := factory.CreateNoticeSettingRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// } else {
// noticeSettingRepository = value
// }
// _, noticeSets, err := noticeSettingRepository.Find(map[string]interface{}{
// "companyId": initCommand.CompanyId,
// "orgId": initCommand.OrgId,
// })
// if err != nil {
// return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// notticeExist := make(map[string]int)
// for i := range noticeSets {
// notticeExist[noticeSets[i].ModuleAction] = 1
// }
// defaultModuleAction := domain.GetNoticeModuleActionList()
// newEmptySetting := []domain.NoticeSetting{}
// for _, act := range defaultModuleAction {
// if _, ok := notticeExist[act.ActionCode]; !ok {
// newEmptySetting = append(newEmptySetting, domain.NoticeSetting{
// CompanyId: initCommand.CompanyId,
// OrgId: initCommand.OrgId,
// Module: act.ModuleCode,
// ModuleAction: act.ActionCode,
// CreatedAt: time.Now(),
// UpdatedAt: time.Now(),
// Content: "",
// IsPush: domain.NoticeSettingIsNotPush,
// })
// }
// }
// for i := range newEmptySetting {
// _, err = noticeSettingRepository.Save(&newEmptySetting[i])
// if err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// }
// if err := transactionContext.CommitTransaction(); err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// return nil
// }
func NewNoticeSettingService(options map[string]interface{}) *NoticeSettingService {
newNoticeSettingService := &NoticeSettingService{}
return newNoticeSettingService
... ...
... ... @@ -17,7 +17,7 @@ type NoticeEmpty struct {
}
type NoticeEmptyRepository interface {
// Save(noticeEmpty *NoticeEmpty) (*NoticeEmpty, error) //用不到
Save(noticeEmpty *NoticeEmpty) (*NoticeEmpty, error) //用不到
// Remove(noticeEmpty *NoticeEmpty) (*NoticeEmpty, error)//用户不到
// FindOne(queryOptions map[string]interface{}) (*NoticeEmpty, error) //用不到
Find(queryOptions map[string]interface{}) (int64, []*NoticeEmpty, error)
... ...
... ... @@ -2,6 +2,12 @@ package domain
import "time"
// 是否推送 【是:1】【否:2】
const (
NoticeSettingIsPush = 1
NoticeSettingIsNotPush = 2
)
// 编排消息通知内容
type NoticeSetting struct {
// 消息id
... ... @@ -18,8 +24,6 @@ type NoticeSetting struct {
ModuleAction string `json:"moduleAction"`
// 组织id
OrgId int64 `json:"orgId"`
// 消息对应的编码
SysCode string `json:"sysCode"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 删除时间
... ... @@ -28,6 +32,8 @@ type NoticeSetting struct {
UpdatedAt time.Time `json:"updatedAt"`
}
//OrgId+ModuleAction 做唯一索引
type NoticeSettingRepository interface {
Save(noticeSetting *NoticeSetting) (*NoticeSetting, error)
Remove(noticeSetting *NoticeSetting) (*NoticeSetting, error)
... ... @@ -64,9 +70,6 @@ func (noticeSetting *NoticeSetting) Update(data map[string]interface{}) error {
if organizationId, ok := data["orgId"]; ok {
noticeSetting.OrgId = organizationId.(int64)
}
if sysCode, ok := data["sysCode"]; ok {
noticeSetting.SysCode = sysCode.(string)
}
if createdAt, ok := data["createdAt"]; ok {
noticeSetting.CreatedAt = createdAt.(time.Time)
}
... ...
... ... @@ -18,8 +18,6 @@ type NoticeSetting struct {
NoticeSettingId int64
// 组织id
OrgId int64
// 消息对应的编码
SysCode string
// 创建时间
CreatedAt time.Time
// 删除时间
... ...
... ... @@ -13,8 +13,7 @@ func TransformToNoticeSettingDomainModelFromPgModels(noticeSettingModel *models.
Module: noticeSettingModel.Module,
ModuleAction: noticeSettingModel.ModuleAction,
NoticeSettingId: noticeSettingModel.NoticeSettingId,
OrganizationId: noticeSettingModel.OrganizationId,
SysCode: noticeSettingModel.SysCode,
OrgId: noticeSettingModel.OrgId,
CreatedAt: noticeSettingModel.CreatedAt,
DeletedAt: noticeSettingModel.DeletedAt,
UpdatedAt: noticeSettingModel.UpdatedAt,
... ...
... ... @@ -27,8 +27,7 @@ func (repository *NoticeSettingRepository) Save(noticeSetting *domain.NoticeSett
"module",
"module_action",
"notice_setting_id",
"organization_id",
"sys_code",
"org_id",
"created_at",
"deleted_at",
"updated_at",
... ... @@ -55,7 +54,6 @@ func (repository *NoticeSettingRepository) Save(noticeSetting *domain.NoticeSett
&noticeSetting.ModuleAction,
&noticeSetting.NoticeSettingId,
&noticeSetting.OrgId,
&noticeSetting.SysCode,
&noticeSetting.CreatedAt,
&noticeSetting.DeletedAt,
&noticeSetting.UpdatedAt,
... ... @@ -68,7 +66,6 @@ func (repository *NoticeSettingRepository) Save(noticeSetting *domain.NoticeSett
noticeSetting.ModuleAction,
noticeSetting.NoticeSettingId,
noticeSetting.OrgId,
noticeSetting.SysCode,
noticeSetting.CreatedAt,
noticeSetting.DeletedAt,
noticeSetting.UpdatedAt,
... ... @@ -85,7 +82,6 @@ func (repository *NoticeSettingRepository) Save(noticeSetting *domain.NoticeSett
&noticeSetting.ModuleAction,
&noticeSetting.NoticeSettingId,
&noticeSetting.OrgId,
&noticeSetting.SysCode,
&noticeSetting.CreatedAt,
&noticeSetting.DeletedAt,
&noticeSetting.UpdatedAt,
... ... @@ -98,7 +94,6 @@ func (repository *NoticeSettingRepository) Save(noticeSetting *domain.NoticeSett
noticeSetting.ModuleAction,
noticeSetting.NoticeSettingId,
noticeSetting.OrgId,
noticeSetting.SysCode,
noticeSetting.CreatedAt,
noticeSetting.DeletedAt,
noticeSetting.UpdatedAt,
... ... @@ -109,6 +104,7 @@ func (repository *NoticeSettingRepository) Save(noticeSetting *domain.NoticeSett
}
return noticeSetting, nil
}
func (repository *NoticeSettingRepository) Remove(noticeSetting *domain.NoticeSetting) (*domain.NoticeSetting, error) {
tx := repository.transactionContext.PgTx
noticeSettingModel := new(models.NoticeSetting)
... ... @@ -118,6 +114,7 @@ func (repository *NoticeSettingRepository) Remove(noticeSetting *domain.NoticeSe
}
return noticeSetting, nil
}
func (repository *NoticeSettingRepository) FindOne(queryOptions map[string]interface{}) (*domain.NoticeSetting, error) {
tx := repository.transactionContext.PgTx
noticeSettingModel := new(models.NoticeSetting)
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticeSetting/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticeSetting/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticeSetting/service"
)
type NoticeSettingController struct {
beego.BaseController
}
func (controller *NoticeSettingController) UpdateNoticeSetting() {
noticeSettingService := service.NewNoticeSettingService(nil)
updateNoticeSettingCommand := &command.UpdateNoticeSettingCommand{}
controller.Unmarshal(updateNoticeSettingCommand)
Id, _ := controller.GetInt64(":settingId")
updateNoticeSettingCommand.NoticeSettingId = Id
data, err := noticeSettingService.UpdateNoticeSetting(updateNoticeSettingCommand)
controller.Response(data, err)
}
func (controller *NoticeSettingController) GetNoticeSetting() {
noticeSettingService := service.NewNoticeSettingService(nil)
getNoticeSettingQuery := &query.GetNoticeSettingQuery{}
Id, _ := controller.GetInt64(":settingId")
getNoticeSettingQuery.NoticeSettingId = Id
data, err := noticeSettingService.GetNoticeSetting(getNoticeSettingQuery)
controller.Response(data, err)
}
func (controller *NoticeSettingController) ListNoticeSetting() {
noticeSettingService := service.NewNoticeSettingService(nil)
listNoticeSettingQuery := &query.ListNoticeSettingQuery{}
data, err := noticeSettingService.ListNoticeSetting(listNoticeSettingQuery)
controller.Response(data, err)
}
func (controller *NoticeSettingController) AddNoticeSetting() {
noticeSettingService := service.NewNoticeSettingService(nil)
addNoticeSettingCommand := &command.AddNoticeSettingCommand{}
controller.Unmarshal(addNoticeSettingCommand)
data, err := noticeSettingService.AddNoticeSetting(addNoticeSettingCommand)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/port/beego/controllers"
)
func init() {
web.Router("/notice-setting/:settingId", &controllers.NoticeSettingController{}, "Put:UpdateNoticeSetting")
web.Router("/notice-setting/:settingId", &controllers.NoticeSettingController{}, "Get:GetNoticeSetting")
web.Router("/notice-setting/", &controllers.NoticeSettingController{}, "Get:ListNoticeSetting")
web.Router("/notice-setting", &controllers.NoticeSettingController{}, "Post:UpdateNoticeSetting")
}
... ...