comment.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package yb
  2. import (
  3. "errors"
  4. "hongze/hz_crm_api/models"
  5. "hongze/hz_crm_api/models/company"
  6. "hongze/hz_crm_api/models/system"
  7. ybModels "hongze/hz_crm_api/models/yb"
  8. "hongze/hz_crm_api/models/yb/request"
  9. ybResp "hongze/hz_crm_api/models/yb/response"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 查询当前操作用户是否留言板管理员权限
  16. func CheckCommentPermission(adminId int) (brMsg, brErrMsg string) {
  17. // 查询留言管理员的角色ID
  18. roleInfo, err := system.GetSysRoleByRoleTypeCode("yb_comment_admin")
  19. if err != nil {
  20. if err.Error() == utils.ErrNoRow() {
  21. brMsg = "该角色不存在!"
  22. brErrMsg = "该角色不存在,Err:" + err.Error()
  23. return
  24. } else {
  25. brMsg = "查询角色信息失败!"
  26. brErrMsg = "查询角色信息失败,Err:" + err.Error()
  27. return
  28. }
  29. }
  30. _, err = system.GetRoleIdsByAdminIdRoleId(adminId, roleInfo.RoleId)
  31. if err != nil {
  32. brMsg = "无权操作"
  33. brErrMsg = "无权操作,Err:" + err.Error()
  34. return
  35. }
  36. return
  37. }
  38. // GetProductComments 获取板块里的列表
  39. func GetProductComments(adminId int, productType, keyWord, createDate string, startSize, pageSize int) (list []*ybResp.ProductCommentItemResp, total int64, err error, errMsg string) {
  40. condition := ""
  41. pars := make([]interface{}, 0)
  42. if productType != "" {
  43. condition += ` AND t.product_type = ? `
  44. pars = append(pars, productType)
  45. }
  46. if keyWord != "" {
  47. likeKey := `%` + keyWord + `%`
  48. condition += ` AND t.content like ? `
  49. pars = append(pars, likeKey)
  50. }
  51. if createDate != "" {
  52. createTime, _ := time.Parse(utils.FormatDate, createDate)
  53. nextTime := createTime.AddDate(0, 0, 1)
  54. condition += " and t.create_time >= ? and t.create_time < ?"
  55. pars = append(pars, createTime, nextTime)
  56. }
  57. // 获取数据列表
  58. var tmpList []*ybModels.ProductCommentItem
  59. tmpList, total, err = ybModels.GetProductCommentList(condition, pars, startSize, pageSize)
  60. if err != nil {
  61. errMsg = "获取评论列表失败,Err:" + err.Error()
  62. err = errors.New("获取评论列表失败!")
  63. return
  64. }
  65. var reportCommentIds string
  66. var reportIds string
  67. var userIds string
  68. questionIds := make([]int, 0)
  69. videoIds := make([]int, 0)
  70. roadVideoIds := make([]int, 0)
  71. for _, v := range tmpList {
  72. if v.ProductType == 1 {
  73. // 报告
  74. reportCommentIds += "," + strconv.Itoa(v.CommentId)
  75. if v.ReportId > 0 {
  76. reportIds += "," + strconv.Itoa(v.ReportId)
  77. }
  78. } else if v.ProductType == 2 {
  79. // 问答社区
  80. if v.CommunityQuestionId > 0 {
  81. questionIds = append(questionIds, v.CommunityQuestionId)
  82. }
  83. } else if v.ProductType == 3 {
  84. // 视频社区
  85. if v.CommunityQuestionId > 0 {
  86. videoIds = append(videoIds, v.CommunityQuestionId)
  87. }
  88. } else if v.ProductType == 4 {
  89. // 路演视频
  90. if v.CommunityQuestionId > 0 {
  91. roadVideoIds = append(roadVideoIds, v.CommunityQuestionId)
  92. }
  93. }
  94. userIds += "," + strconv.Itoa(v.UserId)
  95. }
  96. // 查询所有的回复列表
  97. replyListMap := make(map[int][]*ybResp.ReplyItem)
  98. if reportCommentIds != "" {
  99. reportCommentIds = "(" + strings.Trim(reportCommentIds, ",") + ")"
  100. replyList, tErr := ybModels.GetReplyByReplyCommentIds(reportCommentIds)
  101. if tErr != nil {
  102. errMsg = "获取回复列表失败,Err:" + tErr.Error()
  103. err = errors.New("获取回复列表失败")
  104. return
  105. }
  106. for _, v := range replyList {
  107. t := new(ybResp.ReplyItem)
  108. t.CommentId = v.CommentId
  109. t.Content = v.Content
  110. t.AdminId = v.AdminId
  111. t.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  112. t.AdminName = "admin"
  113. t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
  114. t.ReplyCommentId = v.ReplyCommentId
  115. replyListMap[int(v.ReplyCommentId)] = append(replyListMap[int(v.ReplyCommentId)], t)
  116. }
  117. }
  118. // 查询报告标题,带日期
  119. reportMap := make(map[int]*models.ReportDetail)
  120. if reportIds != "" {
  121. reportIds = "(" + strings.Trim(reportIds, ",") + ")"
  122. reportList, tErr := models.GetReportByIds(reportIds)
  123. if tErr != nil {
  124. errMsg = "获取报告列表失败,Err:" + tErr.Error()
  125. err = errors.New("获取报告列表失败")
  126. return
  127. }
  128. for _, v := range reportList {
  129. reportMap[v.Id] = v
  130. }
  131. }
  132. // 查询问答内容
  133. questionMap := make(map[int]*ybModels.CommunityQuestion)
  134. if len(questionIds) > 0 {
  135. questionList, tErr := ybModels.GetQuestionByIds(questionIds)
  136. if tErr != nil {
  137. errMsg = "获取问答列表失败,Err:" + tErr.Error()
  138. err = errors.New("获取问答列表失败")
  139. return
  140. }
  141. for _, v := range questionList {
  142. questionMap[v.CommunityQuestionId] = v
  143. }
  144. }
  145. // 查询视频内容
  146. videoMap := make(map[int]*ybModels.CommunityVideo)
  147. if len(videoIds) > 0 {
  148. vdList, e := ybModels.GetCommunityVideoListByIds(videoIds)
  149. if e != nil {
  150. errMsg = "获取视频列表失败, Err:" + e.Error()
  151. err = errors.New("获取视频列表失败")
  152. return
  153. }
  154. for _, v := range vdList {
  155. videoMap[v.CommunityVideoId] = v
  156. }
  157. }
  158. // 查询路演视频内容
  159. roadVideoMap := make(map[int]*ybModels.RoadVideo)
  160. if len(roadVideoIds) > 0 {
  161. rvList, e := ybModels.GetRoadVideoListByIds(roadVideoIds)
  162. if e != nil {
  163. errMsg = "获取路演视频列表失败, Err:" + e.Error()
  164. err = errors.New("获取路演视频列表失败")
  165. return
  166. }
  167. for _, v := range rvList {
  168. roadVideoMap[v.RoadVideoId] = v
  169. }
  170. }
  171. // 查询联系人相关信息
  172. var userOthers []*models.WxUser
  173. if len(userIds) > 0 {
  174. userOthers, err = models.GetWxUserListByUserIds(strings.Trim(userIds, ","))
  175. if err != nil {
  176. errMsg = "查询用户出错,Err:" + err.Error()
  177. err = errors.New("查询用户出错")
  178. return
  179. }
  180. }
  181. //查询客户相关信息
  182. usersMap := make(map[int]*models.WxUser)
  183. companyIdMap := make(map[int]int)
  184. companyIdList := make([]int, 0)
  185. for _, v := range userOthers {
  186. usersMap[int(v.UserId)] = v
  187. if _, ok := companyIdMap[v.CompanyId]; !ok { //避免company_id重复append
  188. companyIdList = append(companyIdList, v.CompanyId)
  189. companyIdMap[v.CompanyId] = v.CompanyId
  190. }
  191. }
  192. // 查询相关用户的公司
  193. companyProductMap := make(map[int]*company.CompanyProduct)
  194. {
  195. if len(companyIdList) > 0 {
  196. var companyProductOthers []*company.CompanyProduct
  197. companyProductOthers, err = company.GetCompanyProductsByCompanyIdsAndProductId(companyIdList, 1)
  198. if err != nil {
  199. errMsg = "查询客户出错,Err:" + err.Error()
  200. err = errors.New("查询客户出错")
  201. return
  202. }
  203. for _, v := range companyProductOthers {
  204. companyProductMap[v.CompanyId] = v
  205. }
  206. }
  207. }
  208. // 将留言评论全部设置成已读
  209. err = ybModels.UpdateCommentSysIsRead(adminId)
  210. if err != nil {
  211. errMsg = "更新已读出错,Err:" + err.Error()
  212. err = errors.New("更新已读出错")
  213. return
  214. }
  215. list = make([]*ybResp.ProductCommentItemResp, 0)
  216. for _, v := range tmpList {
  217. tmp := new(ybResp.ProductCommentItemResp)
  218. tmp.UserId = v.UserId
  219. tmp.ReportId = v.ReportId
  220. tmp.ReportChapterId = v.ReportChapterId
  221. tmp.CommunityQuestionId = v.CommunityQuestionId
  222. tmp.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  223. if v.ProductType == 1 {
  224. tmp.ProductName = "研报"
  225. if v.ReportId > 0 {
  226. if rep, ok := reportMap[v.ReportId]; ok {
  227. if rep.VideoName == "" {
  228. tt, _ := time.ParseInLocation(utils.FormatDateTime, rep.PublishTime, time.Local)
  229. ttStr := tt.Format(utils.FormatMonthDayUnSpace)
  230. tmp.Title = rep.Title + `(` + ttStr + `)`
  231. } else {
  232. tmp.Title = rep.VideoName
  233. }
  234. }
  235. }
  236. } else if v.ProductType == 2 {
  237. tmp.ProductName = "问答社区"
  238. if que, ok := questionMap[v.CommunityQuestionId]; ok {
  239. tmp.Title = que.QuestionContent
  240. }
  241. } else if v.ProductType == 3 {
  242. tmp.ProductName = "视频社区"
  243. if vd, ok := videoMap[v.CommunityQuestionId]; ok {
  244. tmp.Title = vd.Title
  245. }
  246. } else if v.ProductType == 4 {
  247. tmp.ProductName = "线上路演"
  248. if rv, ok := roadVideoMap[v.CommunityQuestionId]; ok {
  249. tmp.Title = rv.Title
  250. }
  251. }
  252. tmp.Content = v.Content
  253. tmp.ProductType = v.ProductType
  254. tmp.RealName = v.RealName
  255. tmp.CommentId = v.CommentId
  256. tmp.IsTop = v.IsTop
  257. if existList, ok := replyListMap[v.CommentId]; ok && v.ProductType == 1 {
  258. tmp.ReplyList = existList
  259. }
  260. if info, ok := usersMap[v.UserId]; ok {
  261. tmp.RealName = info.RealName
  262. //客户信息
  263. if companyProductInfo, ok1 := companyProductMap[info.CompanyId]; ok1 {
  264. tmp.CompanyId = companyProductInfo.CompanyId
  265. tmp.CompanyName = companyProductInfo.CompanyName
  266. tmp.CompanyStatus = companyProductInfo.Status
  267. tmp.SellerName = companyProductInfo.SellerName
  268. }
  269. }
  270. list = append(list, tmp)
  271. }
  272. return
  273. }
  274. // DelProductComments 删除板块合辑里的留言
  275. func DelProductComments(req request.CommentProductDelReq) (err error, errMsg string) {
  276. //查询评论状态,
  277. if req.ProductType == 1 {
  278. commentInfo, tErr := ybModels.GetCommentByCommentId(req.CommentId)
  279. if tErr != nil {
  280. if tErr.Error() == utils.ErrNoRow() {
  281. errMsg = "留言不存在或者已删除"
  282. err = errors.New("留言不存在或者已删除")
  283. return
  284. }
  285. errMsg = "查询留言失败!,Err:" + tErr.Error()
  286. err = errors.New("查询留言失败!")
  287. return
  288. }
  289. if commentInfo.Type == 1 { //删除留言
  290. err = ybModels.DeleteComment(req.CommentId)
  291. if err != nil {
  292. errMsg = "删除留言操作失败!,Err:" + err.Error()
  293. err = errors.New("删除留言操作失败!")
  294. return
  295. }
  296. } else { // 删除回复
  297. err = ybModels.DeleteCommentReply(req.CommentId)
  298. if err != nil {
  299. errMsg = "删除回复操作失败!,Err:" + err.Error()
  300. err = errors.New("删除回复操作失败!")
  301. return
  302. }
  303. }
  304. } else { //删除问答社区/视频社区/线上路演评论
  305. err, errMsg = DeleteQuestionComment(req.CommentId)
  306. return
  307. }
  308. return
  309. }