plat_followup.go 6.2 KB

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