plat_followup.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "fmt"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/util/gconv"
  13. "strconv"
  14. "strings"
  15. )
  16. type followupService struct {
  17. *service.ContextService
  18. Dao *plat.PlatFollowupDao
  19. }
  20. func NewFollowupService(ctx context.Context) (svc *followupService, err error) {
  21. svc = new(followupService)
  22. if svc.ContextService, err = svc.Init(ctx); err != nil {
  23. return nil, err
  24. }
  25. svc.Dao = plat.NewPlatFollowupDao(svc.Tenant)
  26. return svc, nil
  27. }
  28. // 跟进记录信息列表
  29. func (s *followupService) GetList(req *model.SearchPlatFollowupReq) (total int, followupList []*model.PlatFollowup, err error) {
  30. followupModel := s.Dao.M
  31. if req.CustId != "" {
  32. followupModel = followupModel.Where("cust_id", req.CustId)
  33. }
  34. if req.TargetId != "" {
  35. followupModel = followupModel.Where("target_id", req.TargetId)
  36. }
  37. if req.TargetType != "" {
  38. followupModel = followupModel.Where("target_type", req.TargetType)
  39. }
  40. // 负责人查询
  41. if req.ManagerId != "" {
  42. followupModel = followupModel.Where("created_by", req.ManagerId)
  43. }
  44. total, err = followupModel.Count()
  45. if err != nil {
  46. g.Log().Error(err)
  47. err = gerror.New("获取总行数失败")
  48. return
  49. }
  50. err = followupModel.Page(req.GetPage()).Order("follow_date DESC").Scan(&followupList)
  51. return
  52. }
  53. // 添加信息
  54. func (s *followupService) Create(req *model.AddPlatFollowupReq) (err error) {
  55. platFollowup := new(model.PlatFollowup)
  56. var files []*model.PlatFollowupFile
  57. if err = gconv.Struct(req, platFollowup); err != nil {
  58. return
  59. }
  60. // 填充创建信息
  61. service.SetCreatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
  62. // 填充更新信息
  63. //service.SetUpdatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
  64. res, err := s.Dao.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. if err != nil {
  86. return
  87. }
  88. }
  89. // 更新客户 最后跟进时间 字段
  90. _, err = s.Dao.DB.Update("cust_customer", fmt.Sprintf("follow_up_date='%v'", req.FollowDate.Format("Y-m-d H:i:s")), fmt.Sprintf("id='%v' AND follow_up_date<'%v'", req.CustId, req.FollowDate.Format("Y-m-d H:i:s")))
  91. return
  92. }
  93. // 跟进记录信息列表:按照日期显示,并附带评论
  94. func (s *followupService) GetListByDay(req *model.SearchPlatFollowupReq) (total int, followupList []*model.FollowupInfoResp, err error) {
  95. followupModel := s.Dao.M
  96. if req.CustId != "" {
  97. followupModel = followupModel.Where("cust_id", req.CustId)
  98. }
  99. if req.TargetId != "" {
  100. followupModel = followupModel.Where("target_id", req.TargetId)
  101. }
  102. if req.TargetType != "" {
  103. followupModel = followupModel.Where("target_type", req.TargetType)
  104. }
  105. // 负责人查询
  106. if req.ManagerId != "" {
  107. followupModel = followupModel.Where("created_by", req.ManagerId)
  108. }
  109. // 日期条件
  110. if req.DaysBeforeToday >= 0 { // 获取前N天的跟进记录
  111. now := gtime.Now()
  112. begin := now.AddDate(0, 0, -req.DaysBeforeToday).Format("Y-m-d 00:00:00")
  113. followupModel = followupModel.Where("follow_date>=?", begin)
  114. }
  115. // 获取日期区间范围内的记录
  116. if req.BeginTime != "" && req.EndTime != "" {
  117. begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
  118. end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
  119. followupModel = followupModel.Where("follow_date>=? AND follow_date<=?", begin, end)
  120. }
  121. //total, err = followupModel.Count()
  122. //if err != nil {
  123. // g.Log().Error(err)
  124. // err = gerror.New("获取总行数失败")
  125. // return
  126. //}
  127. // 查询原始记录
  128. var originalFollowupList []model.FollowupInfo
  129. err = followupModel.Order("follow_date DESC").Scan(&originalFollowupList)
  130. if err != nil && err != sql.ErrNoRows {
  131. return
  132. }
  133. // 查询一级评论
  134. var comments []model.PlatFollowupComment
  135. 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)
  136. if err != nil && err != sql.ErrNoRows {
  137. return
  138. }
  139. // 构造数据
  140. var days []string
  141. followupMap := make(map[string][]*model.FollowupInfo, 0)
  142. commentMap := make(map[int][]*model.PlatFollowupComment, 0)
  143. // 评论数据map
  144. for index, comment := range comments {
  145. if _, ok := commentMap[comment.FollowId]; !ok {
  146. commentMap[comment.FollowId] = make([]*model.PlatFollowupComment, 0)
  147. }
  148. commentMap[comment.FollowId] = append(commentMap[comment.FollowId], &comments[index])
  149. }
  150. // 跟进记录map
  151. for index, followup := range originalFollowupList {
  152. if _, ok := followupMap[followup.FollowDate.Format("Y-m-d")]; !ok {
  153. days = append(days, followup.FollowDate.Format("Y-m-d"))
  154. followupMap[followup.FollowDate.Format("Y-m-d")] = make([]*model.FollowupInfo, 0)
  155. }
  156. if followup.Comments == nil {
  157. originalFollowupList[index].Comments = make([]*model.PlatFollowupComment, 0)
  158. }
  159. // 为跟进记录填充评论数据
  160. if _, ok := commentMap[followup.Id]; ok {
  161. originalFollowupList[index].Comments = append(originalFollowupList[index].Comments, commentMap[followup.Id]...)
  162. }
  163. originalFollowupList[index].CommentNumber = len(originalFollowupList[index].Comments)
  164. followupMap[followup.FollowDate.Format("Y-m-d")] = append(followupMap[followup.FollowDate.Format("Y-m-d")], &originalFollowupList[index])
  165. }
  166. for _, day := range days {
  167. var followup model.FollowupInfoResp
  168. followup.FollowDay = day
  169. followup.FollowupList = followupMap[day]
  170. followupList = append(followupList, &followup)
  171. }
  172. return
  173. }