plat_followup.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package plat
  2. import (
  3. "context"
  4. "dashoo.cn/micro/app/dao/plat"
  5. model "dashoo.cn/micro/app/model/plat"
  6. "dashoo.cn/micro/app/service"
  7. "database/sql"
  8. "github.com/gogf/gf/errors/gerror"
  9. "github.com/gogf/gf/frame/g"
  10. "github.com/gogf/gf/os/gtime"
  11. "github.com/gogf/gf/util/gconv"
  12. "strconv"
  13. "strings"
  14. )
  15. type followupService struct {
  16. *service.ContextService
  17. Dao *plat.PlatFollowupDao
  18. }
  19. func NewFollowupService(ctx context.Context) (svc *followupService, err error) {
  20. svc = new(followupService)
  21. if svc.ContextService, err = svc.Init(ctx); err != nil {
  22. return nil, err
  23. }
  24. svc.Dao = plat.NewPlatFollowupDao(svc.Tenant)
  25. return svc, nil
  26. }
  27. // 跟进记录信息列表
  28. func (s *followupService) GetList(req *model.SearchPlatFollowupReq) (total int, followupList []*model.PlatFollowup, err error) {
  29. followupModel := s.Dao.M
  30. if req.CustId != "" {
  31. followupModel = followupModel.Where("cust_id", req.CustId)
  32. }
  33. if req.TargetId != "" {
  34. followupModel = followupModel.Where("target_id", req.TargetId)
  35. }
  36. if req.TargetType != "" {
  37. followupModel = followupModel.Where("target_type", req.TargetType)
  38. }
  39. // 负责人查询
  40. if req.ManagerId != "" {
  41. followupModel = followupModel.Where("created_by", req.ManagerId)
  42. }
  43. total, err = followupModel.Count()
  44. if err != nil {
  45. g.Log().Error(err)
  46. err = gerror.New("获取总行数失败")
  47. return
  48. }
  49. err = followupModel.Page(req.PageNum, req.PageSize).Order("follow_date DESC").Scan(&followupList)
  50. return
  51. }
  52. // 添加信息
  53. func (s *followupService) Create(req *model.AddPlatFollowupReq) (err error) {
  54. platFollowup := new(model.PlatFollowup)
  55. var files []*model.PlatFollowupFile
  56. if err = gconv.Struct(req, platFollowup); err != nil {
  57. return
  58. }
  59. // 填充创建信息
  60. service.SetCreatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
  61. // 填充更新信息
  62. //service.SetUpdatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
  63. Model := s.Dao.M
  64. res, err := Model.Insert(platFollowup)
  65. if err != nil {
  66. return
  67. }
  68. // 更新附件数据
  69. id, _ := res.LastInsertId()
  70. for _, file := range req.Files {
  71. var fileData model.PlatFollowupFile
  72. if err = gconv.Struct(file, &fileData); err != nil {
  73. return
  74. }
  75. fileData.FollowId = strconv.Itoa(int(id))
  76. // 填充创建信息
  77. service.SetCreatedInfo(&fileData, s.GetCxtUserId(), s.GetCxtUserName())
  78. // 填充更新信息
  79. //service.SetUpdatedInfo(fileData, s.GetCxtUserId(), s.GetCxtUserName())
  80. files = append(files, &fileData)
  81. }
  82. // 保存附件信息
  83. if len(files) > 0 {
  84. _, err = s.Dao.DB.Insert(plat.PlatFollowupFile.Table, files)
  85. }
  86. return
  87. }
  88. // 跟进记录信息列表:按照日期显示,并附带评论
  89. func (s *followupService) GetListByDay(req *model.SearchPlatFollowupReq) (total int, followupList []*model.FollowupInfoResp, err error) {
  90. followupModel := s.Dao.M
  91. if req.CustId != "" {
  92. followupModel = followupModel.Where("cust_id", req.CustId)
  93. }
  94. if req.TargetId != "" {
  95. followupModel = followupModel.Where("target_id", req.TargetId)
  96. }
  97. if req.TargetType != "" {
  98. followupModel = followupModel.Where("target_type", req.TargetType)
  99. }
  100. // 负责人查询
  101. if req.ManagerId != "" {
  102. followupModel = followupModel.Where("created_by", req.ManagerId)
  103. }
  104. // 日期条件
  105. if req.DaysBeforeToday >= 0 { // 获取前N天的跟进记录
  106. now := gtime.Now()
  107. begin := now.AddDate(0, 0, -req.DaysBeforeToday).Format("Y-m-d 00:00:00")
  108. followupModel = followupModel.Where("follow_date>=?", begin)
  109. }
  110. // 获取日期区间范围内的记录
  111. if req.BeginTime != "" && req.EndTime != "" {
  112. begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
  113. end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
  114. followupModel = followupModel.Where("follow_date>=? AND follow_date<=?", begin, end)
  115. }
  116. //total, err = followupModel.Count()
  117. //if err != nil {
  118. // g.Log().Error(err)
  119. // err = gerror.New("获取总行数失败")
  120. // return
  121. //}
  122. // 查询原始记录
  123. var originalFollowupList []model.FollowupInfo
  124. err = followupModel.Order("follow_date DESC").Scan(&originalFollowupList)
  125. if err != nil && err != sql.ErrNoRows {
  126. return
  127. }
  128. // 查询一级评论
  129. var comments []model.PlatFollowupComment
  130. err = s.Dao.InnerJoin(plat.PlatFollowupComment.Table, "plat_followup.id=plat_followup_comment.follow_id").Where("plat_followup_comment.pid=0 OR plat_followup_comment.pid IS NULL").Fields("plat_followup_comment.*").Structs(&comments)
  131. if err != nil && err != sql.ErrNoRows {
  132. return
  133. }
  134. // 构造数据
  135. var days []string
  136. followupMap := make(map[string][]*model.FollowupInfo, 0)
  137. commentMap := make(map[int][]*model.PlatFollowupComment, 0)
  138. // 评论数据map
  139. for index, comment := range comments {
  140. if _, ok := commentMap[comment.FollowId]; !ok {
  141. commentMap[comment.FollowId] = make([]*model.PlatFollowupComment, 0)
  142. }
  143. commentMap[comment.FollowId] = append(commentMap[comment.FollowId], &comments[index])
  144. }
  145. // 跟进记录map
  146. for index, followup := range originalFollowupList {
  147. if _, ok := followupMap[followup.FollowDate.Format("Y-m-d")]; !ok {
  148. days = append(days, followup.FollowDate.Format("Y-m-d"))
  149. followupMap[followup.FollowDate.Format("Y-m-d")] = make([]*model.FollowupInfo, 0)
  150. }
  151. if followup.Comments == nil {
  152. originalFollowupList[index].Comments = make([]*model.PlatFollowupComment, 0)
  153. }
  154. // 为跟进记录填充评论数据
  155. if _, ok := commentMap[followup.Id]; ok {
  156. originalFollowupList[index].Comments = append(originalFollowupList[index].Comments, commentMap[followup.Id]...)
  157. }
  158. originalFollowupList[index].CommentNumber = len(originalFollowupList[index].Comments)
  159. followupMap[followup.FollowDate.Format("Y-m-d")] = append(followupMap[followup.FollowDate.Format("Y-m-d")], &originalFollowupList[index])
  160. }
  161. for _, day := range days {
  162. var followup model.FollowupInfoResp
  163. followup.FollowDay = day
  164. followup.FollowupList = followupMap[day]
  165. followupList = append(followupList, &followup)
  166. }
  167. return
  168. }