sa_compare.go 13 KB

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