完善文案的状态流转

This commit is contained in:
sjk
2025-12-20 01:05:46 +08:00
parent 6802624e59
commit 15b579d64a
13 changed files with 547 additions and 181 deletions

View File

@@ -43,12 +43,12 @@ func (s *EmployeeService) SendXHSCode(phone string) error {
// 执行命令
err := cmd.Run()
// 打印Python脚本的日志输出stderr
if stderr.Len() > 0 {
log.Printf("[Python日志-发送验证码] %s", stderr.String())
}
if err != nil {
return fmt.Errorf("执行Python脚本失败: %w, stderr: %s", err, stderr.String())
}
@@ -80,7 +80,7 @@ func (s *EmployeeService) GetProfile(employeeID int) (*models.User, error) {
if err != nil {
return nil, err
}
// 如果已绑定小红书且有Cookie,验证Cookie是否有效
if employee.IsBoundXHS == 1 && employee.XHSCookie != "" {
// 检查绑定时间刚绑定的30秒内不验证避免与绑定操作冲突
@@ -96,7 +96,7 @@ func (s *EmployeeService) GetProfile(employeeID int) (*models.User, error) {
log.Printf("GetProfile - 用户%d有Cookie长度: %d已跳过自动验证", employeeID, len(employee.XHSCookie))
// go s.VerifyCookieAndClear(employeeID)
}
return &employee, nil
}
@@ -164,7 +164,7 @@ func (s *EmployeeService) BindXHS(employeeID int, xhsPhone, code string) (string
} else {
log.Printf("绑定小红书 - 用户%d - 警告: cookiesData为nil", employeeID)
}
if cookiesJSON == "" {
log.Printf("绑定小红书 - 用户%d - 错误: 未能获取到Cookie数据", employeeID)
return "", errors.New("登录成功但未能获取到Cookie数据请重试")
@@ -195,7 +195,7 @@ func (s *EmployeeService) BindXHS(employeeID int, xhsPhone, code string) (string
log.Printf("绑定小红书 - 用户%d - 数据库更新失败: %v", employeeID, err)
return "", fmt.Errorf("更新员工绑定状态失败: %w", err)
}
log.Printf("绑定小红书 - 用户%d - 数据库更新成功", employeeID)
// 提交事务
@@ -203,7 +203,7 @@ func (s *EmployeeService) BindXHS(employeeID int, xhsPhone, code string) (string
log.Printf("绑定小红书 - 用户%d - 事务提交失败: %v", employeeID, err)
return "", fmt.Errorf("提交事务失败: %w", err)
}
log.Printf("绑定小红书 - 用户%d - 绑定成功 - 账号: %s", employeeID, xhsNickname)
return xhsNickname, nil
}
@@ -381,7 +381,7 @@ func (s *EmployeeService) VerifyCookieAndClear(employeeID int) error {
if employee.IsBoundXHS == 0 || employee.XHSCookie == "" {
return nil // 没有绑定或已无Cookie,直接返回
}
// 检查绑定时间刚绑定的30秒内不验证避免与绑定操作冲突
if employee.BoundAt != nil {
timeSinceBound := time.Since(*employee.BoundAt)
@@ -492,20 +492,37 @@ func (s *EmployeeService) clearXHSCookie(employeeID int) error {
return nil
}
// GetAvailableCopies 获取可领取的文案列表
// GetAvailableCopies 获取可领取的文案列表根据作者ID、产品ID和状态筛选
func (s *EmployeeService) GetAvailableCopies(employeeID int, productID int) (map[string]interface{}, error) {
// 获取当前用户信息查找对应的作者ID
var employee models.User
if err := database.DB.First(&employee, employeeID).Error; err != nil {
return nil, fmt.Errorf("获取用户信息失败: %w", err)
}
// 查找对应的作者记录
var author models.Author
if err := database.DB.Where("phone = ? AND enterprise_id = ?", employee.Phone, employee.EnterpriseID).First(&author).Error; err != nil {
return nil, fmt.Errorf("未找到对应的作者记录: %w", err)
}
// 获取产品信息
var product models.Product
if err := database.DB.First(&product, productID).Error; err != nil {
return nil, err
}
// 获取该产品下所有可用文案注意新数据库中status有更多状态
// 根据产品ID、作者ID和状态筛选文案
// status = 'assign_authors' 表示已分配作者的文案
var copies []models.Article
if err := database.DB.Where("product_id = ? AND status IN ?", productID, []string{"draft", "approved"}).Order("created_at DESC").Find(&copies).Error; err != nil {
return nil, err
query := database.DB.Where("product_id = ? AND author_id = ? AND status = ?", productID, author.ID, "assign_authors")
if err := query.Order("created_at DESC").Find(&copies).Error; err != nil {
return nil, fmt.Errorf("查询文案列表失败: %w", err)
}
log.Printf("[获取文案列表] 用户ID=%d, 作者ID=%d, 产品ID=%d, 筛选到 %d 条文案", employeeID, author.ID, productID, len(copies))
return map[string]interface{}{
"product": map[string]interface{}{
"id": product.ID,
@@ -513,14 +530,106 @@ func (s *EmployeeService) GetAvailableCopies(employeeID int, productID int) (map
"image": product.ImageURL,
},
"copies": copies,
"author": map[string]interface{}{
"id": author.ID,
"name": author.AuthorName,
},
}, nil
}
// UpdateArticleStatus 更新文案状态(通过/拒绝)
func (s *EmployeeService) UpdateArticleStatus(employeeID int, articleID int, status string) error {
// 获取当前用户信息
var employee models.User
if err := database.DB.First(&employee, employeeID).Error; err != nil {
return fmt.Errorf("获取用户信息失败: %w", err)
}
// 查找对应的作者记录
var author models.Author
if err := database.DB.Where("phone = ? AND enterprise_id = ?", employee.Phone, employee.EnterpriseID).First(&author).Error; err != nil {
return fmt.Errorf("未找到对应的作者记录: %w", err)
}
// 获取文案信息
var article models.Article
if err := database.DB.First(&article, articleID).Error; err != nil {
return fmt.Errorf("获取文案信息失败: %w", err)
}
// 验证文案是否属于当前作者且状态为assign_authors
if article.AuthorID == nil || *article.AuthorID != author.ID {
return fmt.Errorf("无权操作此文案")
}
if article.Status != "assign_authors" {
return fmt.Errorf("文案当前状态为%s无法操作", article.Status)
}
// 开启事务
tx := database.DB.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
now := time.Now()
// 更新文案状态
err := tx.Model(&article).Updates(map[string]interface{}{
"status": status,
"review_user_id": employeeID,
}).Error
if err != nil {
tx.Rollback()
return fmt.Errorf("更新状态失败: %w", err)
}
// 创建操作记录到 ai_article_published_records
actionType := "通过"
if status == "rejected" {
actionType = "拒绝"
}
record := models.PublishRecord{
ArticleID: &article.ID,
EnterpriseID: employee.EnterpriseID,
ProductID: article.ProductID,
Topic: article.Topic,
Title: article.Title,
CreatedUserID: article.CreatedUserID,
ReviewUserID: &employeeID,
Status: status,
PublishTime: &now,
WordCount: article.WordCount,
ImageCount: article.ImageCount,
Channel: article.Channel,
ReviewComment: fmt.Sprintf("作者%s于%s", actionType, now.Format("2006-01-02 15:04:05")),
}
if err := tx.Create(&record).Error; err != nil {
tx.Rollback()
return fmt.Errorf("创建操作记录失败: %w", err)
}
// 提交事务
if err := tx.Commit().Error; err != nil {
return fmt.Errorf("提交事务失败: %w", err)
}
log.Printf("[更新文案状态] 用户ID=%d, 作者ID=%d, 文案ID=%d, 状态: assign_authors => %s, 记录ID=%d", employeeID, author.ID, articleID, status, record.ID)
return nil
}
// ClaimCopy 领取文案(新版本:直接返回文案信息,不再创建领取记录)
func (s *EmployeeService) ClaimCopy(employeeID int, copyID int, productID int) (map[string]interface{}, error) {
// 检查文案是否存在且可用注意新数据库中status有更多状态
// assign_authors: 已分配给作者,可以直接发布
// draft: 草稿状态
// approved: 已审核通过
var copy models.Article
if err := database.DB.Where("id = ? AND status IN ?", copyID, []string{"draft", "approved"}).First(&copy).Error; err != nil {
if err := database.DB.Where("id = ? AND status IN ?", copyID, []string{"draft", "approved", "assign_authors"}).First(&copy).Error; err != nil {
return nil, errors.New("文案不存在或不可用")
}
@@ -542,7 +651,7 @@ func (s *EmployeeService) ClaimCopy(employeeID int, copyID int, productID int) (
func (s *EmployeeService) ClaimRandomCopy(employeeID int, productID int) (map[string]interface{}, error) {
// 查询未领取的可用文案注意新数据库中status有更多状态
var copy models.Article
query := database.DB.Where("product_id = ? AND status IN ?", productID, []string{"draft", "approved"})
query := database.DB.Where("product_id = ? AND status IN ?", productID, []string{"draft", "approved", "assign_authors"})
if err := query.Order("RAND()").First(&copy).Error; err != nil {
return nil, errors.New("暂无可领取的文案")