plat_followup.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package plat
  2. import (
  3. "context"
  4. "dashoo.cn/micro/app/dao/cust"
  5. projdao "dashoo.cn/micro/app/dao/proj"
  6. "dashoo.cn/opms_libary/myerrors"
  7. "database/sql"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gtime"
  10. "github.com/gogf/gf/util/gconv"
  11. "strconv"
  12. "strings"
  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. filter := map[string]interface{}{}
  119. if req.TargetType == "20" && req.TargetId != "" {
  120. filter = map[string]interface{}{
  121. "target_id": req.TargetId,
  122. "orcols": "target_id",
  123. }
  124. } else if req.CustId != "" {
  125. filter = map[string]interface{}{
  126. "cust_id": req.CustId,
  127. "orcols": "cust_id",
  128. }
  129. } else {
  130. if s.DataScope["userIds"] != "-1" {
  131. vals, _ := cust.NewCustCustomerDao(s.Tenant).DataScope(s.Ctx, "sales_id").Where("is_public", "20").Fields("id").Array()
  132. filter = map[string]interface{}{
  133. "cust_id": vals,
  134. "orcols": "cust_id",
  135. }
  136. }
  137. }
  138. followupModel := s.Dao.DataScope(s.Ctx, filter)
  139. // 用户仅有销售工程师角色展示自己的数据,其他人可以看到所有数据
  140. //if garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") {
  141. // followupModel = followupModel.WhereIn("created_by", s.DataScope["userIds"])
  142. //}
  143. if req.CustId != "" {
  144. followupModel = followupModel.Where("cust_id", req.CustId)
  145. }
  146. if req.CustName != "" {
  147. followupModel = followupModel.WhereLike("cust_name", "%"+req.CustName+"%")
  148. }
  149. if req.TargetId != "" {
  150. followupModel = followupModel.Where("target_id", req.TargetId)
  151. }
  152. if req.TargetType != "" {
  153. followupModel = followupModel.Where("target_type", req.TargetType)
  154. }
  155. if req.TargetName != "" {
  156. followupModel = followupModel.WhereLike("target_name", "%"+req.TargetName+"%")
  157. }
  158. if req.CreatedName != "" {
  159. followupModel = followupModel.WhereLike("created_name", "%"+req.CreatedName+"%")
  160. }
  161. // 负责人查询
  162. if req.ManagerId != "" {
  163. followupModel = followupModel.Where("created_by", req.ManagerId)
  164. }
  165. if req.IsMyself == "1" {
  166. followupModel = followupModel.Where("created_by", s.GetCxtUserId())
  167. }
  168. // 日期条件
  169. if req.DaysBeforeToday > 0 { // 获取前N天的跟进记录
  170. now := gtime.Now()
  171. begin := now.AddDate(0, 0, -req.DaysBeforeToday).Format("Y-m-d 00:00:00")
  172. followupModel = followupModel.Where("follow_date>=?", begin)
  173. }
  174. // 获取日期区间范围内的记录
  175. if req.BeginTime != "" && req.EndTime != "" {
  176. begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
  177. end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
  178. followupModel = followupModel.Where("follow_date>=? AND follow_date<=?", begin, end)
  179. }
  180. if req.FollowType != "" {
  181. followupModel = followupModel.Where("follow_type", req.FollowType)
  182. }
  183. //跟进记录销售 添加销售人权限
  184. if req.Sell != "" {
  185. followupModel = followupModel.Where("created_by", s.CxtUser.Id)
  186. }
  187. //total, err = followupModel.Count()
  188. //if err != nil {
  189. // g.Log().Error(err)
  190. // err = gerror.New("获取总行数失败")
  191. // return
  192. //}
  193. // 查询原始记录
  194. var originalFollowupList []model.FollowupInfo
  195. err = followupModel.Order("follow_date DESC").Scan(&originalFollowupList)
  196. if err != nil && err != sql.ErrNoRows {
  197. return
  198. }
  199. // 查询一级评论
  200. var comments []model.PlatFollowupComment
  201. 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)
  202. if err != nil && err != sql.ErrNoRows {
  203. return
  204. }
  205. // 构造数据
  206. var days []string
  207. followupMap := make(map[string][]*model.FollowupInfo, 0)
  208. commentMap := make(map[int][]*model.PlatFollowupComment, 0)
  209. // 评论数据map
  210. for index, comment := range comments {
  211. if _, ok := commentMap[comment.FollowId]; !ok {
  212. commentMap[comment.FollowId] = make([]*model.PlatFollowupComment, 0)
  213. }
  214. commentMap[comment.FollowId] = append(commentMap[comment.FollowId], &comments[index])
  215. }
  216. // 跟进记录map
  217. for index, followup := range originalFollowupList {
  218. if _, ok := followupMap[followup.FollowDate.Format("Y-m-d")]; !ok {
  219. days = append(days, followup.FollowDate.Format("Y-m-d"))
  220. followupMap[followup.FollowDate.Format("Y-m-d")] = make([]*model.FollowupInfo, 0)
  221. }
  222. if followup.Comments == nil {
  223. originalFollowupList[index].Comments = make([]*model.PlatFollowupComment, 0)
  224. }
  225. // 为跟进记录填充评论数据
  226. if _, ok := commentMap[followup.Id]; ok {
  227. originalFollowupList[index].Comments = append(originalFollowupList[index].Comments, commentMap[followup.Id]...)
  228. }
  229. originalFollowupList[index].CommentNumber = len(originalFollowupList[index].Comments)
  230. followupMap[followup.FollowDate.Format("Y-m-d")] = append(followupMap[followup.FollowDate.Format("Y-m-d")], &originalFollowupList[index])
  231. }
  232. for _, day := range days {
  233. var followup model.FollowupInfoResp
  234. followup.FollowDay = day
  235. followup.FollowupList = followupMap[day]
  236. followupList = append(followupList, &followup)
  237. }
  238. return
  239. }