plat_followup.go 6.0 KB

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