// ========================================================================== // GFast自动生成logic操作代码。 // 生成日期:2024-04-11 16:56:36 // 生成路径: internal/app/system/logic/notifications.go // 生成人:gfast // desc:通知信息 // company:云南奇讯科技有限公司 // ========================================================================== package logic import ( "context" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/jinzhu/copier" "github.com/samber/lo" "github.com/tiger1103/gfast/v3/api/v1/system" "github.com/tiger1103/gfast/v3/internal/app/system/consts" "github.com/tiger1103/gfast/v3/internal/app/system/dao" "github.com/tiger1103/gfast/v3/internal/app/system/model" "github.com/tiger1103/gfast/v3/internal/app/system/model/do" "github.com/tiger1103/gfast/v3/internal/app/system/model/entity" "github.com/tiger1103/gfast/v3/internal/app/system/service" "github.com/tiger1103/gfast/v3/library/liberr" ) func init() { service.RegisterNotifications(New()) } func New() *sNotifications { return &sNotifications{} } type sNotifications struct{} func (s *sNotifications) List(ctx context.Context, req *system.NotificationsSearchReq) (listRes *system.NotificationsSearchRes, err error) { listRes = new(system.NotificationsSearchRes) err = g.Try(ctx, func(ctx context.Context) { m := dao.Notifications.Ctx(ctx).WithAll() if req.Id != "" { m = m.Where(dao.Notifications.Columns().Id+" = ?", req.Id) } if len(req.DateRange) != 0 { m = m.Where(dao.Notifications.Columns().CreatedAt+" >=? AND "+dao.Notifications.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1]) } if req.NotificationText != "" { m = m.Where(dao.Notifications.Columns().NotificationText+" = ?", req.NotificationText) } if req.Title != "" { m = m.Where(dao.Notifications.Columns().Title+" = ?", req.Title) } if req.Route != "" { m = m.Where(dao.Notifications.Columns().Route+" = ?", req.Route) } if req.NotificationTime != "" { m = m.Where(dao.Notifications.Columns().NotificationTime+" = ?", req.NotificationTime) } if req.Initiator != "" { m = m.Where(dao.Notifications.Columns().Initiator+" = ?", gconv.Int(req.Initiator)) } if req.ProjectId != "" { m = m.Where(dao.Notifications.Columns().ProjectId+" = ?", gconv.Int(req.ProjectId)) } if req.Type != "" { m = m.Where(dao.Notifications.Columns().IsApp, gconv.Int(req.Type)) } m = m.LeftJoin("sys_user AS initiator_user", "notifications.initiator = initiator_user.id"). LeftJoin("notification_recipients AS nr", "notifications.id = nr.notification_id"). LeftJoin("sys_user AS recipient_user", "nr.recipient_id = recipient_user.id"). Where(dao.Notifications.Columns().IsApp, req.IsApp) if req.PageNum == 0 { req.PageNum = 1 } listRes.CurrentPage = req.PageNum if req.PageSize == 0 { req.PageSize = consts.PageSize } order := "id desc" if req.OrderBy != "" { order = req.OrderBy } var res []*model.NotificationsInfoRes err = m.Fields(`notifications.id,notifications.created_at, notification_text, title, route, notification_time, initiator, project_id, positions, initiator_user.user_nickname AS initiator_nickname, GROUP_CONCAT(recipient_user.user_nickname) AS recipients_nickname`).Page(req.PageNum, req.PageSize).Order(order).Group("notifications.id").Scan(&res) liberr.ErrIsNil(ctx, err, "获取数据失败") listRes.Total, err = m.CountColumn("DISTINCT notifications.id") liberr.ErrIsNil(ctx, err, "获取总行数失败") listRes.List = make([]*model.NotificationsListRes, len(res)) copier.Copy(&listRes.List, &res) }) return } func (s *sNotifications) GetById(ctx context.Context, id uint) (res *model.NotificationsInfoRes, err error) { err = g.Try(ctx, func(ctx context.Context) { err = dao.Notifications.Ctx(ctx).WithAll().Where(dao.Notifications.Columns().Id, id).Scan(&res) liberr.ErrIsNil(ctx, err, "获取信息失败") }) return } func (s *sNotifications) Add(ctx context.Context, req *system.NotificationsAddReq) (err error) { err = g.Try(ctx, func(ctx context.Context) { _, err = dao.Notifications.Ctx(ctx).Insert(do.Notifications{ NotificationText: req.NotificationText, Route: req.Route, NotificationTime: req.NotificationTime, Initiator: req.Initiator, ProjectId: req.ProjectId, }) liberr.ErrIsNil(ctx, err, "添加失败") }) return } func (s *sNotifications) Edit(ctx context.Context, req *system.NotificationsEditReq) (err error) { err = g.Try(ctx, func(ctx context.Context) { _, err = dao.Notifications.Ctx(ctx).WherePri(req.Id).Update(do.Notifications{ Title: req.Title, NotificationText: req.NotificationText, Route: req.Route, NotificationTime: req.NotificationTime, Initiator: req.Initiator, ProjectId: req.ProjectId, }) liberr.ErrIsNil(ctx, err, "修改失败") // 修改在 sys_project_introduce 表中的数据 subQuery := g.Model("notifications").Fields("introduce_id").Where("id", req.Id) _, err = dao.SysProjectIntroduce.Ctx(ctx).Data(do.SysProjectIntroduce{ Headline: req.Title, RichText: req.NotificationText, }).Where("id = ?", subQuery).Update() liberr.ErrIsNil(ctx, err, "修改失败") }) return } func (s *sNotifications) Delete(ctx context.Context, ids []uint) (err error) { err = g.Try(ctx, func(ctx context.Context) { // 获取对应的 IntroduceId introduces := []entity.SysProjectIntroduce{} err = dao.Notifications.Ctx(ctx).Fields("introduce_id").WhereIn("id", ids).Scan(&introduces) liberr.ErrIsNil(ctx, err, "获取 IntroduceId 失败") introduceIDS := lo.FilterMap(introduces, func(item entity.SysProjectIntroduce, _ int) (int, bool) { return int(item.IntroduceID), true }) _, err = dao.Notifications.Ctx(ctx).Delete(dao.Notifications.Columns().Id+" in (?)", ids) liberr.ErrIsNil(ctx, err, "删除失败") // 删除在 Notification_Recipients 表中的数据 _, err = dao.NotificationRecipients.Ctx(ctx).Delete(dao.NotificationRecipients.Columns().NotificationId+" in (?)", ids) liberr.ErrIsNil(ctx, err, "Recipients 删除失败") // 删除在 sys_project_introduce 表中的数据 _, err = dao.SysProjectIntroduce.Ctx(ctx).Delete(dao.Notifications.Columns().Id+" in (?)", introduceIDS) liberr.ErrIsNil(ctx, err, "Introduce 删除失败") }) return }