sa_compare.go 14 KB

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