sa_doc.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. func InsertSaDocAndSections(doc *SaDoc, sections []*SaDocSection) (err error) {
  118. tx := global.DEFAULT_DmSQL.Begin()
  119. defer func() {
  120. if err != nil {
  121. _ = tx.Rollback()
  122. return
  123. }
  124. _ = tx.Commit()
  125. }()
  126. e := tx.Create(doc).Error
  127. if e != nil {
  128. err = fmt.Errorf("insert doc err: %v", e)
  129. return
  130. }
  131. if len(sections) > 0 {
  132. for i := range sections {
  133. sections[i].DocId = doc.SaDocId
  134. e = tx.Create(sections[i]).Error
  135. if e != nil {
  136. err = e
  137. return
  138. }
  139. }
  140. }
  141. return
  142. }
  143. func DelSaDocAndSections(docId int) (err error) {
  144. tx := global.DEFAULT_DmSQL.Begin()
  145. defer func() {
  146. if err != nil {
  147. _ = tx.Rollback()
  148. return
  149. }
  150. _ = tx.Commit()
  151. }()
  152. sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
  153. e := tx.Exec(sql, docId).Error
  154. if e != nil {
  155. err = fmt.Errorf("delete doc err: %v", e)
  156. return
  157. }
  158. sql = `DELETE FROM sa_doc_section WHERE doc_id = ?`
  159. e = tx.Exec(sql, docId).Error
  160. if e != nil {
  161. err = fmt.Errorf("delete sections err: %v", e)
  162. return
  163. }
  164. return
  165. }
  166. type SaDocAddReq struct {
  167. Title string `description:"标题"`
  168. Theme string `description:"主题"`
  169. ClassifyId int `description:"文档分类ID"`
  170. Content string `description:"文档内容"`
  171. }
  172. type SaDocEditReq struct {
  173. SaDocId int `description:"文档ID"`
  174. Title string `description:"标题"`
  175. Theme string `description:"主题"`
  176. ClassifyId int `description:"文档分类ID"`
  177. SectionList []*SaDocSection `description:"段落列表"`
  178. }
  179. type SaDocDelReq struct {
  180. SaDocId int `description:"文档ID"`
  181. }
  182. type SaDocMoveReq struct {
  183. SaDocClassifyId int `description:"文档分类ID"`
  184. SaDocId int `description:"文档ID"`
  185. PrevSaDocId int `description:"上一个文档ID"`
  186. NextSaDocId int `description:"下一个文档ID"`
  187. }
  188. type SaDocPageListResp struct {
  189. List []*SaDocItem
  190. Paging *paging.PagingItem `description:"分页数据"`
  191. }
  192. type SaDocItem struct {
  193. SaDocId int `description:"文档ID"`
  194. ClassifyId int `description:"文档分类ID"`
  195. ClassifyName string `description:"分类名称"`
  196. Title string `description:"标题"`
  197. Theme string `description:"主题"`
  198. CoverImg string `description:"封面图"`
  199. SysAdminId int `description:"创建人ID"`
  200. SysAdminName string `description:"创建人姓名"`
  201. Sort int `description:"排序"`
  202. CreateTime string `description:"创建时间"`
  203. UseNum int `description:"引用数"`
  204. }
  205. type SaDocDetail struct {
  206. SaDocItem
  207. SectionList []*SaDocSectionItem
  208. }
  209. func UpdateSaDocClassifyByClassifyId(classifyId int, classifyName string) (err error) {
  210. sql := `UPDATE sa_doc SET classify_name = ? WHERE classify_id = ?`
  211. err = global.DEFAULT_DmSQL.Exec(sql, classifyName, classifyId).Error
  212. return
  213. }
  214. func UpdateSaDocAndSections(docItem *SaDoc, insertSecs, updateSecs []*SaDocSection, delSecIds []int, updateCols, updateSecCols []string) (err error) {
  215. tx := global.DEFAULT_DmSQL.Begin()
  216. defer func() {
  217. if err != nil {
  218. _ = tx.Rollback()
  219. return
  220. }
  221. _ = tx.Commit()
  222. }()
  223. e := tx.Select(updateCols).Updates(docItem).Error
  224. if e != nil {
  225. err = fmt.Errorf("update doc err: %v", e)
  226. return
  227. }
  228. if len(insertSecs) > 0 {
  229. e = tx.CreateInBatches(insertSecs, utils.MultiAddNum).Error
  230. if e != nil {
  231. err = fmt.Errorf("insert sections err: %v", e)
  232. return
  233. }
  234. }
  235. for _, s := range updateSecs {
  236. e = tx.Select(updateSecCols).Updates(s).Error
  237. if e != nil {
  238. err = fmt.Errorf("update section err: %v", e)
  239. return
  240. }
  241. }
  242. if len(delSecIds) > 0 {
  243. sql := `DELETE FROM sa_doc_section WHERE doc_id = ? AND sa_doc_section_id IN (%s)`
  244. sql = fmt.Sprintf(sql, utils.GetOrmInReplace(len(delSecIds)))
  245. e = tx.Exec(sql, docItem.SaDocId, delSecIds).Error
  246. if e != nil {
  247. err = fmt.Errorf("remove sections err: %v", e)
  248. }
  249. }
  250. return
  251. }
  252. type ElasticSaDoc struct {
  253. SaDocId int `description:"文档ID"`
  254. SaDocSectionId int `description:"段落ID"`
  255. ClassifyId int `description:"分类ID"`
  256. ClassifyName string `description:"分类名称"`
  257. Title string `description:"标题"`
  258. Theme string `description:"主题"`
  259. BodyContent string `description:"内容"`
  260. Author string `description:"创建人"`
  261. CoverImg string `description:"封面图"`
  262. CreateTime string `description:"创建时间"`
  263. }
  264. func GetSaDocsWithUseNumByCondition(condition string, pars []interface{}) (items []*SaDocItem, err error) {
  265. sql := `SELECT
  266. 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
  267. FROM
  268. sa_doc AS a
  269. LEFT JOIN sa_compare_label AS b ON a.sa_doc_id = b.doc_id
  270. WHERE
  271. 1 = 1 %s
  272. GROUP BY
  273. 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
  274. ORDER BY
  275. a.sort ASC, a.create_time DESC, a.sa_doc_id ASC`
  276. sql = fmt.Sprintf(sql, condition)
  277. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  278. return
  279. }
  280. func UpdateSaDocSort(classifyId, nowSort int, prevId int, updateSort string) (err error) {
  281. sql := ` UPDATE sa_doc SET sort = ` + updateSort + ` WHERE classify_id = ? AND `
  282. if prevId > 0 {
  283. sql += ` ( sort > ? or ( sa_doc_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) `
  284. }
  285. err = global.DEFAULT_DmSQL.Exec(sql, classifyId, nowSort).Error
  286. return
  287. }
  288. func GetFirstSortSaDoc(classifyId int) (item *SaDoc, err error) {
  289. sql := ` SELECT * FROM sa_doc WHERE classify_id = ? ORDER BY sort ASC,sa_doc_id ASC LIMIT 1`
  290. err = global.DEFAULT_DmSQL.Raw(sql, classifyId).First(&item).Error
  291. return
  292. }