report_free_layout.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package report
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/clause"
  8. "time"
  9. )
  10. type ReportFreeLayout struct {
  11. Id int `gorm:"primaryKey;autoIncrement;column:id"` // 主键
  12. ReportId int `gorm:"column:report_id"` // 研报Id
  13. ReportChapterId int `gorm:"column:report_chapter_id"` // 章节Id
  14. Page int `gorm:"column:page"` // 页码
  15. IsChapter int `gorm:"column:is_chapter"` // 是否多章节
  16. Content string `gorm:"column:content;size:255"` // 内容
  17. ContentStruct string `gorm:"column:content_struct;size:255"` // 内容
  18. CreateTime time.Time `gorm:"column:create_time"` // 创建时间
  19. ModifyTime time.Time `gorm:"column:modify_time"` // 修改时间
  20. }
  21. type PagePositionEnum string
  22. const (
  23. Left PagePositionEnum = "left"
  24. Right PagePositionEnum = "right"
  25. Center PagePositionEnum = "center"
  26. )
  27. type ContentPage struct {
  28. Id int `json:"Id"`
  29. Page int `json:"Page"`
  30. Content string `json:"Content"`
  31. ContentStruct string `json:"ContentStruct"`
  32. ReportId int `json:"ChapterId"`
  33. ReportChapterId int `json:"ReportChapterId"`
  34. }
  35. func (cp *ContentPage) ToView(isChapter bool, ReportId int, ReportChapterId int) *ReportFreeLayout {
  36. if isChapter {
  37. return &ReportFreeLayout{
  38. ReportId: ReportId,
  39. ReportChapterId: ReportChapterId,
  40. Page: cp.Page,
  41. IsChapter: 1,
  42. Content: cp.Content,
  43. ContentStruct: cp.ContentStruct,
  44. CreateTime: time.Now(),
  45. }
  46. } else {
  47. return &ReportFreeLayout{
  48. ReportId: ReportId,
  49. ReportChapterId: ReportChapterId,
  50. Page: cp.Page,
  51. IsChapter: 0,
  52. Content: cp.Content,
  53. ContentStruct: cp.ContentStruct,
  54. CreateTime: time.Now(),
  55. }
  56. }
  57. }
  58. func (cp *ReportFreeLayout) ToPageView() *ContentPage {
  59. return &ContentPage{
  60. Page: cp.Page,
  61. Content: cp.Content,
  62. ContentStruct: cp.ContentStruct,
  63. ReportId: cp.ReportId,
  64. ReportChapterId: cp.ReportChapterId,
  65. }
  66. }
  67. func ToOrmViewList(srcList []ContentPage, isChapter bool, ReportId int, ReportChapterId int) (list []*ReportFreeLayout) {
  68. for _, v := range srcList {
  69. list = append(list, v.ToView(isChapter, ReportId, ReportChapterId))
  70. }
  71. return
  72. }
  73. func ToPageViewList(srcList []*ReportFreeLayout) (list []*ContentPage) {
  74. for _, v := range srcList {
  75. list = append(list, v.ToPageView())
  76. }
  77. return
  78. }
  79. // TableName 设置表名
  80. func (*ReportFreeLayout) TableName() string {
  81. return "report_free_layout"
  82. }
  83. func SortPage(reportId int, tx *gorm.DB) (err error) {
  84. if tx == nil {
  85. tx = global.DbMap[utils.DbNameReport].Begin()
  86. defer func() {
  87. if err != nil {
  88. _ = tx.Rollback()
  89. return
  90. }
  91. _ = tx.Commit()
  92. }()
  93. }
  94. sql := `select * from report_free_layout where report_id = ? and is_chapter=1 order by page asc`
  95. var ormList []*ReportFreeLayout
  96. err = tx.Raw(sql, reportId).Find(&ormList).Error
  97. if err != nil {
  98. return
  99. }
  100. if len(ormList) == 0 {
  101. return
  102. }
  103. chapterPages := make(map[int][]*ReportFreeLayout)
  104. for _, v := range ormList {
  105. chapterPages[v.ReportChapterId] = append(chapterPages[v.ReportChapterId], v)
  106. }
  107. chapterSql := `select report_chapter_id from report_chapter where report_id =? order by sort asc`
  108. var chapterIds []int
  109. err = tx.Raw(chapterSql, reportId).Scan(&chapterIds).Error
  110. if err != nil {
  111. return
  112. }
  113. initPage := 1
  114. for _, chapter := range chapterIds {
  115. chapterList := chapterPages[chapter]
  116. for _, v := range chapterList {
  117. v.Page = initPage
  118. initPage++
  119. }
  120. }
  121. var updateList []*ReportFreeLayout
  122. for _, chapterList := range chapterPages {
  123. updateList = append(updateList, chapterList...)
  124. }
  125. err = tx.Model(&ReportFreeLayout{}).Clauses(clause.OnConflict{
  126. Columns: []clause.Column{{Name: "Id"}},
  127. DoUpdates: clause.AssignmentColumns([]string{"Page"}),
  128. }).CreateInBatches(updateList, len(updateList)).Error
  129. return
  130. }
  131. func DeleteChapters(reportId int, chapterId int) (err error) {
  132. tx := global.DbMap[utils.DbNameReport].Begin()
  133. defer func() {
  134. if err != nil {
  135. _ = tx.Rollback()
  136. return
  137. }
  138. _ = tx.Commit()
  139. }()
  140. err = tx.Exec("delete from report_free_layout where report_id = ? and report_chapter_id=? and is_chapter=1", reportId, chapterId).Error
  141. if err != nil {
  142. return
  143. }
  144. err = SortPage(reportId, tx)
  145. return
  146. }
  147. func BatchInsertOrUpdatePages(tx *gorm.DB, list []*ReportFreeLayout, isChapter bool, reportId, chapterId int) (err error) {
  148. if isChapter {
  149. err = tx.Exec("delete from report_free_layout where report_id = ? and report_chapter_id=? and is_chapter=1", reportId, chapterId).Error
  150. if err != nil {
  151. return
  152. }
  153. //err = tx.Model(&ReportFreeLayout{}).Clauses(clause.OnConflict{
  154. // Columns: []clause.Column{{Name: "id"}},
  155. // DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "modify_time"}),
  156. //}).CreateInBatches(list, len(list)).Error
  157. err = tx.Model(&ReportFreeLayout{}).CreateInBatches(list, len(list)).Error
  158. if err != nil {
  159. return
  160. }
  161. err = SortPage(reportId, tx)
  162. return
  163. } else {
  164. err = tx.Exec("delete from report_free_layout where report_id = ? and is_chapter=0", reportId).Error
  165. if err != nil {
  166. return
  167. }
  168. err = tx.Model(&ReportFreeLayout{}).CreateInBatches(list, len(list)).Error
  169. //.Clauses(clause.OnConflict{
  170. // Columns: []clause.Column{{Name: "id"}},
  171. // DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "page", "modify_time"}),
  172. //})
  173. }
  174. return
  175. }
  176. func GetPrevFreeLayoutChaptersPagesByChapterId(reportId int, chapterId int) (pageNum int, err error) {
  177. var pageNumNullable sql2.NullInt64
  178. sql := `SELECT count(*)
  179. FROM report_free_layout rfl
  180. JOIN report_chapter rc ON rc.report_id = rfl.report_id and rc.report_chapter_id=rfl.report_chapter_id
  181. WHERE rfl.report_id = ?
  182. AND rc.sort < (
  183. SELECT sort
  184. FROM report_chapter
  185. WHERE report_id = ? and report_chapter_id=?
  186. )`
  187. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, reportId, chapterId).Scan(&pageNumNullable).Error
  188. if err != nil {
  189. return
  190. }
  191. if pageNumNullable.Valid {
  192. pageNum = int(pageNumNullable.Int64)
  193. }
  194. return
  195. }
  196. func GetFreeLayoutChapterPagesByReportId(reportId int) (list []*ContentPage, err error) {
  197. var ormList []*ReportFreeLayout
  198. sql := `select rfl.*,rc.sort from report_free_layout rfl LEFT JOIN report_chapter rc on rc.report_id=rfl.report_id and rc.report_chapter_id=rfl.report_chapter_id where rfl.report_id =? order by rc.sort,rfl.page asc`
  199. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId).Find(&ormList).Error
  200. if err != nil {
  201. return nil, err
  202. }
  203. list = ToPageViewList(ormList)
  204. return
  205. }
  206. func GetSingleFreeLayoutChapterPagesByReportId(reportId, chapterId int) (list []*ContentPage, err error) {
  207. var ormList []*ReportFreeLayout
  208. sql := `select * from report_free_layout where report_id =? and report_chapter_id=? order by page asc`
  209. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, chapterId).Find(&ormList).Error
  210. if err != nil {
  211. return nil, err
  212. }
  213. list = ToPageViewList(ormList)
  214. return
  215. }
  216. func GetFreeLayoutPagesByReportId(id int) (list []*ContentPage, err error) {
  217. var ormList []*ReportFreeLayout
  218. sql := `select * from report_free_layout where report_id =? and is_chapter=0 order by page asc`
  219. err = global.DbMap[utils.DbNameReport].Raw(sql, id).Find(&ormList).Error
  220. if err != nil {
  221. return nil, err
  222. }
  223. list = ToPageViewList(ormList)
  224. return
  225. }
  226. // GetReportFreeLayoutListByReportId
  227. // @Description: 根据报告ID和章节ID获取所有的布局列表
  228. // @author: Roc
  229. // @datetime 2025-04-16 13:46:38
  230. // @param reportId int
  231. // @param chapterId int
  232. // @return list []*ReportFreeLayout
  233. // @return err error
  234. func GetReportFreeLayoutListByReportId(reportId, chapterId int) (list []*ReportFreeLayout, err error) {
  235. sql := `select * from report_free_layout where report_id =? and report_chapter_id=? order by page asc`
  236. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, chapterId).Find(&list).Error
  237. return
  238. }
  239. // GetAllReportFreeLayoutListByReportId
  240. // @Description: 根据报告id获取所有的报告自由布局列表(含章节的)
  241. // @author: Roc
  242. // @datetime 2025-04-16 13:46:24
  243. // @param reportId int
  244. // @return list []*ReportFreeLayout
  245. // @return err error
  246. func GetAllReportFreeLayoutListByReportId(reportId int) (list []*ReportFreeLayout, err error) {
  247. sql := `select * from report_free_layout where report_id =? order by page asc`
  248. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId).Find(&list).Error
  249. return
  250. }