package semantic_analysis import ( "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "strings" "time" ) type SaDoc struct { SaDocId int `gorm:"primaryKey;column:sa_doc_id;type:int(10) unsigned;not null"` // 语义分析-文档Id ClassifyId int `gorm:"index:idx_classify_id;column:classify_id;type:int(10) unsigned;not null;default:0"` // 文档分类Id ClassifyName string `gorm:"column:classify_name;type:varchar(128);not null;default:''"` // 文档分类名称 Title string `gorm:"column:title;type:varchar(255);not null;default:''"` // 文档标题 Theme string `gorm:"column:theme;type:varchar(128);not null;default:''"` // 主题 CoverImg string `gorm:"column:cover_img;type:varchar(255);not null;default:''"` // 封面图 ContentMd5 string `gorm:"column:content_md5;type:char(32);not null;default:''"` // 内容md5后的字符串,用于内容去重 SysAdminId int `gorm:"column:sys_admin_id;type:int(10) unsigned;not null;default:0"` // 创建人Id SysAdminName string `gorm:"column:sys_admin_name;type:varchar(128);not null;default:''"` // 创建人姓名 Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 排序 CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间 ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间 } var SaDocColumns = struct { SaDocId string ClassifyId string ClassifyName string Title string Theme string CoverImg string ContentMd5 string SysAdminId string SysAdminName string Sort string CreateTime string ModifyTime string }{ SaDocId: "sa_doc_id", ClassifyId: "classify_id", ClassifyName: "classify_name", Title: "title", Theme: "theme", CoverImg: "cover_img", ContentMd5: "content_md5", SysAdminId: "sys_admin_id", SysAdminName: "sys_admin_name", Sort: "sort", CreateTime: "create_time", ModifyTime: "modify_time", } func (m *SaDoc) TableName() string { return "sa_doc" } func (m *SaDoc) Create() (err error) { err = global.DEFAULT_DmSQL.Create(m).Error return } func (m *SaDoc) Update(cols []string) (err error) { err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error return } func (m *SaDoc) Del() (err error) { sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1` err = global.DEFAULT_DmSQL.Exec(sql, m.SaDocId).Error return } func (m *SaDoc) GetItemById(id int) (err error) { sql := `SELECT * FROM sa_doc WHERE sa_doc_id = ? LIMIT 1` err = global.DEFAULT_DmSQL.Raw(sql, id).First(&m).Error return } func (m *SaDoc) GetItemByCondition(condition string, pars []interface{}) (err error) { sql := `SELECT * FROM sa_doc WHERE 1=1 ` sql += condition sql += ` LIMIT 1` err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&m).Error return } func (m *SaDoc) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error return } func (m *SaDoc) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDoc, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error return } func (m *SaDoc) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDoc, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z` err = global.DEFAULT_DmSQL.Raw(totalSql, pars...).Scan(&total).Error if err != nil { return } sql += ` LIMIT ?,?` pars = append(pars, startSize, pageSize) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error return } // InsertSaDocAndSections 新增文档及段落 func InsertSaDocAndSections(doc *SaDoc, sections []*SaDocSection) (err error) { tx := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() e := tx.Create(doc).Error if e != nil { err = fmt.Errorf("insert doc err: %v", e) return } if len(sections) > 0 { for i := range sections { sections[i].DocId = doc.SaDocId e = tx.Create(sections[i]).Error if e != nil { err = e return } } } return } // DelSaDocAndSections 删除文档和段落 func DelSaDocAndSections(docId int) (err error) { tx := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1` e := tx.Exec(sql, docId).Error if e != nil { err = fmt.Errorf("delete doc err: %v", e) return } sql = `DELETE FROM sa_doc_section WHERE doc_id = ?` e = tx.Exec(sql, docId).Error if e != nil { err = fmt.Errorf("delete sections err: %v", e) return } return } type SaDocAddReq struct { Title string `description:"标题"` Theme string `description:"主题"` ClassifyId int `description:"文档分类ID"` Content string `description:"文档内容"` } type SaDocEditReq struct { SaDocId int `description:"文档ID"` Title string `description:"标题"` Theme string `description:"主题"` ClassifyId int `description:"文档分类ID"` SectionList []*SaDocSection `description:"段落列表"` } type SaDocDelReq struct { SaDocId int `description:"文档ID"` } type SaDocMoveReq struct { SaDocClassifyId int `description:"文档分类ID"` SaDocId int `description:"文档ID"` PrevSaDocId int `description:"上一个文档ID"` NextSaDocId int `description:"下一个文档ID"` } type SaDocPageListResp struct { List []*SaDocItem Paging *paging.PagingItem `description:"分页数据"` } type SaDocItem struct { SaDocId int `description:"文档ID"` ClassifyId int `description:"文档分类ID"` ClassifyName string `description:"分类名称"` Title string `description:"标题"` Theme string `description:"主题"` CoverImg string `description:"封面图"` SysAdminId int `description:"创建人ID"` SysAdminName string `description:"创建人姓名"` Sort int `description:"排序"` CreateTime string `description:"创建时间"` UseNum int `description:"引用数"` } type SaDocDetail struct { SaDocItem SectionList []*SaDocSectionItem } // UpdateSaDocClassifyByClassifyId 更新文档分类信息 func UpdateSaDocClassifyByClassifyId(classifyId int, classifyName string) (err error) { sql := `UPDATE sa_doc SET classify_name = ? WHERE classify_id = ?` err = global.DEFAULT_DmSQL.Exec(sql, classifyName, classifyId).Error return } // UpdateSaDocAndSections 更新文档和段落 func UpdateSaDocAndSections(docItem *SaDoc, insertSecs, updateSecs []*SaDocSection, delSecIds []int, updateCols, updateSecCols []string) (err error) { tx := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() // 更新文档 e := tx.Select(updateCols).Updates(docItem).Error if e != nil { err = fmt.Errorf("update doc err: %v", e) return } // 段落-新增/更新/删除 if len(insertSecs) > 0 { e = tx.CreateInBatches(insertSecs, utils.MultiAddNum).Error if e != nil { err = fmt.Errorf("insert sections err: %v", e) return } } for _, s := range updateSecs { e = tx.Select(updateSecCols).Updates(s).Error if e != nil { err = fmt.Errorf("update section err: %v", e) return } } if len(delSecIds) > 0 { sql := `DELETE FROM sa_doc_section WHERE doc_id = ? AND sa_doc_section_id IN (%s)` sql = fmt.Sprintf(sql, utils.GetOrmInReplace(len(delSecIds))) e = tx.Exec(sql, docItem.SaDocId, delSecIds).Error if e != nil { err = fmt.Errorf("remove sections err: %v", e) } } return } // ElasticSaDoc ES-语义分析文档 type ElasticSaDoc struct { SaDocId int `description:"文档ID"` SaDocSectionId int `description:"段落ID"` ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` Title string `description:"标题"` Theme string `description:"主题"` BodyContent string `description:"内容"` Author string `description:"创建人"` CoverImg string `description:"封面图"` CreateTime string `description:"创建时间"` } // GetSaDocsWithUseNumByCondition 获取文档及引用数 func GetSaDocsWithUseNumByCondition(condition string, pars []interface{}) (items []*SaDocItem, err error) { sql := `SELECT a.sa_doc_id,a.classify_id,a.classify_name,a.title,a.theme,a.cover_img,a.content_md5,a.sys_admin_id,a.sys_admin_name,a.sort,a.create_time,a.modify_time, COUNT(b.sa_compare_label_id) AS use_num FROM sa_doc AS a LEFT JOIN sa_compare_label AS b ON a.sa_doc_id = b.doc_id WHERE 1 = 1 %s GROUP BY a.sa_doc_id,a.classify_id,a.classify_name,a.title,a.theme,a.cover_img,a.content_md5,a.sys_admin_id,a.sys_admin_name,a.sort,a.create_time,a.modify_time ORDER BY a.sort ASC, a.create_time DESC, a.sa_doc_id ASC` sql = fmt.Sprintf(sql, condition) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error return } // UpdateSaDocSort 根据分类ID更新排序 func UpdateSaDocSort(classifyId, nowSort int, prevId int, updateSort string) (err error) { sql := ` UPDATE sa_doc SET sort = ` + updateSort + ` WHERE classify_id = ? AND ` if prevId > 0 { sql += ` ( sort > ? or ( sa_doc_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) ` } err = global.DEFAULT_DmSQL.Exec(sql, classifyId, nowSort).Error return } // GetFirstSortSaDoc 获取排序最前的文档 func GetFirstSortSaDoc(classifyId int) (item *SaDoc, err error) { sql := ` SELECT * FROM sa_doc WHERE classify_id = ? ORDER BY sort ASC,sa_doc_id ASC LIMIT 1` err = global.DEFAULT_DmSQL.Raw(sql, classifyId).First(&item).Error return }