Files
zmkgC/test/intro_test.go
2025-07-07 20:11:59 +08:00

76 lines
2.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package test
import (
"context"
"testing"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/util/gconv"
"github.com/samber/lo"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
wxDao "github.com/tiger1103/gfast/v3/internal/app/wxApplet/dao"
"github.com/tiger1103/gfast/v3/library/liberr"
richtext "github.com/tiger1103/gfast/v3/third/richText"
)
// 获取所有的后台项目公告同步一份给App
func TestSyncIntro(t *testing.T) {
intructID := []entity.SysProjectIntroduce{}
if err := dao.SysProjectIntroduce.Ctx(context.Background()).Scan(&intructID); err != nil {
t.Error(err)
}
ctx := context.Background()
var notificationsList []do.NotificationRecipients
for _, v := range intructID {
// imags := richtext.ExtractImageURLs(v.RichText)
data := entity.Notifications{
Title: v.Headline,
NotificationText: richtext.RemoveRichText(v.RichText),
NotificationTime: v.CreatedAt.Format("2006-01-02 15:04:05"),
IsApp: 2,
Initiator: gconv.Int(v.CreatedBy),
ProjectId: int(v.ProjectId),
IntroduceId: int(v.ID),
}
notifiID, err := dao.Notifications.Ctx(ctx).InsertAndGetId(data)
if err != nil {
t.Fatal(err)
}
// 获取项目下的所有用户
projectUserIDs, err := wxDao.SysUserProjectRelevancy.Ctx(ctx).
Where(wxDao.SysUserProjectRelevancy.Columns().ProjectId, int(v.ProjectId)).
Fields(wxDao.SysUserProjectRelevancy.Columns().UserId).Distinct().Array()
liberr.ErrIsNil(ctx, err)
constructionUserIDs, err := dao.BusConstructionUser.Ctx(ctx).As("bcs").
InnerJoin("sys_user as su", "bcs.phone = su.mobile").
Where("bcs.project_id", int(v.ProjectId)).
Fields("su.id").Distinct().Array()
liberr.ErrIsNil(ctx, err)
// 将两个用户ID列表合并并去重
Users := lo.Uniq(lo.Map(append(projectUserIDs, constructionUserIDs...), func(item *gvar.Var, _ int) int {
return item.Int()
}))
lo.ForEach(Users, func(item int, _ int) {
notificationsList = append(notificationsList, do.NotificationRecipients{
NotificationId: notifiID, // 通知ID
RecipientId: item, // 接收者ID
NotificationStatus: 0, // 0 未读
IntroduceId: int(v.ID), // 关联的PC 端项目新闻ID
})
})
}
if _, err := dao.NotificationRecipients.Ctx(ctx).Insert(notificationsList); err != nil {
t.Error(err)
}
}