plat_followup.go 8.9 KB

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