sa_compare.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package semantic_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "strings"
  7. "time"
  8. )
  9. type SaCompare struct {
  10. SaCompareId int `orm:"column(sa_compare_id);pk" description:"比对ID"`
  11. ClassifyId int `description:"比对分类ID"`
  12. ClassifyName string `description:"比对分类名称"`
  13. Title string `description:"标题"`
  14. ResultImg string `description:"比对结果图片"`
  15. SysAdminId int `description:"创建人ID"`
  16. SysAdminName string `description:"创建人姓名"`
  17. Sort int `description:"排序"`
  18. CreateTime time.Time `description:"创建时间"`
  19. ModifyTime time.Time `description:"修改时间"`
  20. }
  21. var SaCompareColumns = struct {
  22. SaCompareId string
  23. ClassifyId string
  24. ClassifyName string
  25. Title string
  26. ResultImg string
  27. SysAdminId string
  28. SysAdminName string
  29. Sort string
  30. CreateTime string
  31. ModifyTime string
  32. }{
  33. SaCompareId: "sa_compare_id",
  34. ClassifyId: "classify_id",
  35. ClassifyName: "classify_name",
  36. Title: "title",
  37. ResultImg: "result_img",
  38. SysAdminId: "sys_admin_id",
  39. SysAdminName: "sys_admin_name",
  40. Sort: "sort",
  41. CreateTime: "create_time",
  42. ModifyTime: "modify_time",
  43. }
  44. func (m *SaCompare) TableName() string {
  45. return "sa_compare"
  46. }
  47. func (m *SaCompare) Create() (err error) {
  48. o := orm.NewOrm()
  49. id, err := o.Insert(m)
  50. if err != nil {
  51. return
  52. }
  53. m.SaCompareId = int(id)
  54. return
  55. }
  56. func (m *SaCompare) Update(cols []string) (err error) {
  57. o := orm.NewOrm()
  58. _, err = o.Update(m, cols...)
  59. return
  60. }
  61. func (m *SaCompare) Del() (err error) {
  62. o := orm.NewOrm()
  63. sql := `DELETE FROM sa_compare WHERE sa_compare_id = ? LIMIT 1`
  64. _, err = o.Raw(sql, m.SaCompareId).Exec()
  65. return
  66. }
  67. func (m *SaCompare) GetItemById(id int) (err error) {
  68. o := orm.NewOrm()
  69. sql := `SELECT * FROM sa_compare WHERE sa_compare_id = ? LIMIT 1`
  70. err = o.Raw(sql, id).QueryRow(&m)
  71. return
  72. }
  73. func (m *SaCompare) GetItemByCondition(condition string, pars []interface{}) (err error) {
  74. o := orm.NewOrm()
  75. sql := `SELECT * FROM sa_compare WHERE 1=1 `
  76. sql += condition
  77. sql += ` LIMIT 1`
  78. err = o.Raw(sql, pars).QueryRow(&m)
  79. return
  80. }
  81. func (m *SaCompare) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  82. o := orm.NewOrm()
  83. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  84. err = o.Raw(sql, pars).QueryRow(&count)
  85. return
  86. }
  87. func (m *SaCompare) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaCompare, err error) {
  88. o := orm.NewOrm()
  89. fields := strings.Join(fieldArr, ",")
  90. if len(fieldArr) == 0 {
  91. fields = `*`
  92. }
  93. order := `ORDER BY create_time DESC`
  94. if orderRule != "" {
  95. order = ` ORDER BY ` + orderRule
  96. }
  97. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  98. _, err = o.Raw(sql, pars).QueryRows(&items)
  99. return
  100. }
  101. func (m *SaCompare) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaCompare, err error) {
  102. o := orm.NewOrm()
  103. fields := strings.Join(fieldArr, ",")
  104. if len(fieldArr) == 0 {
  105. fields = `*`
  106. }
  107. order := `ORDER BY create_time DESC`
  108. if orderRule != "" {
  109. order = ` ORDER BY ` + orderRule
  110. }
  111. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  112. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  113. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  114. return
  115. }
  116. sql += ` LIMIT ?,?`
  117. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  118. return
  119. }
  120. // SaCompareSaveReq 比对保存请求体
  121. type SaCompareSaveReq struct {
  122. SaCompareId int `description:"比对ID"`
  123. Title string `description:"标题"`
  124. ClassifyId int `description:"分类ID"`
  125. CompareList []struct {
  126. SaDocId int `description:"文档ID"`
  127. SectionList []struct {
  128. SaDocSectionId int `description:"段落ID"`
  129. IsPart int `description:"是否为片段: 0-整段; 1-片段"`
  130. StartIndex int `description:"片段开始下标"`
  131. EndIndex int `description:"片段结束下标"`
  132. Content string `description:"打标签的内容"`
  133. LabelIds []int `description:"标签IDs"`
  134. } `description:"段落标签信息"`
  135. } `description:"比对列表"`
  136. }
  137. // SaCompareSaveResp 比对保存响应体
  138. type SaCompareSaveResp struct {
  139. SaCompareId int `description:"比对ID"`
  140. TitleList []string `description:"标题列表"`
  141. LabelList []*SaCompareSaveRespLabel `description:"标签列表"`
  142. //ThemeList []string `description:"主题列表"`
  143. }
  144. // SaCompareSaveRespLabel 比对响应标签
  145. type SaCompareSaveRespLabel struct {
  146. SaLabelId int `description:"标签ID"`
  147. LabelName string `description:"标签名称"`
  148. DocList []*SaCompareSaveRespDoc `description:"文档列表"`
  149. }
  150. // SaCompareSaveRespDoc 比对响应文档
  151. type SaCompareSaveRespDoc struct {
  152. SaDocId int `description:"文档ID"`
  153. Title string `description:"文档标题"`
  154. SectionList []*SaCompareSaveRespSection `description:"段落列表"`
  155. }
  156. // SaCompareSaveRespSection 比对响应段落
  157. type SaCompareSaveRespSection struct {
  158. SaDocSectionId int `description:"段落ID"`
  159. IsPart int `description:"是否为片段: 0-否 1-是"`
  160. Content string `description:"段落内容"`
  161. }
  162. type SaComparePageListResp struct {
  163. List []*SaCompareItem
  164. Paging *paging.PagingItem `description:"分页数据"`
  165. }
  166. type SaCompareItem struct {
  167. SaCompareId int `description:"比对ID"`
  168. ClassifyId int `description:"比对分类ID"`
  169. ClassifyName string `description:"比对分类名称"`
  170. Title string `description:"标题"`
  171. ResultImg string `description:"比对结果图片"`
  172. SysAdminId int `description:"创建人ID"`
  173. SysAdminName string `description:"创建人姓名"`
  174. CreateTime string `description:"创建时间"`
  175. }
  176. type SaCompareElastic struct {
  177. SaCompareId int `description:"比对ID"`
  178. ClassifyId int `description:"比对分类ID"`
  179. ClassifyName string `description:"比对分类名称"`
  180. Title string `description:"标题"`
  181. ResultImg string `description:"比对结果图片"`
  182. SysAdminId int `description:"创建人ID"`
  183. SysAdminName string `description:"创建人姓名"`
  184. CreateTime string `description:"创建时间"`
  185. ModifyTime string `description:"修改时间"`
  186. }
  187. // SaCompareUpdateResultImgReq 更新比对结果图片请求体
  188. type SaCompareUpdateResultImgReq struct {
  189. SaCompareId int `description:"比对ID"`
  190. ResultImg string `description:"结果图片"`
  191. }
  192. // UpdateSaCompareClassifyByClassifyId 更新比对分类信息
  193. func UpdateSaCompareClassifyByClassifyId(classifyId int, classifyName string) (err error) {
  194. o := orm.NewOrm()
  195. sql := `UPDATE sa_compare SET classify_name = ? WHERE classify_id = ?`
  196. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  197. return
  198. }
  199. // CreateSaCompare 新增比对、比对段落和比对标签
  200. func CreateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel) (err error) {
  201. o := orm.NewOrm()
  202. tx, err := o.Begin()
  203. if err != nil {
  204. return
  205. }
  206. defer func() {
  207. if err != nil {
  208. _ = tx.Rollback()
  209. } else {
  210. _ = tx.Commit()
  211. }
  212. }()
  213. // 新增比对
  214. lastId, err := tx.Insert(compareItem)
  215. if err != nil {
  216. return
  217. }
  218. compareId := int(lastId)
  219. compareItem.SaCompareId = compareId
  220. // 新增比对段落
  221. if len(compareDocs) > 0 {
  222. for i := range compareDocs {
  223. compareDocs[i].CompareId = compareId
  224. }
  225. if _, e := tx.InsertMulti(len(compareDocs), compareDocs); e != nil {
  226. err = e
  227. return
  228. }
  229. }
  230. // 新增比对标签
  231. if len(compareLabels) > 0 {
  232. for i := range compareLabels {
  233. compareLabels[i].CompareId = compareId
  234. }
  235. if _, e := tx.InsertMulti(len(compareLabels), compareLabels); e != nil {
  236. err = e
  237. return
  238. }
  239. }
  240. return
  241. }
  242. // UpdateSaCompare 保存比对、比对段落和比对标签
  243. func UpdateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel, compareUpCols []string) (err error) {
  244. o := orm.NewOrm()
  245. tx, err := o.Begin()
  246. if err != nil {
  247. return
  248. }
  249. defer func() {
  250. if err != nil {
  251. _ = tx.Rollback()
  252. } else {
  253. _ = tx.Commit()
  254. }
  255. }()
  256. // 删除比对段落和标签
  257. sql := `DELETE FROM sa_compare_doc WHERE compare_id = ?`
  258. _, err = tx.Raw(sql, compareItem.SaCompareId).Exec()
  259. if err != nil {
  260. return
  261. }
  262. sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
  263. _, err = tx.Raw(sql, compareItem.SaCompareId).Exec()
  264. if err != nil {
  265. return
  266. }
  267. // 更新比对
  268. _, err = tx.Update(compareItem, compareUpCols...)
  269. if err != nil {
  270. return
  271. }
  272. // 新增比对段落
  273. if len(compareDocs) > 0 {
  274. for i := range compareDocs {
  275. compareDocs[i].CompareId = compareItem.SaCompareId
  276. }
  277. if _, e := tx.InsertMulti(len(compareDocs), compareDocs); e != nil {
  278. err = e
  279. return
  280. }
  281. }
  282. // 新增比对标签
  283. if len(compareLabels) > 0 {
  284. for i := range compareLabels {
  285. compareLabels[i].CompareId = compareItem.SaCompareId
  286. }
  287. if _, e := tx.InsertMulti(len(compareLabels), compareLabels); e != nil {
  288. err = e
  289. return
  290. }
  291. }
  292. return
  293. }
  294. type SaCompareDetail struct {
  295. SaCompareItem
  296. HeadLabel []*SaCompareDetailHeadLabel `description:"标签列表"`
  297. DocList []*SaCompareDetailDoc `description:"文档列表"`
  298. KeywordsList []string `description:"历史搜索关键词"`
  299. }
  300. type SaCompareDetailHeadLabel struct {
  301. LabelId int `description:"标签ID"`
  302. LabelName string `description:"标签名称"`
  303. IsMine int `description:"是否为我的:0-否;1-是"`
  304. }
  305. type SaCompareDetailDoc struct {
  306. DocId int `description:"文档ID"`
  307. Title string `description:"文档标题"`
  308. Theme string `description:"文档主题"`
  309. ClassifyName string `description:"分类名称"`
  310. SectionList []*SaCompareDetailSection `description:"段落列表"`
  311. }
  312. type SaCompareDetailSection struct {
  313. SectionId int `description:"段落ID"`
  314. Content string `description:"段落内容"`
  315. Sort int `description:"段落排序"`
  316. IsPart int `description:"是否为片段: 0-否; 1-是"`
  317. StartIndex int `description:"片段开始下标"`
  318. EndIndex int `description:"片段结束下标"`
  319. LabelList []*SaCompareDetailFormatLabel `description:"段落标签"`
  320. }
  321. type SaCompareDetailFormatLabel struct {
  322. LabelId int `description:"标签ID"`
  323. LabelName string `description:"标签名称"`
  324. IsThis int `description:"是否为当前标签"`
  325. IsHistory int `description:"是否为历史标签"`
  326. IsOther int `description:"是否为其他人标签"`
  327. }
  328. type SaCompareDelReq struct {
  329. SaCompareId int `description:"比对ID"`
  330. }
  331. // DelSaCompare 删除文档和段落
  332. func DelSaCompare(compareId int) (err error) {
  333. o := orm.NewOrm()
  334. tx, err := o.Begin()
  335. if err != nil {
  336. return
  337. }
  338. defer func() {
  339. if err != nil {
  340. _ = tx.Rollback()
  341. } else {
  342. _ = tx.Commit()
  343. }
  344. }()
  345. sql := `DELETE FROM sa_compare WHERE sa_compare_id = ? LIMIT 1`
  346. _, err = tx.Raw(sql, compareId).Exec()
  347. if err != nil {
  348. return
  349. }
  350. sql = `DELETE FROM sa_compare_doc WHERE compare_id = ?`
  351. _, err = tx.Raw(sql, compareId).Exec()
  352. if err != nil {
  353. return
  354. }
  355. sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
  356. _, err = tx.Raw(sql, compareId).Exec()
  357. return
  358. }
  359. // SaCompareMoveReq 移动比对请求体
  360. type SaCompareMoveReq struct {
  361. SaCompareClassifyId int `description:"比对分类ID"`
  362. SaCompareId int `description:"比对ID"`
  363. PrevSaCompareId int `description:"上一个比对ID"`
  364. NextSaCompareId int `description:"下一个比对ID"`
  365. }
  366. // UpdateSaCompareSort 根据分类ID更新排序
  367. func UpdateSaCompareSort(classifyId, nowSort int, prevId int, updateSort string) (err error) {
  368. o := orm.NewOrm()
  369. sql := ` UPDATE sa_compare SET sort = ` + updateSort + ` WHERE classify_id = ? AND `
  370. if prevId > 0 {
  371. sql += ` ( sort > ? or ( sa_compare_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) `
  372. }
  373. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  374. return
  375. }
  376. // GetFirstSortSaCompare 获取排序最前的比对
  377. func GetFirstSortSaCompare(classifyId int) (item *SaCompare, err error) {
  378. o := orm.NewOrm()
  379. sql := ` SELECT * FROM sa_compare WHERE classify_id = ? ORDER BY sort ASC,sa_compare_id ASC LIMIT 1`
  380. err = o.Raw(sql, classifyId).QueryRow(&item)
  381. return
  382. }
  383. // CompareListByEsResp 文档对比Es搜索返回
  384. type CompareListByEsResp struct {
  385. Paging *paging.PagingItem
  386. List []*SaCompareElastic
  387. }