yb_community_question_comment.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package yb_community_question
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/company_approval_message"
  8. "hongze/hongze_yb/models/tables/wx_user"
  9. "hongze/hongze_yb/models/tables/yb_comment_anonymous_user"
  10. "hongze/hongze_yb/models/tables/yb_community_question_comment"
  11. "hongze/hongze_yb/services/user"
  12. "hongze/hongze_yb/services/wx_app"
  13. "hongze/hongze_yb/utils"
  14. "strconv"
  15. "time"
  16. )
  17. // Comment 发布留言
  18. func Comment(user user.UserInfo, communityQuestionID uint32, content string, sourceAgent, isShowName int8) (ybCommunityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment, err error, errMsg string) {
  19. errMsg = "发布留言失败"
  20. defer func() {
  21. if err != nil {
  22. global.LOG.Critical(fmt.Sprintf("yb_community_question Comment: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  23. }
  24. }()
  25. //校验请求入参
  26. if isShowName != 0 && isShowName != 1 {
  27. errMsg = "匿名设置出错"
  28. err = errors.New(errMsg)
  29. return
  30. }
  31. if content == "" {
  32. errMsg = "请输入留言内容"
  33. err = errors.New(errMsg)
  34. return
  35. }
  36. if sourceAgent == 0 {
  37. errMsg = "请输入留言来源"
  38. err = errors.New(errMsg)
  39. return
  40. }
  41. if communityQuestionID <= 0 {
  42. errMsg = "请输入问答ID"
  43. err = errors.New(errMsg)
  44. return
  45. }
  46. // 敏感词过滤
  47. if user.RecordInfo.OpenID != "" && user.RecordInfo.CreatePlatform == 6 { //只有小程序的用户才能走敏感词过滤接口
  48. checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, content)
  49. /*if tErr != nil {
  50. errMsg = "敏感词过滤失败" + tErr.Error()
  51. err = errors.New("敏感词过滤失败")
  52. return
  53. }*/
  54. if tErr == nil {
  55. if checkResult.Result != nil {
  56. if checkResult.Result.Suggest != "pass" {
  57. errMsg = "含有违禁词,不允许发布:" + checkResult.Result.Suggest + ".命中标签:" + strconv.Itoa(checkResult.Result.Label)
  58. err = errors.New("含有违禁词,不允许发布。")
  59. return
  60. }
  61. }
  62. }
  63. }
  64. //新增留言
  65. now := time.Now()
  66. ybCommunityQuestionComment = &yb_community_question_comment.YbCommunityQuestionComment{
  67. //CommunityQuestionCommentID: 0,
  68. CommunityQuestionID: communityQuestionID,
  69. UserID: user.UserID,
  70. Content: content,
  71. //ReplyCommentID: 0,
  72. //IsTop: 0,
  73. //IsHot: 0,
  74. //HotTopTime: time.Time{},
  75. Type: 1,
  76. Enabled: 1,
  77. IsShowName: isShowName,
  78. SourceAgent: sourceAgent,
  79. //TopTime: time.Time{},
  80. //HotTime: time.Time{},
  81. ModifyTime: now,
  82. CreateTime: now,
  83. }
  84. err = ybCommunityQuestionComment.Create()
  85. if err != nil {
  86. errMsg = "新增留言失败"
  87. return
  88. }
  89. // TODO 给管理员发送模板消息
  90. return
  91. }
  92. // Delete 删除留言
  93. func Delete(user user.UserInfo, communityQuestionCommentID uint64) (err error, errMsg string) {
  94. errMsg = `删除留言失败`
  95. defer func() {
  96. if err != nil {
  97. global.LOG.Critical(fmt.Sprintf("comment Delete: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  98. }
  99. }()
  100. if communityQuestionCommentID <= 0 {
  101. errMsg = `请输入留言ID`
  102. err = errors.New(errMsg)
  103. return
  104. }
  105. //校验请求入参
  106. communityQuestionCommentInfo, err := yb_community_question_comment.GetByCommunityQuestionCommentId(communityQuestionCommentID)
  107. if err != nil {
  108. errMsg = `查询留言出错`
  109. return
  110. }
  111. if communityQuestionCommentInfo.CommunityQuestionCommentID <= 0 {
  112. errMsg = `留言不存在`
  113. err = errors.New(errMsg)
  114. return
  115. }
  116. if communityQuestionCommentInfo.UserID != user.UserID {
  117. errMsg = `不允许删除他人的留言`
  118. err = errors.New(errMsg)
  119. return
  120. }
  121. if communityQuestionCommentInfo.Type != 1 {
  122. errMsg = `不允许删除回复`
  123. err = errors.New(errMsg)
  124. return
  125. }
  126. if communityQuestionCommentInfo.Enabled == 0 {
  127. return
  128. }
  129. err = yb_community_question_comment.Delete(user.UserID, communityQuestionCommentInfo.CommunityQuestionCommentID)
  130. if err != nil {
  131. errMsg = `删除留言出错`
  132. return
  133. }
  134. return
  135. }
  136. // MyList 我的留言列表
  137. func MyList(userId uint64, communityQuestionID int, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, total int64, err error, errMsg string) {
  138. list, total, err, errMsg = List(userId, communityQuestionID, false, page, pageSize)
  139. if err != nil {
  140. return
  141. }
  142. return
  143. }
  144. // List 查询精选留言列表或我的留言列表
  145. func List(userId uint64, communityQuestionID int, hotFlag bool, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, total int64, err error, errMsg string) {
  146. defer func() {
  147. if err != nil {
  148. global.LOG.Critical(fmt.Sprintf("comment List: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
  149. }
  150. }()
  151. list = make([]*response.RespCommunityQuestionCommentItem, 0)
  152. if communityQuestionID <= 0 {
  153. err = errors.New("请输入问答ID")
  154. return
  155. }
  156. var commentList []*yb_community_question_comment.YbCommunityQuestionComment
  157. //查询精选留言
  158. if hotFlag {
  159. commentList, err = yb_community_question_comment.GetHotListByCommunityQuestionID(communityQuestionID, (page-1)*pageSize, pageSize)
  160. if err != nil {
  161. errMsg = `查询精选留言列表出错`
  162. return
  163. }
  164. total, err = yb_community_question_comment.GetHotListTotalByCommunityQuestionID(communityQuestionID)
  165. if err != nil {
  166. errMsg = `查询精选留言总数出错`
  167. return
  168. }
  169. } else {
  170. //查询个人留言
  171. commentList, err = yb_community_question_comment.GetListByUserIdCommunityQuestionID(userId, communityQuestionID)
  172. if err != nil {
  173. errMsg = `查询我的留言列表出错`
  174. return
  175. }
  176. total, err = yb_community_question_comment.GetListTotalByUserIdCommunityQuestionID(userId, communityQuestionID)
  177. if err != nil {
  178. errMsg = `查询我的留言总数出错`
  179. return
  180. }
  181. }
  182. var commentIds []uint64
  183. var userIds []uint64
  184. for _, v := range commentList {
  185. commentIds = append(commentIds, v.CommunityQuestionCommentID)
  186. userIds = append(userIds, v.UserID)
  187. }
  188. // 查询所有的回复列表
  189. //{
  190. // replyListMap := make(map[uint64][]*response.ReplyItem)
  191. // if len(commentIds) > 0 {
  192. // replyList, tErr := yb_community_question_comment.GetReplyListByReplyCommentId(commentIds)
  193. // if tErr != nil {
  194. // errMsg = tErr.Error()
  195. // err = errors.New("查询回复出错")
  196. // return
  197. // }
  198. // for _, v := range replyList {
  199. // t := new(response.ReplyItem)
  200. // t.CommentId = v.CommunityQuestionCommentID
  201. // t.Content = v.Content
  202. // t.AdminId = v.AdminID
  203. // t.CreateTime = v.CreateTime
  204. // t.ReplyCommentId = v.ReplyCommentId
  205. // t.AdminName = "弘则研究"
  206. // t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
  207. // replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
  208. // }
  209. // }
  210. //}
  211. // 查询精选留言相关的用户
  212. var userOthers []*wx_user.WxUser
  213. if len(userIds) > 0 {
  214. userOthers, err = wx_user.GetByUserIds(userIds)
  215. if err != nil {
  216. errMsg = err.Error()
  217. err = errors.New("查询精选留言用户出错")
  218. return
  219. }
  220. }
  221. usersMap := make(map[uint64]*wx_user.WxUser)
  222. for _, v := range userOthers {
  223. usersMap[v.UserID] = v
  224. }
  225. for _, v := range commentList {
  226. tmp := &response.RespCommunityQuestionCommentItem{
  227. CommunityQuestionCommentID: v.CommunityQuestionCommentID,
  228. UserId: v.UserID,
  229. Content: v.Content,
  230. IsTop: v.IsTop,
  231. IsHot: v.IsHot,
  232. HotTopTime: v.HotTopTime,
  233. IsShowName: v.IsShowName,
  234. UserName: "匿名用户" + strconv.Itoa(int(3333+v.UserID)),
  235. UserImgUrl: utils.DEFAULT_HONGZE_USER_LOGO,
  236. CreateTime: v.CreateTime,
  237. ReplyList: nil,
  238. }
  239. if info, ok := usersMap[v.UserID]; ok && v.IsShowName == 1 {
  240. tmp.UserName = info.NickName
  241. tmp.UserImgUrl = info.Headimgurl
  242. }
  243. //if existList, ok := replyListMap[v.CommentId]; ok {
  244. // tmp.ReplyList = existList
  245. //}
  246. list = append(list, tmp)
  247. }
  248. return
  249. }
  250. // GetNeedCommentAnonymousUserTips 获取是否 弹出让去设置头像 的提示框
  251. func GetNeedCommentAnonymousUserTips(userId uint64) (ok bool, err error) {
  252. ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
  253. if err != nil {
  254. return
  255. }
  256. //如果找不到,那么认为是第一次评论,那么需要弹框提示
  257. if ybCommentAnonymousUser.UserID <= 0 {
  258. ok = true
  259. }
  260. return
  261. }
  262. // SetYbCommentAnonymousUserTips 设置不再提示 弹出让去设置头像 的提示框
  263. func SetYbCommentAnonymousUserTips(userId uint64) (err error) {
  264. ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
  265. if err != nil {
  266. return
  267. }
  268. //如果找不到,那么认为是第一次评论,那么需要弹框提示
  269. if ybCommentAnonymousUser.UserID <= 0 {
  270. //ybCommentAnonymousUser = &yb_comment_anonymous_user.YbCommentAnonymousUser{
  271. // UserID: userId,
  272. // CreateTime: time.Now(),
  273. //}
  274. //err = ybCommentAnonymousUser.Create()
  275. err = yb_comment_anonymous_user.CreateBySql(userId, time.Now())
  276. }
  277. return
  278. }
  279. // HandleCommentByCommunityQuestionItemList 问答 评论 数据
  280. func HandleCommentByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
  281. listLen := len(questionList)
  282. if listLen == 0 {
  283. return
  284. }
  285. idArr := make([]uint32, 0)
  286. idMap := make(map[uint32]int)
  287. userIds := []uint64{userId} //所有的评论人
  288. for i := 0; i < listLen; i++ {
  289. idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
  290. idMap[uint32(questionList[i].CommunityQuestionID)] = 1
  291. }
  292. //精选评论数据、我的评论数据
  293. ybCommunityQuestionCommentMap := make(map[uint32]*yb_community_question_comment.YbCommunityQuestionComment)
  294. //获取精选评论数据
  295. hotList, err := yb_community_question_comment.GetLastHotListByCommunityQuestionIds(idArr)
  296. if err != nil {
  297. return
  298. }
  299. for _, v := range hotList {
  300. ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
  301. delete(idMap, v.CommunityQuestionID)
  302. //评论人id
  303. userIds = append(userIds, v.UserID)
  304. }
  305. myIdArr := make([]uint32, 0)
  306. for id := range idMap {
  307. myIdArr = append(myIdArr, id)
  308. }
  309. //获取我的评论数据
  310. myList, err := yb_community_question_comment.GetLastMyListByCommunityQuestionIds(userId, idArr)
  311. if err != nil {
  312. return
  313. }
  314. for _, v := range myList {
  315. ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
  316. }
  317. // 获取留言汇总数
  318. numCommentMap := make(map[uint32]int)
  319. numCommentList, err := yb_community_question_comment.GetNumCommentByCommunityQuestionIds(idArr)
  320. if err != nil {
  321. return
  322. }
  323. for _, v := range numCommentList {
  324. numCommentMap[v.CommunityQuestionID] = v.Total
  325. }
  326. var userOthers []*wx_user.WxUser
  327. if len(userIds) > 0 {
  328. userOthers, err = wx_user.GetByUserIds(userIds)
  329. if err != nil {
  330. err = errors.New("查询精选留言用户出错")
  331. return
  332. }
  333. }
  334. usersMap := make(map[uint64]*wx_user.WxUser)
  335. for _, v := range userOthers {
  336. usersMap[v.UserID] = v
  337. }
  338. for _, v := range questionList {
  339. //评论汇总数
  340. if tmpTotal, ok := numCommentMap[uint32(v.CommunityQuestionID)]; ok {
  341. v.CommentTotal = tmpTotal
  342. }
  343. //最近一条 精选/我的 评论
  344. if ybCommunityQuestionCommentInfo, ok := ybCommunityQuestionCommentMap[uint32(v.CommunityQuestionID)]; ok {
  345. v.Comment = ybCommunityQuestionCommentInfo.Content
  346. v.CommentUserName = "匿名用户" + strconv.Itoa(int(3333+ybCommunityQuestionCommentInfo.UserID))
  347. if info, ok := usersMap[ybCommunityQuestionCommentInfo.UserID]; ok && ybCommunityQuestionCommentInfo.IsShowName == 1 {
  348. v.CommentUserName = info.NickName
  349. //v.UserImgUrl = info.Headimgurl
  350. }
  351. }
  352. }
  353. return
  354. }
  355. // AddCompanyApprovalMessage 添加系统消息
  356. func AddCompanyApprovalMessage(createUserId, receiveUserId, companyId, companyApprovalId int, msgType, sourceType, approvalStatus int8, companyName, remark, content, mobile string) (err error) {
  357. msgItem := &company_approval_message.CompanyApprovalMessage{
  358. //ID: 0,
  359. CreateUserID: createUserId,
  360. ReceiveUserID: receiveUserId,
  361. MessageStatus: 0, //消息状态:0未读,1:已读,2:作废
  362. Remark: remark,
  363. Content: content,
  364. CompanyID: companyId,
  365. CompanyName: companyName,
  366. CreateTime: time.Now(),
  367. ModifyTime: time.Now(),
  368. CompanyApprovalID: companyApprovalId,
  369. ApprovalStatus: approvalStatus, //审批状态,1:待审批,2:已审批,3:已驳回
  370. OperationStatus: 1, //消息状态:1:待审批,2:已审批
  371. MessageType: msgType, //1:申请消息,2:审批结果,3:消息通知
  372. SourceType: sourceType, //消息来源,1:客户,2:合同,3:用印
  373. }
  374. err = msgItem.Create()
  375. return
  376. }