notice.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package common
  2. import (
  3. "fmt"
  4. "mtp2_if/db"
  5. "mtp2_if/global/app"
  6. "mtp2_if/global/e"
  7. "mtp2_if/logger"
  8. "mtp2_if/models"
  9. "mtp2_if/utils"
  10. "net/http"
  11. "sort"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // QueryNoticeReq 通知公告系统消息查询请求参数
  15. type QueryNoticeReq struct {
  16. app.PageInfo
  17. LoginID int `form:"loginID"` // 登录账号
  18. MsgType int `form:"msgType"` // 消息类型 - 1:公告通知 2:系统消息
  19. OnlyUnRead bool `form:"onlyUnRead"` // 是否未读信息
  20. LastID int `form:"lastID"` // 自增ID
  21. }
  22. // QueryNoticeRsp 通知公告系统消息查询返回模型
  23. type QueryNoticeRsp struct {
  24. models.Noticemsg `xorm:"extends"`
  25. Readed bool `json:"readed" xorm:"-"` // 是否已读
  26. }
  27. // QueryNotice 通知公告系统消息查询
  28. // @Summary 通知公告系统消息查询
  29. // @Produce json
  30. // @Security ApiKeyAuth
  31. // @Param page query int false "页码"
  32. // @Param pagesize query int false "每页条数"
  33. // @Param loginID query int true "登录账号"
  34. // @Param msgType query int false "消息类型 - 1:公告通知 2:系统消息"
  35. // @Param onlyUnRead query bool false "是否只获取未读信息"
  36. // @Param lastID query int false "自增ID,传入后会返回这个ID后面的记录"
  37. // @Success 200 {object} QueryNoticeRsp
  38. // @Failure 500 {object} app.Response
  39. // @Router /Common/QueryNotice [get]
  40. // @Tags 通用服务
  41. func QueryNotice(c *gin.Context) {
  42. appG := app.Gin{C: c}
  43. // 获取请求参数
  44. var req QueryNoticeReq
  45. if err := appG.C.ShouldBindQuery(&req); err != nil {
  46. logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
  47. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  48. return
  49. }
  50. rst := make([]QueryNoticeRsp, 0)
  51. engine := db.GetEngine()
  52. // 获取登录账号所属会员信息
  53. var userAccount models.Useraccount
  54. if has, _ := engine.Join("LEFT", "LOGINACCOUNT", "LOGINACCOUNT.USERID = USERACCOUNT.USERID").
  55. Where("LOGINACCOUNT.LOGINID = ?", req.LoginID).Get(&userAccount); !has {
  56. // 查询失败
  57. logger.GetLogger().Errorf("QueryNotice failed: %s", "获取登录账号所属会员ID失败")
  58. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  59. return
  60. }
  61. // 获取登录账号已读公告ID, 这里要使用的是个人的UserID来查询
  62. var msgReceivers []int
  63. engine.Table("MSGRECEIVER").Select("AUTOID").Where("RECEIVERTYPE = 2 and MANAGERID = ?", userAccount.Userid).Find(&msgReceivers)
  64. // 查询SENDTYPE = 1的数据
  65. datas1 := make([]QueryNoticeRsp, 0)
  66. s := engine.Where("SENDTYPE = 1 and SYSDATE > SCHEDULETIME and SYSDATE < ENDTIME and SENTSTATUS=1").
  67. And("PUBLISHER = ? or PUBLISHER in (select USERACCOUNT.USERID from USERACCOUNT where USERACCOUNT.USERTYPE = 1)", userAccount.Memberuserid)
  68. if req.MsgType > 0 {
  69. s = s.And("MSGTYPE = ?", req.MsgType)
  70. }
  71. if req.LastID != 0 {
  72. s = s.And("AUTOID > ?", req.LastID)
  73. }
  74. if req.OnlyUnRead && len(msgReceivers) > 0 {
  75. s = s.NotIn("AUTOID", msgReceivers)
  76. }
  77. if err := s.Find(&datas1); err != nil {
  78. // 查询失败
  79. logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
  80. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  81. return
  82. }
  83. rst = append(rst, datas1...)
  84. // 查询SENDTYPE = 2的数据
  85. datas2 := make([]QueryNoticeRsp, 0)
  86. s = engine.Where(fmt.Sprintf(`AUTOID in (select MEMBERRECV.MSGID from MEMBERRECV where MEMBERRECV.MEMBERID = %d)`, userAccount.Memberuserid)).
  87. And("SENDTYPE = 2 and SYSDATE > SCHEDULETIME and SYSDATE < ENDTIME and SENTSTATUS=1")
  88. if req.MsgType > 0 {
  89. s = s.And("MSGTYPE = ?", req.MsgType)
  90. }
  91. if req.LastID != 0 {
  92. s = s.And("AUTOID > ?", req.LastID)
  93. }
  94. if req.OnlyUnRead && len(msgReceivers) > 0 {
  95. s = s.NotIn("AUTOID", msgReceivers)
  96. }
  97. if err := s.Find(&datas2); err != nil {
  98. // 查询失败
  99. logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
  100. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  101. return
  102. }
  103. rst = append(rst, datas2...)
  104. // 查询SENDTYPE = 3的数据
  105. datas3 := make([]QueryNoticeRsp, 0)
  106. s = engine.Where(fmt.Sprintf(`USERID = (SELECT USERID FROM LOGINACCOUNT WHERE LOGINID = %d)`, req.LoginID)).
  107. And("SENDTYPE = 3 and SYSDATE > SCHEDULETIME and SYSDATE < ENDTIME and SENTSTATUS=1")
  108. if req.MsgType > 0 {
  109. s = s.And("MSGTYPE = ?", req.MsgType)
  110. }
  111. if req.LastID != 0 {
  112. s = s.And("AUTOID > ?", req.LastID)
  113. }
  114. if req.OnlyUnRead && len(msgReceivers) > 0 {
  115. s = s.NotIn("AUTOID", msgReceivers)
  116. }
  117. if err := s.Find(&datas3); err != nil {
  118. // 查询失败
  119. logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
  120. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  121. return
  122. }
  123. rst = append(rst, datas3...)
  124. // 排序
  125. sort.Slice(rst, func(i int, j int) bool {
  126. return rst[i].Scheduletime.After(rst[j].Scheduletime)
  127. })
  128. // 分页
  129. total := len(rst)
  130. if req.PageSize > 0 {
  131. rstByPage := make([]QueryNoticeRsp, 0)
  132. // 开始上标
  133. start := req.Page * req.PageSize
  134. // 结束下标
  135. end := start + req.PageSize
  136. if start <= len(rst) {
  137. // 判断结束下标是否越界
  138. if end > len(rst) {
  139. end = len(rst)
  140. }
  141. rstByPage = rst[start:end]
  142. } else {
  143. rstByPage = rst[0:0]
  144. }
  145. rst = rstByPage
  146. }
  147. // 设置已读标志
  148. for i, v := range rst {
  149. if utils.SortInIntSlice(msgReceivers, int(v.Autoid)) {
  150. item := &rst[i]
  151. item.Readed = true
  152. }
  153. }
  154. // 查询成功返回
  155. if req.PageSize > 0 {
  156. logger.GetLogger().Debugln("QueryNotice successed: %v", rst)
  157. appG.ResponseByPage(http.StatusOK, e.SUCCESS, rst, app.PageInfo{Page: req.Page, PageSize: req.PageSize, Total: total})
  158. } else {
  159. logger.GetLogger().Debugln("QueryNotice successed: %v", rst)
  160. appG.Response(http.StatusOK, e.SUCCESS, rst)
  161. }
  162. }
  163. // NoticeReadedReq 通知公告设置已读请求参数
  164. type NoticeReadedReq struct {
  165. LoginID int `form:"loginID" binding:"required"` // 登录账号
  166. NoticeID int `form:"noticeID" binding:"required"` // 通知公告ID
  167. }
  168. // NoticeReaded 通知公告设置已读请求
  169. // @Summary 通知公告设置已读请求
  170. // @Produce json
  171. // @Security ApiKeyAuth
  172. // @Param loginID query int true "登录账号"
  173. // @Param noticeID query int true "通知公告ID"
  174. // @Success 200 {object} app.Response
  175. // @Failure 500 {object} app.Response
  176. // @Router /Common/NoticeReaded [post]
  177. // @Tags 通用服务
  178. func NoticeReaded(c *gin.Context) {
  179. appG := app.Gin{C: c}
  180. // 获取请求参数
  181. var req NoticeReadedReq
  182. if err := appG.C.ShouldBind(&req); err != nil {
  183. logger.GetLogger().Errorf("NoticeReaded failed: %s", err.Error())
  184. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  185. return
  186. }
  187. engine := db.GetEngine()
  188. // 获取登录账号所属会员信息
  189. var userAccount models.Useraccount
  190. if has, _ := engine.Join("LEFT", "LOGINACCOUNT", "LOGINACCOUNT.USERID = USERACCOUNT.USERID").
  191. Where("LOGINACCOUNT.LOGINID = ?", req.LoginID).Get(&userAccount); !has {
  192. // 查询失败
  193. logger.GetLogger().Errorf("NoticeReaded failed: %s", "获取登录账号所属会员ID失败")
  194. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  195. return
  196. }
  197. // 尝试获取指定已读数据
  198. msgReceiver := &models.Msgreceiver{Autoid: int32(req.NoticeID), Managerid: userAccount.Userid, Receivertype: 2}
  199. has, err := engine.Get(msgReceiver)
  200. if err != nil {
  201. // 执行失败
  202. logger.GetLogger().Errorf("NoticeReaded failed: %s", err.Error())
  203. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  204. return
  205. }
  206. if has {
  207. // 找到记录则更新已读状态
  208. msgReceiver.Readstatus = 2
  209. if _, err := engine.Update(msgReceiver); err != nil {
  210. // 执行失败
  211. logger.GetLogger().Errorf("NoticeReaded failed: %s", err.Error())
  212. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  213. return
  214. }
  215. } else {
  216. // 找不到记录则新建一条
  217. m := new(models.Msgreceiver)
  218. m.Autoid = int32(req.NoticeID)
  219. m.Managerid = userAccount.Userid
  220. m.Readstatus = 2
  221. m.Receivertype = 2
  222. // m.Updatetime = time.Now()
  223. if _, err := engine.Insert(m); err != nil {
  224. // 执行失败
  225. logger.GetLogger().Errorf("NoticeReaded failed: %s", err.Error())
  226. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  227. return
  228. }
  229. }
  230. // 执行成功
  231. logger.GetLogger().Debugln("NoticeReaded successed: %v", "ok")
  232. appG.Response(http.StatusOK, e.SUCCESS, "")
  233. }