plugai_updsrv/pkg/dal/dao/news.go

324 lines
8.6 KiB
Go
Raw 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 dao
import (
"fmt"
"intent-system/pkg/dal/models"
"github.com/civet148/log"
"github.com/civet148/sqlca/v2"
)
type NewsAsyncCondition struct {
Org_Id int64
Digest string
}
type NewsPullNewCondition struct {
OrgIDs []int64 `json:"org_ids"`
}
const (
NewsState_NotPublish = 0 //未发布
NewsState_Published = 1 //已发布到订阅列表
NewsState_Pushed = 2 //已推送订阅
)
type NewsCondition struct {
PageNo int
PageSize int
Id int64
Tag string
All bool
IsDeleted bool
ContainExtra bool
Asc bool
Search string
Language string
}
type NewsDAO struct {
db *sqlca.Engine
}
func NewNewsDAO(db *sqlca.Engine) *NewsDAO {
return &NewsDAO{
db: db,
}
}
// insert into table by data model
func (dao *NewsDAO) Insert(do *models.NewsDO) (lastInsertId int64, err error) {
return dao.db.Model(&do).Table(models.TableNameNews).Insert()
}
// insert if not exist or update columns on duplicate key...
func (dao *NewsDAO) Upsert(do *models.NewsDO, columns ...string) (lastInsertId int64, err error) {
return dao.db.Model(&do).Table(models.TableNameNews).Select(columns...).Upsert()
}
// update table set columns where id=xxx
func (dao *NewsDAO) Update(do *models.NewsDO, columns ...string) (rows int64, err error) {
return dao.db.Model(&do).Table(models.TableNameNews).Select(columns...).Update()
}
func (dao *NewsDAO) UpdateByOrgId(do *models.NewsDO, columns ...string) (rows int64, err error) {
return dao.db.Model(&do).Table(models.TableNameNews).Eq(models.NEWS_COLUMN_ORG_ID, do.OrgId).Select(columns...).Update()
}
// query records by id
func (dao *NewsDAO) QueryById(id int64, columns ...string) (do *models.NewsDO, err error) {
if _, err = dao.db.Model(&do).Table(models.TableNameNews).Id(id).Select(columns...).Query(); err != nil {
return nil, log.Errorf(err.Error())
}
return
}
// query records by url
func (dao *NewsDAO) QueryByUrl(url string, columns ...string) (do *models.NewsDO, err error) {
if _, err = dao.db.Model(&do).
Table(models.TableNameNews).
Where("url = ?", url).
Select(columns...).
Query(); err != nil {
return nil, log.Errorf(err.Error())
}
return
}
func (dao *NewsDAO) QueryAllByUrl(fullPath string) ([]*models.NewsDO, error) {
var list []*models.NewsDO
_, err := dao.db.Model(&list).
Table(models.TableNameNews).
Where("url = ?", fullPath). // ✅ 参数绑定,不拼接
Query()
return list, err
}
func (dao *NewsDAO) QueryOriginalNews(orgId int64, lang string, columns ...string) (do *models.NewsDO, err error) {
if _, err = dao.db.Model(&do).
Table(models.TableNameNews).
Eq(models.NEWS_COLUMN_ORG_ID, orgId).
Eq(models.NEWS_COLUMN_LANGUAGE, lang).
Eq(models.NEWS_COLUMN_IS_REPLICATE, 0).
Eq(models.NEWS_COLUMN_IS_OVERWRITTEN, 1).
Select(columns...).
Query(); err != nil {
return nil, log.Errorf(err.Error())
}
return
}
// query records by conditions
func (dao *NewsDAO) QueryByCondition(conditions map[string]interface{}, columns ...string) (dos []*models.NewsDO, err error) {
if len(conditions) == 0 {
return nil, fmt.Errorf("condition must not be empty")
}
e := dao.db.Model(&dos).Table(models.TableNameNews).Select(columns...)
for k, v := range conditions {
e.Eq(k, v)
}
if _, err = e.Query(); err != nil {
return nil, err
}
return
}
// query max news id
func (dao *NewsDAO) QueryMaxSyncId(lang models.LanguageType) (lastId int64, err error) {
if _, err = dao.db.Model(&lastId).
Table(models.TableNameNews).
Max(models.NEWS_COLUMN_ORG_ID).
Eq(models.NEWS_COLUMN_LANGUAGE, lang).
Query(); err != nil {
return 0, err
}
return
}
func (dao *NewsDAO) QueryNotPushed(pageNo, pageSize int) (dos []*models.NewsDO, total int64, err error) {
e := dao.db.Model(&dos).
Table(models.TableNameNews).
Eq(models.NEWS_COLUMN_IS_DELETED, 0).
Eq(models.NEWS_COLUMN_STATE, NewsState_NotPublish).
Page(pageNo, pageSize).
Desc(models.QUESTION_ANSWER_COLUMN_UPDATED_TIME)
_, total, err = e.QueryEx()
if err != nil {
return nil, 0, log.Errorf(err.Error())
}
return
}
func (dao *NewsDAO) QueryList(cond *NewsCondition) (dos []*models.NewsDO, total int64, err error) {
e := dao.db.Model(&dos).
Table(models.TableNameNews).
Select(
models.NEWS_COLUMN_ID,
models.NEWS_COLUMN_ORG_ID,
models.NEWS_COLUMN_SPIDER_ID,
models.NEWS_COLUMN_PNAME_ID,
models.NEWS_COLUMN_TAG,
models.NEWS_COLUMN_CATEGORY,
models.NEWS_COLUMN_MAIN_TITLE,
models.NEWS_COLUMN_SUB_TITLE,
models.NEWS_COLUMN_SUMMARY,
models.NEWS_COLUMN_KEYWORDS,
models.NEWS_COLUMN_SEO_KEYWORDS,
models.NEWS_COLUMN_TAGS,
models.NEWS_COLUMN_URL,
models.NEWS_COLUMN_IMAGE_URL,
models.NEWS_COLUMN_LOGO_URL,
models.NEWS_COLUMN_MODEL_PARAMETER,
models.NEWS_COLUMN_CONTENT,
models.NEWS_COLUMN_IS_HOTSPOT,
models.NEWS_COLUMN_IS_OVERWRITTEN,
models.NEWS_COLUMN_IS_DELETED,
models.NEWS_COLUMN_IS_REPLICATE,
models.NEWS_COLUMN_STATE,
models.NEWS_COLUMN_CREATED_TIME,
models.NEWS_COLUMN_UPDATED_TIME,
).
Page(cond.PageNo, cond.PageSize)
if cond.Id != 0 {
e.Eq(models.NEWS_COLUMN_ID, cond.Id)
}
if cond.Tag != "" {
e.JsonContainArray(models.NEWS_COLUMN_TAGS, cond.Tag)
}
if !cond.All {
e.Eq(models.NEWS_COLUMN_IS_OVERWRITTEN, 0)
}
if cond.ContainExtra {
e.Select(models.NEWS_COLUMN_EXTRA_DATA)
}
if cond.IsDeleted {
e.Eq(models.NEWS_COLUMN_IS_DELETED, 1)
} else {
e.Eq(models.NEWS_COLUMN_IS_DELETED, 0)
}
if cond.Language != "" {
e.Eq(models.NEWS_COLUMN_LANGUAGE, cond.Language)
}
if cond.Search != "" {
e.Like(models.NEWS_COLUMN_MAIN_TITLE, cond.Search)
}
if cond.Asc {
e.Asc(models.NEWS_COLUMN_UPDATED_TIME, models.NEWS_COLUMN_ID)
} else {
e.Desc(models.NEWS_COLUMN_UPDATED_TIME, models.NEWS_COLUMN_ID)
}
_, total, err = e.QueryEx()
if err != nil {
return nil, 0, log.Errorf(err.Error())
}
return
}
func (dao *NewsDAO) QueryAsync(cond *NewsAsyncCondition) (dos []*models.NewsDO, total int64, err error) {
e := dao.db.Model(&dos).
Table(models.TableNameNews).
Select(
models.NEWS_COLUMN_ID,
models.NEWS_COLUMN_ORG_ID,
models.NEWS_COLUMN_SPIDER_ID,
models.NEWS_COLUMN_PNAME_ID,
models.NEWS_COLUMN_TAG,
models.NEWS_COLUMN_CATEGORY,
models.NEWS_COLUMN_MAIN_TITLE,
models.NEWS_COLUMN_SUB_TITLE,
models.NEWS_COLUMN_SUMMARY,
models.NEWS_COLUMN_KEYWORDS,
models.NEWS_COLUMN_SEO_KEYWORDS,
models.NEWS_COLUMN_TAGS,
models.NEWS_COLUMN_URL,
models.NEWS_COLUMN_IMAGE_URL,
models.NEWS_COLUMN_LOGO_URL,
models.NEWS_COLUMN_MODEL_PARAMETER,
models.NEWS_COLUMN_CONTENT,
models.NEWS_COLUMN_IS_HOTSPOT,
models.NEWS_COLUMN_IS_OVERWRITTEN,
models.NEWS_COLUMN_IS_DELETED,
models.NEWS_COLUMN_IS_REPLICATE,
models.NEWS_COLUMN_STATE,
models.NEWS_COLUMN_CREATED_TIME,
models.NEWS_COLUMN_UPDATED_TIME,
models.NEWS_COLUMN_EXTRA_DATA,
)
if cond.Org_Id != 0 {
e.Eq(models.NEWS_COLUMN_ORG_ID, cond.Org_Id)
}
_, total, err = e.QueryEx()
if err != nil {
return nil, 0, log.Errorf(err.Error())
}
// == digest过滤逻辑 ==
if cond.Digest != "" {
filtered := make([]*models.NewsDO, 0, len(dos))
for _, v := range dos {
digestVal, _ := v.ExtraData["digest"]
if ds, ok := digestVal.(string); ok {
if ds != cond.Digest {
filtered = append(filtered, v)
}
// 相等就不加
} else {
// 取不到digest字段保留
filtered = append(filtered, v)
}
}
dos = filtered
total = int64(len(filtered))
}
log.Infof(".................[DEBUG] QueryAsync 返回数据: %+v", dos)
return
}
func (dao *NewsDAO) QueryPullNew(cond *NewsPullNewCondition) (dos []*models.NewsDO, total int64, err error) {
e := dao.db.Model(&dos).
Table(models.TableNameNews).
Select(
models.NEWS_COLUMN_ID,
models.NEWS_COLUMN_ORG_ID,
models.NEWS_COLUMN_SPIDER_ID,
models.NEWS_COLUMN_PNAME_ID,
models.NEWS_COLUMN_TAG,
models.NEWS_COLUMN_CATEGORY,
models.NEWS_COLUMN_MAIN_TITLE,
models.NEWS_COLUMN_SUB_TITLE,
models.NEWS_COLUMN_SUMMARY,
models.NEWS_COLUMN_KEYWORDS,
models.NEWS_COLUMN_SEO_KEYWORDS,
models.NEWS_COLUMN_TAGS,
models.NEWS_COLUMN_URL,
models.NEWS_COLUMN_IMAGE_URL,
models.NEWS_COLUMN_LOGO_URL,
models.NEWS_COLUMN_MODEL_PARAMETER,
models.NEWS_COLUMN_CONTENT,
models.NEWS_COLUMN_IS_HOTSPOT,
models.NEWS_COLUMN_IS_OVERWRITTEN,
models.NEWS_COLUMN_IS_DELETED,
models.NEWS_COLUMN_IS_REPLICATE,
models.NEWS_COLUMN_STATE,
models.NEWS_COLUMN_CREATED_TIME,
models.NEWS_COLUMN_UPDATED_TIME,
models.NEWS_COLUMN_EXTRA_DATA,
)
// 只按 org_ids 过滤
if len(cond.OrgIDs) > 0 {
e.NotIn(models.NEWS_COLUMN_ORG_ID, cond.OrgIDs)
}
_, total, err = e.QueryEx()
if err != nil {
return nil, 0, log.Errorf(err.Error())
}
log.Infof(".................[DEBUG] QueryPullNew 返回数据: %+v", dos)
return
}