sa_compare.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. type SaCompareSaveReq struct {
  112. SaCompareId int `description:"比对ID"`
  113. Title string `description:"标题"`
  114. ClassifyId int `description:"分类ID"`
  115. CompareList []struct {
  116. SaDocId int `description:"文档ID"`
  117. SectionList []struct {
  118. SaDocSectionId int `description:"段落ID"`
  119. IsPart int `description:"是否为片段: 0-整段; 1-片段"`
  120. StartIndex int `description:"片段开始下标"`
  121. EndIndex int `description:"片段结束下标"`
  122. Content string `description:"打标签的内容"`
  123. LabelIds []int `description:"标签IDs"`
  124. } `description:"段落标签信息"`
  125. } `description:"比对列表"`
  126. }
  127. type SaCompareSaveResp struct {
  128. SaCompareId int `description:"比对ID"`
  129. TitleList []string `description:"标题列表"`
  130. LabelList []*SaCompareSaveRespLabel `description:"标签列表"`
  131. }
  132. type SaCompareSaveRespLabel struct {
  133. SaLabelId int `description:"标签ID"`
  134. LabelName string `description:"标签名称"`
  135. DocList []*SaCompareSaveRespDoc `description:"文档列表"`
  136. }
  137. type SaCompareSaveRespDoc struct {
  138. SaDocId int `description:"文档ID"`
  139. Title string `description:"文档标题"`
  140. SectionList []*SaCompareSaveRespSection `description:"段落列表"`
  141. }
  142. type SaCompareSaveRespSection struct {
  143. SaDocSectionId int `description:"段落ID"`
  144. IsPart int `description:"是否为片段: 0-否 1-是"`
  145. Content string `description:"段落内容"`
  146. }
  147. type SaComparePageListResp struct {
  148. List []*SaCompareItem
  149. Paging *paging.PagingItem `description:"分页数据"`
  150. }
  151. type SaCompareItem struct {
  152. SaCompareId int `description:"比对ID"`
  153. ClassifyId int `description:"比对分类ID"`
  154. ClassifyName string `description:"比对分类名称"`
  155. Title string `description:"标题"`
  156. ResultImg string `description:"比对结果图片"`
  157. SysAdminId int `description:"创建人ID"`
  158. SysAdminName string `description:"创建人姓名"`
  159. CreateTime string `description:"创建时间"`
  160. }
  161. type SaCompareElastic struct {
  162. SaCompareId int `description:"比对ID"`
  163. ClassifyId int `description:"比对分类ID"`
  164. ClassifyName string `description:"比对分类名称"`
  165. Title string `description:"标题"`
  166. ResultImg string `description:"比对结果图片"`
  167. SysAdminId int `description:"创建人ID"`
  168. SysAdminName string `description:"创建人姓名"`
  169. CreateTime string `description:"创建时间"`
  170. ModifyTime string `description:"修改时间"`
  171. }
  172. type SaCompareUpdateResultImgReq struct {
  173. SaCompareId int `description:"比对ID"`
  174. ResultImg string `description:"结果图片"`
  175. }
  176. func UpdateSaCompareClassifyByClassifyId(classifyId int, classifyName string) (err error) {
  177. sql := `UPDATE sa_compare SET classify_name = ? WHERE classify_id = ?`
  178. err = global.DEFAULT_DmSQL.Exec(sql, classifyName, classifyId).Error
  179. return
  180. }
  181. func CreateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel) (err error) {
  182. tx := global.DEFAULT_DmSQL.Begin()
  183. defer func() {
  184. if err != nil {
  185. _ = tx.Rollback()
  186. return
  187. }
  188. _ = tx.Commit()
  189. }()
  190. e := tx.Create(compareItem).Error
  191. if e != nil {
  192. err = fmt.Errorf("insert compare err: %v", e)
  193. return
  194. }
  195. if len(compareDocs) > 0 {
  196. for i := range compareDocs {
  197. compareDocs[i].CompareId = compareItem.SaCompareId
  198. }
  199. e = tx.CreateInBatches(compareDocs, utils.MultiAddNum).Error
  200. if e != nil {
  201. err = fmt.Errorf("insert docs err: %v", e)
  202. return
  203. }
  204. }
  205. if len(compareLabels) > 0 {
  206. for i := range compareLabels {
  207. compareLabels[i].CompareId = compareItem.SaCompareId
  208. }
  209. e = tx.CreateInBatches(compareLabels, utils.MultiAddNum).Error
  210. if e != nil {
  211. err = fmt.Errorf("insert labels err: %v", e)
  212. return
  213. }
  214. }
  215. return
  216. }
  217. func UpdateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel, compareUpCols []string) (err error) {
  218. tx := global.DEFAULT_DmSQL.Begin()
  219. defer func() {
  220. if err != nil {
  221. _ = tx.Rollback()
  222. return
  223. }
  224. _ = tx.Commit()
  225. }()
  226. sql := `DELETE FROM sa_compare_doc WHERE compare_id = ?`
  227. e := tx.Exec(sql, compareItem.SaCompareId).Error
  228. if e != nil {
  229. err = fmt.Errorf("delete docs err: %v", e)
  230. return
  231. }
  232. sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
  233. e = tx.Exec(sql, compareItem.SaCompareId).Error
  234. if e != nil {
  235. err = fmt.Errorf("delete labels err: %v", e)
  236. return
  237. }
  238. e = tx.Select(compareUpCols).Updates(compareItem).Error
  239. if e != nil {
  240. err = fmt.Errorf("update compare err: %v", e)
  241. return
  242. }
  243. if len(compareDocs) > 0 {
  244. for i := range compareDocs {
  245. compareDocs[i].CompareId = compareItem.SaCompareId
  246. }
  247. e = tx.CreateInBatches(compareDocs, utils.MultiAddNum).Error
  248. if e != nil {
  249. err = fmt.Errorf("insert docs err: %v", e)
  250. return
  251. }
  252. }
  253. if len(compareLabels) > 0 {
  254. for i := range compareLabels {
  255. compareLabels[i].CompareId = compareItem.SaCompareId
  256. }
  257. e = tx.CreateInBatches(compareLabels, utils.MultiAddNum).Error
  258. if e != nil {
  259. err = fmt.Errorf("insert labels err: %v", e)
  260. return
  261. }
  262. }
  263. return
  264. }
  265. type SaCompareDetail struct {
  266. SaCompareItem
  267. HeadLabel []*SaCompareDetailHeadLabel `description:"标签列表"`
  268. DocList []*SaCompareDetailDoc `description:"文档列表"`
  269. KeywordsList []string `description:"历史搜索关键词"`
  270. }
  271. type SaCompareDetailHeadLabel struct {
  272. LabelId int `description:"标签ID"`
  273. LabelName string `description:"标签名称"`
  274. IsMine int `description:"是否为我的:0-否;1-是"`
  275. }
  276. type SaCompareDetailDoc struct {
  277. DocId int `description:"文档ID"`
  278. Title string `description:"文档标题"`
  279. Theme string `description:"文档主题"`
  280. ClassifyName string `description:"分类名称"`
  281. SectionList []*SaCompareDetailSection `description:"段落列表"`
  282. }
  283. type SaCompareDetailSection struct {
  284. SectionId int `description:"段落ID"`
  285. Content string `description:"段落内容"`
  286. Sort int `description:"段落排序"`
  287. IsPart int `description:"是否为片段: 0-否; 1-是"`
  288. StartIndex int `description:"片段开始下标"`
  289. EndIndex int `description:"片段结束下标"`
  290. LabelList []*SaCompareDetailFormatLabel `description:"段落标签"`
  291. }
  292. type SaCompareDetailFormatLabel struct {
  293. LabelId int `description:"标签ID"`
  294. LabelName string `description:"标签名称"`
  295. IsThis int `description:"是否为当前标签"`
  296. IsHistory int `description:"是否为历史标签"`
  297. IsOther int `description:"是否为其他人标签"`
  298. }
  299. type SaCompareDelReq struct {
  300. SaCompareId int `description:"比对ID"`
  301. }
  302. func DelSaCompare(compareId int) (err error) {
  303. tx := global.DEFAULT_DmSQL.Begin()
  304. defer func() {
  305. if err != nil {
  306. _ = tx.Rollback()
  307. return
  308. }
  309. _ = tx.Commit()
  310. }()
  311. sql := `DELETE FROM sa_compare WHERE sa_compare_id = ? LIMIT 1`
  312. e := tx.Exec(sql, compareId).Error
  313. if e != nil {
  314. err = fmt.Errorf("delete compare err: %v", e)
  315. return
  316. }
  317. sql = `DELETE FROM sa_compare_doc WHERE compare_id = ?`
  318. e = tx.Exec(sql, compareId).Error
  319. if e != nil {
  320. err = fmt.Errorf("delete docs err: %v", e)
  321. return
  322. }
  323. sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
  324. e = tx.Exec(sql, compareId).Error
  325. if e != nil {
  326. err = fmt.Errorf("delete labels err: %v", e)
  327. }
  328. return
  329. }
  330. type SaCompareMoveReq struct {
  331. SaCompareClassifyId int `description:"比对分类ID"`
  332. SaCompareId int `description:"比对ID"`
  333. PrevSaCompareId int `description:"上一个比对ID"`
  334. NextSaCompareId int `description:"下一个比对ID"`
  335. }
  336. func UpdateSaCompareSort(classifyId, nowSort int, prevId int, updateSort string) (err error) {
  337. sql := ` UPDATE sa_compare SET sort = ` + updateSort + ` WHERE classify_id = ? AND `
  338. if prevId > 0 {
  339. sql += ` ( sort > ? or ( sa_compare_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) `
  340. }
  341. err = global.DEFAULT_DmSQL.Exec(sql, classifyId, nowSort).Error
  342. return
  343. }
  344. func GetFirstSortSaCompare(classifyId int) (item *SaCompare, err error) {
  345. sql := ` SELECT * FROM sa_compare WHERE classify_id = ? ORDER BY sort ASC,sa_compare_id ASC LIMIT 1`
  346. err = global.DEFAULT_DmSQL.Raw(sql, classifyId).Find(&item).Error
  347. return
  348. }
  349. type CompareListByEsResp struct {
  350. Paging *paging.PagingItem
  351. List []*SaCompareElastic
  352. }