report_send_ths_detail.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "time"
  6. )
  7. // 报告推送给同花顺的表结构体
  8. type ReportSendThsDetail struct {
  9. SendId int `orm:"column(send_id);pk" description:"发送给同花顺的Id"`
  10. ReportId int `description:"报告id"`
  11. ReportType string `description:"报告类型"`
  12. Status int8 `description:"发送结果,0:待发送,-1发送失败,1发送成功"`
  13. Remark string `description:"失败原因"`
  14. MsgType int `description:"消息类型,1:h5链接;2:小程序,3:文字;4:图片"`
  15. Title string `description:"推送标题"`
  16. LabelStr string `description:"推送标签"`
  17. Content string `description:"推送内容"`
  18. JumpUrl string `description:"跳转地址"`
  19. Pic string `description:"推送图片"`
  20. Level int `description:"等级,1:紧急,2:优先,3:普通"`
  21. PushTime time.Time `description:"实际开始推送时间/预推送时间"`
  22. CreateTime time.Time `description:"发送时间"`
  23. }
  24. type ReportSendThsListResp struct {
  25. Paging *paging.PagingItem
  26. List []*ReportSendThsListItem
  27. }
  28. type ReportSendThsTypeListResp struct {
  29. List []*ReportSendThsTypeListItem
  30. }
  31. type ReportSendThsTypeListItem struct {
  32. ReportType string `description:"报告类型"`
  33. }
  34. type ReportSendThsListItem struct {
  35. SendId int `description:"发送给同花顺的Id"`
  36. ReportId int `description:"报告id"`
  37. ReportType string `description:"报告类型"`
  38. Status int8 `description:"发送结果,0:待发送,-1发送失败,1发送成功"`
  39. Remark string `description:"失败原因"`
  40. Title string `description:"推送标题"`
  41. LabelStr string `description:"推送标签"`
  42. PushTime string `description:"实际开始推送时间/预推送时间"`
  43. CreateTime string `description:"发送时间"`
  44. Level int `description:"等级,1:紧急,2:优先,3:普通"`
  45. }
  46. // 新增报告发送给同花顺的记录
  47. func AddReportSendThsDetail(item *ReportSendThsDetail) (lastId int64, err error) {
  48. o := orm.NewOrm()
  49. //o.Using("rddp")
  50. lastId, err = o.Insert(item)
  51. return
  52. }
  53. // 修改报告发送给同花顺的记录状态
  54. func ModifyReportSendThsDetailStatus(sendId int, status int8, remark string) (err error) {
  55. o := orm.NewOrm()
  56. //o.Using("rddp")
  57. sql := `UPDATE report_send_ths_detail SET status = ?,remark=? WHERE send_id = ? `
  58. _, err = o.Raw(sql, status, remark, sendId).Exec()
  59. return
  60. }
  61. // 根据报告id获取发送记录
  62. func GetReportSendThsDetailByReportId(reportId int, reportType string) (item *ReportSendThsDetail, err error) {
  63. o := orm.NewOrm()
  64. sql := ` SELECT * FROM report_send_ths_detail WHERE report_id=? and report_type=? order by send_id desc`
  65. //o.Using("rddp")
  66. err = o.Raw(sql, reportId, reportType).QueryRow(&item)
  67. return
  68. }
  69. // GetLatelyReportSendThsDetail 获取发送中/发送成功的 距离现在最近的一条记录
  70. func GetLatelyReportSendThsDetail() (item *ReportSendThsDetail, err error) {
  71. o := orm.NewOrm()
  72. sql := ` SELECT * FROM report_send_ths_detail WHERE status >=0 order by push_time desc,send_id desc`
  73. //o.Using("rddp")
  74. err = o.Raw(sql).QueryRow(&item)
  75. return
  76. }
  77. // GetLatelyWaitReportSendThs 获取待发送的 距离现在最近的一条记录
  78. func GetLatelyWaitReportSendThs() (item *ReportSendThsDetail, err error) {
  79. o := orm.NewOrm()
  80. sql := ` SELECT * FROM report_send_ths_detail WHERE status =2 order by push_time desc,send_id desc`
  81. //o.Using("rddp")
  82. err = o.Raw(sql).QueryRow(&item)
  83. return
  84. }
  85. // GetNearlyWaitReportSendThsByPushTime 查询是否存在和设置时间相近的待推送记录
  86. func GetNearlyWaitReportSendThsByPushTime(sendId int, pushTimeStart, pushTimeEnd time.Time) (item *ReportSendThsDetail, err error) {
  87. o := orm.NewOrm()
  88. sql := ` SELECT * FROM report_send_ths_detail WHERE send_id !=? and status =2 and (push_time >= ? and push_time <= ?) order by push_time desc,send_id desc`
  89. //o.Using("rddp")
  90. err = o.Raw(sql, sendId, pushTimeStart, pushTimeEnd).QueryRow(&item)
  91. return
  92. }
  93. func GetWaitReportSendThsList(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportSendThsDetail, err error) {
  94. o := orm.NewOrm()
  95. sql := `SELECT * FROM report_send_ths_detail WHERE status = 2 `
  96. if condition != "" {
  97. sql += condition
  98. }
  99. sql += ` order by push_time asc,send_id asc limit ?,? `
  100. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  101. return
  102. }
  103. func GetWaitReportSendThsType() (items []*ReportSendThsDetail, err error) {
  104. o := orm.NewOrm()
  105. sql := `SELECT * FROM report_send_ths_detail WHERE status = 2 group by report_type`
  106. _, err = o.Raw(sql).QueryRows(&items)
  107. return
  108. }
  109. func GetWaitReportSendThsCount(condition string, pars []interface{}) (count int64, err error) {
  110. o := orm.NewOrm()
  111. sql := `SELECT COUNT(1) AS count FROM report_send_ths_detail WHERE status = 2 `
  112. if condition != "" {
  113. sql += condition
  114. }
  115. err = o.Raw(sql, pars).QueryRow(&count)
  116. return
  117. }
  118. func GetWaitReportSendThsAll(condition string, pars []interface{}) (items []*ReportSendThsDetail, err error) {
  119. o := orm.NewOrm()
  120. sql := `SELECT * FROM report_send_ths_detail WHERE status = 2 `
  121. if condition != "" {
  122. sql += condition
  123. }
  124. sql += ` order by push_time asc,send_id asc`
  125. _, err = o.Raw(sql, pars).QueryRows(&items)
  126. return
  127. }
  128. // 根据sendId获取发送记录
  129. func GetReportSendThsBySendId(sendId int) (item *ReportSendThsDetail, err error) {
  130. o := orm.NewOrm()
  131. sql := `SELECT * FROM report_send_ths_detail WHERE send_id=? `
  132. //o.Using("rddp")
  133. err = o.Raw(sql, sendId).QueryRow(&item)
  134. return
  135. }
  136. // MultiUpdateReportSendThsPushTime 批量修改配置时间
  137. func MultiUpdateReportSendThsPushTime(sendId int, newTime, multiSql, updateSendIds string) (err error) {
  138. o := orm.NewOrm()
  139. to, err := o.Begin()
  140. if err != nil {
  141. return
  142. }
  143. defer func() {
  144. if err != nil {
  145. _ = to.Rollback()
  146. } else {
  147. _ = to.Commit()
  148. }
  149. }()
  150. //修改单条数据
  151. sql := ` UPDATE report_send_ths_detail SET push_time = ? WHERE send_id = ? `
  152. _, err = to.Raw(sql, newTime, sendId).Exec()
  153. if err != nil {
  154. return
  155. }
  156. if multiSql != "" {
  157. //修改定时推送时间
  158. sql = `UPDATE report_send_ths_detail
  159. SET push_time =
  160. CASE send_id ` + multiSql + ` END
  161. WHERE
  162. status = 2 AND send_id IN (` + updateSendIds + ` ) AND push_time > ? `
  163. _, err = to.Raw(sql, newTime).Exec()
  164. }
  165. return
  166. }
  167. // GetLatelyWaitLevelReportSendThs 获取相同推送等级level下待发送的 距离现在最近的一条记录
  168. func GetLatelyWaitLevelReportSendThs(level int) (item *ReportSendThsDetail, err error) {
  169. o := orm.NewOrm()
  170. sql := ` SELECT * FROM report_send_ths_detail WHERE status =2 AND level=? order by push_time desc,send_id desc`
  171. err = o.Raw(sql, level).QueryRow(&item)
  172. return
  173. }
  174. // GetEarliestWaitGtLevelReportSendThs 获取大于该推送等级level(推送级别更低)下待发送的 距离现在最早的一条记录
  175. func GetEarliestWaitGtLevelReportSendThs(level int) (item *ReportSendThsDetail, err error) {
  176. o := orm.NewOrm()
  177. sql := ` SELECT * FROM report_send_ths_detail WHERE status = 2 AND level > ? order by push_time asc,send_id asc`
  178. err = o.Raw(sql, level).QueryRow(&item)
  179. return
  180. }
  181. // UpdateReportSendThsPushTime 批量修改配置时间
  182. func UpdateReportSendThsPushTime(pushTime time.Time, newTime string) (err error) {
  183. o := orm.NewOrm()
  184. //UPDATE comment c set c.time = DATE_ADD(c.time, INTERVAL 7 DAY) ;
  185. //select date_add(@dt, interval 1 minute); - 加1分钟
  186. //修改定时推送时间
  187. sql := `UPDATE report_send_ths_detail
  188. SET push_time = ` + newTime + ` WHERE status = 2 AND push_time > ? `
  189. _, err = o.Raw(sql, pushTime).Exec()
  190. return
  191. }
  192. // UpdateReportSendThsPushTimeByEqGtPushTime 根据发布时间批量修改配置时间(大于等于该发布时间)
  193. func UpdateReportSendThsPushTimeByEqGtPushTime(pushTime time.Time, newTime string) (err error) {
  194. o := orm.NewOrm()
  195. //修改定时推送时间
  196. sql := `UPDATE report_send_ths_detail
  197. SET push_time = ` + newTime + ` WHERE status = 2 AND push_time >= ? `
  198. _, err = o.Raw(sql, pushTime).Exec()
  199. return
  200. }
  201. // GetLatelyIsSendSendThsDetail 获取发送中/发送成功/发送失败的 距离现在最近的一条记录
  202. func GetLatelyIsSendSendThsDetail() (item *ReportSendThsDetail, err error) {
  203. o := orm.NewOrm()
  204. //`status` tinyint(1) DEFAULT '0' COMMENT '发送结果,0:待发送,1发送成功,2:等待系统定时发送',
  205. sql := ` SELECT * FROM report_send_ths_detail WHERE status in (0,1) order by push_time desc,send_id desc`
  206. err = o.Raw(sql).QueryRow(&item)
  207. return
  208. }