| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package common
- import (
- "fmt"
- "mtp2_if/db"
- "mtp2_if/global/app"
- "mtp2_if/global/e"
- "mtp2_if/logger"
- "mtp2_if/models"
- "mtp2_if/utils"
- "net/http"
- "sort"
- "github.com/gin-gonic/gin"
- )
- // QueryNoticeReq 通知公告系统消息查询请求参数
- type QueryNoticeReq struct {
- app.PageInfo
- LoginID int `form:"loginID"` // 登录账号
- MsgType int `form:"msgType"` // 消息类型 - 1:公告通知 2:系统消息
- OnlyUnRead bool `form:"onlyUnRead"` // 是否未读信息
- }
- // QueryNoticeRsp 通知公告系统消息查询返回模型
- type QueryNoticeRsp struct {
- models.Noticemsg `xorm:"extends"`
- Readed bool `json:"readed" xorm:"-"` // 是否已读
- }
- // QueryNotice 通知公告系统消息查询
- // @Summary 通知公告系统消息查询
- // @Produce json
- // @Security ApiKeyAuth
- // @Param page query int false "页码"
- // @Param pagesize query int false "每页条数"
- // @Param loginID query int true "登录账号"
- // @Param msgType query int false "消息类型 - 1:公告通知 2:系统消息"
- // @Param onlyUnRead query bool false "是否未读信息"
- // @Success 200 {object} QueryNoticeRsp
- // @Failure 500 {object} app.Response
- // @Router /Common/QueryNotice [get]
- // @Tags 通用服务
- func QueryNotice(c *gin.Context) {
- appG := app.Gin{C: c}
- // 获取请求参数
- var req QueryNoticeReq
- if err := appG.C.ShouldBindQuery(&req); err != nil {
- logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
- return
- }
- rst := make([]QueryNoticeRsp, 0)
- engine := db.GetEngine()
- // 获取登录账号所属会员信息
- var userAccount models.Useraccount
- if has, _ := engine.Join("LEFT", "LOGINACCOUNT", "LOGINACCOUNT.USERID = USERACCOUNT.USERID").
- Where("LOGINACCOUNT.LOGINID = ?", req.LoginID).Get(&userAccount); !has {
- // 查询失败
- logger.GetLogger().Errorf("QueryNotice failed: %s", "获取登录账号所属会员ID失败")
- appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
- return
- }
- // 获取登录账号已读公告ID
- var msgReceivers []int
- engine.Table("MSGRECEIVER").Select("AUTOID").Where("RECEIVERTYPE = 2 and MANAGERID = ?", userAccount.Memberuserid).Find(&msgReceivers)
- // 查询SENDTYPE = 1的数据
- datas1 := make([]QueryNoticeRsp, 0)
- s := engine.Where("SENDTYPE = 1 and SYSDATE > SCHEDULETIME and SENTSTATUS=1").
- And("PUBLISHER = ? or PUBLISHER in (select USERACCOUNT.USERID from USERACCOUNT where USERACCOUNT.USERTYPE = 1)", userAccount.Memberuserid)
- if req.MsgType > 0 {
- s = s.And("MSGTYPE = ?", req.LoginID)
- }
- if req.OnlyUnRead && len(msgReceivers) > 0 {
- s = s.NotIn("AUTOID", msgReceivers)
- }
- if err := s.Find(&datas1); err != nil {
- // 查询失败
- logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
- return
- }
- rst = append(rst, datas1...)
- // 查询SENDTYPE = 2的数据
- datas2 := make([]QueryNoticeRsp, 0)
- s = engine.Where(fmt.Sprintf(`AUTOID in (select MEMBERRECV.MSGID from MEMBERRECV where MEMBERRECV.MEMBERID = %d)`, userAccount.Memberuserid)).
- And("SENDTYPE = 2 and SYSDATE > SCHEDULETIME and SENTSTATUS=1")
- if req.MsgType > 0 {
- s = s.And("MSGTYPE = ?", req.LoginID)
- }
- if req.OnlyUnRead && len(msgReceivers) > 0 {
- s = s.NotIn("AUTOID", msgReceivers)
- }
- if err := s.Find(&datas2); err != nil {
- // 查询失败
- logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
- return
- }
- rst = append(rst, datas2...)
- // 查询SENDTYPE = 3的数据
- datas3 := make([]QueryNoticeRsp, 0)
- s = engine.Where(fmt.Sprintf(`USERID = (SELECT USERID FROM LOGINACCOUNT WHERE LOGINID = %d)`, req.LoginID)).
- And("SENDTYPE = 3 and SYSDATE > SCHEDULETIME and SENTSTATUS=1")
- if req.MsgType > 0 {
- s = s.And("MSGTYPE = ?", req.LoginID)
- }
- if req.OnlyUnRead && len(msgReceivers) > 0 {
- s = s.NotIn("AUTOID", msgReceivers)
- }
- if err := s.Find(&datas3); err != nil {
- // 查询失败
- logger.GetLogger().Errorf("QueryNotice failed: %s", err.Error())
- appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
- return
- }
- rst = append(rst, datas3...)
- // 排序
- sort.Slice(rst, func(i int, j int) bool {
- return rst[i].Scheduletime.After(rst[j].Scheduletime)
- })
- // 分页
- total := len(rst)
- if req.PageSize > 0 {
- rstByPage := make([]QueryNoticeRsp, 0)
- // 开始上标
- start := req.Page * req.PageSize
- // 结束下标
- end := start + req.PageSize
- if start <= len(rst) {
- // 判断结束下标是否越界
- if end > len(rst) {
- end = len(rst)
- }
- rstByPage = rst[start:end]
- } else {
- rstByPage = rst[0:0]
- }
- rst = rstByPage
- }
- // 设置已读标志
- for i, v := range rst {
- if utils.SortInIntSlice(msgReceivers, int(v.Autoid)) {
- item := &rst[i]
- item.Readed = true
- }
- }
- // 查询成功返回
- if req.PageSize > 0 {
- logger.GetLogger().Infof("QueryNotice successed: %v", rst)
- appG.ResponseByPage(http.StatusOK, e.SUCCESS, rst, app.PageInfo{Page: req.Page, PageSize: req.PageSize, Total: total})
- } else {
- logger.GetLogger().Infof("QueryNotice successed: %v", rst)
- appG.Response(http.StatusOK, e.SUCCESS, rst)
- }
- }
|