notice_personal_dao.go 1.2 KB
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(userBaseId 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_base_id=?", userBaseId).
			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).
		Update()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}