report_message_config.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package models
  2. import (
  3. "eta_gn/eta_api/global"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. )
  9. // ReportMessageConfig 研报ICE订阅号消息推送配置表
  10. type ReportMessageConfig struct {
  11. ConfigId int64 `gorm:"column:config_id;primaryKey;autoIncrement" description:"主键ID"`
  12. ClassifyId int `gorm:"column:classify_id;default:0" description:"分类ID"`
  13. NotifyUsers string `gorm:"column:notify_users" description:"通知用户列表"`
  14. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  15. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  16. }
  17. // TableName 设置表名
  18. func (m *ReportMessageConfig) TableName() string {
  19. return "report_message_config"
  20. }
  21. // Create 创建配置
  22. func (m *ReportMessageConfig) Create() (err error) {
  23. err = global.DmSQL["rddp"].Create(m).Error
  24. return
  25. }
  26. // Update 更新配置
  27. func (m *ReportMessageConfig) Update(cols []string) (err error) {
  28. err = global.DmSQL["rddp"].Select(cols).Updates(m).Error
  29. return
  30. }
  31. // GetItemById 根据ID获取配置
  32. func (m *ReportMessageConfig) GetItemById(id int64) (item *ReportMessageConfig, err error) {
  33. sql := fmt.Sprintf(`SELECT * FROM %s WHERE config_id = ? LIMIT 1`, m.TableName())
  34. err = global.DmSQL["rddp"].Raw(sql, id).First(&item).Error
  35. return
  36. }
  37. // GetItemByClassifyId 根据分类ID获取配置
  38. func (m *ReportMessageConfig) GetItemByClassifyId(classifyId int) (item *ReportMessageConfig, err error) {
  39. sql := fmt.Sprintf(`SELECT * FROM %s WHERE classify_id = ? LIMIT 1`, m.TableName())
  40. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&item).Error
  41. return
  42. }
  43. // GetItemsByCondition 根据条件获取配置列表
  44. func (m *ReportMessageConfig) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportMessageConfig, err error) {
  45. fields := strings.Join(fieldArr, ",")
  46. if len(fieldArr) == 0 {
  47. fields = `*`
  48. }
  49. order := `ORDER BY create_time DESC`
  50. if orderRule != "" {
  51. order = ` ORDER BY ` + orderRule
  52. }
  53. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  54. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  55. return
  56. }
  57. // GetPageItemsByCondition 分页获取配置列表
  58. func (m *ReportMessageConfig) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ReportMessageConfig, err error) {
  59. fields := strings.Join(fieldArr, ",")
  60. if len(fieldArr) == 0 {
  61. fields = `*`
  62. }
  63. order := `ORDER BY create_time DESC`
  64. if orderRule != "" {
  65. order = ` ORDER BY ` + orderRule
  66. }
  67. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  68. pars = append(pars, startSize)
  69. pars = append(pars, pageSize)
  70. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  71. return
  72. }
  73. // GetCountByCondition 获取符合条件的配置数量
  74. func (m *ReportMessageConfig) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  75. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  76. err = global.DmSQL["rddp"].Raw(sql, pars...).Scan(&count).Error
  77. return
  78. }
  79. // ReportMessageConfigListReq 配置列表请求参数
  80. type ReportMessageConfigListReq struct {
  81. PageSize int `form:"PageSize"`
  82. CurrentIndex int `form:"CurrentIndex"`
  83. ClassifyId int `form:"ClassifyId" description:"分类ID"`
  84. }
  85. // ReportMessageConfigListResp 配置列表响应体
  86. type ReportMessageConfigListResp struct {
  87. List []*ReportMessageConfig
  88. Paging *paging.PagingItem `description:"分页数据"`
  89. }
  90. // ReportMessageConfigAddReq 新增配置请求体
  91. type ReportMessageConfigAddReq struct {
  92. ClassifyId int `json:"classify_id" description:"分类ID"`
  93. NotifyUsers string `json:"notify_users" description:"通知用户列表"`
  94. }
  95. // ReportMessageConfigEditReq 编辑配置请求体
  96. type ReportMessageConfigEditReq struct {
  97. ConfigId int64 `json:"config_id" description:"配置ID"`
  98. ClassifyId int `json:"classify_id" description:"分类ID"`
  99. NotifyUsers string `json:"notify_users" description:"通知用户列表"`
  100. }
  101. // ReportMessageRecord 研报ICE订阅号消息推送记录表
  102. type ReportMessageRecord struct {
  103. MessageId int64 `gorm:"column:message_id;primaryKey;autoIncrement" description:"主键ID"`
  104. ReportId int64 `gorm:"column:report_id" description:"关联的研报ID"`
  105. ReportTitle string `gorm:"column:report_title" description:"研报标题"`
  106. PptId int64 `gorm:"column:ppt_id" description:"关联的PPT ID"`
  107. ClassifyId int `gorm:"column:classify_id;default:0" description:"分类ID"`
  108. ClassifyType int8 `gorm:"column:classify_type" description:"分类类型1-研报,2-PPT"`
  109. SendAdminId int64 `gorm:"column:send_admin_id" description:"发送者ID"`
  110. SendTime time.Time `gorm:"column:send_time" description:"发送时间"`
  111. ReceiveAdminId int64 `gorm:"column:receive_admin_id" description:"接收者ID"`
  112. ReceiveAdminCode string `gorm:"column:receive_admin_code" description:"接收者编码"`
  113. Status int8 `gorm:"column:status" description:"状态;1发送成功,2发送失败"`
  114. TaskId string `gorm:"column:task_id" description:"任务ID"`
  115. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  116. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  117. Content string `gorm:"column:content" description:"消息内容"`
  118. Url string `gorm:"column:url" description:"消息URL"`
  119. }
  120. type ReportIceMsgRecordListItem struct {
  121. MessageId int64 `description:"主键ID"`
  122. ReportId int64 `description:"关联的研报ID"`
  123. ReportTitle string `description:"研报标题"`
  124. PptId int64 `description:"关联的PPT ID"`
  125. ClassifyId int `description:"分类ID"`
  126. ClassifyFullName string `description:"分类名称"`
  127. ClassifyType int8 `description:"分类类型:1-研报,2-PPT"`
  128. SendAdminId int64 `description:"发送者ID"`
  129. SendAdminName string `description:"发送者名称"`
  130. SendTime string `description:"发送时间"`
  131. ReceiveAdminId int64 `description:"接收者ID"`
  132. ReceiveAdminName string `description:"接收者名称"`
  133. Status int8 `description:"状态:1发送成功,2发送失败"`
  134. CreateTime string `description:"创建时间"`
  135. }
  136. // TableName 设置表名
  137. func (ReportMessageRecord) TableName() string {
  138. return "report_message_record"
  139. }
  140. type ReportIceMsgConfigReq struct {
  141. ClassifyId int `description:"分类ID"`
  142. NotifyUsers []int `description:"通知用户列表"`
  143. }
  144. // 查询配置是否已存在
  145. func (m *ReportMessageConfig) IsExist(classifyId int) (bool, error) {
  146. var count int64
  147. err := global.DmSQL["rddp"].Model(&ReportMessageConfig{}).Where("classify_id = ?", classifyId).Count(&count).Error
  148. return count > 0, err
  149. }
  150. // 查询消息推送记录列表
  151. func (m *ReportMessageRecord) GetList(condition string, pars []interface{}, startSize, pageSize int) (list []*ReportMessageRecord, err error) {
  152. // 使用子查询获取每组最新的一条记录
  153. fields := "*"
  154. subQuery := fmt.Sprintf(`
  155. SELECT MAX(message_id) as max_id
  156. FROM %s
  157. WHERE 1=1 %s
  158. GROUP BY COALESCE(report_id, ''), COALESCE(ppt_id, ''), receive_admin_id
  159. `, m.TableName(), condition)
  160. // 主查询使用 IN 子查询获取完整记录
  161. sql := fmt.Sprintf(`
  162. SELECT %s
  163. FROM %s
  164. WHERE message_id IN (%s)
  165. ORDER BY create_time DESC, message_id DESC
  166. LIMIT ?,?
  167. `, fields, m.TableName(), subQuery)
  168. pars = append(pars, startSize)
  169. pars = append(pars, pageSize)
  170. err = global.DmSQL["rddp"].Raw(sql, pars...).Scan(&list).Error
  171. return
  172. }
  173. // 查询消息推送记录列表数量
  174. func (m *ReportMessageRecord) GetListCount(condition string, pars []interface{}) (count int, err error) {
  175. sql := fmt.Sprintf(`SELECT COUNT(*)
  176. FROM (
  177. SELECT 1
  178. FROM %s
  179. WHERE 1=1 %s
  180. GROUP BY COALESCE(report_id, ''), COALESCE(ppt_id, ''), receive_admin_id
  181. ) t`, m.TableName(), condition)
  182. err = global.DmSQL["rddp"].Raw(sql, pars...).Scan(&count).Error
  183. return
  184. }
  185. // 批量新增推送记录
  186. func (m *ReportMessageRecord) BatchAdd(list []*ReportMessageRecord) (err error) {
  187. err = global.DmSQL["rddp"].Create(list).Error
  188. return
  189. }
  190. // ReportIceMsgRecordListResp 消息推送记录列表响应体
  191. type ReportIceMsgRecordListResp struct {
  192. List []*ReportIceMsgRecordListItem
  193. Paging *paging.PagingItem `description:"分页数据"`
  194. }
  195. type ReportIceMsgPushReq struct {
  196. ReportId int `description:"研报ID"`
  197. PptId int `description:"PPT ID"`
  198. }
  199. type ReportIceMsgPushResp struct {
  200. MsgSendTime time.Time `description:"消息发送时间"`
  201. }
  202. func (m *ReportMessageConfig) GetListByClassifyIdList(classifyIdList []int) (list []*ReportMessageConfig, err error) {
  203. sql := fmt.Sprintf(`SELECT * FROM %s WHERE classify_id IN (?)`, m.TableName())
  204. err = global.DmSQL["rddp"].Raw(sql, classifyIdList).Find(&list).Error
  205. return
  206. }
  207. // 查询消息推送记录
  208. func (m *ReportMessageRecord) GetSuccessRecordAdminIdsByReportId(reportId int, title, content, url string) (adminIds []int, err error) {
  209. sql := fmt.Sprintf(`SELECT DISTINCT receive_admin_id FROM %s WHERE report_id = ? AND report_title = ? AND content = ? AND url = ? AND status = 1`, m.TableName())
  210. err = global.DmSQL["rddp"].Raw(sql, reportId, title, content, url).Scan(&adminIds).Error
  211. return
  212. }
  213. // 查询消息推送记录
  214. func (m *ReportMessageRecord) GetSuccessRecordAdminIdsByPptId(pptId int, title, content, url string) (adminIds []int, err error) {
  215. sql := fmt.Sprintf(`SELECT DISTINCT receive_admin_id FROM %s WHERE ppt_id = ? AND ppt_title = ? AND ppt_content = ? AND ppt_url = ? AND status = 1`, m.TableName())
  216. err = global.DmSQL["rddp"].Raw(sql, pptId, title, content, url).Scan(&adminIds).Error
  217. return
  218. }