plat_followup_comment.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package plat
  2. import (
  3. "context"
  4. "dashoo.cn/opms_libary/myerrors"
  5. "database/sql"
  6. "fmt"
  7. "github.com/gogf/gf/frame/g"
  8. "github.com/gogf/gf/util/gconv"
  9. "strings"
  10. "dashoo.cn/micro/app/dao/plat"
  11. model "dashoo.cn/micro/app/model/plat"
  12. "dashoo.cn/micro/app/service"
  13. )
  14. type followupCommentService struct {
  15. *service.ContextService
  16. Dao *plat.PlatFollowupCommentDao
  17. FollowupDao *plat.PlatFollowupDao
  18. }
  19. func NewFollowupCommentService(ctx context.Context) (svc *followupCommentService, err error) {
  20. svc = new(followupCommentService)
  21. if svc.ContextService, err = svc.Init(ctx); err != nil {
  22. return nil, err
  23. }
  24. svc.Dao = plat.NewPlatFollowupCommentDao(svc.Tenant)
  25. svc.FollowupDao = plat.NewPlatFollowupDao(svc.Tenant)
  26. return svc, nil
  27. }
  28. // 跟进记录留言信息列表
  29. func (s *followupCommentService) GetList(req *model.SearchPlatFollowupCommentReq) (total int, followupCommentList []*model.FollowupCommentEx, err error) {
  30. followupCommentModel := s.Dao.M
  31. if req.FollowId != "" {
  32. followupCommentModel = followupCommentModel.Where("plat_followup_comment.follow_id", req.FollowId)
  33. }
  34. if req.Pid != "" {
  35. followupCommentModel = followupCommentModel.Where("plat_followup_comment.pid", req.Pid)
  36. }
  37. total, err = followupCommentModel.Count()
  38. if err != nil {
  39. g.Log().Error(err)
  40. err = myerrors.DbError("获取总行数失败。")
  41. return
  42. }
  43. err = followupCommentModel.Order("plat_followup_comment.created_time DESC").Scan(&followupCommentList)
  44. if err != nil && err != sql.ErrNoRows {
  45. g.Log().Error(err)
  46. err = myerrors.DbError("获取数据失败。")
  47. return
  48. }
  49. // 获取回复数据
  50. var replyComments []*model.FollowupCommentEx
  51. err = followupCommentModel.InnerJoin("plat_followup_comment b", "b.pid=plat_followup_comment.id").Fields("b.*").Order("b.id ASC").Scan(&replyComments)
  52. if err != nil && err != sql.ErrNoRows {
  53. g.Log().Error(err)
  54. err = myerrors.DbError("获取数据失败。")
  55. return
  56. }
  57. commentMap := make(map[int]*model.FollowupCommentEx)
  58. for index, comment := range followupCommentList {
  59. commentMap[comment.Id] = followupCommentList[index]
  60. }
  61. // 构造回复数据
  62. for _, comment := range replyComments {
  63. if commentMap[comment.Pid].ReplyComments == nil {
  64. commentMap[comment.Pid].ReplyComments = make([]*model.FollowupCommentEx, 0)
  65. }
  66. commentMap[comment.Pid].ReplyComments = append(commentMap[comment.Pid].ReplyComments, comment)
  67. }
  68. return
  69. }
  70. // NeedReplyList 本人需要回复的评论列表
  71. func (s *followupCommentService) NeedReplyList(req *model.SearchNeedReplyCommentsReq) (total int, followupCommentList []*model.PlatFollowupComment, err error) {
  72. followupModel := s.FollowupDao.As("a").M.InnerJoin(plat.PlatFollowupComment.Table+" b", "a.id=b.follow_id").LeftJoin(plat.PlatFollowupComment.Table+" reply", "b.id=reply.pid")
  73. // 本人创建的评论
  74. followupModel = followupModel.Where("a."+s.Dao.C.CreatedBy, s.GetCxtUserId())
  75. // 第一级的 未回复的 评论
  76. followupModel = followupModel.Where("(b.pid=0 OR b.pid IS NULL) AND reply.id IS NULL")
  77. // 分组去重
  78. followupModel = followupModel.Group("b.id")
  79. total, err = followupModel.Count()
  80. if err != nil {
  81. g.Log().Error(err)
  82. err = myerrors.DbError("获取总行数失败。")
  83. return
  84. }
  85. err = followupModel.Order("b.created_time ASC").Fields("b.*").Scan(&followupCommentList)
  86. return
  87. }
  88. // NeedReplyCount 本人需要回复的评论数量
  89. func (s *followupCommentService) NeedReplyCount(req *model.SearchNeedReplyCommentsReq) (total int, err error) {
  90. followupModel := s.FollowupDao.As("a").M.InnerJoin(plat.PlatFollowupComment.Table+" b", "a.id=b.follow_id").LeftJoin(plat.PlatFollowupComment.Table+" reply", "b.id=reply.pid")
  91. // 本人创建的评论
  92. followupModel = followupModel.Where("a."+s.Dao.C.CreatedBy, s.GetCxtUserId())
  93. // 第一级的 未回复的 评论
  94. followupModel = followupModel.Where("(b.pid=0 OR b.pid IS NULL) AND reply.id IS NULL")
  95. // 分组去重
  96. followupModel = followupModel.Group("b.id")
  97. total, err = followupModel.Count()
  98. if err != nil {
  99. g.Log().Error(err)
  100. err = myerrors.DbError("获取总行数失败。")
  101. return
  102. }
  103. return
  104. }
  105. // LatestComments 获取最新50条数据
  106. func (s *followupCommentService) LatestComments(req *model.SearchPlatFollowupCommentReq) (total int, followupCommentList []*model.FollowupCommentEx2, err error) {
  107. // 默认50条数据
  108. if req.PageSize == 0 {
  109. req.PageSize = 50
  110. }
  111. if req.PageNum == 0 {
  112. req.PageNum = 1
  113. }
  114. followupCommentModel := s.Dao.M.InnerJoin("plat_followup c", "plat_followup_comment.follow_id=c.id")
  115. // 只查询第一层评论
  116. followupCommentModel = followupCommentModel.Where("plat_followup_comment.pid=0 OR plat_followup_comment.pid IS NULL")
  117. err = followupCommentModel.Order("plat_followup_comment.created_time DESC").Page(req.GetPage()).Fields("plat_followup_comment.*,c.target_type,c.target_name").Scan(&followupCommentList)
  118. if err != nil && err != sql.ErrNoRows {
  119. g.Log().Error(err)
  120. err = myerrors.DbError("获取数据失败。")
  121. return
  122. }
  123. // 获取回复数据
  124. var replyComments []*model.FollowupCommentEx2
  125. err = followupCommentModel.InnerJoin("plat_followup_comment b", "b.pid=plat_followup_comment.id").Fields("b.*,c.target_type,c.target_name").Order("b.id ASC").Scan(&replyComments)
  126. if err != nil && err != sql.ErrNoRows {
  127. g.Log().Error(err)
  128. err = myerrors.DbError("获取数据失败。")
  129. return
  130. }
  131. commentMap := make(map[int]*model.FollowupCommentEx2)
  132. for index, comment := range followupCommentList {
  133. commentMap[comment.Id] = followupCommentList[index]
  134. }
  135. // 构造回复数据
  136. for _, comment := range replyComments {
  137. if commentMap[comment.Pid].ReplyComments == nil {
  138. commentMap[comment.Pid].ReplyComments = make([]*model.FollowupCommentEx2, 0)
  139. }
  140. commentMap[comment.Pid].ReplyComments = append(commentMap[comment.Pid].ReplyComments, comment)
  141. }
  142. return
  143. }
  144. // 添加评论
  145. func (s *followupCommentService) Create(req *model.AddPlatFollowupCommentReq) (err error) {
  146. followup, err := s.FollowupDao.WherePri(req.FollowId).One()
  147. if err != nil {
  148. return err
  149. }
  150. platFollowupComment := new(model.PlatFollowupComment)
  151. if err = gconv.Struct(req, platFollowupComment); err != nil {
  152. return
  153. }
  154. // 填充创建信息
  155. service.SetCreatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName())
  156. // 填充更新信息
  157. //service.SetUpdatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName())
  158. _, err = s.Dao.Insert(platFollowupComment)
  159. if err != nil {
  160. return
  161. }
  162. // 从配置中获取消息提醒设置 销售总监、销售助理
  163. configs, err := g.DB(s.Tenant).Model("sys_config").Where("config_key IN ('SalesDirector','SalesAssociate')").FindAll()
  164. if err != nil && err != sql.ErrNoRows {
  165. g.Log().Error(err)
  166. return
  167. }
  168. var recvUserIds []string
  169. for _, config := range configs {
  170. recvUserIds = append(recvUserIds, strings.Split(config["config_value"].String(), ",")...)
  171. }
  172. recvUserIds = append(recvUserIds, gconv.String(followup.CreatedBy))
  173. msg := g.MapStrStr{
  174. "msgTitle": "跟进记录评论提醒",
  175. "msgContent": fmt.Sprintf("%v %v 评论:%v", followup.TargetName, platFollowupComment.CreatedName, platFollowupComment.Content),
  176. "msgType": "20",
  177. "recvUserIds": strings.Join(recvUserIds, ","),
  178. "msgStatus": "10",
  179. "sendType": "30",
  180. }
  181. if err := service.CreateSystemMessage(msg); err != nil {
  182. g.Log().Error("消息提醒异常:", err)
  183. }
  184. return
  185. }
  186. // Reply 回复评论
  187. func (s *followupCommentService) Reply(req *model.AddPlatFollowupCommentReq) (err error) {
  188. platFollowupComment := new(model.PlatFollowupComment)
  189. if err = gconv.Struct(req, platFollowupComment); err != nil {
  190. return
  191. }
  192. // 填充创建信息
  193. service.SetCreatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName())
  194. // 填充更新信息
  195. //service.SetUpdatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName())
  196. _, err = s.Dao.Insert(platFollowupComment)
  197. if err != nil {
  198. return
  199. }
  200. return
  201. }