report.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package report
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/utils/date"
  6. "eta/eta_mini_ht_api/models"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type ReportStatus string
  12. type ReportSource string
  13. type SendStatus string
  14. const (
  15. SEND SendStatus = "SEND"
  16. UNSEND SendStatus = "UNSEND"
  17. SourceETA ReportSource = "ETA"
  18. SourceHT ReportSource = "HT"
  19. StatusInit ReportStatus = "INIT"
  20. StatusPending ReportStatus = "PENDING"
  21. StatusDone ReportStatus = "DONE"
  22. MaxBatchNum = 1000
  23. CommonColumns = "id,org_id,author,abstract,title,source,cover_src,published_time"
  24. taskColumns = "id,author,published_time"
  25. )
  26. type Report struct {
  27. ID int `gorm:"column:id;primary_key;comment:'id'" json:"id"`
  28. OrgID int `gorm:"column:org_id;comment:'原始id'" json:"org_id"`
  29. Source ReportSource `gorm:"column:source;comment:'研报来源1:eta 2:海通'" json:"source"`
  30. Title string `gorm:"column:title;comment:'标题'" json:"title"`
  31. Abstract string `gorm:"column:abstract;comment:'摘要'" json:"abstract"`
  32. Author string `gorm:"column:author;comment:'作者'" json:"author"`
  33. CoverSrc int `gorm:"column:cover_src;comment:'封面图片'" json:"cover_src"`
  34. Status ReportStatus `gorm:"column:status;comment:'报告状态 init:初始化 pending:同步中 done:完成同步'" json:"status"`
  35. SendStatus SendStatus `gorm:"column:send_status;comment:'发送状态'" json:"send_status"`
  36. PublishedTime string `gorm:"column:published_time;comment:'发布时间'" json:"published_time"`
  37. CreatedTime time.Time `gorm:"column:created_time;comment:'创建时间'" json:"created_time"`
  38. UpdatedTime time.Time `gorm:"column:updated_time;comment:'修改时间'" json:"updated_time"`
  39. }
  40. func BatchInsertReport(list *[]Report) (err error) {
  41. db := models.Main()
  42. //手动事务
  43. tx := db.Begin()
  44. err = db.CreateInBatches(list, MaxBatchNum).Error
  45. if err != nil {
  46. logger.Error("批量插入研报失败:%v", err)
  47. tx.Rollback()
  48. return
  49. }
  50. tx.Commit()
  51. return nil
  52. }
  53. func (t *Report) BeforeCreate(_ *gorm.DB) (err error) {
  54. t.CreatedTime = time.Now()
  55. return
  56. }
  57. func GetAuthorByOrgId(orgId int, source string) (names []string, err error) {
  58. db := models.Main()
  59. err = db.Model(&Report{}).Select("author").Where("org_id = ? and source =? ", orgId, source).Scan(&names).Error
  60. return
  61. }
  62. func GetReportByOrgId(orgId int, source string) (reports []Report, err error) {
  63. db := models.Main()
  64. err = db.Select(CommonColumns).Where("org_id = ? and source =? ", orgId, source).Find(&reports).Error
  65. return
  66. }
  67. func GetReportById(reportId int) (report Report, err error) {
  68. db := models.Main()
  69. err = db.Select(CommonColumns).Where("id = ?", reportId).First(&report).Error
  70. if err != nil {
  71. logger.Error("查询报告失败:%v", err)
  72. }
  73. return
  74. }
  75. func GetLatestReportIdBySource(source ReportSource) (id int, err error) {
  76. sql := "select IFNULL(max(org_id),0) from reports where source = ?"
  77. err = DoSql(sql, &id, source)
  78. return
  79. }
  80. func DoSql(sql string, result interface{}, values ...interface{}) (err error) {
  81. db := models.Main()
  82. err = db.Raw(sql, values...).Scan(result).Error
  83. if err != nil {
  84. logger.Error("执行sql[%v]失败:%v", sql, err)
  85. }
  86. return
  87. }
  88. func GetListOrderByCondition(week bool, column string, limit int, order models.Order) (reports []Report, err error) {
  89. db := models.Main()
  90. if week {
  91. end := time.Now()
  92. begin := date.GetBeginOfTheWeek(end, time.Monday)
  93. err = db.Select(CommonColumns).Where("DATE(published_time) BETWEEN ? AND ?", begin, end).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error
  94. } else {
  95. err = db.Select(CommonColumns).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error
  96. }
  97. if err != nil {
  98. logger.Error("查询报告列表失败:%v", err)
  99. }
  100. if reports == nil {
  101. return []Report{}, nil
  102. }
  103. return
  104. }
  105. func GetListByCondition[T any](column string, values []T) (reports []Report, err error) {
  106. if len(values) == 0 {
  107. logger.Error("查询条件的值不能为空")
  108. return []Report{}, nil
  109. }
  110. db := models.Main()
  111. err = db.Select(CommonColumns).Where(fmt.Sprintf("%s in ?", column), values).Find(&reports).Error
  112. if err != nil {
  113. logger.Error("查询报告列表失败:%v", err)
  114. }
  115. if reports == nil {
  116. return []Report{}, nil
  117. }
  118. return
  119. }
  120. func GetMaxIdByPermissionIds(orgIds []int) (maxId int64) {
  121. db := models.Main()
  122. err := db.Model(&Report{}).Select("MAX(id) id").Where("org_id in ?", orgIds).Scan(&maxId).Error
  123. if err != nil {
  124. logger.Error("获取报告最大ID失败:%v", err)
  125. return 0
  126. }
  127. return
  128. }
  129. func GetTotalPageCount() (total int64) {
  130. db := models.Main()
  131. err := db.Model(&Report{}).Count(&total).Error
  132. if err != nil {
  133. logger.Error("统计报告数量失败:%v", err)
  134. }
  135. return
  136. }
  137. func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) {
  138. db := models.Main()
  139. err := db.Model(&Report{}).Where("author =?", analyst).Count(&total).Error
  140. if err != nil {
  141. return
  142. }
  143. err = db.Model(&Report{}).Select("Max(id)").Where("author =?", analyst).Scan(&latestId).Error
  144. if err != nil {
  145. return
  146. }
  147. return
  148. }
  149. func GetReportPage(latestId int64, limit int, offset int) (list []Report, err error) {
  150. if latestId < 0 {
  151. err = errors.New("非法的id参数")
  152. logger.Error("非法的id参数:%d", latestId)
  153. return
  154. }
  155. if limit <= 0 {
  156. err = errors.New("非法的limit参数")
  157. logger.Error("非法的limit参数:%d", limit)
  158. }
  159. db := models.Main()
  160. err = db.Select(CommonColumns).Where("id<= ?", latestId).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
  161. return
  162. }
  163. func GetReportPageByOrgIds(latestId int64, limit int, offset int, orgIds []int) (list []Report, err error) {
  164. if len(orgIds) == 0 {
  165. return GetReportPage(latestId, limit, offset)
  166. }
  167. if latestId < 0 {
  168. err = errors.New("非法的id参数")
  169. logger.Error("非法的id参数:%d", latestId)
  170. return
  171. }
  172. if limit <= 0 {
  173. err = errors.New("非法的limit参数")
  174. logger.Error("非法的limit参数:%d", limit)
  175. }
  176. db := models.Main()
  177. err = db.Select(CommonColumns).Where("id<= ? and org_id in ?", latestId, orgIds).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
  178. return
  179. }
  180. func GetNewReportByPublishTime(time time.Time) (list []Report) {
  181. db := models.Main()
  182. err := db.Select(taskColumns).Where("published_time >= ?", time).Order("published_time desc").Find(&list).Error
  183. if err != nil {
  184. logger.Error("查询新发布的报告列表失败:%v", err)
  185. }
  186. return
  187. }
  188. func GetReportPageByAnalyst(latestId int64, limit int, offset int, analyst string) (list []Report, err error) {
  189. if latestId < 0 {
  190. err = errors.New("非法的id参数")
  191. logger.Error("非法的id参数:%d", latestId)
  192. return
  193. }
  194. if limit <= 0 {
  195. err = errors.New("非法的limit参数")
  196. logger.Error("非法的limit参数:%d", limit)
  197. }
  198. db := models.Main()
  199. err = db.Select(CommonColumns).Where("id<= ? and author = ?", latestId, analyst).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error
  200. return
  201. }