plat_followup.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.PageNum, req.PageSize).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. Model := s.Dao.M
  65. res, err := Model.Insert(platFollowup)
  66. if err != nil {
  67. return
  68. }
  69. // 更新附件数据
  70. id, _ := res.LastInsertId()
  71. for _, file := range req.Files {
  72. var fileData model.PlatFollowupFile
  73. if err = gconv.Struct(file, &fileData); err != nil {
  74. return
  75. }
  76. fileData.FollowId = strconv.Itoa(int(id))
  77. // 填充创建信息
  78. service.SetCreatedInfo(&fileData, s.GetCxtUserId(), s.GetCxtUserName())
  79. // 填充更新信息
  80. //service.SetUpdatedInfo(fileData, s.GetCxtUserId(), s.GetCxtUserName())
  81. files = append(files, &fileData)
  82. }
  83. // 保存附件信息
  84. if len(files) > 0 {
  85. _, err = s.Dao.DB.Insert(plat.PlatFollowupFile.Table, files)
  86. if err != nil {
  87. return
  88. }
  89. }
  90. // 更新客户 最后跟进时间 字段
  91. _, 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")))
  92. return
  93. }
  94. // 跟进记录信息列表:按照日期显示,并附带评论
  95. func (s *followupService) GetListByDay(req *model.SearchPlatFollowupReq) (total int, followupList []*model.FollowupInfoResp, err error) {
  96. followupModel := s.Dao.M
  97. if req.CustId != "" {
  98. followupModel = followupModel.Where("cust_id", req.CustId)
  99. }
  100. if req.TargetId != "" {
  101. followupModel = followupModel.Where("target_id", req.TargetId)
  102. }
  103. if req.TargetType != "" {
  104. followupModel = followupModel.Where("target_type", req.TargetType)
  105. }
  106. // 负责人查询
  107. if req.ManagerId != "" {
  108. followupModel = followupModel.Where("created_by", req.ManagerId)
  109. }
  110. // 日期条件
  111. if req.DaysBeforeToday >= 0 { // 获取前N天的跟进记录
  112. now := gtime.Now()
  113. begin := now.AddDate(0, 0, -req.DaysBeforeToday).Format("Y-m-d 00:00:00")
  114. followupModel = followupModel.Where("follow_date>=?", begin)
  115. }
  116. // 获取日期区间范围内的记录
  117. if req.BeginTime != "" && req.EndTime != "" {
  118. begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
  119. end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
  120. followupModel = followupModel.Where("follow_date>=? AND follow_date<=?", begin, end)
  121. }
  122. //total, err = followupModel.Count()
  123. //if err != nil {
  124. // g.Log().Error(err)
  125. // err = gerror.New("获取总行数失败")
  126. // return
  127. //}
  128. // 查询原始记录
  129. var originalFollowupList []model.FollowupInfo
  130. err = followupModel.Order("follow_date DESC").Scan(&originalFollowupList)
  131. if err != nil && err != sql.ErrNoRows {
  132. return
  133. }
  134. // 查询一级评论
  135. var comments []model.PlatFollowupComment
  136. 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)
  137. if err != nil && err != sql.ErrNoRows {
  138. return
  139. }
  140. // 构造数据
  141. var days []string
  142. followupMap := make(map[string][]*model.FollowupInfo, 0)
  143. commentMap := make(map[int][]*model.PlatFollowupComment, 0)
  144. // 评论数据map
  145. for index, comment := range comments {
  146. if _, ok := commentMap[comment.FollowId]; !ok {
  147. commentMap[comment.FollowId] = make([]*model.PlatFollowupComment, 0)
  148. }
  149. commentMap[comment.FollowId] = append(commentMap[comment.FollowId], &comments[index])
  150. }
  151. // 跟进记录map
  152. for index, followup := range originalFollowupList {
  153. if _, ok := followupMap[followup.FollowDate.Format("Y-m-d")]; !ok {
  154. days = append(days, followup.FollowDate.Format("Y-m-d"))
  155. followupMap[followup.FollowDate.Format("Y-m-d")] = make([]*model.FollowupInfo, 0)
  156. }
  157. if followup.Comments == nil {
  158. originalFollowupList[index].Comments = make([]*model.PlatFollowupComment, 0)
  159. }
  160. // 为跟进记录填充评论数据
  161. if _, ok := commentMap[followup.Id]; ok {
  162. originalFollowupList[index].Comments = append(originalFollowupList[index].Comments, commentMap[followup.Id]...)
  163. }
  164. originalFollowupList[index].CommentNumber = len(originalFollowupList[index].Comments)
  165. followupMap[followup.FollowDate.Format("Y-m-d")] = append(followupMap[followup.FollowDate.Format("Y-m-d")], &originalFollowupList[index])
  166. }
  167. for _, day := range days {
  168. var followup model.FollowupInfoResp
  169. followup.FollowDay = day
  170. followup.FollowupList = followupMap[day]
  171. followupList = append(followupList, &followup)
  172. }
  173. return
  174. }