作者 tangxuhui

添加消息读取

package factory
import (
"github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/dao"
)
func CreateNoticePersonalDao(options map[string]interface{}) (*dao.NoticePersonalDao, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return dao.NewNoticePersonalDao(transactionContext)
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type ReadNoticeCommand struct {
UserId int `json:"userId" valid:"Required"` //读取谁的消息读
NoticeId int `json:"noticeId"` //消息id
ReadAll bool `json:"readAll"` //是否全部标记为已读
}
func (readNoticeCommand *ReadNoticeCommand) Valid(validation *validation.Validation) {
if !readNoticeCommand.ReadAll {
if readNoticeCommand.NoticeId <= 0 {
validation.AddError("消息id", "未填写")
}
}
}
func (readNoticeCommand *ReadNoticeCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(readNoticeCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -12,6 +12,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticePersonal/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/noticePersonal/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/dao"
)
// 个人消息通知
... ... @@ -627,6 +628,40 @@ func (noticePersonalService *NoticePersonalService) CreditAccountPayment(creditA
return noticePersonal, nil
}
// 设置消息:账期结算消息-分红预算
func (noticePersonalService *NoticePersonalService) ReadNotice(readNoticeCommand *command.ReadNoticeCommand) (interface{}, error) {
if err := readNoticeCommand.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()
}()
// 共创合约DAO初始化
var noticePersonalDao *dao.NoticePersonalDao
if value, err := factory.CreateNoticePersonalDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
noticePersonalDao = value
}
_, err = noticePersonalDao.ReadNoticePersonal(int64(readNoticeCommand.UserId),
readNoticeCommand.NoticeId, readNoticeCommand.ReadAll)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return readNoticeCommand, nil
}
func NewNoticePersonalService(options map[string]interface{}) *NoticePersonalService {
newNoticePersonalService := &NoticePersonalService{}
return newNoticePersonalService
... ...
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)
type NoticePersonalDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewNoticePersonalDao(transactionContext *pgTransaction.TransactionContext) (*NoticePersonalDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &NoticePersonalDao{
transactionContext: transactionContext,
}, nil
}
}
//
func (dao *NoticePersonalDao) ReadNoticePersonal(userId int64, noticeId int, readAll bool) (int, error) {
tx := dao.transactionContext.PgTx
updateData := map[string]interface{}{
"is_read": domain.NoticePersonalIsRead,
}
if readAll {
result, err := tx.Model(&updateData).
TableExpr("notice_personals").
Where("user_id=?", userId).
Where("is_read=?", domain.NoticePersonalIsNotRead).
Update()
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
result, err := tx.Model(&updateData).
TableExpr("notice_personals").
Where("notice_personal_id=?", noticeId).
Where("user_id=?", userId).
Update()
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
... ...
... ... @@ -68,3 +68,11 @@ func (controller *NoticePersonalController) CreditAccountPayment() {
data, err := noticePersonalService.CreditAccountPayment(creditAccountPaymentCommand)
controller.Response(data, err)
}
func (controller *NoticePersonalController) ReadNotice() {
noticePersonalService := service.NewNoticePersonalService(nil)
readNoticeCommand := &command.ReadNoticeCommand{}
controller.Unmarshal(readNoticeCommand)
data, err := noticePersonalService.ReadNotice(readNoticeCommand)
controller.Response(data, err)
}
... ...
... ... @@ -7,6 +7,7 @@ import (
func init() {
web.Router("/notice-personal/", &controllers.NoticePersonalController{}, "Post:GetNoticePersonalList")
web.Router("/notice-personal/read", &controllers.NoticePersonalController{}, "Post:ReadNotice")
web.Router("/notice-personal/agree-join-creation-project", &controllers.NoticePersonalController{}, "Post:AgreeJoinCreationProject")
web.Router("/notice-personal/inform-expected-dividends", &controllers.NoticePersonalController{}, "Post:InformExpectedDividends")
web.Router("/notice-personal/refuse-join-creation-project", &controllers.NoticePersonalController{}, "Post:RefuseJoinCreationProject")
... ...