business_cron.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package proj
  2. import (
  3. projDao "dashoo.cn/micro/app/dao/proj"
  4. "dashoo.cn/micro/app/service"
  5. "database/sql"
  6. "fmt"
  7. "github.com/gogf/gf/container/gset"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gcron"
  10. "github.com/gogf/gf/os/glog"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/text/gstr"
  13. "github.com/gogf/gf/util/gconv"
  14. "strings"
  15. )
  16. // 初始化,创建每10分钟执行一次的定时任务
  17. func init() {
  18. // 定时任务
  19. spec := "0 0 9 * * *" // 每天凌晨9点执行
  20. gcron.AddSingleton(spec, businessFollowRun)
  21. }
  22. // businessFollowRun 项目跟进超时任务逻辑
  23. func businessFollowRun() {
  24. tenant := g.Config().GetString("micro_srv.tenant")
  25. if tenant == "" {
  26. glog.Error("定时任务租户码未设置,请前往配置")
  27. return
  28. }
  29. // 从配置中获取消息提醒设置
  30. configs, err := g.DB(tenant).Model("sys_config").Where("config_key IN ('SalesDirector','SalesAssociate')").FindAll()
  31. if err != nil && err != sql.ErrNoRows {
  32. glog.Error(err)
  33. return
  34. }
  35. // 销售总监用户Id
  36. salesDirector := []string{}
  37. // 销售助理用户Id
  38. salesAssociate := []string{}
  39. for _, config := range configs {
  40. if config["config_key"].String() == "SalesDirector" {
  41. salesDirector = strings.Split(config["config_value"].String(), ",")
  42. } else if config["config_key"].String() == "SalesAssociate" {
  43. salesAssociate = strings.Split(config["config_value"].String(), ",")
  44. }
  45. }
  46. businessFollowOverdueSalesAssociate(tenant, []string{"13"})
  47. if len(salesDirector) > 0 {
  48. go businessFollowOverdueSalesDirector(tenant, salesDirector)
  49. }
  50. if len(salesAssociate) > 0 {
  51. go businessFollowOverdueSalesDirector(tenant, salesAssociate)
  52. }
  53. go businessFollowOverdueSalesEngineer(tenant)
  54. }
  55. // 超期3天提醒销售总监
  56. func businessFollowOverdueSalesDirector(tenant string, userIds []string) {
  57. now := gtime.Now().StartOfDay()
  58. LastWeekDay := now.AddDate(0, 0, -10)
  59. LastTwoWeekDay := now.AddDate(0, 0, -17)
  60. LastMonthDay := now.AddDate(0, -1, -3)
  61. businessACount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusA).
  62. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekDay).Count()
  63. if err != nil {
  64. g.Log().Error("获取A类超期3天未跟进项目", err)
  65. }
  66. businessBCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusB).
  67. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekDay).Count()
  68. if err != nil {
  69. g.Log().Error("获取B类超期3天未跟进项目", err)
  70. }
  71. businessCCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusC).
  72. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthDay).Count()
  73. if err != nil {
  74. g.Log().Error("获取C类超期3天未跟进项目", err)
  75. }
  76. var msg string
  77. if businessACount > 0 {
  78. msg += fmt.Sprintf("您有超期3天未跟进A类项目%v个,", businessACount)
  79. }
  80. if businessBCount > 0 {
  81. msg += fmt.Sprintf("您有超期3天未跟进B类项目%v个,", businessBCount)
  82. }
  83. if businessCCount > 0 {
  84. msg += fmt.Sprintf("您有超期3天未跟进C类项目%v个,", businessCCount)
  85. }
  86. if msg != "" {
  87. businessNotifyMessage(userIds, gstr.TrimRightStr(msg, ","))
  88. }
  89. }
  90. // 超期1天后,超期3天 提醒销售助理
  91. func businessFollowOverdueSalesAssociate(tenant string, userIds []string) {
  92. now := gtime.Now().StartOfDay()
  93. LastWeekDay := now.AddDate(0, 0, -8)
  94. LastTwoWeekDay := now.AddDate(0, 0, -15)
  95. LastMonthDay := now.AddDate(0, -1, -1)
  96. LastWeekTridDay := now.AddDate(0, 0, -10)
  97. LastTwoWeekTridDay := now.AddDate(0, 0, -17)
  98. LastMonthTridDay := now.AddDate(0, -1, -3)
  99. businessATridCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusA).
  100. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekDay).Count()
  101. if err != nil {
  102. g.Log().Error("获取A类超期3天未跟进项目", err)
  103. }
  104. businessBTridCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusB).
  105. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekDay).Count()
  106. if err != nil {
  107. g.Log().Error("获取B类超期3天未跟进项目", err)
  108. }
  109. businessCTridCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusC).
  110. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthDay).Count()
  111. if err != nil {
  112. g.Log().Error("获取C类超期3天未跟进项目", err)
  113. }
  114. businessACount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusA).
  115. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekDay).WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekTridDay).
  116. Count()
  117. if err != nil {
  118. g.Log().Error("获取A类超期1天未跟进项目", err)
  119. }
  120. businessBCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusB).
  121. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekDay).WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekTridDay).
  122. Count()
  123. if err != nil {
  124. g.Log().Error("获取B类超期1天未跟进项目", err)
  125. }
  126. businessCCount, err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusC).
  127. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthDay).WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthTridDay).
  128. Count()
  129. if err != nil {
  130. g.Log().Error("获取C类超期1天未跟进项目", err)
  131. }
  132. var msg string
  133. if businessATridCount > 0 {
  134. msg += fmt.Sprintf("您有超期3天未跟进A类项目%v个,", businessATridCount)
  135. }
  136. if businessBTridCount > 0 {
  137. msg += fmt.Sprintf("您有超期3天未跟进B类项目%v个,", businessBTridCount)
  138. }
  139. if businessCTridCount > 0 {
  140. msg += fmt.Sprintf("您有超期3天未跟进C类项目%v个,", businessCTridCount)
  141. }
  142. if businessACount > 0 {
  143. msg += fmt.Sprintf("您有超期1天未跟进A类项目%v个,", businessACount)
  144. }
  145. if businessBCount > 0 {
  146. msg += fmt.Sprintf("您有超期1天未跟进B类项目%v个,", businessBCount)
  147. }
  148. if businessCCount > 0 {
  149. msg += fmt.Sprintf("您有超期1天未跟进C类项目%v个,", businessCCount)
  150. }
  151. if msg != "" {
  152. businessNotifyMessage(userIds, gstr.TrimRightStr(msg, ","))
  153. }
  154. }
  155. type FollowOverdue struct {
  156. SaleId int64 `json:"saleId"`
  157. Count int64 `json:"count"`
  158. }
  159. // 在到达超期时间前3天进行提醒、当天进行提醒,这两次提醒只提醒销售工程师, 超期1天后提醒
  160. func businessFollowOverdueSalesEngineer(tenant string) {
  161. now := gtime.Now().StartOfDay()
  162. // 超期一天
  163. LastWeekOneDay := now.AddDate(0, 0, -8)
  164. LastTwoWeekOneDay := now.AddDate(0, 0, -15)
  165. LastMonthOneDay := now.AddDate(0, -1, -1)
  166. // 超期当天
  167. LastWeekCurrentDay := now.AddDate(0, 0, -7)
  168. LastTwoWeekCurrentDay := now.AddDate(0, 0, -14)
  169. LastMonthCurrentDay := now.AddDate(0, -1, 0)
  170. // 在到达超期时间前3天进行提醒
  171. LastWeekBeforeTridDay := now.AddDate(0, 0, -4)
  172. LastTwoWeekBeforeTridDay := now.AddDate(0, 0, -11)
  173. LastMonthBeforeTridDay := now.AddDate(0, -1, 3)
  174. LastWeekOneCount, LastTwoWeekOneCount, LastMonthOneCount := make([]FollowOverdue, 0), make([]FollowOverdue, 0), make([]FollowOverdue, 0)
  175. LastWeekCurrentCount, LastTwoWeekCurrentCount, LastMonthCurrentCount := make([]FollowOverdue, 0), make([]FollowOverdue, 0), make([]FollowOverdue, 0)
  176. LastWeekBeforeTridCount, LastTwoWeekBeforeTridCount, LastMonthBeforeTridCount := make([]FollowOverdue, 0), make([]FollowOverdue, 0), make([]FollowOverdue, 0)
  177. err := projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusA).
  178. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekOneDay).
  179. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastWeekOneCount)
  180. if err != nil {
  181. g.Log().Error("获取A类超期一天未跟进项目", err)
  182. }
  183. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusB).
  184. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekOneDay).
  185. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastTwoWeekOneCount)
  186. if err != nil {
  187. g.Log().Error("获取B类超期一天未跟进项目", err)
  188. }
  189. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusC).
  190. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthOneDay).
  191. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastMonthOneCount)
  192. if err != nil {
  193. g.Log().Error("获取C类超期一天未跟进项目", err)
  194. }
  195. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusA).
  196. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekCurrentDay).
  197. WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekOneDay).
  198. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastWeekCurrentCount)
  199. if err != nil {
  200. g.Log().Error("获取A类超期当天未跟进项目", err)
  201. }
  202. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusB).
  203. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekCurrentDay).
  204. WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekOneDay).
  205. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastTwoWeekCurrentCount)
  206. if err != nil {
  207. g.Log().Error("获取B类超期当天未跟进项目", err)
  208. }
  209. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusC).
  210. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthCurrentDay).
  211. WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthOneDay).
  212. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastMonthCurrentCount)
  213. if err != nil {
  214. g.Log().Error("获取C类超期当天未跟进项目", err)
  215. }
  216. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusA).
  217. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekBeforeTridDay).
  218. WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastWeekCurrentDay).
  219. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastWeekBeforeTridCount)
  220. if err != nil {
  221. g.Log().Error("获取A类超期当天未跟进项目", err)
  222. }
  223. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusB).
  224. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekBeforeTridDay).
  225. WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastTwoWeekCurrentDay).
  226. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastTwoWeekBeforeTridCount)
  227. if err != nil {
  228. g.Log().Error("获取B类超期当天未跟进项目", err)
  229. }
  230. err = projDao.NewProjBusinessDao(tenant).Where(projDao.ProjBusiness.C.NboType, StatusC).
  231. WhereLTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthBeforeTridDay).
  232. WhereGTE(projDao.ProjBusiness.C.FinalFollowTime, LastMonthCurrentDay).
  233. Fields("sale_id, count(id) as count").Group("sale_id").OrderAsc("sale_id").Scan(&LastMonthBeforeTridCount)
  234. if err != nil {
  235. g.Log().Error("获取C类超期当天未跟进项目", err)
  236. }
  237. allSaleIds := gset.NewStrSet(true)
  238. saleIds, lastWeekOneCountMap := handleSalesEngineerData(LastWeekOneCount)
  239. allSaleIds = allSaleIds.Union(saleIds)
  240. saleIds, LastTwoWeekOneCountMap := handleSalesEngineerData(LastTwoWeekOneCount)
  241. allSaleIds = allSaleIds.Union(saleIds)
  242. saleIds, LastMonthOneCountMap := handleSalesEngineerData(LastMonthOneCount)
  243. allSaleIds = allSaleIds.Union(saleIds)
  244. saleIds, LastWeekCurrentCountMap := handleSalesEngineerData(LastWeekCurrentCount)
  245. allSaleIds = allSaleIds.Union(saleIds)
  246. saleIds, LastTwoWeekCurrentCountMap := handleSalesEngineerData(LastTwoWeekCurrentCount)
  247. allSaleIds = allSaleIds.Union(saleIds)
  248. saleIds, LastMonthCurrentCountMap := handleSalesEngineerData(LastMonthCurrentCount)
  249. allSaleIds = allSaleIds.Union(saleIds)
  250. saleIds, LastWeekBeforeTridCountMap := handleSalesEngineerData(LastWeekBeforeTridCount)
  251. allSaleIds = allSaleIds.Union(saleIds)
  252. saleIds, LastTwoWeekBeforeTridCountMap := handleSalesEngineerData(LastTwoWeekBeforeTridCount)
  253. allSaleIds = allSaleIds.Union(saleIds)
  254. saleIds, LastMonthBeforeTridCountMap := handleSalesEngineerData(LastMonthBeforeTridCount)
  255. allSaleIds = allSaleIds.Union(saleIds)
  256. for _, saleId := range allSaleIds.Slice() {
  257. var msg string
  258. if lastWeekOneCountMap[saleId] != "" {
  259. msg += fmt.Sprintf("您有超期1天未跟进A类项目%v个,", lastWeekOneCountMap[saleId])
  260. }
  261. if LastTwoWeekOneCountMap[saleId] != "" {
  262. msg += fmt.Sprintf("您有超期1天未跟进B类项目%v个,", LastTwoWeekOneCountMap[saleId])
  263. }
  264. if LastMonthOneCountMap[saleId] != "" {
  265. msg += fmt.Sprintf("您有超期1天未跟进C类项目%v个,", LastMonthOneCountMap[saleId])
  266. }
  267. if LastWeekCurrentCountMap[saleId] != "" {
  268. msg += fmt.Sprintf("您今天有超期未跟进A类项目%v个,", LastWeekCurrentCountMap[saleId])
  269. }
  270. if LastTwoWeekCurrentCountMap[saleId] != "" {
  271. msg += fmt.Sprintf("您今天有超期未跟进B类项目%v个,", LastTwoWeekCurrentCountMap[saleId])
  272. }
  273. if LastMonthCurrentCountMap[saleId] != "" {
  274. msg += fmt.Sprintf("您今天有超期未跟进C类项目%v个,", LastMonthCurrentCountMap[saleId])
  275. }
  276. if LastWeekBeforeTridCountMap[saleId] != "" {
  277. msg += fmt.Sprintf("您3天后有超期未跟进A类项目%v个,", LastWeekBeforeTridCountMap[saleId])
  278. }
  279. if LastTwoWeekBeforeTridCountMap[saleId] != "" {
  280. msg += fmt.Sprintf("您3天后有超期未跟进B类项目%v个,", LastTwoWeekBeforeTridCountMap[saleId])
  281. }
  282. if LastMonthBeforeTridCountMap[saleId] != "" {
  283. msg += fmt.Sprintf("您3天后有超期未跟进C类项目%v个,", LastMonthBeforeTridCountMap[saleId])
  284. }
  285. if msg != "" {
  286. businessNotifyMessage([]string{saleId}, gstr.TrimRightStr(msg, ","))
  287. }
  288. }
  289. }
  290. func handleSalesEngineerData(countData []FollowOverdue) (saleIds *gset.StrSet, data g.MapStrStr) {
  291. saleIds = gset.NewStrSet(true)
  292. data = make(g.MapStrStr)
  293. for _, v := range countData {
  294. saleId := gconv.String(v.SaleId)
  295. saleIds.Add(saleId)
  296. data[saleId] = gconv.String(v.Count)
  297. }
  298. return
  299. }
  300. // 项目跟进的消息通知
  301. func businessNotifyMessage(userIds []string, message string) {
  302. ids := strings.Join(userIds, ",")
  303. // 调用统一的消息通知方式
  304. notifyMessage(ids, message)
  305. }
  306. // notifyMessage 发送消息通知
  307. func notifyMessage(ids, message string) {
  308. msg := g.MapStrStr{
  309. "msgTitle": "项目跟进超期提醒",
  310. "msgContent": message,
  311. "msgType": "20",
  312. "recvUserIds": ids,
  313. "msgStatus": "10",
  314. "sendType": "10,20,30,40",
  315. }
  316. if err := service.CreateSystemMessage(msg); err != nil {
  317. glog.Error("消息提醒异常:", err)
  318. }
  319. }