plat_followup.go 6.6 KB

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