sa_doc.go 9.5 KB

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