report_free_layout.go 8.9 KB

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