report_free_layout.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if err != nil {
  158. return
  159. }
  160. err = SortPage(reportId, tx)
  161. return
  162. } else {
  163. err = tx.Exec("delete from report_free_layout where report_id = ? and is_chapter=0", reportId).Error
  164. if err != nil {
  165. return
  166. }
  167. err = tx.Model(&ReportFreeLayout{}).Clauses(clause.OnConflict{
  168. Columns: []clause.Column{{Name: "id"}},
  169. DoUpdates: clause.AssignmentColumns([]string{"content", "content_struct", "page", "modify_time"}),
  170. }).CreateInBatches(list, len(list)).Error
  171. }
  172. return
  173. }
  174. func GetPrevFreeLayoutChaptersPagesByChapterId(reportId int, chapterId int) (pageNum int, err error) {
  175. var pageNumNullable sql2.NullInt64
  176. sql := `SELECT count(*)
  177. FROM report_free_layout rfl
  178. JOIN report_chapter rc ON rc.report_id = rfl.report_id and rc.report_chapter_id=rfl.report_chapter_id
  179. WHERE rfl.report_id = ?
  180. AND rc.sort < (
  181. SELECT sort
  182. FROM report_chapter
  183. WHERE report_id = ? and report_chapter_id=?
  184. )`
  185. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, reportId, chapterId).Scan(&pageNumNullable).Error
  186. if err != nil {
  187. return
  188. }
  189. if pageNumNullable.Valid {
  190. pageNum = int(pageNumNullable.Int64)
  191. }
  192. return
  193. }
  194. func GetFreeLayoutChapterPagesByReportId(reportId int) (list []*ContentPage, err error) {
  195. var ormList []*ReportFreeLayout
  196. 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`
  197. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId).Find(&ormList).Error
  198. if err != nil {
  199. return nil, err
  200. }
  201. list = ToPageViewList(ormList)
  202. return
  203. }
  204. func GetSingleFreeLayoutChapterPagesByReportId(reportId, chapterId int) (list []*ContentPage, err error) {
  205. var ormList []*ReportFreeLayout
  206. sql := `select * from report_free_layout where report_id =? and report_chapter_id=? order by page asc`
  207. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, chapterId).Find(&ormList).Error
  208. if err != nil {
  209. return nil, err
  210. }
  211. list = ToPageViewList(ormList)
  212. return
  213. }
  214. func GetFreeLayoutPagesByReportId(id int) (list []*ContentPage, err error) {
  215. var ormList []*ReportFreeLayout
  216. sql := `select * from report_free_layout where report_id =? and is_chapter=0 order by page asc`
  217. err = global.DbMap[utils.DbNameReport].Raw(sql, id).Find(&ormList).Error
  218. if err != nil {
  219. return nil, err
  220. }
  221. list = ToPageViewList(ormList)
  222. return
  223. }
  224. // GetReportFreeLayoutListByReportId
  225. // @Description: 根据报告ID和章节ID获取所有的布局列表
  226. // @author: Roc
  227. // @datetime 2025-04-16 13:46:38
  228. // @param reportId int
  229. // @param chapterId int
  230. // @return list []*ReportFreeLayout
  231. // @return err error
  232. func GetReportFreeLayoutListByReportId(reportId, chapterId int) (list []*ReportFreeLayout, err error) {
  233. sql := `select * from report_free_layout where report_id =? and report_chapter_id=? order by page asc`
  234. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId, chapterId).Find(&list).Error
  235. return
  236. }
  237. // GetAllReportFreeLayoutListByReportId
  238. // @Description: 根据报告id获取所有的报告自由布局列表(含章节的)
  239. // @author: Roc
  240. // @datetime 2025-04-16 13:46:24
  241. // @param reportId int
  242. // @return list []*ReportFreeLayout
  243. // @return err error
  244. func GetAllReportFreeLayoutListByReportId(reportId int) (list []*ReportFreeLayout, err error) {
  245. sql := `select * from report_free_layout where report_id =? order by page asc`
  246. err = global.DbMap[utils.DbNameReport].Raw(sql, reportId).Find(&list).Error
  247. return
  248. }