unusual_renew_company.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package statistic_report
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models/company"
  5. "hongze/hz_crm_api/models/contract"
  6. "hongze/hz_crm_api/utils"
  7. "strings"
  8. "time"
  9. )
  10. type UnusualCompanyRenewDataMap struct {
  11. UnusualRenewMap map[int]int `description:"续约异常客户数"`
  12. UnusualRenewIdMap map[int]string `description:"续约异常客户ids"`
  13. UnusualRenewTotal int `description:"续约异常客户总数(去重)"`
  14. FormalCompanyTotal int `description:"当前正常客户总数(去重)"`
  15. StartDate string `description:"开始日期"`
  16. EndDate string `description:"开始日期"`
  17. }
  18. // GetUnusualRenewWeekDataNum 周度数据
  19. func GetUnusualRenewWeekDataNum(nowWeekMonday time.Time, productId, source, dataNum int) (adminDataList []UnusualCompanyRenewDataMap, firstDate time.Time, err error) {
  20. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  21. chanList := make([]chan UnusualCompanyRenewDataMap, 0)
  22. for i := 0; i < dataNum; i++ {
  23. tmpCh := make(chan UnusualCompanyRenewDataMap, 0)
  24. chanList = append(chanList, tmpCh)
  25. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7*i)
  26. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7*i)
  27. // 第一个开始日期
  28. firstDate = lastWeekMonday
  29. go getUnusualRenewSectionData(productId, source, lastWeekMonday, lastWeekSunday, tmpCh)
  30. }
  31. adminDataList = make([]UnusualCompanyRenewDataMap, 0)
  32. for _, vChan := range chanList {
  33. var weekUnusualCompanyRenewDataMap UnusualCompanyRenewDataMap
  34. weekUnusualCompanyRenewDataMap = <-vChan
  35. close(vChan)
  36. adminDataList = append(adminDataList, weekUnusualCompanyRenewDataMap)
  37. }
  38. return
  39. }
  40. // GetUnusualRenewMonthDataNum 月度数据
  41. func GetUnusualRenewMonthDataNum(nowMonthFirstDay time.Time, productId, source, dataNum int) (adminDataList []UnusualCompanyRenewDataMap, firstDate time.Time, err error) { //三个协程返回
  42. //nowMonthLastDay := nowMonthFirstDay.AddDate(0, 1, 0).Add(-1 * time.Second) //本月最后一天
  43. chanList := make([]chan UnusualCompanyRenewDataMap, 0)
  44. for i := 0; i < dataNum; i++ {
  45. tmpCh := make(chan UnusualCompanyRenewDataMap, 0)
  46. chanList = append(chanList, tmpCh)
  47. lastMonthFirstDay := nowMonthFirstDay.AddDate(0, -1*i, 0) //上个月第一天
  48. lastMonthLastDay := nowMonthFirstDay.AddDate(0, -1*(i-1), 0).Add(-1 * time.Second) //上个月最后一天
  49. // 第一个开始日期
  50. firstDate = lastMonthFirstDay
  51. go getUnusualRenewSectionData(productId, source, lastMonthFirstDay, lastMonthLastDay, tmpCh)
  52. }
  53. adminDataList = make([]UnusualCompanyRenewDataMap, 0)
  54. for _, vChan := range chanList {
  55. var weekUnusualCompanyRenewDataMap UnusualCompanyRenewDataMap
  56. weekUnusualCompanyRenewDataMap = <-vChan
  57. close(vChan)
  58. adminDataList = append(adminDataList, weekUnusualCompanyRenewDataMap)
  59. }
  60. return
  61. }
  62. // GetUnusualRenewTimeIntervalData 区间数据
  63. func GetUnusualRenewTimeIntervalData(productId, source int, startDate, endDate string) (adminDataMapList []UnusualCompanyRenewDataMap, firstDate time.Time, err error) { //三个协程返回
  64. startDateTimer, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  65. endDateTimer, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  66. endDateTimer = endDateTimer.AddDate(0, 0, 1).Add(-1 * time.Second) //本月最后一天
  67. //最早的一天
  68. firstDate = startDateTimer
  69. ch1 := make(chan UnusualCompanyRenewDataMap, 0)
  70. go getUnusualRenewSectionData(productId, source, startDateTimer, endDateTimer, ch1)
  71. var adminDataMap UnusualCompanyRenewDataMap
  72. adminDataMap = <-ch1
  73. close(ch1)
  74. adminDataMapList = make([]UnusualCompanyRenewDataMap, 0)
  75. adminDataMapList = append(adminDataMapList, adminDataMap)
  76. return
  77. }
  78. // getUnusualRenewSectionData 获取周期数据
  79. func getUnusualRenewSectionData(productId, source int, startDate, endDate time.Time, ch chan UnusualCompanyRenewDataMap) (adminDataMap UnusualCompanyRenewDataMap, err error) {
  80. defer func() {
  81. ch <- adminDataMap
  82. }()
  83. unusualRenewMap := make(map[int]int) // 续约异常客户
  84. unusualRenewCompanyIdMap := make(map[int]string) // 续约异常客户id
  85. var unusualRenewCompanyTotal, formalCompanyTotal int //续约异常客户总数 , 当前正式的客户数
  86. // 当前正式的客户数
  87. formalCompanyTotal, err = contract.GetCountFormalCompany(endDate, productId)
  88. if err != nil {
  89. utils.FileLog.Error(err.Error())
  90. return
  91. }
  92. // 续约异常
  93. {
  94. data, tmpErr := company.GetUnusualRenewCompanyByShareSellerList(startDate, endDate, productId, source)
  95. if tmpErr != nil {
  96. err = tmpErr
  97. utils.FileLog.Error(err.Error())
  98. return
  99. }
  100. unusualRenewCompanyIdListMap := make(map[int][]string) // 续约异常客户
  101. for _, v := range data {
  102. // 共享人id为空
  103. if v.ShareSellerId <= 0 {
  104. continue
  105. }
  106. // 共享人id不为空,且不是所属销售
  107. tmp, ok := unusualRenewMap[v.ShareSellerId]
  108. if !ok {
  109. tmp = 0
  110. }
  111. companyIdSlice := strings.Split(v.CompanyIds, ",")
  112. unusualRenewMap[v.ShareSellerId] = tmp + len(companyIdSlice)
  113. // 客户id
  114. tmpCompanyIdList2, ok := unusualRenewCompanyIdListMap[v.ShareSellerId]
  115. if !ok {
  116. tmpCompanyIdList2 = make([]string, 0)
  117. }
  118. unusualRenewCompanyIdListMap[v.ShareSellerId] = append(tmpCompanyIdList2, fmt.Sprint(v.CompanyIds))
  119. unusualRenewCompanyTotal += len(companyIdSlice)
  120. }
  121. for sellerId, tmpCompanyIdList := range unusualRenewCompanyIdListMap {
  122. unusualRenewCompanyIdMap[sellerId] = strings.Join(tmpCompanyIdList, ",")
  123. }
  124. }
  125. adminDataMap = UnusualCompanyRenewDataMap{
  126. UnusualRenewMap: unusualRenewMap,
  127. UnusualRenewIdMap: unusualRenewCompanyIdMap,
  128. UnusualRenewTotal: unusualRenewCompanyTotal,
  129. FormalCompanyTotal: formalCompanyTotal,
  130. StartDate: startDate.Format(utils.FormatDate),
  131. EndDate: endDate.Format(utils.FormatDate),
  132. }
  133. return
  134. }