123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- package semantic_analysis
- import (
- "eta/eta_api/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strings"
- "time"
- )
- type SaDoc struct {
- SaDocId int `orm:"column(sa_doc_id);pk" description:"文档ID"`
- ClassifyId int `description:"文档分类ID"`
- ClassifyName string `description:"分类名称"`
- Title string `description:"标题"`
- Theme string `description:"主题"`
- CoverImg string `description:"封面图"`
- ContentMd5 string `description:"内容md5, 用于内容去重"`
- SysAdminId int `description:"创建人ID"`
- SysAdminName string `description:"创建人姓名"`
- Sort int `description:"排序"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"修改时间"`
- }
- 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) {
- o := orm.NewOrm()
- id, err := o.Insert(m)
- if err != nil {
- return
- }
- m.SaDocId = int(id)
- return
- }
- func (m *SaDoc) Update(cols []string) (err error) {
- o := orm.NewOrm()
- _, err = o.Update(m, cols...)
- return
- }
- func (m *SaDoc) Del() (err error) {
- o := orm.NewOrm()
- sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
- _, err = o.Raw(sql, m.SaDocId).Exec()
- return
- }
- func (m *SaDoc) GetItemById(id int) (err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
- err = o.Raw(sql, id).QueryRow(&m)
- return
- }
- func (m *SaDoc) GetItemByCondition(condition string, pars []interface{}) (err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM sa_doc WHERE 1=1 `
- sql += condition
- sql += ` LIMIT 1`
- err = o.Raw(sql, pars).QueryRow(&m)
- return
- }
- func (m *SaDoc) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func (m *SaDoc) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDoc, err error) {
- o := orm.NewOrm()
- 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 = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- func (m *SaDoc) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDoc, err error) {
- o := orm.NewOrm()
- 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`
- if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
- return
- }
- sql += ` LIMIT ?,?`
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- // InsertSaDocAndSections 新增文档及段落
- func InsertSaDocAndSections(doc *SaDoc, sections []*SaDocSection) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- lastId, err := tx.Insert(doc)
- if err != nil {
- return
- }
- doc.SaDocId = int(lastId)
- if len(sections) > 0 {
- for i := range sections {
- sections[i].DocId = int(lastId)
- secId, e := tx.Insert(sections[i])
- if e != nil {
- err = e
- return
- }
- sections[i].SaDocSectionId = int(secId)
- }
- }
- return
- }
- // DelSaDocAndSections 删除文档和段落
- func DelSaDocAndSections(docId int) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
- _, err = tx.Raw(sql, docId).Exec()
- if err != nil {
- return
- }
- sql = `DELETE FROM sa_doc_section WHERE doc_id = ?`
- _, err = tx.Raw(sql, docId).Exec()
- 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) {
- o := orm.NewOrm()
- sql := `UPDATE sa_doc SET classify_name = ? WHERE classify_id = ?`
- _, err = o.Raw(sql, classifyName, classifyId).Exec()
- return
- }
- // UpdateSaDocAndSections 更新文档和段落
- func UpdateSaDocAndSections(docItem *SaDoc, insertSecs, updateSecs []*SaDocSection, delSecIds []int, updateCols, updateSecCols []string) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- // 更新文档
- if _, err = tx.Update(docItem, updateCols...); err != nil {
- return
- }
- // 段落-新增/更新/删除
- if len(insertSecs) > 0 {
- if _, err = tx.InsertMulti(len(insertSecs), insertSecs); err != nil {
- return
- }
- }
- for _, s := range updateSecs {
- if _, err = tx.Update(s, updateSecCols...); err != nil {
- 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)))
- _, err = tx.Raw(sql, docItem.SaDocId, delSecIds).Exec()
- }
- 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) {
- o := orm.NewOrm()
- sql := `SELECT
- a.*, 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
- ORDER BY
- a.sort ASC, a.create_time DESC, a.sa_doc_id ASC`
- sql = fmt.Sprintf(sql, condition)
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- // UpdateSaDocSort 根据分类ID更新排序
- func UpdateSaDocSort(classifyId, nowSort int, prevId int, updateSort string) (err error) {
- o := orm.NewOrm()
- 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 = o.Raw(sql, classifyId, nowSort).Exec()
- return
- }
- // GetFirstSortSaDoc 获取排序最前的文档
- func GetFirstSortSaDoc(classifyId int) (item *SaDoc, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM sa_doc WHERE classify_id = ? ORDER BY sort ASC,sa_doc_id ASC LIMIT 1`
- err = o.Raw(sql, classifyId).QueryRow(&item)
- return
- }
|