package controllers import ( "encoding/json" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hongze_mfyx/models" "hongze/hongze_mfyx/services" "hongze/hongze_mfyx/utils" "time" ) type YanxuanSpecialMessageController struct { BaseAuthController } // @Title 添加留言 // @Description 添加留言接口 // @Param request body models.AddCygxYanxuanSpecialMessageReq true "type json string" // @Success Ret=200 {object} // @router /message/add [post] func (this *YanxuanSpecialMessageController) MessageAdd() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req models.AddCygxYanxuanSpecialMessageReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } yanxuanSpecialId := req.YanxuanSpecialId content := req.Content parentId := req.ParentId if content == "" { br.Msg = "请输入留言内容!" return } if yanxuanSpecialId == 0 { br.Msg = "留言失败!" br.ErrMsg = "专栏信息不能为空" return } // 研选专栏 yanxuanSpecialBySpeciaDetail, err := models.GetYanxuanSpecialBySpecialId(yanxuanSpecialId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } item := new(models.CygxYanxuanSpecialMessage) item.UserId = user.UserId item.Mobile = user.Mobile item.Email = user.Email item.CompanyId = user.CompanyId item.CompanyName = user.CompanyName item.RealName = user.RealName item.SellerName, _ = services.GetSellerName(user) // 销售姓名 item.YanxuanSpecialId = yanxuanSpecialId item.Content = content item.ParentId = parentId item.YanxuanSpecialUserId = yanxuanSpecialBySpeciaDetail.UserId item.SourceTitle = yanxuanSpecialBySpeciaDetail.Title item.CreateTime = time.Now() item.ModifyTime = time.Now() item.RegisterPlatform = utils.REGISTER_PLATFORM messageId, err := models.AddCygxYanxuanSpecialMessage(item) if err != nil { br.Msg = "留言失败!" br.ErrMsg = "留言失败,Err:" + err.Error() return } var msgText string if parentId == 0 { msgText = "留言成功" go services.SendWxCategoryMsgByYanxuanSpecialMassageByAuthor(content, yanxuanSpecialBySpeciaDetail) } else { msgText = "回复成功" go services.SendWxCategoryMsgByYanxuanSpecialMassage(parentId, int(messageId), content, yanxuanSpecialBySpeciaDetail) } br.Ret = 200 br.Success = true br.Msg = msgText } // @Title 留言管理列表 // @Description 留言管理列表接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param MessageType query int false "留言类型1:普通留言、2:公开留言" // @Param WxMessageId query int false "留言ID" // @Success 200 {object} models.SpecialListResp // @router /message/manage/list [get] func (this *YanxuanSpecialMessageController) MessageManageList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") messageType, _ := this.GetInt("MessageType", 1) messageId, _ := this.GetInt("WxMessageId") // 前端做区分 resp := new(models.YanxuanSpecialMessageManageRespListResp) if messageId > 0 { messagerDetail, err := models.GetCygxYanxuanSpecialMessagerDetailById(messageId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } if messagerDetail.Status == -1 { resp.IsDelete = true } } var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) var condition string var pars []interface{} userId := user.UserId if messageType == 1 { condition += ` AND status = 0 ` } else { condition += ` AND status = 1 ` } condition += ` AND yanxuan_special_user_id = ? ` pars = append(pars, userId) conditionChildren := condition // 子集留言筛选条件 condition += ` AND parent_id = 0 ` total, err := models.GetCygxYanxuanSpecialMessagerCount(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } condition += ` ORDER BY message_id DESC ` list, err := models.GetCygxYanxuanSpecialMessageList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } if len(list) > 0 { var messageIds []int var userIds []int for _, v := range list { messageIds = append(messageIds, v.MessageId) userIds = append(userIds, v.UserId) } //获取子集评论 conditionChildren += ` AND parent_id IN (` + utils.GetOrmInReplace(len(messageIds)) + `) ORDER BY message_id ASC ` pars = append(pars, messageIds) listChild, err := models.GetCygxYanxuanSpecialMessageList(conditionChildren, pars, 0, 999) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } itemChildMap := make(map[int][]*models.CygxYanxuanSpecialMessageManageChildResp) for _, v := range listChild { userIds = append(userIds, v.UserId) itemChild := new(models.CygxYanxuanSpecialMessageManageChildResp) itemChild.UserId = v.UserId itemChild.MessageId = v.MessageId itemChild.Content = v.Content itemChild.CreateTime = v.CreateTime.Format(utils.FormatDateTime) itemChildMap[v.ParentId] = append(itemChildMap[v.ParentId], itemChild) } //用户头像 listUser, err := models.GetWxUserListByUserIdsArr(userIds) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } mapUserImg := make(map[int]string) for _, v := range listUser { if v.Headimgurl != "" { mapUserImg[v.UserId] = v.Headimgurl } else { mapUserImg[v.UserId] = utils.DefaultHeadimgurl } } for _, v := range userIds { if mapUserImg[v] == "" { mapUserImg[v] = utils.DefaultHeadimgurl } } for _, v := range list { item := new(models.CygxYanxuanSpecialMessageManageResp) item.MessageId = v.MessageId item.RealName = v.RealName item.Headimgurl = mapUserImg[v.UserId] item.YanxuanSpecialId = v.YanxuanSpecialId item.Content = v.Content item.TopTime = v.TopTime item.SourceTitle = v.SourceTitle item.CreateTime = v.CreateTime.Format(utils.FormatDateTime) if len(itemChildMap[v.MessageId]) > 0 { for _, vm := range itemChildMap[v.MessageId] { vm.Headimgurl = mapUserImg[vm.UserId] } item.ChildList = itemChildMap[v.MessageId] } else { item.ChildList = make([]*models.CygxYanxuanSpecialMessageManageChildResp, 0) } item.CheckIds = make([]int, 0) resp.List = append(resp.List, item) } } else { resp.List = make([]*models.CygxYanxuanSpecialMessageManageResp, 0) } page := paging.GetPaging(currentIndex, pageSize, total) resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @Title 删除留言 // @Description 删除留言接口 // @Param request body models.DeleteCygxYanxuanSpecialMessageReq true "type json string" // @Success Ret=200 {object} // @router /message/delete [post] func (this *YanxuanSpecialMessageController) MessageDelete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req models.DeleteCygxYanxuanSpecialMessageReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } messageId := req.MessageId err = models.DeleteCygxYanxuanSpecialMessage(messageId) if err != nil { br.Msg = "删除失败!" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除成功" } // @Title 置顶/取消置顶留言 // @Description 置顶/取消置顶留言接口 // @Param request body models.TopCygxYanxuanSpecialMessageReq true "type json string" // @Success Ret=200 {object} // @router /message/top [post] func (this *YanxuanSpecialMessageController) MessageTop() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req models.TopCygxYanxuanSpecialMessageReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } messageId := req.MessageId doType := req.DoType messagerDetail, err := models.GetCygxYanxuanSpecialMessagerDetailById(messageId) if err != nil { br.Msg = "操作失败!" br.ErrMsg = "操作失败,Err:" + err.Error() } yanxuanSpecialId := messagerDetail.YanxuanSpecialId var topTime int var msgContent string if doType == 1 { countTop, err := models.GetCygxYanxuanSpecialMessagerCountTop(yanxuanSpecialId) if err != nil { br.Msg = "操作失败!" br.ErrMsg = "操作失败,Err:" + err.Error() return } if countTop > 0 { br.Msg = "此文章已有置顶留言" return } topTime = int(time.Now().Unix()) msgContent = "已置顶" } else { msgContent = "已取消置顶" } err = models.UpdateCygxYanxuanSpecialMessageTopTime(topTime, messageId) if err != nil { br.Msg = "操作失败!" br.ErrMsg = "操作失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = msgContent } // @Title 公开/取消公开留言 // @Description 公开/取消公开留言接口 // @Param request body models.TopCygxYanxuanSpecialMessageReq true "type json string" // @Success Ret=200 {object} // @router /message/public [post] func (this *YanxuanSpecialMessageController) MessagePublic() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req models.PubliceCygxYanxuanSpecialMessageReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } messageIds := req.MessageIds doType := req.DoType var status int var publicTime string var msgContent string if doType == 1 { status = 1 publicTime = time.Now().Format(utils.FormatDateTime) msgContent = "已公开" } else { msgContent = "已取消公开" } err = models.UpdateCygxYanxuanSpecialMessageStatus(status, publicTime, messageIds) if err != nil { br.Msg = "操作失败!" br.ErrMsg = "操作失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = msgContent } // @Title 点赞/取消点赞 // @Description 点赞/取消点赞接口 // @Param request body models.TopCygxYanxuanSpecialMessageReq true "type json string" // @Success Ret=200 {object} // @router /message/like [post] func (this *YanxuanSpecialMessageController) MessageLike() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } var req models.TopCygxYanxuanSpecialMessageReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } messageId := req.MessageId doType := req.DoType userId := user.UserId total, err := models.GetCygxYanxuanSpecialMessageLikeCountByUidMid(userId, messageId) if err != nil { br.Msg = "操作失败!" br.ErrMsg = "操作失败,Err:" + err.Error() return } var msgContent string if doType == 1 { if total == 0 { item := new(models.CygxYanxuanSpecialMessageLike) item.UserId = userId item.Mobile = user.Mobile item.Email = user.Email item.CompanyId = user.CompanyId item.CompanyName = user.CompanyName item.RealName = user.RealName item.MessageId = messageId item.CreateTime = time.Now() item.ModifyTime = time.Now() item.RegisterPlatform = utils.REGISTER_PLATFORM err = models.AddCygxYanxuanSpecialMessageLike(item) if err != nil { br.Msg = "点赞失败!" br.ErrMsg = "点赞失败,Err:" + err.Error() return } } msgContent = "已点赞" } else { if total > 0 { err = models.DeleteCygxYanxuanSpecialMessageLike(userId, messageId) if err != nil { br.Msg = "点赞失败!" br.ErrMsg = "点赞失败,Err:" + err.Error() return } } msgContent = "已取消点赞" } br.Ret = 200 br.Success = true br.Msg = msgContent } // @Title 专栏文章留言列表 // @Description 专栏文章留言列表接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param YanxuanSpecialId query int false "研选专栏文章ID" // @Success 200 {object} models.SpecialListResp // @router /message/special/list [get] func (this *YanxuanSpecialMessageController) MessageSpecialList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") yanxuanSpecialId, _ := this.GetInt("YanxuanSpecialId") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) userId := user.UserId resp := new(models.YanxuanSpecialMessageManageRespListResp) publiccTotal, err := models.GetCygxYanxuanSpecialMessagerPublicCount(yanxuanSpecialId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } resp.PublicMessageTotal = publiccTotal var condition string var pars []interface{} var conditionMyself string var parsMyself []interface{} conditionMyself += ` AND status = 0 AND parent_id = 0 AND user_id = ? AND yanxuan_special_id = ? ORDER BY message_id DESC ` parsMyself = append(parsMyself, userId, yanxuanSpecialId) var listMyself []*models.CygxYanxuanSpecialMessage if currentIndex == 1 { //获取自己未公开的留言 listMyself, err = models.GetCygxYanxuanSpecialMessageList(conditionMyself, parsMyself, 0, 999) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } } condition += ` AND status = 1 ` condition += ` AND yanxuan_special_id = ? ` pars = append(pars, yanxuanSpecialId) conditionChildren := condition // 子集留言筛选条件 condition += ` AND parent_id = 0 ` total, err := models.GetCygxYanxuanSpecialMessagerCount(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } condition += ` ORDER BY top_time DESC , message_id DESC ` list, err := models.GetCygxYanxuanSpecialMessageList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } for _, v := range list { listMyself = append(listMyself, v) } if len(listMyself) > 0 { var messageIds []int var messageIdsAll []int // 所有的留言消息 var userIds []int for _, v := range list { messageIds = append(messageIds, v.MessageId) } for _, v := range listMyself { userIds = append(userIds, v.UserId) } itemChildMap := make(map[int][]*models.CygxYanxuanSpecialMessageManageChildResp) if len(messageIds) > 0 { //获取子集评论 conditionChildren += ` AND parent_id IN (` + utils.GetOrmInReplace(len(messageIds)) + `) ORDER BY message_id ASC ` pars = append(pars, messageIds) listChild, err := models.GetCygxYanxuanSpecialMessageList(conditionChildren, pars, 0, 999) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } for _, v := range listChild { userIds = append(userIds, v.UserId) messageIdsAll = append(messageIdsAll, v.MessageId) itemChild := new(models.CygxYanxuanSpecialMessageManageChildResp) itemChild.UserId = v.UserId itemChild.MessageId = v.MessageId itemChild.Content = v.Content itemChild.LikeCount = v.LikeCount itemChild.CreateTime = v.CreateTime.Format(utils.FormatDateTime) itemChildMap[v.ParentId] = append(itemChildMap[v.ParentId], itemChild) } } likeMap := services.GetYanxuanSpecialMessageLikeMap(userId) //用户头像 listUser, err := models.GetWxUserListByUserIdsArr(userIds) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } mapUserImg := make(map[int]string) for _, v := range listUser { if v.Headimgurl != "" { mapUserImg[v.UserId] = v.Headimgurl } else { mapUserImg[v.UserId] = utils.DefaultHeadimgurl } } for _, v := range listMyself { item := new(models.CygxYanxuanSpecialMessageManageResp) item.MessageId = v.MessageId item.RealName = v.RealName item.Headimgurl = mapUserImg[v.UserId] item.YanxuanSpecialId = v.YanxuanSpecialId item.Content = v.Content item.TopTime = v.TopTime item.SourceTitle = v.SourceTitle item.LikeCount = v.LikeCount item.IsLikeCount = likeMap[v.MessageId] item.Status = v.Status if v.UserId == userId { item.IsMySelf = true } item.CreateTime = v.CreateTime.Format(utils.FormatDateTime) if len(itemChildMap[v.MessageId]) > 0 { for _, vm := range itemChildMap[v.MessageId] { vm.IsLikeCount = likeMap[vm.MessageId] vm.Headimgurl = mapUserImg[vm.UserId] } item.ChildList = itemChildMap[v.MessageId] } else { item.ChildList = make([]*models.CygxYanxuanSpecialMessageManageChildResp, 0) } item.CheckIds = make([]int, 0) resp.List = append(resp.List, item) } } else { resp.List = make([]*models.CygxYanxuanSpecialMessageManageResp, 0) } page := paging.GetPaging(currentIndex, pageSize, total) resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @Title 通过模版消息查看留言详情 // @Description 通过模版消息查看留言详情接口 // @Param MessageId query int true "留言ID" // @Success 200 {object} models.SpecialListResp // @router /message/detail/wxtmp [get] func (this *YanxuanSpecialMessageController) MessageDetailWxtmp() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } messageId, _ := this.GetInt("MessageId") messagerDetail, err := models.GetCygxYanxuanSpecialMessagerDetailById(messageId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, Err:" + err.Error() return } resp := new(models.MessageDetailWxtmpResp) if messagerDetail.Status == -1 { resp.IsDelete = true } else { resp.Content = messagerDetail.Content } resp.YanxuanSpecialId = messagerDetail.YanxuanSpecialId br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" }