sa_doc.go 11 KB

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