2023-01-21 13:13:33 +00:00
|
|
|
package dao
|
2023-01-12 12:42:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-05-04 12:36:17 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-01-21 11:31:23 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/model"
|
2023-01-12 12:42:16 +00:00
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-02-06 09:58:24 +00:00
|
|
|
func GetPostsByIds(a ...any) (m map[uint64]models.Posts, err error) {
|
|
|
|
ctx := a[0].(context.Context)
|
2023-01-12 12:42:16 +00:00
|
|
|
m = make(map[uint64]models.Posts)
|
2023-02-06 09:58:24 +00:00
|
|
|
ids := a[1].([]uint64)
|
|
|
|
rawPosts, err := model.Finds[models.Posts](ctx, model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{{"Id", "in", ""}}),
|
|
|
|
model.Join(model.SqlBuilder{
|
|
|
|
{"a", "left join", "wp_term_relationships b", "a.Id=b.object_id"},
|
|
|
|
{"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id"},
|
|
|
|
{"left join", "wp_terms d", "c.term_id=d.term_id"},
|
|
|
|
}),
|
2023-04-15 16:57:50 +00:00
|
|
|
model.Fields("a.*,ifnull(d.name,'') category_name,ifnull(c.term_id,0) terms_id,ifnull(taxonomy,'') `taxonomy`"),
|
2023-02-06 09:58:24 +00:00
|
|
|
model.In(slice.ToAnySlice(ids)),
|
|
|
|
))
|
|
|
|
|
2023-01-12 12:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
postsMap := make(map[uint64]models.Posts)
|
|
|
|
for i, post := range rawPosts {
|
|
|
|
v, ok := postsMap[post.Id]
|
|
|
|
if !ok {
|
|
|
|
v = rawPosts[i]
|
|
|
|
}
|
|
|
|
if post.Taxonomy == "category" {
|
|
|
|
v.Categories = append(v.Categories, post.CategoryName)
|
|
|
|
} else if post.Taxonomy == "post_tag" {
|
|
|
|
v.Tags = append(v.Tags, post.CategoryName)
|
|
|
|
}
|
2023-04-15 16:57:50 +00:00
|
|
|
if post.TermsId > 0 {
|
|
|
|
v.TermIds = append(v.TermIds, post.TermsId)
|
|
|
|
}
|
2023-01-12 12:42:16 +00:00
|
|
|
postsMap[post.Id] = v
|
|
|
|
}
|
2023-01-30 12:16:03 +00:00
|
|
|
//host, _ := wpconfig.Options.Load("siteurl")
|
|
|
|
host := ""
|
2023-02-06 09:58:24 +00:00
|
|
|
meta, _ := GetPostMetaByPostIds(ctx, ids)
|
2023-01-12 12:42:16 +00:00
|
|
|
for k, pp := range postsMap {
|
|
|
|
if len(pp.Categories) > 0 {
|
|
|
|
t := make([]string, 0, len(pp.Categories))
|
|
|
|
for _, cat := range pp.Categories {
|
|
|
|
t = append(t, fmt.Sprintf(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
|
|
|
|
}
|
|
|
|
pp.CategoriesHtml = strings.Join(t, "、")
|
2023-01-17 15:18:31 +00:00
|
|
|
}
|
|
|
|
mm, ok := meta[pp.Id]
|
|
|
|
if ok {
|
|
|
|
attMeta, ok := mm["_wp_attachment_metadata"]
|
2023-01-12 12:42:16 +00:00
|
|
|
if ok {
|
2023-01-17 15:18:31 +00:00
|
|
|
att, ok := attMeta.(models.WpAttachmentMetadata)
|
|
|
|
if ok {
|
|
|
|
pp.AttachmentMetadata = att
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pp.PostType != "attachment" {
|
2023-01-14 13:12:26 +00:00
|
|
|
thumb := ToPostThumb(ctx, mm, host)
|
2023-01-12 12:42:16 +00:00
|
|
|
if thumb.Path != "" {
|
|
|
|
pp.Thumbnail = thumb
|
|
|
|
}
|
2023-01-17 15:18:31 +00:00
|
|
|
} else if pp.PostType == "attachment" && pp.AttachmentMetadata.File != "" {
|
2023-02-14 11:47:47 +00:00
|
|
|
thumb := wpconfig.Thumbnail(pp.AttachmentMetadata, "thumbnail", host, "thumbnail", "post-thumbnail")
|
2023-01-17 15:18:31 +00:00
|
|
|
if thumb.Path != "" {
|
|
|
|
pp.Thumbnail = thumb
|
|
|
|
}
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
2023-01-17 15:18:31 +00:00
|
|
|
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
if len(pp.Tags) > 0 {
|
|
|
|
t := make([]string, 0, len(pp.Tags))
|
|
|
|
for _, cat := range pp.Tags {
|
|
|
|
t = append(t, fmt.Sprintf(`<a href="/p/tag/%s" rel="tag">%s</a>`, cat, cat))
|
|
|
|
}
|
|
|
|
pp.TagsHtml = strings.Join(t, "、")
|
|
|
|
}
|
|
|
|
m[k] = pp
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchPostIds(args ...any) (ids PostIds, err error) {
|
|
|
|
ctx := args[0].(context.Context)
|
2023-02-25 15:10:42 +00:00
|
|
|
q := args[1].(model.QueryCondition)
|
2023-02-27 13:34:48 +00:00
|
|
|
page := args[2].(int)
|
|
|
|
pageSize := args[3].(int)
|
2023-02-25 15:10:42 +00:00
|
|
|
q.Fields = "ID"
|
2023-02-27 13:34:48 +00:00
|
|
|
res, total, err := model.Pagination[models.Posts](ctx, q, page, pageSize)
|
2023-01-12 12:42:16 +00:00
|
|
|
for _, posts := range res {
|
|
|
|
ids.Ids = append(ids.Ids, posts.Id)
|
|
|
|
}
|
|
|
|
ids.Length = total
|
|
|
|
totalR := int(atomic.LoadInt64(&TotalRaw))
|
|
|
|
if total > totalR {
|
|
|
|
tt := int64(total)
|
|
|
|
atomic.StoreInt64(&TotalRaw, tt)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
func GetMaxPostId(a ...any) (uint64, error) {
|
2023-01-12 12:42:16 +00:00
|
|
|
ctx := a[0].(context.Context)
|
2023-02-06 12:50:25 +00:00
|
|
|
r, err := model.SimpleFind[models.Posts](ctx,
|
|
|
|
model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}},
|
|
|
|
"max(ID) ID",
|
|
|
|
)
|
2023-01-12 12:42:16 +00:00
|
|
|
var id uint64
|
|
|
|
if len(r) > 0 {
|
|
|
|
id = r[0].Id
|
|
|
|
}
|
2023-01-20 10:10:13 +00:00
|
|
|
return id, err
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RecentPosts(a ...any) (r []models.Posts, err error) {
|
|
|
|
ctx := a[0].(context.Context)
|
2023-02-06 12:50:25 +00:00
|
|
|
num := a[1].(int)
|
|
|
|
r, err = model.Finds[models.Posts](ctx, model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{
|
|
|
|
{"post_type", "post"},
|
|
|
|
{"post_status", "publish"},
|
|
|
|
}),
|
2023-03-06 15:43:58 +00:00
|
|
|
model.Fields("ID,post_title,post_password,post_date_gmt"),
|
|
|
|
model.Order([][]string{{"post_date", "desc"}}),
|
2023-02-06 12:50:25 +00:00
|
|
|
model.Limit(num),
|
|
|
|
))
|
2023-01-12 12:42:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPostContext(arg ...any) (r PostContext, err error) {
|
|
|
|
ctx := arg[0].(context.Context)
|
|
|
|
t := arg[1].(time.Time)
|
|
|
|
next, err := model.FirstOne[models.Posts](ctx, model.SqlBuilder{
|
|
|
|
{"post_date", ">", t.Format("2006-01-02 15:04:05")},
|
|
|
|
{"post_status", "in", ""},
|
|
|
|
{"post_type", "post"},
|
|
|
|
}, "ID,post_title,post_password", nil, []any{"publish"})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prev, err := model.FirstOne[models.Posts](ctx, model.SqlBuilder{
|
|
|
|
{"post_date", "<", t.Format("2006-01-02 15:04:05")},
|
|
|
|
{"post_status", "in", ""},
|
|
|
|
{"post_type", "post"},
|
|
|
|
}, "ID,post_title", model.SqlBuilder{{"post_date", "desc"}}, []any{"publish"})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = PostContext{
|
|
|
|
Prev: prev,
|
|
|
|
Next: next,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func MonthPost(args ...any) (r []uint64, err error) {
|
|
|
|
ctx := args[0].(context.Context)
|
|
|
|
year, month := args[1].(string), args[2].(string)
|
|
|
|
where := model.SqlBuilder{
|
2023-02-06 10:51:58 +00:00
|
|
|
{"post_type", "post"},
|
|
|
|
{"post_status", "publish"},
|
2023-01-12 12:42:16 +00:00
|
|
|
{"year(post_date)", year},
|
|
|
|
{"month(post_date)", month},
|
|
|
|
}
|
2023-03-19 12:53:27 +00:00
|
|
|
r, err = model.Column[models.Posts, uint64](ctx, func(v models.Posts) (uint64, bool) {
|
2023-02-06 10:51:58 +00:00
|
|
|
return v.Id, true
|
|
|
|
}, model.Conditions(
|
2023-02-06 11:07:55 +00:00
|
|
|
model.Fields("ID"),
|
2023-02-06 10:51:58 +00:00
|
|
|
model.Where(where),
|
|
|
|
))
|
2023-03-19 12:53:27 +00:00
|
|
|
l := int64(len(r))
|
|
|
|
if l > atomic.LoadInt64(&TotalRaw) {
|
|
|
|
atomic.StoreInt64(&TotalRaw, l)
|
|
|
|
}
|
|
|
|
return
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|