sa_doc.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package semantic_analysis
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. type SaDoc struct {
  11. SaDocId int `orm:"column(sa_doc_id);pk" description:"文档ID"`
  12. ClassifyId int `description:"文档分类ID"`
  13. ClassifyName string `description:"分类名称"`
  14. Title string `description:"标题"`
  15. Theme string `description:"主题"`
  16. CoverImg string `description:"封面图"`
  17. ContentMd5 string `description:"内容md5, 用于内容去重"`
  18. SysAdminId int `description:"创建人ID"`
  19. SysAdminName string `description:"创建人姓名"`
  20. Sort int `description:"排序"`
  21. CreateTime time.Time `description:"创建时间"`
  22. ModifyTime time.Time `description:"修改时间"`
  23. }
  24. var SaDocColumns = struct {
  25. SaDocId string
  26. ClassifyId string
  27. ClassifyName string
  28. Title string
  29. Theme string
  30. CoverImg string
  31. ContentMd5 string
  32. SysAdminId string
  33. SysAdminName string
  34. Sort string
  35. CreateTime string
  36. ModifyTime string
  37. }{
  38. SaDocId: "sa_doc_id",
  39. ClassifyId: "classify_id",
  40. ClassifyName: "classify_name",
  41. Title: "title",
  42. Theme: "theme",
  43. CoverImg: "cover_img",
  44. ContentMd5: "content_md5",
  45. SysAdminId: "sys_admin_id",
  46. SysAdminName: "sys_admin_name",
  47. Sort: "sort",
  48. CreateTime: "create_time",
  49. ModifyTime: "modify_time",
  50. }
  51. func (m *SaDoc) TableName() string {
  52. return "sa_doc"
  53. }
  54. func (m *SaDoc) Create() (err error) {
  55. o := orm.NewOrm()
  56. id, err := o.Insert(m)
  57. if err != nil {
  58. return
  59. }
  60. m.SaDocId = int(id)
  61. return
  62. }
  63. func (m *SaDoc) Update(cols []string) (err error) {
  64. o := orm.NewOrm()
  65. _, err = o.Update(m, cols...)
  66. return
  67. }
  68. func (m *SaDoc) Del() (err error) {
  69. o := orm.NewOrm()
  70. sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
  71. _, err = o.Raw(sql, m.SaDocId).Exec()
  72. return
  73. }
  74. func (m *SaDoc) GetItemById(id int) (err error) {
  75. o := orm.NewOrm()
  76. sql := `SELECT * FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
  77. err = o.Raw(sql, id).QueryRow(&m)
  78. return
  79. }
  80. func (m *SaDoc) GetItemByCondition(condition string, pars []interface{}) (err error) {
  81. o := orm.NewOrm()
  82. sql := `SELECT * FROM sa_doc WHERE 1=1 `
  83. sql += condition
  84. sql += ` LIMIT 1`
  85. err = o.Raw(sql, pars).QueryRow(&m)
  86. return
  87. }
  88. func (m *SaDoc) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  89. o := orm.NewOrm()
  90. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  91. err = o.Raw(sql, pars).QueryRow(&count)
  92. return
  93. }
  94. func (m *SaDoc) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDoc, err error) {
  95. o := orm.NewOrm()
  96. fields := strings.Join(fieldArr, ",")
  97. if len(fieldArr) == 0 {
  98. fields = `*`
  99. }
  100. order := `ORDER BY create_time DESC`
  101. if orderRule != "" {
  102. order = ` ORDER BY ` + orderRule
  103. }
  104. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  105. _, err = o.Raw(sql, pars).QueryRows(&items)
  106. return
  107. }
  108. func (m *SaDoc) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDoc, err error) {
  109. o := orm.NewOrm()
  110. fields := strings.Join(fieldArr, ",")
  111. if len(fieldArr) == 0 {
  112. fields = `*`
  113. }
  114. order := `ORDER BY create_time DESC`
  115. if orderRule != "" {
  116. order = ` ORDER BY ` + orderRule
  117. }
  118. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  119. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  120. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  121. return
  122. }
  123. sql += ` LIMIT ?,?`
  124. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  125. return
  126. }
  127. // InsertSaDocAndSections 新增文档及段落
  128. func InsertSaDocAndSections(doc *SaDoc, sections []*SaDocSection) (err error) {
  129. o := orm.NewOrm()
  130. tx, err := o.Begin()
  131. if err != nil {
  132. return
  133. }
  134. defer func() {
  135. if err != nil {
  136. _ = tx.Rollback()
  137. } else {
  138. _ = tx.Commit()
  139. }
  140. }()
  141. lastId, err := tx.Insert(doc)
  142. if err != nil {
  143. return
  144. }
  145. doc.SaDocId = int(lastId)
  146. if len(sections) > 0 {
  147. for i := range sections {
  148. sections[i].DocId = int(lastId)
  149. secId, e := tx.Insert(sections[i])
  150. if e != nil {
  151. err = e
  152. return
  153. }
  154. sections[i].SaDocSectionId = int(secId)
  155. }
  156. }
  157. return
  158. }
  159. // DelSaDocAndSections 删除文档和段落
  160. func DelSaDocAndSections(docId int) (err error) {
  161. o := orm.NewOrm()
  162. tx, err := o.Begin()
  163. if err != nil {
  164. return
  165. }
  166. defer func() {
  167. if err != nil {
  168. _ = tx.Rollback()
  169. } else {
  170. _ = tx.Commit()
  171. }
  172. }()
  173. sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
  174. _, err = tx.Raw(sql, docId).Exec()
  175. if err != nil {
  176. return
  177. }
  178. sql = `DELETE FROM sa_doc_section WHERE doc_id = ?`
  179. _, err = tx.Raw(sql, docId).Exec()
  180. return
  181. }
  182. type SaDocAddReq struct {
  183. Title string `description:"标题"`
  184. Theme string `description:"主题"`
  185. ClassifyId int `description:"文档分类ID"`
  186. Content string `description:"文档内容"`
  187. }
  188. type SaDocEditReq struct {
  189. SaDocId int `description:"文档ID"`
  190. Title string `description:"标题"`
  191. Theme string `description:"主题"`
  192. ClassifyId int `description:"文档分类ID"`
  193. SectionList []*SaDocSection `description:"段落列表"`
  194. }
  195. type SaDocDelReq struct {
  196. SaDocId int `description:"文档ID"`
  197. }
  198. type SaDocMoveReq struct {
  199. SaDocClassifyId int `description:"文档分类ID"`
  200. SaDocId int `description:"文档ID"`
  201. PrevSaDocId int `description:"上一个文档ID"`
  202. NextSaDocId int `description:"下一个文档ID"`
  203. }
  204. type SaDocPageListResp struct {
  205. List []*SaDocItem
  206. Paging *paging.PagingItem `description:"分页数据"`
  207. }
  208. type SaDocItem struct {
  209. SaDocId int `description:"文档ID"`
  210. ClassifyId int `description:"文档分类ID"`
  211. ClassifyName string `description:"分类名称"`
  212. Title string `description:"标题"`
  213. Theme string `description:"主题"`
  214. CoverImg string `description:"封面图"`
  215. SysAdminId int `description:"创建人ID"`
  216. SysAdminName string `description:"创建人姓名"`
  217. Sort int `description:"排序"`
  218. CreateTime string `description:"创建时间"`
  219. UseNum int `description:"引用数"`
  220. }
  221. type SaDocDetail struct {
  222. SaDocItem
  223. SectionList []*SaDocSectionItem
  224. }
  225. // UpdateSaDocClassifyByClassifyId 更新文档分类信息
  226. func UpdateSaDocClassifyByClassifyId(classifyId int, classifyName string) (err error) {
  227. o := orm.NewOrm()
  228. sql := `UPDATE sa_doc SET classify_name = ? WHERE classify_id = ?`
  229. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  230. return
  231. }
  232. // UpdateSaDocAndSections 更新文档和段落
  233. func UpdateSaDocAndSections(docItem *SaDoc, insertSecs, updateSecs []*SaDocSection, delSecIds []int, updateCols, updateSecCols []string) (err error) {
  234. o := orm.NewOrm()
  235. tx, err := o.Begin()
  236. if err != nil {
  237. return
  238. }
  239. defer func() {
  240. if err != nil {
  241. _ = tx.Rollback()
  242. } else {
  243. _ = tx.Commit()
  244. }
  245. }()
  246. // 更新文档
  247. if _, err = tx.Update(docItem, updateCols...); err != nil {
  248. return
  249. }
  250. // 段落-新增/更新/删除
  251. if len(insertSecs) > 0 {
  252. if _, err = tx.InsertMulti(len(insertSecs), insertSecs); err != nil {
  253. return
  254. }
  255. }
  256. for _, s := range updateSecs {
  257. if _, err = tx.Update(s, updateSecCols...); err != nil {
  258. return
  259. }
  260. }
  261. if len(delSecIds) > 0 {
  262. sql := `DELETE FROM sa_doc_section WHERE doc_id = ? AND sa_doc_section_id IN (%s)`
  263. sql = fmt.Sprintf(sql, utils.GetOrmInReplace(len(delSecIds)))
  264. _, err = tx.Raw(sql, docItem.SaDocId, delSecIds).Exec()
  265. }
  266. return
  267. }
  268. // ElasticSaDoc ES-语义分析文档
  269. type ElasticSaDoc struct {
  270. SaDocId int `description:"文档ID"`
  271. SaDocSectionId int `description:"段落ID"`
  272. ClassifyId int `description:"分类ID"`
  273. ClassifyName string `description:"分类名称"`
  274. Title string `description:"标题"`
  275. Theme string `description:"主题"`
  276. BodyContent string `description:"内容"`
  277. Author string `description:"创建人"`
  278. CoverImg string `description:"封面图"`
  279. CreateTime string `description:"创建时间"`
  280. }
  281. // GetSaDocsWithUseNumByCondition 获取文档及引用数
  282. func GetSaDocsWithUseNumByCondition(condition string, pars []interface{}) (items []*SaDocItem, err error) {
  283. o := orm.NewOrm()
  284. sql := `SELECT
  285. a.*, COUNT(b.sa_compare_label_id) AS use_num
  286. FROM
  287. sa_doc AS a
  288. LEFT JOIN sa_compare_label AS b ON a.sa_doc_id = b.doc_id
  289. WHERE
  290. 1 = 1 %s
  291. GROUP BY
  292. a.sa_doc_id
  293. ORDER BY
  294. a.sort ASC, a.create_time DESC, a.sa_doc_id ASC`
  295. sql = fmt.Sprintf(sql, condition)
  296. _, err = o.Raw(sql, pars).QueryRows(&items)
  297. return
  298. }
  299. // UpdateSaDocSort 根据分类ID更新排序
  300. func UpdateSaDocSort(classifyId, nowSort int, prevId int, updateSort string) (err error) {
  301. o := orm.NewOrm()
  302. sql := ` UPDATE sa_doc SET sort = ` + updateSort + ` WHERE classify_id = ? AND `
  303. if prevId > 0 {
  304. sql += ` ( sort > ? or ( sa_doc_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) `
  305. }
  306. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  307. return
  308. }
  309. // GetFirstSortSaDoc 获取排序最前的文档
  310. func GetFirstSortSaDoc(classifyId int) (item *SaDoc, err error) {
  311. o := orm.NewOrm()
  312. sql := ` SELECT * FROM sa_doc WHERE classify_id = ? ORDER BY sort ASC,sa_doc_id ASC LIMIT 1`
  313. err = o.Raw(sql, classifyId).QueryRow(&item)
  314. return
  315. }