plat_followup.go 8.5 KB

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