245 lines
5.7 KiB
Go
245 lines
5.7 KiB
Go
package impl
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"geo/internal/data/model"
|
|
"geo/tmpl/dataTemp"
|
|
"geo/utils"
|
|
"time"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
type PublishImpl struct {
|
|
dataTemp.DataTemp
|
|
db *utils.Db
|
|
}
|
|
|
|
func NewPublishImpl(db *utils.Db) *PublishImpl {
|
|
return &PublishImpl{
|
|
DataTemp: *dataTemp.NewDataTemp(db, new(model.Publish)),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (m *PublishImpl) PrimaryKey() string {
|
|
return "id"
|
|
}
|
|
|
|
func (m *PublishImpl) GetTemp() *dataTemp.DataTemp {
|
|
return &m.DataTemp
|
|
}
|
|
|
|
// BatchInsert 批量插入发布记录
|
|
func (p *PublishImpl) BatchInsert(ctx context.Context, tokenID int, records []map[string]interface{}) (int64, error) {
|
|
if len(records) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
sql := `INSERT INTO publish
|
|
(token_id, user_index, request_id, title, tag, type, plat_index, url, publish_time, status, create_time, img)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
|
|
var total int64
|
|
for _, record := range records {
|
|
result := p.db.Client.WithContext(ctx).Exec(sql,
|
|
tokenID,
|
|
record["user_index"],
|
|
record["request_id"],
|
|
record["title"],
|
|
record["tag"],
|
|
record["type"],
|
|
record["plat_index"],
|
|
record["url"],
|
|
record["publish_time"],
|
|
1, // status = 1 待发布
|
|
time.Now(),
|
|
record["img"],
|
|
)
|
|
if result.Error != nil {
|
|
return total, result.Error
|
|
}
|
|
total += result.RowsAffected
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetOneWithPlat 查询单条发布记录并关联平台信息
|
|
func (p *PublishImpl) GetOneWithPlat(ctx context.Context, cond *builder.Cond) (map[string]interface{}, error) {
|
|
query, err := builder.ToBoundSQL(*cond)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// 构建带平台信息的查询
|
|
sql := fmt.Sprintf(`
|
|
SELECT
|
|
p.id,
|
|
p.token_id,
|
|
p.user_index,
|
|
p.request_id,
|
|
p.title,
|
|
p.tag,
|
|
p.type,
|
|
p.plat_index,
|
|
p.url,
|
|
p.publish_time,
|
|
p.status,
|
|
p.create_time,
|
|
p.img,
|
|
p.msg,
|
|
pl.index as plat_index_db,
|
|
pl.name as plat_name,
|
|
pl.login_url,
|
|
pl.edit_url,
|
|
pl.logined_url,
|
|
pl.desc as plat_desc,
|
|
pl.img_url as plat_img_url
|
|
FROM publish p
|
|
INNER JOIN plat pl ON p.plat_index = pl.index AND pl.status = 1
|
|
WHERE %s
|
|
LIMIT 1
|
|
`, query)
|
|
|
|
var result map[string]interface{}
|
|
err = p.db.Client.WithContext(ctx).Raw(sql).Scan(&result).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result == nil || len(result) == 0 {
|
|
return nil, nil
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// GetOneWithPlatByRequestID 根据request_id查询发布记录并关联平台信息
|
|
func (p *PublishImpl) GetOneWithPlatByRequestID(ctx context.Context, requestID string) (map[string]interface{}, error) {
|
|
sql := `
|
|
SELECT
|
|
p.id,
|
|
p.token_id,
|
|
p.user_index,
|
|
p.request_id,
|
|
p.title,
|
|
p.tag,
|
|
p.type,
|
|
p.plat_index,
|
|
p.url,
|
|
p.publish_time,
|
|
p.status,
|
|
p.create_time,
|
|
p.img,
|
|
p.msg,
|
|
pl.index as plat_index_db,
|
|
pl.name as plat_name,
|
|
pl.login_url,
|
|
pl.edit_url,
|
|
pl.logined_url,
|
|
pl.desc as plat_desc,
|
|
pl.img_url as plat_img_url
|
|
FROM publish p
|
|
INNER JOIN plat pl ON p.plat_index = pl.index AND pl.status = 1
|
|
WHERE p.request_id = ?
|
|
LIMIT 1
|
|
`
|
|
|
|
var result map[string]interface{}
|
|
err := p.db.Client.WithContext(ctx).Raw(sql, requestID).Scan(&result).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result == nil || len(result) == 0 {
|
|
return nil, nil
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// UpdateStatus 更新发布状态
|
|
func (p *PublishImpl) UpdateStatus(ctx context.Context, requestID string, status int, msg string) error {
|
|
updateData := map[string]interface{}{
|
|
"status": status,
|
|
}
|
|
if msg != "" {
|
|
updateData["msg"] = msg
|
|
}
|
|
return p.db.Client.WithContext(ctx).
|
|
Model(&model.Publish{}).
|
|
Where("request_id = ?", requestID).
|
|
Updates(updateData).Error
|
|
}
|
|
|
|
// GetListWithUser 获取发布列表并关联用户信息
|
|
func (p *PublishImpl) GetListWithUser(ctx context.Context, tokenID int32, page, pageSize int, filters map[string]interface{}) ([]map[string]interface{}, int64, error) {
|
|
// 构建基础查询
|
|
query := p.db.Client.WithContext(ctx).
|
|
Table("publish p").
|
|
Select(`
|
|
p.id,
|
|
p.user_index,
|
|
u.name as user_name,
|
|
p.request_id,
|
|
p.title,
|
|
p.tag,
|
|
p.type,
|
|
CASE
|
|
WHEN p.type = 1 THEN '文章'
|
|
WHEN p.type = 2 THEN '视频'
|
|
ELSE '未知'
|
|
END as type_name,
|
|
p.plat_index,
|
|
pl.name as plat_name,
|
|
p.url,
|
|
p.publish_time,
|
|
p.status,
|
|
CASE
|
|
WHEN p.status = 1 THEN '待发布'
|
|
WHEN p.status = 2 THEN '发布中'
|
|
WHEN p.status = 3 THEN '发布失败'
|
|
WHEN p.status = 4 THEN '发布成功'
|
|
ELSE '未知'
|
|
END as status_name,
|
|
p.create_time,
|
|
p.msg
|
|
`).
|
|
Joins("LEFT JOIN user u ON p.user_index = u.user_index").
|
|
Joins("LEFT JOIN plat pl ON p.plat_index = pl.index").
|
|
Where("u.token_id = ?", tokenID)
|
|
|
|
// 添加过滤条件
|
|
if userIndex, ok := filters["user_index"]; ok && userIndex != "" {
|
|
query = query.Where("p.user_index = ?", userIndex)
|
|
}
|
|
if tag, ok := filters["tag"]; ok && tag != "" {
|
|
query = query.Where("p.tag LIKE ?", "%"+tag.(string)+"%")
|
|
}
|
|
if typeFilter, ok := filters["type"]; ok && typeFilter != 0 {
|
|
query = query.Where("p.type = ?", typeFilter)
|
|
}
|
|
if platIndex, ok := filters["plat_index"]; ok && platIndex != "" {
|
|
query = query.Where("p.plat_index = ?", platIndex)
|
|
}
|
|
if status, ok := filters["status"]; ok && status != 0 {
|
|
query = query.Where("p.status = ?", status)
|
|
}
|
|
if requestID, ok := filters["request_id"]; ok && requestID != "" {
|
|
query = query.Where("p.request_id LIKE ?", "%"+requestID.(string)+"%")
|
|
}
|
|
|
|
// 获取总数
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// 分页查询
|
|
var results []map[string]interface{}
|
|
offset := (page - 1) * pageSize
|
|
err := query.
|
|
Order("p.publish_time DESC, p.create_time DESC").
|
|
Limit(pageSize).
|
|
Offset(offset).
|
|
Scan(&results).Error
|
|
|
|
return results, total, err
|
|
}
|