plat_followup.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package plat
  2. import (
  3. "context"
  4. projdao "dashoo.cn/micro/app/dao/proj"
  5. "database/sql"
  6. "github.com/gogf/gf/container/garray"
  7. "strconv"
  8. "strings"
  9. "dashoo.cn/opms_libary/myerrors"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/util/gconv"
  13. "dashoo.cn/micro/app/dao/plat"
  14. model "dashoo.cn/micro/app/model/plat"
  15. "dashoo.cn/micro/app/service"
  16. )
  17. type followupService struct {
  18. *service.ContextService
  19. Dao *plat.PlatFollowupDao
  20. }
  21. func NewFollowupService(ctx context.Context) (svc *followupService, err error) {
  22. svc = new(followupService)
  23. if svc.ContextService, err = svc.Init(ctx); err != nil {
  24. return nil, err
  25. }
  26. svc.Dao = plat.NewPlatFollowupDao(svc.Tenant)
  27. return svc, nil
  28. }
  29. // 跟进记录信息列表
  30. func (s *followupService) GetList(req *model.SearchPlatFollowupReq) (total int, followupList []*model.PlatFollowup, err error) {
  31. followupModel := s.Dao.DataScope(s.Ctx)
  32. if req.CustId != "" {
  33. followupModel = followupModel.Where("cust_id", req.CustId)
  34. }
  35. if req.CustName != "" {
  36. followupModel = followupModel.WhereLike("cust_name", "%"+req.CustName+"%")
  37. }
  38. if req.TargetId != "" {
  39. followupModel = followupModel.Where("target_id", req.TargetId)
  40. }
  41. if req.TargetType != "" {
  42. followupModel = followupModel.Where("target_type", req.TargetType)
  43. }
  44. if req.TargetName != "" {
  45. followupModel = followupModel.WhereLike("target_name", "%"+req.TargetName+"%")
  46. }
  47. // 负责人查询
  48. if req.ManagerId != "" {
  49. followupModel = followupModel.Where("created_by", req.ManagerId)
  50. }
  51. if req.IsMyself == "1" {
  52. followupModel = followupModel.Where("created_by", s.GetCxtUserId())
  53. }
  54. total, err = followupModel.Count()
  55. if err != nil {
  56. g.Log().Error(err)
  57. err = myerrors.DbError("获取总行数失败。")
  58. return
  59. }
  60. err = followupModel.Page(req.GetPage()).Order("follow_date DESC").Scan(&followupList)
  61. return
  62. }
  63. // 添加信息
  64. func (s *followupService) Create(req *model.AddPlatFollowupReq) (err error) {
  65. platFollowup := new(model.PlatFollowup)
  66. var files []*model.PlatFollowupFile
  67. if err = gconv.Struct(req, platFollowup); err != nil {
  68. return
  69. }
  70. // 填充创建信息
  71. service.SetCreatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
  72. // 填充更新信息
  73. //service.SetUpdatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
  74. res, err := s.Dao.Insert(platFollowup)
  75. if err != nil {
  76. return
  77. }
  78. // 更新附件数据
  79. id, _ := res.LastInsertId()
  80. for _, file := range req.Files {
  81. var fileData model.PlatFollowupFile
  82. if err = gconv.Struct(file, &fileData); err != nil {
  83. return
  84. }
  85. fileData.FollowId = strconv.Itoa(int(id))
  86. // 填充创建信息
  87. service.SetCreatedInfo(&fileData, s.GetCxtUserId(), s.GetCxtUserName())
  88. // 填充更新信息
  89. //service.SetUpdatedInfo(fileData, s.GetCxtUserId(), s.GetCxtUserName())
  90. files = append(files, &fileData)
  91. }
  92. // 保存附件信息
  93. if len(files) > 0 {
  94. _, err = s.Dao.DB.Insert(plat.PlatFollowupFile.Table, files)
  95. if err != nil {
  96. return
  97. }
  98. }
  99. // 更新客户 最后跟进时间 字段
  100. toUpdate := map[string]interface{}{
  101. "follow_up_date": req.FollowDate,
  102. "follow_up_man": s.GetCxtUserName(),
  103. }
  104. _, err = s.Dao.DB.Update("cust_customer", toUpdate, "id = ?", req.CustId)
  105. if platFollowup.TargetType == "20" {
  106. // 更新客户 最后跟进时间 字段
  107. toUpdate := map[string]interface{}{
  108. projdao.ProjBusiness.C.FinalFollowTime: req.FollowDate,
  109. projdao.ProjBusiness.C.FinalFollowId: s.GetCxtUserId(),
  110. projdao.ProjBusiness.C.FinalFollowName: s.GetCxtUserName(),
  111. }
  112. _, err = s.Dao.DB.Update(projdao.ProjBusiness.Table, toUpdate, "id = ?", req.TargetId)
  113. }
  114. return
  115. }
  116. // 跟进记录信息列表:按照日期显示,并附带评论
  117. func (s *followupService) GetListByDay(req *model.SearchPlatFollowupReq) (total int, followupList []*model.FollowupInfoResp, err error) {
  118. followupModel := s.Dao.M
  119. // 用户仅有销售工程师角色展示自己的数据,其他人可以看到所有数据
  120. if garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") {
  121. followupModel = followupModel.WhereIn("created_by", s.DataScope["userIds"])
  122. }
  123. if req.CustId != "" {
  124. followupModel = followupModel.Where("cust_id", req.CustId)
  125. }
  126. if req.CustName != "" {
  127. followupModel = followupModel.WhereLike("cust_name", "%"+req.CustName+"%")
  128. }
  129. if req.TargetId != "" {
  130. followupModel = followupModel.Where("target_id", req.TargetId)
  131. }
  132. if req.TargetType != "" {
  133. followupModel = followupModel.Where("target_type", req.TargetType)
  134. }
  135. if req.TargetName != "" {
  136. followupModel = followupModel.WhereLike("target_name", "%"+req.TargetName+"%")
  137. }
  138. if req.CreatedName != "" {
  139. followupModel = followupModel.WhereLike("created_name", "%"+req.CreatedName+"%")
  140. }
  141. // 负责人查询
  142. if req.ManagerId != "" {
  143. followupModel = followupModel.Where("created_by", req.ManagerId)
  144. }
  145. if req.IsMyself == "1" {
  146. followupModel = followupModel.Where("created_by", s.GetCxtUserId())
  147. }
  148. // 日期条件
  149. if req.DaysBeforeToday > 0 { // 获取前N天的跟进记录
  150. now := gtime.Now()
  151. begin := now.AddDate(0, 0, -req.DaysBeforeToday).Format("Y-m-d 00:00:00")
  152. followupModel = followupModel.Where("follow_date>=?", begin)
  153. }
  154. // 获取日期区间范围内的记录
  155. if req.BeginTime != "" && req.EndTime != "" {
  156. begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
  157. end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
  158. followupModel = followupModel.Where("follow_date>=? AND follow_date<=?", begin, end)
  159. }
  160. //total, err = followupModel.Count()
  161. //if err != nil {
  162. // g.Log().Error(err)
  163. // err = gerror.New("获取总行数失败")
  164. // return
  165. //}
  166. // 查询原始记录
  167. var originalFollowupList []model.FollowupInfo
  168. err = followupModel.Order("follow_date DESC").Scan(&originalFollowupList)
  169. if err != nil && err != sql.ErrNoRows {
  170. return
  171. }
  172. // 查询一级评论
  173. var comments []model.PlatFollowupComment
  174. 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)
  175. if err != nil && err != sql.ErrNoRows {
  176. return
  177. }
  178. // 构造数据
  179. var days []string
  180. followupMap := make(map[string][]*model.FollowupInfo, 0)
  181. commentMap := make(map[int][]*model.PlatFollowupComment, 0)
  182. // 评论数据map
  183. for index, comment := range comments {
  184. if _, ok := commentMap[comment.FollowId]; !ok {
  185. commentMap[comment.FollowId] = make([]*model.PlatFollowupComment, 0)
  186. }
  187. commentMap[comment.FollowId] = append(commentMap[comment.FollowId], &comments[index])
  188. }
  189. // 跟进记录map
  190. for index, followup := range originalFollowupList {
  191. if _, ok := followupMap[followup.FollowDate.Format("Y-m-d")]; !ok {
  192. days = append(days, followup.FollowDate.Format("Y-m-d"))
  193. followupMap[followup.FollowDate.Format("Y-m-d")] = make([]*model.FollowupInfo, 0)
  194. }
  195. if followup.Comments == nil {
  196. originalFollowupList[index].Comments = make([]*model.PlatFollowupComment, 0)
  197. }
  198. // 为跟进记录填充评论数据
  199. if _, ok := commentMap[followup.Id]; ok {
  200. originalFollowupList[index].Comments = append(originalFollowupList[index].Comments, commentMap[followup.Id]...)
  201. }
  202. originalFollowupList[index].CommentNumber = len(originalFollowupList[index].Comments)
  203. followupMap[followup.FollowDate.Format("Y-m-d")] = append(followupMap[followup.FollowDate.Format("Y-m-d")], &originalFollowupList[index])
  204. }
  205. for _, day := range days {
  206. var followup model.FollowupInfoResp
  207. followup.FollowDay = day
  208. followup.FollowupList = followupMap[day]
  209. followupList = append(followupList, &followup)
  210. }
  211. return
  212. }