作者 tangxvhui
... ... @@ -16,7 +16,7 @@ info(
service Core {
@doc "用户申请加入公司"
@handler miniUserApplyJoinCompany
post /mini/user/apply-join-company(MiniUserApplyJoinCompanyRequest) returns (MiniUserApplyJoinCompanyResponse)
post /mini/user/apply_join_company(MiniUserApplyJoinCompanyRequest) returns (MiniUserApplyJoinCompanyResponse)
@doc "用户登录"
@handler miniUserLogin
post /mini/user/login (MiniUserLoginRequest) returns (MiniUserLoginResponse)
... ... @@ -29,7 +29,7 @@ service Core {
service Core {
@doc "切换账号"
@handler miniUserSwitchAccount
post /mini/user/switch-account (MiniUserSwitchAccountRequest) returns (MiniUserLoginResponse)
post /mini/user/switch_account (MiniUserSwitchAccountRequest) returns (MiniUserLoginResponse)
@doc "用户信息"
@handler miniUserInfo
post /mini/user/info (MiniUserInfoRequest) returns (MiniUserInfoResponse)
... ... @@ -38,22 +38,31 @@ service Core {
post /mini/user/statistics (UserStatisticsRequest) returns (UserStatisticsResponse)
@doc "用户审核列表"
@handler miniUserAuditList
post /mini/user/audit-list (UserSearchRequest)returns(UserSearchResponse)
post /mini/user/audit_list (UserSearchRequest)returns(UserSearchResponse)
@doc "用户审核"
@handler miniUserAudit
post /mini/user/audit (MiniUserAuditRequest)
@doc "部门用户列表"
@handler miniUserDepartmentUsers
post /mini/user/department-users (MiniUserDepartmentUsersRequest)
post /mini/user/department_users (MiniUserDepartmentUsersRequest)
@doc "用户列表"
@handler miniUsersList
post /mini/user/user-list (MiniUsersListRequest)
post /mini/user/user_list (MiniUsersListRequest)
@doc "用户快讯"
@handler miniUserNews
post /mini/user/news (MiniUserNewsRequest)returns(MiniUserNewsResposne)
@doc "关注我的人"
@handler miniUserFollower
post /mini/user/follower (MiniUserFollowedSearchRequest)returns(MiniUserFollowedSearchResponse)
@doc "我关注的人"
@handler miniUserFollowing
post /mini/user/following (MiniUserFollowedSearchRequest)returns(MiniUserFollowedSearchResponse)
@doc "我关注的人-最新未读列表(未读标红)"
@handler miniUserFollowingLatestUnreadList
post /mini/user/following/latest_unread_list (MiniUserFollowedSearchRequest)returns(MiniUserFollowedSearchResponse)
@doc "我关注的人-标记已读"
@handler miniUserFollowingMarkRead
post /mini/user/following/mark_read (MiniUserFollowingMarkReadRequest)
@doc "关注"
@handler miniUserFollow
post /mini/user/follow (FollowRequest)
... ... @@ -115,6 +124,23 @@ type(
ArticleId int64 `json:"articleId,optional"` // 按文章ID(返回文章可见的用户)
RoleId int64 `json:"roleId,optional"` // 按角色角色关联的用户
}
MiniUserNewsRequest{
AuthorId int64 `json:"authorId,optional"` // 特定作者ID
LastArticleId int64 `json:"lastArticleId,optional"`// 最后文章ID
Size int `json:"size"` // 数量
}
MiniUserNewsResposne{
List []UserNewsItem `json:"list"`
Total int64 `json:"total"`
}
UserNewsItem{
NewsId int64 `json:"newsId"` // 快讯ID
Type string `json:"type"` // 快讯类型 文章:Article 讨论:Discuss ...
Title string `json:"title"` // 标题
Summary string `json:"summary"` // 快讯概要
Time int64 `json:"time"` // 时间
ReadFlag bool `json:"readFlag"` // 已读标识 true:已读 false:未读
}
MiniUserFollowedSearchRequest{
Page int `json:"page,optional"`
Size int `json:"size,optional"`
... ... @@ -123,6 +149,11 @@ type(
List []*UserFollowItem `json:"users"`
Total int64 `json:"total"`
}
MiniUserFollowingMarkReadRequest{
UserId int64 `json:"userId"`
}
UserItem {
Id int64 `json:"id,omitempty"` // 用户ID
CompanyId int64 `json:"companyId,omitempty"` // 公司ID
... ... @@ -176,6 +207,7 @@ type(
Position string `json:"position"` // 职位
Followed bool `json:"followed"` // 关注
MutualFollowed bool `json:"mutualFollowed"` // 互相关注标识
ReadFlag bool `json:"readFlag"` // 已读标识 true:已读 false:未读(小红点)
}
)
... ... @@ -229,7 +261,7 @@ service Core {
post /system/user/statistics (UserStatisticsRequest) returns (UserStatisticsResponse)
@doc "用户列表"
@handler systemUsersList
post /system/user/user-list (MiniUsersListRequest)
post /system/user/user_list (MiniUsersListRequest)
@doc "用户详情"
@handler systemUserGet
... ...
... ... @@ -148,7 +148,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{
{
Method: http.MethodPost,
Path: "/mini/user/apply-join-company",
Path: "/mini/user/apply_join_company",
Handler: user.MiniUserApplyJoinCompanyHandler(serverCtx),
},
{
... ... @@ -164,7 +164,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{
{
Method: http.MethodPost,
Path: "/mini/user/switch-account",
Path: "/mini/user/switch_account",
Handler: user.MiniUserSwitchAccountHandler(serverCtx),
},
{
... ... @@ -179,7 +179,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/mini/user/audit-list",
Path: "/mini/user/audit_list",
Handler: user.MiniUserAuditListHandler(serverCtx),
},
{
... ... @@ -189,16 +189,21 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/mini/user/department-users",
Path: "/mini/user/department_users",
Handler: user.MiniUserDepartmentUsersHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/mini/user/user-list",
Path: "/mini/user/user_list",
Handler: user.MiniUsersListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/mini/user/news",
Handler: user.MiniUserNewsHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/mini/user/follower",
Handler: user.MiniUserFollowerHandler(serverCtx),
},
... ... @@ -209,6 +214,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/mini/user/following/latest_unread_list",
Handler: user.MiniUserFollowingLatestUnreadListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/mini/user/following/mark_read",
Handler: user.MiniUserFollowingMarkReadHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/mini/user/follow",
Handler: user.MiniUserFollowHandler(serverCtx),
},
... ... @@ -243,7 +258,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/system/user/user-list",
Path: "/system/user/user_list",
Handler: user.SystemUsersListHandler(serverCtx),
},
{
... ...
package user
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/user"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniUserFollowingLatestUnreadListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniUserFollowedSearchRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewMiniUserFollowingLatestUnreadListLogic(r.Context(), svcCtx)
resp, err := l.MiniUserFollowingLatestUnreadList(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
package user
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/user"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniUserFollowingMarkReadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniUserFollowingMarkReadRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewMiniUserFollowingMarkReadLogic(r.Context(), svcCtx)
resp, err := l.MiniUserFollowingMarkRead(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
package user
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/user"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniUserNewsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniUserNewsRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewMiniUserNewsLogic(r.Context(), svcCtx)
resp, err := l.MiniUserNews(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -6,6 +6,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -32,6 +33,7 @@ func (l *MiniUserFollowLogic) MiniUserFollow(req *types.FollowRequest) (err erro
conn = l.svcCtx.DefaultDBConn()
user *domain.User
targetUser *domain.User
userFollow *domain.UserFollow
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
... ... @@ -40,6 +42,9 @@ func (l *MiniUserFollowLogic) MiniUserFollow(req *types.FollowRequest) (err erro
if targetUser, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId); err != nil {
return xerr.NewErrMsgErr("关注的用户不存在", err)
}
if userFollow, err = l.svcCtx.UserFollowRepository.FindOneUserFollowing(l.ctx, conn, user.Id, targetUser.Id); err == nil && userFollow != nil {
return xerr.NewErrMsgErr("用户已关注", err)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
if err = user.Follow(targetUser); err != nil {
return err
... ... @@ -50,6 +55,14 @@ func (l *MiniUserFollowLogic) MiniUserFollow(req *types.FollowRequest) (err erro
if targetUser, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, targetUser); err != nil {
return err
}
userFollow = &domain.UserFollow{
FromUserId: user.Id,
ToUserId: targetUser.Id,
LastReadAt: time.Now().Unix(),
}
if userFollow, err = l.svcCtx.UserFollowRepository.Insert(ctx, conn, userFollow); err != nil {
return err
}
return nil
}, true); err != nil {
return xerr.NewErrMsgErr("关注用户失败", err)
... ...
package user
import (
"context"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/collection"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserFollowingLatestUnreadListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserFollowingLatestUnreadListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserFollowingLatestUnreadListLogic {
return &MiniUserFollowingLatestUnreadListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserFollowingLatestUnreadListLogic) MiniUserFollowingLatestUnreadList(req *types.MiniUserFollowedSearchRequest) (resp *types.MiniUserFollowedSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
user *domain.User
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
limit = 6
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
var (
users = user.Following
setUsers = collection.NewSet()
)
_, userArticlesUnread, _ := l.svcCtx.ArticleRepository.FindAuthorsLatestFirstUnreadArticle(l.ctx, conn, user.CompanyId, users, user.Id, limit)
_, userArticlesLatest, _ := l.svcCtx.ArticleRepository.FindAuthorsLatestFirstArticle(l.ctx, conn, user.CompanyId, users, user.Id, limit)
_, userFollows, _ := l.svcCtx.UserFollowRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().WithKV("fromUserId", user.Id).WithFindOnly())
resp = &types.MiniUserFollowedSearchResponse{
Total: int64(len(users)),
List: make([]*types.UserFollowItem, 0),
}
addUser := func(userId int64, readFlag bool) {
if setUsers.Contains(userId) {
return
}
if len(resp.List) == limit {
return
}
setUsers.Add(userId)
if foundUser, _ := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId); foundUser != nil {
resp.List = append(resp.List, &types.UserFollowItem{
Id: foundUser.Id,
Name: foundUser.Name,
Avatar: foundUser.Avatar,
Position: foundUser.Position,
ReadFlag: readFlag,
})
}
return
}
userFollowsMap := lo.KeyBy(userFollows, func(item *domain.UserFollow) int64 {
return item.ToUserId
})
lo.ForEach(userArticlesUnread, func(item *domain.Article, index int) {
var readFlag = true
if v, ok := userFollowsMap[item.AuthorId]; ok && v.LastReadAt < item.CreatedAt {
readFlag = false
}
addUser(item.AuthorId, readFlag)
})
lo.ForEach(userArticlesLatest, func(item *domain.Article, index int) {
addUser(item.AuthorId, true)
})
lo.ForEach(users, func(item int64, index int) {
addUser(item, true)
})
return
}
... ...
package user
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserFollowingMarkReadLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserFollowingMarkReadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserFollowingMarkReadLogic {
return &MiniUserFollowingMarkReadLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserFollowingMarkReadLogic) MiniUserFollowingMarkRead(req *types.MiniUserFollowingMarkReadRequest) (resp interface{}, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
user *domain.User
userFollow *domain.UserFollow
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId); err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
if userFollow, err = l.svcCtx.UserFollowRepository.FindOneUserFollowing(l.ctx, conn, userToken.UserId, user.Id); err != nil {
return nil, xerr.NewErrMsgErr("用户还未关注", err)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
userFollow.LastReadAt = time.Now().Unix()
if userFollow, err = l.svcCtx.UserFollowRepository.UpdateWithVersion(ctx, conn, userFollow); err != nil {
return err
}
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("关注用户标记已读失败", err)
}
resp = struct{}{}
return
}
... ...
package user
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserNewsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserNewsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserNewsLogic {
return &MiniUserNewsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserNewsLogic) MiniUserNews(req *types.MiniUserNewsRequest) (resp *types.MiniUserNewsResposne, err error) {
// todo: add your logic here and delete this line
return
}
... ...
... ... @@ -32,6 +32,7 @@ func (l *MiniUserUnFollowLogic) MiniUserUnFollow(req *types.FollowRequest) (err
conn = l.svcCtx.DefaultDBConn()
user *domain.User
targetUser *domain.User
userFollow *domain.UserFollow
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
... ... @@ -40,6 +41,9 @@ func (l *MiniUserUnFollowLogic) MiniUserUnFollow(req *types.FollowRequest) (err
if targetUser, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId); err != nil {
return xerr.NewErrMsgErr("关注的用户不存在", err)
}
if userFollow, err = l.svcCtx.UserFollowRepository.FindOneUserFollowing(l.ctx, conn, user.Id, targetUser.Id); err != nil {
return xerr.NewErrMsgErr("用户还未关注", err)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
if err = user.Unfollow(targetUser); err != nil {
return err
... ... @@ -50,6 +54,9 @@ func (l *MiniUserUnFollowLogic) MiniUserUnFollow(req *types.FollowRequest) (err
if targetUser, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, targetUser); err != nil {
return err
}
if userFollow, err = l.svcCtx.UserFollowRepository.Delete(ctx, conn, userFollow); err != nil {
return err
}
return nil
}, true); err != nil {
return xerr.NewErrMsgErr("关注用户失败", err)
... ...
... ... @@ -440,6 +440,26 @@ type MiniUsersListRequest struct {
RoleId int64 `json:"roleId,optional"` // 按角色角色关联的用户
}
type MiniUserNewsRequest struct {
AuthorId int64 `json:"authorId,optional"` // 特定作者ID
LastArticleId int64 `json:"lastArticleId,optional"` // 最后文章ID
Size int `json:"size"` // 数量
}
type MiniUserNewsResposne struct {
List []UserNewsItem `json:"list"`
Total int64 `json:"total"`
}
type UserNewsItem struct {
NewsId int64 `json:"newsId"` // 快讯ID
Type string `json:"type"` // 快讯类型 文章:Article 讨论:Discuss ...
Title string `json:"title"` // 标题
Summary string `json:"summary"` // 快讯概要
Time int64 `json:"time"` // 时间
ReadFlag bool `json:"readFlag"` // 已读标识 true:已读 false:未读
}
type MiniUserFollowedSearchRequest struct {
Page int `json:"page,optional"`
Size int `json:"size,optional"`
... ... @@ -450,6 +470,10 @@ type MiniUserFollowedSearchResponse struct {
Total int64 `json:"total"`
}
type MiniUserFollowingMarkReadRequest struct {
UserId int64 `json:"userId"`
}
type UserItem struct {
Id int64 `json:"id,omitempty"` // 用户ID
CompanyId int64 `json:"companyId,omitempty"` // 公司ID
... ... @@ -507,6 +531,7 @@ type UserFollowItem struct {
Position string `json:"position"` // 职位
Followed bool `json:"followed"` // 关注
MutualFollowed bool `json:"mutualFollowed"` // 互相关注标识
ReadFlag bool `json:"readFlag"` // 已读标识 true:已读 false:未读(小红点)
}
type MiniMyLikeRequest struct {
... ...
... ... @@ -9,12 +9,14 @@ import (
)
type UserFollow struct {
Id int64 // 唯一标识
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
Id int64 // 唯一标识
FromUserId int64
ToUserId int64
LastReadAt int64
CreatedAt int64
UpdatedAt int64
DeletedAt int64
Version int
}
func (m *UserFollow) TableName() string {
... ...
... ... @@ -157,6 +157,127 @@ func (repository *ArticleRepository) Find(ctx context.Context, conn transaction.
return total, dms, nil
}
// FindAuthorsLatestArticle 作者最新的文章
func (repository *ArticleRepository) FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn,
companyId int64, authors []int64, whoRead int64, lastId int64, limit int) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).
Where("id < ?", lastId).
Where("company_id=?", companyId).
Where("author_id in (?)", authors).
Where("target_user=0 or who_read @>'[?]'", whoRead).
Where("show = 1").
Order("id desc")
if limit > 0 {
tx.Limit(limit)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
// FindAuthorsLatestFirstArticle 作者最新的第一篇文章
func (repository *ArticleRepository) FindAuthorsLatestFirstArticle(ctx context.Context, conn transaction.Conn,
companyId int64, authors []int64, whoRead int64, limit int) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Select("max(id) id", "max(author_id) author_id", "max(created_at) created_at").
Where("company_id=?", companyId).
Where("author_id in (?)", authors).
Where("target_user=0 or who_read @>'[?]'", whoRead).
Where("show = 1").
Group("author_id").
Order("id desc")
if limit > 0 {
tx.Limit(limit)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
// FindAuthorsLatestFirstUnreadArticle 作者最新的第一篇未读文章
func (repository *ArticleRepository) FindAuthorsLatestFirstUnreadArticle(ctx context.Context, conn transaction.Conn,
companyId int64, authors []int64, whoRead int64, limit int) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Select("max(id) id", "max(author_id) author_id", "max(created_at) created_at").
Where("company_id=?", companyId).
Where("author_id in (?)", authors).
Where("target_user=0 or who_read @>'[?]'", whoRead).
Where("show = 1").
Where("id not in (select article_id from user_read_article where user_id = ?)", whoRead).
Group("author_id").
Order("id desc")
if limit > 0 {
tx.Limit(limit)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ArticleRepository) ModelToDomainModel(from *models.Article) (*domain.Article, error) {
to := &domain.Article{
Id: from.Id,
... ...
... ... @@ -111,6 +111,26 @@ func (repository *UserFollowRepository) FindOne(ctx context.Context, conn transa
return repository.ModelToDomainModel(m)
}
func (repository *UserFollowRepository) FindOneUserFollowing(ctx context.Context, conn transaction.Conn, userId int64, followingId int64) (*domain.UserFollow, error) {
var (
err error
tx = conn.DB()
m = new(models.UserFollow)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("from_user_id = ?", userId).Where("to_user_id = ?", followingId).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.UserFollow)
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserFollowRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.UserFollow, error) {
var (
tx = conn.DB()
... ... @@ -120,6 +140,12 @@ func (repository *UserFollowRepository) Find(ctx context.Context, conn transacti
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["fromUserId"]; ok {
tx.Where("from_user_id = ?", v)
}
if v, ok := queryOptions["toUserIds"]; ok {
tx.Where("to_user_id in (?)", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
... ...
... ... @@ -38,6 +38,8 @@ type ArticleRepository interface {
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Article, error)
Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*Article, error)
FindAuthorsLatestFirstArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error)
FindAuthorsLatestFirstUnreadArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error)
IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //点赞数量变动
IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //评论数量变动
IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //浏览数量变动
... ...
... ... @@ -7,8 +7,9 @@ import (
type UserFollow struct {
Id int64 `json:"id,omitempty"` // 唯一标识
FromUserId int64 `json:"fromUserId,omitempty"` /// 发起关注的人
ToUserId int64 `json:"toUserId,omitempty"` /// 被关注的人
FromUserId int64 `json:"fromUserId,omitempty"` // 发起关注的人
ToUserId int64 `json:"toUserId,omitempty"` // 被关注的人
LastReadAt int64 `json:"lastReadAt,omitempty"` // 上一次读取信息时间
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
... ... @@ -21,6 +22,7 @@ type UserFollowRepository interface {
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *UserFollow) (*UserFollow, error)
Delete(ctx context.Context, conn transaction.Conn, dm *UserFollow) (*UserFollow, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*UserFollow, error)
FindOneUserFollowing(ctx context.Context, conn transaction.Conn, userId int64, followingId int64) (*UserFollow, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*UserFollow, error)
}
... ...