notice.go 7.8 KB

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