company_area_statistics.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package statistic_report
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models/cygx"
  5. "hongze/hz_crm_api/models/statistic_report"
  6. "hongze/hz_crm_api/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type CompanyAreaDataMap struct {
  12. TryOutMap map[string]int `description:"试用客户数"`
  13. TryOutIdMap map[string]string `description:"试用客户ids"`
  14. FormalMap map[string]int `description:"正式客户数"`
  15. FormalIdMap map[string]string `description:"正式客户ids"`
  16. ActiveMap map[string]int `description:"活跃客户状态数"`
  17. ActiveIdMap map[int]string `description:"活跃客户ids"`
  18. AllActiveMap map[string]int `description:"所有活跃客户状态数"`
  19. AllActiveIdMap map[int]string `description:"所有活跃客户ids"`
  20. NoIncrementalActiveMap map[string]int `description:"非新增试用客户的活跃客户状态数"`
  21. NoIncrementalActiveIdMap map[int]string `description:"非新增试用客户的活跃客户ids"`
  22. StartDate string `description:"开始日期"`
  23. EndDate string `description:"开始日期"`
  24. TryStagePushNum map[string]int `description:"试用(推进)状态的客户数量"`
  25. TryStageFollowNum map[string]int `description:"试用(跟踪)状态的客户数量"`
  26. TryStageReadyNum map[string]int `description:"试用(预备)状态的客户数量"`
  27. TryStageInitNum map[string]int `description:"试用(未分类)状态的客户数量"`
  28. TotalNum int `description:"合计客户数量"`
  29. TryStagePushIdsMap map[string]string `description:"试用(推进)状态的客户数量"`
  30. TryStageFollowIdsMap map[string]string `description:"试用(跟踪)状态的客户数量"`
  31. TryStageReadyIdsMap map[string]string `description:"试用(预备)状态的客户数量"`
  32. TryStageInitIdsMap map[string]string `description:"试用(未分类)状态的客户数量"`
  33. }
  34. // GetAreaDateData 某天的区域客户数据
  35. func GetAreaDateData(date string, productId int) (adminDataMapList []CompanyAreaDataMap, firstDate time.Time, err error) { //三个协程返回
  36. dateTimer, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  37. //最早的一天
  38. firstDate = dateTimer
  39. ch1 := make(chan CompanyAreaDataMap, 0)
  40. go GetStackAreaDateData(productId, dateTimer, ch1)
  41. var adminDataMap CompanyAreaDataMap
  42. adminDataMap = <-ch1
  43. close(ch1)
  44. adminDataMapList = make([]CompanyAreaDataMap, 0)
  45. adminDataMapList = append(adminDataMapList, adminDataMap)
  46. return
  47. }
  48. func GetStackAreaDateData(productId int, date time.Time, ch chan CompanyAreaDataMap) (adminDataMap CompanyAreaDataMap, err error) {
  49. defer func() {
  50. ch <- adminDataMap
  51. }()
  52. tryOutMap := make(map[string]int)
  53. tryStagePushMap := make(map[string]int)
  54. tryStageFollowMap := make(map[string]int)
  55. tryStageReadyMap := make(map[string]int)
  56. tryStageInitMap := make(map[string]int)
  57. formalMap := make(map[string]int)
  58. formalIdMap := make(map[string]string) //正式客户的id集合
  59. activeMap := make(map[string]int)
  60. // 其他客户的ID集合
  61. tryOutIdMap := make(map[string]string) //试用客户的id集合
  62. tryStagePushIdsMap := make(map[string]string) //推进客户的id集合
  63. tryStageFollowIdsMap := make(map[string]string) //跟踪客户的id集合
  64. tryStageReadyIdsMap := make(map[string]string) //预备客户的id集合
  65. tryStageInitIdsMap := make(map[string]string) //未分类客户的id集合
  66. //正式客户
  67. {
  68. var condition string
  69. var pars []interface{}
  70. condition = ` and p.status = ? and p.product_id = ?`
  71. pars = append(pars, "正式", productId)
  72. data, tmpErr := statistic_report.GetGroupCompanyAreaList(condition, pars)
  73. if tmpErr != nil {
  74. err = tmpErr
  75. return
  76. }
  77. for _, v := range data {
  78. formalMap[v.City] = v.Num
  79. formalIdMap[v.City] = v.CompanyIds
  80. }
  81. }
  82. //试用客户id
  83. companyIdList := make([]string, 0)
  84. //试用客户
  85. {
  86. data, tmpErr := statistic_report.GetTryGroupCompanyAreaList(productId)
  87. if tmpErr != nil {
  88. err = tmpErr
  89. return
  90. }
  91. for _, v := range data {
  92. tmpSlice := strings.Split(v.CompanyIds, ",")
  93. companyIdList = append(companyIdList, tmpSlice...)
  94. tryOutMap[v.City] += v.Num
  95. switch v.TryStage {
  96. case 1:
  97. tryStageInitMap[v.City] += v.Num
  98. tryStageInitIdsMap[v.City] = v.CompanyIds
  99. case 2:
  100. tryStagePushMap[v.City] += v.Num
  101. tryStagePushIdsMap[v.City] = v.CompanyIds
  102. case 3:
  103. tryStageFollowMap[v.City] += v.Num
  104. tryStageFollowIdsMap[v.City] = v.CompanyIds
  105. case 4:
  106. tryStageReadyMap[v.City] += v.Num
  107. tryStageReadyIdsMap[v.City] = v.CompanyIds
  108. default:
  109. tryStageInitMap[v.City] += v.Num
  110. tryStageInitIdsMap[v.City] = v.CompanyIds
  111. }
  112. tryOutIdMap[v.City] = v.CompanyIds
  113. }
  114. }
  115. //活跃
  116. {
  117. if productId == 1 {
  118. var condition string
  119. var pars []interface{}
  120. condition = ` and a.date <= ?`
  121. pars = append(pars, date)
  122. if len(companyIdList) > 0 {
  123. condition += fmt.Sprint(` and a.company_id in (`, strings.Join(companyIdList, ","), `) and b.product_id=? `)
  124. pars = append(pars, productId)
  125. data, tmpErr := statistic_report.GetCompanyViewTotalAreaList(condition, pars, ActiveViewNum, "")
  126. if tmpErr != nil {
  127. err = tmpErr
  128. return
  129. }
  130. for _, v := range data {
  131. //fmt.Println(v)
  132. activeMap[v.City] = v.Num
  133. }
  134. }
  135. } else {
  136. companyIdMap := make(map[int]int)
  137. finalIdMap := make(map[int]int)
  138. idArr := make([]string, 0)
  139. var ids string
  140. endCh := make(chan cygx.InteractiveList, 0)
  141. //获取结束时间互动量>=50次的公司
  142. go CompanyInteractiveTotal(date.Format(utils.FormatDate), endCh)
  143. var endList cygx.InteractiveList
  144. endList = <-endCh
  145. close(endCh)
  146. for id, v := range endList.TotalMap {
  147. _, ok := companyIdMap[id]
  148. if v >= 50 && ok {
  149. finalIdMap[id] = v
  150. }
  151. }
  152. for k := range finalIdMap {
  153. idArr = append(idArr, strconv.Itoa(k))
  154. }
  155. ids = strings.Join(idArr, ",")
  156. fmt.Println("ids:", ids)
  157. var condition string
  158. if len(ids) > 0 {
  159. condition = ` AND c.company_id IN ( ` + ids + ` ) `
  160. list, tmpErr := statistic_report.GetCompanyCountGroupByCity(condition)
  161. if tmpErr != nil {
  162. err = tmpErr
  163. return
  164. }
  165. for _, item := range list {
  166. activeMap[item.CompanyIds] = item.Num
  167. //allActiveIdMap[item.AdminId] = item.CompanyIds
  168. }
  169. }
  170. }
  171. }
  172. adminDataMap = CompanyAreaDataMap{
  173. TryOutMap: tryOutMap,
  174. FormalMap: formalMap,
  175. FormalIdMap: formalIdMap,
  176. ActiveMap: activeMap,
  177. StartDate: date.Format(utils.FormatDate),
  178. EndDate: date.Format(utils.FormatDate),
  179. TryStageFollowNum: tryStageFollowMap,
  180. TryStagePushNum: tryStagePushMap,
  181. TryStageReadyNum: tryStageReadyMap,
  182. TryStageInitNum: tryStageInitMap,
  183. TryOutIdMap: tryOutIdMap,
  184. TryStagePushIdsMap: tryStagePushIdsMap,
  185. TryStageFollowIdsMap: tryStageFollowIdsMap,
  186. TryStageReadyIdsMap: tryStageReadyIdsMap,
  187. TryStageInitIdsMap: tryStageInitIdsMap,
  188. }
  189. return
  190. }
  191. // CityRecord 系统用户统计信息
  192. type CityRecord struct {
  193. Name string `description:"城市名"`
  194. CompanyId string `description:"客户id集合,多个用英文,隔开"`
  195. CompanyAreaNumNumList []CompanyAreaNum `description:"统计次数"`
  196. }
  197. // CompanyAreaNum 系统客户统计信息
  198. type CompanyAreaNum struct {
  199. TryOutNum int `description:"试用客户数"`
  200. TryOutIds string `description:"试用客户ids"`
  201. FormalNum int `description:"正式客户数"`
  202. FormalIds string `description:"正式客户ids"`
  203. ActiveNum int `description:"活跃客户数"`
  204. ActiveIds string `description:"活跃客户ids"`
  205. AllActiveNum int `description:"所有活跃客户状态数"`
  206. AllActiveIds string `description:"所有活跃客户ids"`
  207. NoIncrementalActiveNum int `description:"非新增试用客户的活跃客户状态数"`
  208. NoIncrementalActiveIds string `description:"非新增试用客户的活跃客户ids"`
  209. StartDate string `description:"开始日期"`
  210. EndDate string `description:"结束日期"`
  211. TryStagePushNum int `description:"试用(推进)状态的客户数量"`
  212. TryStageFollowNum int `description:"试用(跟踪)状态的客户数量"`
  213. TryStageReadyNum int `description:"试用(预备)状态的客户数量"`
  214. TryStageInitNum int `description:"试用(未分类)状态的客户数量"`
  215. TryStagePushIds string `description:"试用(推进)状态的客户Ids"`
  216. TryStageFollowIds string `description:"试用(跟踪)状态的客户Ids"`
  217. TryStageReadyIds string `description:"试用(预备)状态的客户Ids"`
  218. TryStageInitIds string `description:"试用(未分类)状态的客户Ids"`
  219. TotalNum int `description:"合计客户数量"`
  220. }
  221. // GroupCityRecord 分组统计信息
  222. type GroupCityRecord struct {
  223. Item []CityRecord `description:"城市组"`
  224. Name string `description:"省份"`
  225. CompanyAreaNumNumList []CompanyAreaNum `description:"统计次数"`
  226. }
  227. // CustomerAreaStatisticsResp 客户区域分组返回
  228. type CustomerAreaStatisticsResp struct {
  229. List []GroupCityRecord
  230. IsAdmin bool
  231. }