sa_compare.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. // SaCompareUpdateResultImgReq 更新比对结果图片请求体
  177. type SaCompareUpdateResultImgReq struct {
  178. SaCompareId int `description:"比对ID"`
  179. ResultImg string `description:"结果图片"`
  180. }
  181. // UpdateSaCompareClassifyByClassifyId 更新比对分类信息
  182. func UpdateSaCompareClassifyByClassifyId(classifyId int, classifyName string) (err error) {
  183. o := orm.NewOrm()
  184. sql := `UPDATE sa_compare SET classify_name = ? WHERE classify_id = ?`
  185. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  186. return
  187. }
  188. // CreateSaCompare 新增比对、比对段落和比对标签
  189. func CreateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel) (err error) {
  190. o := orm.NewOrm()
  191. tx, err := o.Begin()
  192. if err != nil {
  193. return
  194. }
  195. defer func() {
  196. if err != nil {
  197. _ = tx.Rollback()
  198. } else {
  199. _ = tx.Commit()
  200. }
  201. }()
  202. // 新增比对
  203. lastId, err := tx.Insert(compareItem)
  204. if err != nil {
  205. return
  206. }
  207. compareId := int(lastId)
  208. compareItem.SaCompareId = compareId
  209. // 新增比对段落
  210. if len(compareDocs) > 0 {
  211. for i := range compareDocs {
  212. compareDocs[i].CompareId = compareId
  213. }
  214. if _, e := tx.InsertMulti(len(compareDocs), compareDocs); e != nil {
  215. err = e
  216. return
  217. }
  218. }
  219. // 新增比对标签
  220. if len(compareLabels) > 0 {
  221. for i := range compareLabels {
  222. compareLabels[i].CompareId = compareId
  223. }
  224. if _, e := tx.InsertMulti(len(compareLabels), compareLabels); e != nil {
  225. err = e
  226. return
  227. }
  228. }
  229. return
  230. }
  231. // UpdateSaCompare 保存比对、比对段落和比对标签
  232. func UpdateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel, compareUpCols []string) (err error) {
  233. o := orm.NewOrm()
  234. tx, err := o.Begin()
  235. if err != nil {
  236. return
  237. }
  238. defer func() {
  239. if err != nil {
  240. _ = tx.Rollback()
  241. } else {
  242. _ = tx.Commit()
  243. }
  244. }()
  245. // 删除比对段落和标签
  246. sql := `DELETE FROM sa_compare_doc WHERE compare_id = ?`
  247. _, err = tx.Raw(sql, compareItem.SaCompareId).Exec()
  248. if err != nil {
  249. return
  250. }
  251. sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
  252. _, err = tx.Raw(sql, compareItem.SaCompareId).Exec()
  253. if err != nil {
  254. return
  255. }
  256. // 更新比对
  257. _, err = tx.Update(compareItem, compareUpCols...)
  258. if err != nil {
  259. return
  260. }
  261. // 新增比对段落
  262. if len(compareDocs) > 0 {
  263. for i := range compareDocs {
  264. compareDocs[i].CompareId = compareItem.SaCompareId
  265. }
  266. if _, e := tx.InsertMulti(len(compareDocs), compareDocs); e != nil {
  267. err = e
  268. return
  269. }
  270. }
  271. // 新增比对标签
  272. if len(compareLabels) > 0 {
  273. for i := range compareLabels {
  274. compareLabels[i].CompareId = compareItem.SaCompareId
  275. }
  276. if _, e := tx.InsertMulti(len(compareLabels), compareLabels); e != nil {
  277. err = e
  278. return
  279. }
  280. }
  281. return
  282. }
  283. type SaCompareDetail struct {
  284. SaCompareItem
  285. HeadLabel []*SaCompareDetailHeadLabel `description:"标签列表"`
  286. DocList []*SaCompareDetailDoc `description:"文档列表"`
  287. KeywordsList []string `description:"历史搜索关键词"`
  288. }
  289. type SaCompareDetailHeadLabel struct {
  290. LabelId int `description:"标签ID"`
  291. LabelName string `description:"标签名称"`
  292. IsMine int `description:"是否为我的:0-否;1-是"`
  293. }
  294. type SaCompareDetailDoc struct {
  295. DocId int `description:"文档ID"`
  296. Title string `description:"文档标题"`
  297. Theme string `description:"文档主题"`
  298. ClassifyName string `description:"分类名称"`
  299. SectionList []*SaCompareDetailSection `description:"段落列表"`
  300. }
  301. type SaCompareDetailSection struct {
  302. SectionId int `description:"段落ID"`
  303. Content string `description:"段落内容"`
  304. Sort int `description:"段落排序"`
  305. IsPart int `description:"是否为片段: 0-否; 1-是"`
  306. StartIndex int `description:"片段开始下标"`
  307. EndIndex int `description:"片段结束下标"`
  308. LabelList []*SaCompareDetailFormatLabel `description:"段落标签"`
  309. }
  310. type SaCompareDetailFormatLabel struct {
  311. LabelId int `description:"标签ID"`
  312. LabelName string `description:"标签名称"`
  313. IsThis int `description:"是否为当前标签"`
  314. IsHistory int `description:"是否为历史标签"`
  315. IsOther int `description:"是否为其他人标签"`
  316. }
  317. type SaCompareDelReq struct {
  318. SaCompareId int `description:"比对ID"`
  319. }
  320. // DelSaCompare 删除文档和段落
  321. func DelSaCompare(compareId int) (err error) {
  322. o := orm.NewOrm()
  323. tx, err := o.Begin()
  324. if err != nil {
  325. return
  326. }
  327. defer func() {
  328. if err != nil {
  329. _ = tx.Rollback()
  330. } else {
  331. _ = tx.Commit()
  332. }
  333. }()
  334. sql := `DELETE FROM sa_compare WHERE sa_compare_id = ? LIMIT 1`
  335. _, err = tx.Raw(sql, compareId).Exec()
  336. if err != nil {
  337. return
  338. }
  339. sql = `DELETE FROM sa_compare_doc WHERE compare_id = ?`
  340. _, err = tx.Raw(sql, compareId).Exec()
  341. if err != nil {
  342. return
  343. }
  344. sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
  345. _, err = tx.Raw(sql, compareId).Exec()
  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. o := orm.NewOrm()
  358. sql := ` UPDATE sa_compare SET sort = ` + updateSort + ` WHERE classify_id = ? AND `
  359. if prevId > 0 {
  360. sql += ` ( sort > ? or ( sa_compare_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) `
  361. }
  362. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  363. return
  364. }
  365. // GetFirstSortSaCompare 获取排序最前的比对
  366. func GetFirstSortSaCompare(classifyId int) (item *SaCompare, err error) {
  367. o := orm.NewOrm()
  368. sql := ` SELECT * FROM sa_compare WHERE classify_id = ? ORDER BY sort ASC,sa_compare_id ASC LIMIT 1`
  369. err = o.Raw(sql, classifyId).QueryRow(&item)
  370. return
  371. }