renew_company.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 CompanyRenewDataMap struct {
  11. NotRenewMap map[int]int `description:"未续约客户数"`
  12. NotRenewIdMap map[int]string `description:"未续约客户ids"`
  13. RenewFollowMap map[int]int `description:"续约跟进客户数"`
  14. RenewFollowIdMap map[int]string `description:"续约跟进客户ids"`
  15. ExpireMap map[int]int `description:"到期客户数"`
  16. ExpireIdMap map[int]string `description:"到期客户ids"`
  17. RenewMap map[int]int `description:"续约客户数"`
  18. RenewIdMap map[int]string `description:"续约客户ids"`
  19. UnusualRenewMap map[int]int `description:"续约异常客户数"`
  20. UnusualRenewIdMap map[int]string `description:"续约异常客户ids"`
  21. UnusualRenewTotal int `description:"续约异常客户总数(去重)"`
  22. FormalCompanyTotal int `description:"当前正常客户总数(去重)"`
  23. StartDate string `description:"开始日期"`
  24. EndDate string `description:"开始日期"`
  25. }
  26. // GetRenewWeekDataNum 周度数据
  27. func GetRenewWeekDataNum(nowWeekMonday time.Time, productId, dataNum int) (adminDataList []CompanyRenewDataMap, firstDate time.Time, err error) {
  28. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  29. chanList := make([]chan CompanyRenewDataMap, 0)
  30. for i := 0; i < dataNum; i++ {
  31. tmpCh := make(chan CompanyRenewDataMap, 0)
  32. chanList = append(chanList, tmpCh)
  33. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7*i)
  34. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7*i)
  35. // 第一个开始日期
  36. firstDate = lastWeekMonday
  37. go getRenewSectionData(productId, lastWeekMonday, lastWeekSunday, tmpCh)
  38. }
  39. adminDataList = make([]CompanyRenewDataMap, 0)
  40. for _, vChan := range chanList {
  41. var weekCompanyRenewDataMap CompanyRenewDataMap
  42. weekCompanyRenewDataMap = <-vChan
  43. close(vChan)
  44. adminDataList = append(adminDataList, weekCompanyRenewDataMap)
  45. }
  46. return
  47. }
  48. // GetRenewMonthDataNum 月度数据
  49. func GetRenewMonthDataNum(nowMonthFirstDay time.Time, productId, dataNum int) (adminDataList []CompanyRenewDataMap, firstDate time.Time, err error) { //三个协程返回
  50. //nowMonthLastDay := nowMonthFirstDay.AddDate(0, 1, 0).Add(-1 * time.Second) //本月最后一天
  51. chanList := make([]chan CompanyRenewDataMap, 0)
  52. for i := 0; i < dataNum; i++ {
  53. tmpCh := make(chan CompanyRenewDataMap, 0)
  54. chanList = append(chanList, tmpCh)
  55. lastMonthFirstDay := nowMonthFirstDay.AddDate(0, -1*i, 0) //上个月第一天
  56. lastMonthLastDay := nowMonthFirstDay.AddDate(0, -1*(i-1), 0).Add(-1 * time.Second) //上个月最后一天
  57. // 第一个开始日期
  58. firstDate = lastMonthFirstDay
  59. go getRenewSectionData(productId, lastMonthFirstDay, lastMonthLastDay, tmpCh)
  60. }
  61. adminDataList = make([]CompanyRenewDataMap, 0)
  62. for _, vChan := range chanList {
  63. var weekCompanyRenewDataMap CompanyRenewDataMap
  64. weekCompanyRenewDataMap = <-vChan
  65. close(vChan)
  66. adminDataList = append(adminDataList, weekCompanyRenewDataMap)
  67. }
  68. return
  69. }
  70. // GetRenewTimeIntervalData 区间数据
  71. func GetRenewTimeIntervalData(productId int, startDate, endDate string) (adminDataMapList []CompanyRenewDataMap, firstDate time.Time, err error) { //三个协程返回
  72. startDateTimer, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  73. endDateTimer, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  74. endDateTimer = endDateTimer.AddDate(0, 0, 1).Add(-1 * time.Second) //本月最后一天
  75. //最早的一天
  76. firstDate = startDateTimer
  77. ch1 := make(chan CompanyRenewDataMap, 0)
  78. go getRenewSectionData(productId, startDateTimer, endDateTimer, ch1)
  79. var adminDataMap CompanyRenewDataMap
  80. adminDataMap = <-ch1
  81. close(ch1)
  82. adminDataMapList = make([]CompanyRenewDataMap, 0)
  83. adminDataMapList = append(adminDataMapList, adminDataMap)
  84. return
  85. }
  86. // getRenewSectionData 获取周期数据
  87. func getRenewSectionData(productId int, startDate, endDate time.Time, ch chan CompanyRenewDataMap) (adminDataMap CompanyRenewDataMap, err error) {
  88. defer func() {
  89. if err != nil {
  90. utils.FileLog.Error("getRenewSectionData,err:" + err.Error())
  91. fmt.Println(err)
  92. }
  93. ch <- adminDataMap
  94. }()
  95. notRenewMap := make(map[int]int)
  96. renewFollowMap := make(map[int]int)
  97. expireMap := make(map[int]int)
  98. renewMap := make(map[int]int)
  99. unusualRenewMap := make(map[int]int) // 续约异常客户
  100. //activeMap := make(map[int]int) //新增的试用客户数量(活跃)
  101. //allActiveMap := make(map[int]int) //全部的试用客户数量(活跃)
  102. //noIncrementalActiveMap := make(map[int]int) //非新增的试用客户数量(活跃)
  103. notRenewIdMap := make(map[int]string)
  104. renewFollowIdMap := make(map[int]string)
  105. expireIdMap := make(map[int]string)
  106. renewIdMap := make(map[int]string)
  107. unusualRenewCompanyIdMap := make(map[int]string) // 续约异常客户
  108. var formalCompanyTotal int //续约异常客户总数 , 当前正式的客户数
  109. formalCompanyTotal, err = contract.GetCountFormalCompany(endDate, productId)
  110. if err != nil {
  111. return
  112. }
  113. //activeIdMap := make(map[int]string) //新增的试用客户id集合(活跃)
  114. //allActiveIdMap := make(map[int]string) //全部的试用客户id集合(活跃)
  115. //noIncrementalActiveIdMap := make(map[int]string) //非新增的试用客户id集合(活跃)
  116. //未续约客户
  117. {
  118. //var condition string
  119. //var pars []interface{}
  120. //condition = ` and op.create_time >= ? and op.create_time <= ? `
  121. //pars = append(pars, startDate, endDate)
  122. data, tmpErr := company.GetNotRenewCompanyGroupList(productId)
  123. if tmpErr != nil {
  124. err = tmpErr
  125. utils.FileLog.Error(err.Error())
  126. return
  127. }
  128. var notRenewIdSlice []string
  129. for _, v := range data {
  130. notRenewIdSlice = append(notRenewIdSlice, v.CompanyIds)
  131. notRenewMap[v.SellerId] = v.Num
  132. notRenewIdMap[v.SellerId] = v.CompanyIds
  133. }
  134. }
  135. //续约跟进
  136. companyIdList := make([]string, 0)
  137. {
  138. //var condition string
  139. //var pars []interface{}
  140. //condition = ` and op.create_time >= ? and op.create_time <= ? `
  141. //pars = append(pars, startDate, endDate)
  142. //获取客户本轮合同周期开始时间
  143. list, tmpErr := company.GetCompanyApproveCreateTimeList(productId)
  144. if tmpErr != nil {
  145. err = tmpErr
  146. utils.FileLog.Error(err.Error())
  147. return
  148. }
  149. //获取历史经历过冻结或流失的客户
  150. freezeList, tmpErr := company.GetNotRenewFollowCompanyGroupList(productId)
  151. if tmpErr != nil {
  152. err = tmpErr
  153. utils.FileLog.Error(err.Error())
  154. return
  155. }
  156. freezeCompanyMap := make(map[string]time.Time)
  157. for _, item := range freezeList {
  158. freezeCompanyMap[item.CompanyId] = item.CreateTime
  159. }
  160. for _, item := range list {
  161. if freezeTime, ok := freezeCompanyMap[item.CompanyId]; ok {
  162. //如果有过冻结或流失历史,若时间早于本轮合同开始时间就不排除,如果晚于合同开始时间就排除
  163. if freezeTime.Before(item.CreateTime) {
  164. delete(freezeCompanyMap, item.CompanyId)
  165. }
  166. }
  167. }
  168. idArray := make([]string, 0)
  169. for k := range freezeCompanyMap {
  170. idArray = append(idArray, k)
  171. }
  172. ids := strings.Join(idArray, ",")
  173. data, tmpErr := company.GetRenewFollowCompanyGroupList(ids, productId)
  174. if tmpErr != nil {
  175. err = tmpErr
  176. utils.FileLog.Error(err.Error())
  177. return
  178. }
  179. var renewFollowIdSlice []string
  180. for _, v := range data {
  181. tmpSlice := strings.Split(v.CompanyIds, ",")
  182. companyIdList = append(companyIdList, tmpSlice...)
  183. renewFollowIdSlice = append(renewFollowIdSlice, v.CompanyIds)
  184. renewFollowMap[v.SellerId] = v.Num
  185. renewFollowIdMap[v.SellerId] = v.CompanyIds
  186. }
  187. }
  188. //到期
  189. //companyIdList := make([]string, 0)
  190. {
  191. data, tmpErr := contract.GetExpiresCompanyGroupList(startDate, endDate, productId)
  192. if tmpErr != nil {
  193. err = tmpErr
  194. utils.FileLog.Error(err.Error())
  195. return
  196. }
  197. var expireIdSlice []string
  198. for _, v := range data {
  199. //tmpSlice := strings.Split(v.CompanyIds, ",")
  200. //companyIdList = append(companyIdList, tmpSlice...)
  201. expireIdSlice = append(expireIdSlice, v.CompanyIds)
  202. expireMap[v.SellerId] = v.Num
  203. expireIdMap[v.SellerId] = v.CompanyIds
  204. }
  205. }
  206. // 续约
  207. //companyIdList := make([]string, 0)
  208. {
  209. contractIds, tmpErr := contract.GetLatestContractListByProductId(productId)
  210. if tmpErr != nil {
  211. err = tmpErr
  212. utils.FileLog.Error(err.Error())
  213. return
  214. }
  215. contractId := strings.Join(contractIds, ",")
  216. data, tmpErr := company.GetRenewCompanyGroupList(startDate, endDate, contractId)
  217. if tmpErr != nil {
  218. err = tmpErr
  219. return
  220. }
  221. var renewIdSlice []string
  222. for _, v := range data {
  223. //tmpSlice := strings.Split(v.CompanyIds, ",")
  224. //companyIdList = append(companyIdList, tmpSlice...)
  225. renewIdSlice = append(renewIdSlice, v.CompanyIds)
  226. renewMap[v.SellerId] = v.Num
  227. renewIdMap[v.SellerId] = v.CompanyIds
  228. }
  229. }
  230. // 续约异常
  231. {
  232. data, tmpErr := company.GetUnusualRenewCompanyList(startDate, endDate, productId)
  233. if tmpErr != nil {
  234. err = tmpErr
  235. utils.FileLog.Error(err.Error())
  236. return
  237. }
  238. unusualRenewCompanyIdListMap := make(map[int][]string) // 续约异常客户
  239. for _, v := range data {
  240. tmp, ok := unusualRenewMap[v.SellerId]
  241. if !ok {
  242. tmp = 0
  243. }
  244. unusualRenewMap[v.SellerId] = tmp + 1
  245. // 共享人id不为空,且不是所属销售
  246. if v.ShareSellerId > 0 && v.ShareSellerId != v.SellerId {
  247. tmp2, ok := unusualRenewMap[v.ShareSellerId]
  248. if !ok {
  249. tmp = 0
  250. }
  251. unusualRenewMap[v.ShareSellerId] = tmp2 + 1
  252. }
  253. // 客户id
  254. tmpCompanyIdList, ok := unusualRenewCompanyIdListMap[v.SellerId]
  255. if !ok {
  256. tmpCompanyIdList = make([]string, 0)
  257. }
  258. unusualRenewCompanyIdListMap[v.SellerId] = append(tmpCompanyIdList, fmt.Sprint(v.CompanyId))
  259. tmpCompanyIdList2, ok := unusualRenewCompanyIdListMap[v.ShareSellerId]
  260. if !ok {
  261. tmpCompanyIdList2 = make([]string, 0)
  262. }
  263. unusualRenewCompanyIdListMap[v.ShareSellerId] = append(tmpCompanyIdList2, fmt.Sprint(v.CompanyId))
  264. //unusualRenewCompanyTotal++
  265. }
  266. for sellerId, tmpCompanyIdList := range unusualRenewCompanyIdListMap {
  267. unusualRenewCompanyIdMap[sellerId] = strings.Join(tmpCompanyIdList, ",")
  268. }
  269. }
  270. adminDataMap = CompanyRenewDataMap{
  271. NotRenewMap: notRenewMap,
  272. NotRenewIdMap: notRenewIdMap,
  273. RenewFollowMap: renewFollowMap,
  274. RenewFollowIdMap: renewFollowIdMap,
  275. ExpireMap: expireMap,
  276. ExpireIdMap: expireIdMap,
  277. RenewMap: renewMap,
  278. RenewIdMap: renewIdMap,
  279. UnusualRenewMap: unusualRenewMap,
  280. UnusualRenewIdMap: unusualRenewCompanyIdMap,
  281. //UnusualRenewTotal: unusualRenewCompanyTotal,
  282. FormalCompanyTotal: formalCompanyTotal,
  283. StartDate: startDate.Format(utils.FormatDate),
  284. EndDate: endDate.Format(utils.FormatDate),
  285. }
  286. return
  287. }
  288. // SortGroupSellers 部门用户排序
  289. func SortGroupSellers(adminList []GroupRenewRecord) (list []GroupRenewRecord) {
  290. groupMap := make(map[int][]GroupRenewRecord)
  291. for _, admin := range adminList {
  292. index := 0 //0:普通私募,1:未分组,2:公募组,3:海外销售组
  293. switch admin.GroupId {
  294. case 17:
  295. index = 1
  296. case 10:
  297. index = 2
  298. case 19:
  299. index = 3
  300. default:
  301. index = 0
  302. }
  303. groupList, ok := groupMap[index]
  304. if !ok {
  305. groupList = make([]GroupRenewRecord, 0)
  306. }
  307. groupList = append(groupList, admin)
  308. groupMap[index] = groupList
  309. }
  310. var indexList = []int{0, 1, 2, 3}
  311. for _, index := range indexList {
  312. groupList, ok := groupMap[index]
  313. if ok {
  314. list = append(list, groupList...)
  315. }
  316. }
  317. return
  318. }