report.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. package statistic_report
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models"
  5. "hongze/hz_crm_api/models/company"
  6. "hongze/hz_crm_api/models/cygx"
  7. "hongze/hz_crm_api/models/statistic_report"
  8. "hongze/hz_crm_api/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. const ActiveViewNum = 50 //活跃客户的阅读报告次数(50次)
  14. // GetWeekData 周度数据
  15. func GetWeekData() (adminDataList []CompanyReportDataMap, firstDate time.Time, err error) {
  16. nowWeekMonday := utils.GetNowWeekMonday() //本周周一
  17. //fmt.Println(nowWeekMonday)
  18. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  19. //fmt.Println(nowWeekSunday)
  20. //nowWeekMonday = nowWeekMonday.AddDate(0, 0, -30) //上周周一
  21. //nowWeekSunday = nowWeekSunday.AddDate(0, 0, -30) //上周周日
  22. //adminDataMap, err := getSectionData(nowWeekMonday, nowWeekSunday)
  23. //if err != nil {
  24. // fmt.Println(err)
  25. // return
  26. //}
  27. //fmt.Println(adminDataMap)
  28. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7) //上周周一
  29. //fmt.Println(lastWeekMonday)
  30. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7) //上周周日
  31. //fmt.Println(lastWeekSunday)
  32. lastWeekMonday2 := lastWeekMonday.AddDate(0, 0, -7) //上上周周一
  33. //fmt.Println(lastWeekMonday)
  34. //最早的一天
  35. lastWeekSunday2 := lastWeekSunday.AddDate(0, 0, -7) //上上周周日
  36. //fmt.Println(lastWeekSunday)
  37. lastWeekMonday3 := lastWeekMonday2.AddDate(0, 0, -7) //上上上周周一
  38. //fmt.Println(lastWeekMonday)
  39. //最早的一天
  40. lastWeekSunday3 := lastWeekSunday2.AddDate(0, 0, -7) //上上上周周日
  41. //fmt.Println(lastWeekSunday)
  42. //最早的一天
  43. firstDate = lastWeekMonday3
  44. ch1 := make(chan CompanyReportDataMap, 0)
  45. ch2 := make(chan CompanyReportDataMap, 0)
  46. ch3 := make(chan CompanyReportDataMap, 0)
  47. ch4 := make(chan CompanyReportDataMap, 0)
  48. productId := 1
  49. go getSectionData(productId, lastWeekMonday, lastWeekSunday, ch1)
  50. go getSectionData(productId, nowWeekMonday, nowWeekSunday, ch2)
  51. go getSectionData(productId, lastWeekMonday2, lastWeekSunday2, ch3)
  52. go getSectionData(productId, lastWeekMonday3, lastWeekSunday3, ch4)
  53. var nowWeekCompanyReportDataMap, lastWeekCompanyReportDataMap, lastWeekCompanyReportDataMap3, lastWeekCompanyReportDataMap4 CompanyReportDataMap
  54. lastWeekCompanyReportDataMap = <-ch1
  55. close(ch1)
  56. nowWeekCompanyReportDataMap = <-ch2
  57. close(ch2)
  58. lastWeekCompanyReportDataMap3 = <-ch3
  59. close(ch3)
  60. lastWeekCompanyReportDataMap4 = <-ch4
  61. close(ch4)
  62. adminDataList = make([]CompanyReportDataMap, 0)
  63. adminDataList = append(adminDataList, nowWeekCompanyReportDataMap, lastWeekCompanyReportDataMap, lastWeekCompanyReportDataMap3, lastWeekCompanyReportDataMap4)
  64. return
  65. }
  66. // GetMonthData 月度数据
  67. func GetMonthData() (adminDataMapList []CompanyReportDataMap, firstDate time.Time, err error) { //三个协程返回
  68. nowMonthFirstDay := utils.GetNowMonthFirstDay() //本月第一天
  69. nowMonthLastDay := nowMonthFirstDay.AddDate(0, 1, 0).Add(-1 * time.Second) //本月最后一天
  70. lastMonthFirstDay1 := nowMonthFirstDay.AddDate(0, -1, 0) //上个月第一天
  71. lastMonthLastDay1 := lastMonthFirstDay1.AddDate(0, 1, 0).Add(-1 * time.Second) //上个月最后一天
  72. lastMonthFirstDay2 := nowMonthFirstDay.AddDate(0, -2, 0) //上上个月第一天
  73. lastMonthLastDay2 := lastMonthFirstDay2.AddDate(0, 1, 0).Add(-1 * time.Second) //上上个月最后一天
  74. lastMonthFirstDay3 := nowMonthFirstDay.AddDate(0, -3, 0) //上上上个月第一天
  75. lastMonthLastDay3 := lastMonthFirstDay3.AddDate(0, 1, 0).Add(-1 * time.Second) //上上上个月最后一天
  76. //最早的一天
  77. firstDate = lastMonthFirstDay3
  78. ch1 := make(chan CompanyReportDataMap, 0)
  79. ch2 := make(chan CompanyReportDataMap, 0)
  80. ch3 := make(chan CompanyReportDataMap, 0)
  81. ch4 := make(chan CompanyReportDataMap, 0)
  82. productId := 1
  83. go getSectionData(productId, nowMonthFirstDay, nowMonthLastDay, ch1)
  84. go getSectionData(productId, lastMonthFirstDay1, lastMonthLastDay1, ch2)
  85. go getSectionData(productId, lastMonthFirstDay2, lastMonthLastDay2, ch3)
  86. go getSectionData(productId, lastMonthFirstDay3, lastMonthLastDay3, ch4)
  87. var nowMonthCompanyReportDataMap, lastMonthCompanyReportDataMap1, lastMonthCompanyReportDataMap2, lastMonthCompanyReportDataMap3 CompanyReportDataMap
  88. nowMonthCompanyReportDataMap = <-ch1
  89. close(ch1)
  90. lastMonthCompanyReportDataMap1 = <-ch2
  91. close(ch2)
  92. lastMonthCompanyReportDataMap2 = <-ch3
  93. close(ch3)
  94. lastMonthCompanyReportDataMap3 = <-ch4
  95. close(ch4)
  96. adminDataMapList = make([]CompanyReportDataMap, 0)
  97. adminDataMapList = append(adminDataMapList, nowMonthCompanyReportDataMap, lastMonthCompanyReportDataMap1, lastMonthCompanyReportDataMap2, lastMonthCompanyReportDataMap3)
  98. return
  99. }
  100. // GetTimeIntervalData 区间数据
  101. func GetTimeIntervalData(productId int, startDate, endDate string) (adminDataMapList []CompanyReportDataMap, firstDate time.Time, err error) { //三个协程返回
  102. startDateTimer, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  103. endDateTimer, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  104. endDateTimer = endDateTimer.AddDate(0, 0, 1).Add(-1 * time.Second) //本月最后一天
  105. //最早的一天
  106. firstDate = startDateTimer
  107. ch1 := make(chan CompanyReportDataMap, 0)
  108. go getSectionData(productId, startDateTimer, endDateTimer, ch1)
  109. var adminDataMap CompanyReportDataMap
  110. adminDataMap = <-ch1
  111. close(ch1)
  112. adminDataMapList = make([]CompanyReportDataMap, 0)
  113. adminDataMapList = append(adminDataMapList, adminDataMap)
  114. return
  115. }
  116. type CompanyReportDataMap struct {
  117. TryOutMap map[int]int `description:"试用客户数"`
  118. TryOutIdMap map[int]string `description:"试用客户ids"`
  119. FormalMap map[int]int `description:"正式客户数"`
  120. FormalIdMap map[int]string `description:"正式客户ids"`
  121. ActiveMap map[int]int `description:"活跃客户状态数"`
  122. ActiveIdMap map[int]string `description:"活跃客户ids"`
  123. AllActiveMap map[int]int `description:"所有活跃客户状态数"`
  124. AllActiveIdMap map[int]string `description:"所有活跃客户ids"`
  125. NoIncrementalActiveMap map[int]int `description:"非新增试用客户的活跃客户状态数"`
  126. NoIncrementalActiveIdMap map[int]string `description:"非新增试用客户的活跃客户ids"`
  127. StartDate string `description:"开始日期"`
  128. EndDate string `description:"开始日期"`
  129. TryStagePushNum map[int]int `description:"试用(推进)状态的客户数量"`
  130. TryStageFollowNum map[int]int `description:"试用(跟踪)状态的客户数量"`
  131. TryStageReadyNum map[int]int `description:"试用(预备)状态的客户数量"`
  132. TryStageInitNum map[int]int `description:"试用(未分类)状态的客户数量"`
  133. }
  134. // getSectionData 获取周期数据
  135. func getSectionData(productId int, startDate, endDate time.Time, ch chan CompanyReportDataMap) (adminDataMap CompanyReportDataMap, err error) {
  136. defer func() {
  137. ch <- adminDataMap
  138. }()
  139. tryOutMap := make(map[int]int)
  140. formalMap := make(map[int]int)
  141. activeMap := make(map[int]int) //新增的试用客户数量(活跃)
  142. allActiveMap := make(map[int]int) //全部的试用客户数量(活跃)
  143. noIncrementalActiveMap := make(map[int]int) //非新增的试用客户数量(活跃)
  144. tryOutIdMap := make(map[int]string)
  145. formalIdMap := make(map[int]string)
  146. activeIdMap := make(map[int]string) //新增的试用客户id集合(活跃)
  147. allActiveIdMap := make(map[int]string) //全部的试用客户id集合(活跃)
  148. noIncrementalActiveIdMap := make(map[int]string) //非新增的试用客户id集合(活跃)
  149. //正式客户
  150. {
  151. var condition string
  152. var pars []interface{}
  153. if productId == 1 {
  154. condition = ` and a.approve_status="已审批" and a.approve_time >= ? and a.approve_time <= ? and b.contract_type="新签合同" `
  155. pars = append(pars, startDate, endDate)
  156. condition += ` and a.product_id=? and b.product_id=? `
  157. pars = append(pars, productId, productId)
  158. } else {
  159. condition = ` and a.approve_status="已审批" and b.start_date >= ? and b.start_date <= ? and b.contract_type="新签合同" `
  160. pars = append(pars, startDate, endDate)
  161. condition += ` and a.product_id=? and b.product_id=? `
  162. pars = append(pars, productId, productId)
  163. }
  164. data, tmpErr := company.GetCompanyApprovalReportRecordGroupList(condition, pars)
  165. if tmpErr != nil {
  166. err = tmpErr
  167. return
  168. }
  169. var formalIdSlice []string
  170. for _, v := range data {
  171. formalIdSlice = append(formalIdSlice, v.CompanyIds)
  172. formalMap[v.AdminId] = v.Num
  173. formalIdMap[v.AdminId] = v.CompanyIds
  174. }
  175. }
  176. //试用客户
  177. //试用客户id
  178. companyIdList := make([]string, 0)
  179. {
  180. var condition string
  181. var pars []interface{}
  182. //1、试用:新建试用客户,领取跨部门客户,申请领取同组流失客户,领取跨组流失客户,申请解冻客户的操作时间(审批通过时间),在所选时间段内的客户数量
  183. //`source` varchar(32) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '来源,新增:add\n领取:receive\n试用转正式:turn_positive\n冻结转试用:thaw\n试用延期:delay\n原销售申请领取流失客户:apply_receive\n正式客户申请续约:service_update\n正式客户新增补充协议:add_agreement',
  184. condition = ` and product_id=? and create_time >= ? and create_time <= ? and source in ("add","receive","thaw","apply_receive") `
  185. pars = append(pars, productId, startDate, endDate)
  186. data, tmpErr := company.GetGroupCompanyReportRecordGroupList(condition, pars)
  187. if tmpErr != nil {
  188. err = tmpErr
  189. return
  190. }
  191. var tryOutIdSlice []string
  192. for _, v := range data {
  193. tmpSlice := strings.Split(v.CompanyIds, ",")
  194. companyIdList = append(companyIdList, tmpSlice...)
  195. tryOutIdSlice = append(tryOutIdSlice, v.CompanyIds)
  196. tryOutMap[v.AdminId] = v.Num
  197. tryOutIdMap[v.AdminId] = v.CompanyIds
  198. }
  199. }
  200. //活跃
  201. {
  202. if productId == 1 {
  203. //增量试用客户
  204. if len(companyIdList) > 0 {
  205. moreActiveMap := make(map[int]int)
  206. moreActiveIdMap := make(map[int]string)
  207. lessActiveMap := make(map[int]int)
  208. lessActiveIdMap := make(map[int]string)
  209. // 获取小于等于结束时间 时 阅读次数大于50次的销售map
  210. {
  211. var condition string
  212. var pars []interface{}
  213. condition = ` and a.date <= ?`
  214. condition += fmt.Sprint(` and a.company_id in (`, strings.Join(companyIdList, ","), `) and b.product_id=?`)
  215. pars = append(pars, endDate, productId)
  216. data, tmpErr := models.GetCompanyViewTotalList(condition, pars, ActiveViewNum, "")
  217. if tmpErr != nil {
  218. err = tmpErr
  219. return
  220. }
  221. for _, v := range data {
  222. moreActiveMap[v.AdminId] = v.Num
  223. moreActiveIdMap[v.AdminId] = v.CompanyIds
  224. //fmt.Printf("date:%s;AdminId:%d ; Num:%d ; CompanyIds:%s ;\n", startDate, v.AdminId, v.Num, v.CompanyIds)
  225. }
  226. }
  227. // 获取小于开始时间 时 阅读次数大于50次的销售map
  228. {
  229. var condition string
  230. var pars []interface{}
  231. condition = ` and a.date < ?`
  232. condition += fmt.Sprint(` and a.company_id in (`, strings.Join(companyIdList, ","), `) and b.product_id=?`)
  233. pars = append(pars, startDate, productId)
  234. data, tmpErr := models.GetCompanyViewTotalList(condition, pars, ActiveViewNum, "")
  235. if tmpErr != nil {
  236. err = tmpErr
  237. return
  238. }
  239. //遍历所有之前小于50次的阅读记录的客户,
  240. for _, v := range data {
  241. //fmt.Printf("====date:%s;AdminId:%d ; Num:%d ; CompanyIds:%s ;\n", startDate, v.AdminId, v.Num, v.CompanyIds)
  242. lessActiveMap[v.AdminId] = v.Num
  243. lessActiveIdMap[v.AdminId] = v.CompanyIds
  244. }
  245. }
  246. //遍历最终的日期,然后 取两个数据的差集(差集就是这两个时间段内从 小于50 变成 大于等于50次 的客户)
  247. for adminId, moreIdStr := range moreActiveIdMap {
  248. tmpMoreIdStr := moreIdStr
  249. tmpMoreIdSlice := make([]string, 0)
  250. if tmpMoreIdStr != "" {
  251. tmpMoreIdSlice = strings.Split(tmpMoreIdStr, ",")
  252. }
  253. //判读在新增试用的销售map中 是否存在 该销售的
  254. if activeIds, ok := lessActiveIdMap[adminId]; ok {
  255. //计算出阅读次数大于等于50 与 阅读次数小于50 的客户差集
  256. tmpMoreIdSlice = utils.Minus(tmpMoreIdSlice, strings.Split(activeIds, ","))
  257. if len(tmpMoreIdSlice) > 0 {
  258. tmpMoreIdStr = strings.Join(tmpMoreIdSlice, ",")
  259. } else {
  260. tmpMoreIdStr = ""
  261. }
  262. }
  263. activeMap[adminId] = len(tmpMoreIdSlice)
  264. activeIdMap[adminId] = tmpMoreIdStr
  265. }
  266. }
  267. //全部试用客户
  268. {
  269. moreActiveMap := make(map[int]int)
  270. moreActiveIdMap := make(map[int]string)
  271. lessActiveMap := make(map[int]int)
  272. lessActiveIdMap := make(map[int]string)
  273. // 获取小于等于结束时间 时 阅读次数大于等于50次的销售map
  274. {
  275. var condition string
  276. var pars []interface{}
  277. condition = ` and a.date <= ? and b.product_id=? and b.status="试用" `
  278. pars = append(pars, endDate, productId)
  279. data, tmpErr := models.GetAllCompanyViewTotalList(condition, pars, ActiveViewNum, "")
  280. if tmpErr != nil {
  281. err = tmpErr
  282. return
  283. }
  284. for _, v := range data {
  285. moreActiveMap[v.AdminId] = v.Num
  286. moreActiveIdMap[v.AdminId] = v.CompanyIds
  287. //fmt.Printf("date:%s;AdminId:%d ; Num:%d ; CompanyIds:%s ;\n", startDate, v.AdminId, v.Num, v.CompanyIds)
  288. }
  289. }
  290. // 获取小于开始时间 时 阅读次数大于50次的销售map
  291. {
  292. var condition string
  293. var pars []interface{}
  294. condition = ` and a.date < ? and b.product_id=? and b.status="试用" `
  295. pars = append(pars, startDate, productId)
  296. data, tmpErr := models.GetAllCompanyViewTotalList(condition, pars, ActiveViewNum, "")
  297. if tmpErr != nil {
  298. err = tmpErr
  299. return
  300. }
  301. //遍历所有之前小于50次的阅读记录的客户,
  302. for _, v := range data {
  303. //fmt.Printf("====date:%s;AdminId:%d ; Num:%d ; CompanyIds:%s ;\n", startDate, v.AdminId, v.Num, v.CompanyIds)
  304. lessActiveMap[v.AdminId] = v.Num
  305. lessActiveIdMap[v.AdminId] = v.CompanyIds
  306. }
  307. }
  308. //遍历最终的日期,然后 取两个数据的差集(差集就是这两个时间段内从 小于50 变成 大于等于50次 的客户)
  309. for adminId, moreIdStr := range moreActiveIdMap {
  310. tmpMoreIdStr := moreIdStr
  311. tmpMoreIdSlice := make([]string, 0)
  312. if tmpMoreIdStr != "" {
  313. tmpMoreIdSlice = strings.Split(tmpMoreIdStr, ",")
  314. }
  315. //判读在新增试用的销售map中 是否存在 该销售的
  316. if activeIds, ok := lessActiveIdMap[adminId]; ok {
  317. //计算出阅读次数大于等于50 与 阅读次数小于50 的客户差集
  318. tmpMoreIdSlice = utils.Minus(tmpMoreIdSlice, strings.Split(activeIds, ","))
  319. if len(tmpMoreIdSlice) > 0 {
  320. tmpMoreIdStr = strings.Join(tmpMoreIdSlice, ",")
  321. } else {
  322. tmpMoreIdStr = ""
  323. }
  324. }
  325. allActiveMap[adminId] = len(tmpMoreIdSlice)
  326. allActiveIdMap[adminId] = tmpMoreIdStr
  327. }
  328. }
  329. //非增量试用客户
  330. {
  331. //遍历所有达标的销售map
  332. for adminId, allIdStr := range allActiveIdMap {
  333. tmpNoIncrementalActiveIdStr := allIdStr
  334. tmpNoIncrementalActiveIdSlice := make([]string, 0)
  335. if tmpNoIncrementalActiveIdStr != "" {
  336. tmpNoIncrementalActiveIdSlice = strings.Split(tmpNoIncrementalActiveIdStr, ",")
  337. }
  338. //判读在新增试用的销售map中 是否存在 该销售的
  339. if activeIds, ok := activeIdMap[adminId]; ok {
  340. //计算出阅读次数大于等于50 与 阅读次数小于50 的客户差集
  341. tmpNoIncrementalActiveIdSlice = utils.Minus(tmpNoIncrementalActiveIdSlice, strings.Split(activeIds, ","))
  342. if len(tmpNoIncrementalActiveIdSlice) > 0 {
  343. tmpNoIncrementalActiveIdStr = strings.Join(tmpNoIncrementalActiveIdSlice, ",")
  344. } else {
  345. tmpNoIncrementalActiveIdStr = ""
  346. }
  347. //fmt.Print("adminId:", adminId, ";======allIdStr:", allIdStr, ";=====;activeIds:", activeIds, ";====tmpNoIncrementalActiveIdSlice:", tmpNoIncrementalActiveIdSlice)
  348. }
  349. //fmt.Println("====;tmpNoIncrementalActiveIdSlice:", tmpNoIncrementalActiveIdSlice, ";====count(tmpNoIncrementalActiveIdSlice):", len(tmpNoIncrementalActiveIdSlice))
  350. noIncrementalActiveMap[adminId] = len(tmpNoIncrementalActiveIdSlice)
  351. noIncrementalActiveIdMap[adminId] = tmpNoIncrementalActiveIdStr
  352. }
  353. }
  354. } else {
  355. companyIdMap := make(map[int]int)
  356. finalIdMap := make(map[int]int)
  357. idArr := make([]string, 0)
  358. var ids string
  359. startCh := make(chan cygx.InteractiveList, 0)
  360. endCh := make(chan cygx.InteractiveList, 0)
  361. //先获取开始时间互动量<50次的公司
  362. go CompanyInteractiveTotal(startDate.Format(utils.FormatDate), startCh)
  363. //再获取结束时间互动量>=50次的公司
  364. go CompanyInteractiveTotal(endDate.Format(utils.FormatDate), endCh)
  365. var startList cygx.InteractiveList
  366. var endList cygx.InteractiveList
  367. startList = <-startCh
  368. close(startCh)
  369. endList = <-endCh
  370. close(endCh)
  371. for id, v := range startList.TotalMap {
  372. if v < 50 {
  373. companyIdMap[id] = v
  374. }
  375. }
  376. for id, v := range endList.TotalMap {
  377. _, ok := companyIdMap[id]
  378. if v >= 50 && ok {
  379. finalIdMap[id] = v
  380. }
  381. }
  382. fmt.Println("finalIdMap:", finalIdMap)
  383. for k := range finalIdMap {
  384. idArr = append(idArr, strconv.Itoa(k))
  385. }
  386. ids = strings.Join(idArr, ",")
  387. fmt.Println("ids:", ids)
  388. var condition string
  389. var pars []interface{}
  390. if len(ids) > 0 {
  391. condition = ` AND company_id IN ( ` + ids + ` ) `
  392. pars = append(pars, productId, startDate, endDate)
  393. list, tmpErr := cygx.GetCompanyCountGroupBySeller(condition)
  394. if tmpErr != nil {
  395. err = tmpErr
  396. return
  397. }
  398. for _, item := range list {
  399. allActiveMap[item.AdminId] = item.Num
  400. allActiveIdMap[item.AdminId] = item.CompanyIds
  401. }
  402. }
  403. }
  404. //fmt.Println(allActiveIdMap)
  405. //fmt.Println(activeIdMap)
  406. //fmt.Println("noIncrementalActiveMap:", noIncrementalActiveMap)
  407. //fmt.Println("noIncrementalActiveIdMap:", noIncrementalActiveIdMap)
  408. }
  409. adminDataMap = CompanyReportDataMap{
  410. TryOutMap: tryOutMap,
  411. TryOutIdMap: tryOutIdMap,
  412. FormalMap: formalMap,
  413. FormalIdMap: formalIdMap,
  414. ActiveMap: activeMap,
  415. ActiveIdMap: activeIdMap,
  416. AllActiveMap: allActiveMap,
  417. AllActiveIdMap: allActiveIdMap,
  418. NoIncrementalActiveMap: noIncrementalActiveMap,
  419. NoIncrementalActiveIdMap: noIncrementalActiveIdMap,
  420. StartDate: startDate.Format(utils.FormatDate),
  421. EndDate: endDate.Format(utils.FormatDate),
  422. }
  423. return
  424. }
  425. // GetDateData 某天的存量客户数据
  426. func GetDateData(date string, productId int) (adminDataMapList []CompanyReportDataMap, firstDate time.Time, err error) { //三个协程返回
  427. dateTimer, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  428. //最早的一天
  429. firstDate = dateTimer
  430. ch1 := make(chan CompanyReportDataMap, 0)
  431. go getStackDateData(productId, dateTimer, ch1)
  432. var adminDataMap CompanyReportDataMap
  433. adminDataMap = <-ch1
  434. close(ch1)
  435. adminDataMapList = make([]CompanyReportDataMap, 0)
  436. adminDataMapList = append(adminDataMapList, adminDataMap)
  437. return
  438. }
  439. // getSectionData 获取存量客户某天数据
  440. func getStackDateData(productId int, date time.Time, ch chan CompanyReportDataMap) (adminDataMap CompanyReportDataMap, err error) {
  441. defer func() {
  442. ch <- adminDataMap
  443. }()
  444. tryOutMap := make(map[int]int)
  445. tryStagePushMap := make(map[int]int)
  446. tryStageFollowMap := make(map[int]int)
  447. tryStageReadyMap := make(map[int]int)
  448. tryStageInitMap := make(map[int]int)
  449. formalMap := make(map[int]int)
  450. activeMap := make(map[int]int)
  451. //正式客户
  452. {
  453. var condition string
  454. var pars []interface{}
  455. condition = ` and create_date = ? and status = ? and product_id = ?`
  456. pars = append(pars, date, "正式", productId)
  457. data, tmpErr := statistic_report.GetGroupCompanyReportRecordGroupList(condition, pars)
  458. if tmpErr != nil {
  459. err = tmpErr
  460. return
  461. }
  462. for _, v := range data {
  463. formalMap[v.AdminId] = v.Num
  464. }
  465. }
  466. //试用客户id
  467. companyIdList := make([]string, 0)
  468. //试用客户
  469. {
  470. data, tmpErr := statistic_report.GetTryGroupCompanyReportRecordGroupList(date, productId)
  471. if tmpErr != nil {
  472. err = tmpErr
  473. return
  474. }
  475. for _, v := range data {
  476. tmpSlice := strings.Split(v.CompanyIds, ",")
  477. companyIdList = append(companyIdList, tmpSlice...)
  478. tryOutMap[v.AdminId] += v.Num
  479. switch v.TryStage {
  480. case 1:
  481. tryStageInitMap[v.AdminId] += v.Num
  482. case 2:
  483. tryStagePushMap[v.AdminId] += v.Num
  484. case 3:
  485. tryStageFollowMap[v.AdminId] += v.Num
  486. case 4:
  487. tryStageReadyMap[v.AdminId] += v.Num
  488. default:
  489. tryStageInitMap[v.AdminId] += v.Num
  490. }
  491. }
  492. }
  493. //活跃
  494. {
  495. if productId == 1 {
  496. var condition string
  497. var pars []interface{}
  498. condition = ` and a.date <= ?`
  499. pars = append(pars, date)
  500. if len(companyIdList) > 0 {
  501. condition += fmt.Sprint(` and a.company_id in (`, strings.Join(companyIdList, ","), `) and b.product_id=? `)
  502. pars = append(pars, productId)
  503. data, tmpErr := models.GetCompanyViewTotalList(condition, pars, ActiveViewNum, "")
  504. if tmpErr != nil {
  505. err = tmpErr
  506. return
  507. }
  508. for _, v := range data {
  509. //fmt.Println(v)
  510. activeMap[v.AdminId] = v.Num
  511. }
  512. }
  513. } else {
  514. companyIdMap := make(map[int]int)
  515. finalIdMap := make(map[int]int)
  516. idArr := make([]string, 0)
  517. var ids string
  518. endCh := make(chan cygx.InteractiveList, 0)
  519. //获取结束时间互动量>=50次的公司
  520. go CompanyInteractiveTotal(date.Format(utils.FormatDate), endCh)
  521. var endList cygx.InteractiveList
  522. endList = <-endCh
  523. close(endCh)
  524. for id, v := range endList.TotalMap {
  525. _, ok := companyIdMap[id]
  526. if v >= 50 && ok {
  527. finalIdMap[id] = v
  528. }
  529. }
  530. for k := range finalIdMap {
  531. idArr = append(idArr, strconv.Itoa(k))
  532. }
  533. ids = strings.Join(idArr, ",")
  534. fmt.Println("ids:", ids)
  535. var condition string
  536. if len(ids) > 0 {
  537. condition = ` AND company_id IN ( ` + ids + ` ) `
  538. list, tmpErr := cygx.GetCompanyCountGroupBySeller(condition)
  539. if tmpErr != nil {
  540. err = tmpErr
  541. return
  542. }
  543. for _, item := range list {
  544. activeMap[item.AdminId] = item.Num
  545. //allActiveIdMap[item.AdminId] = item.CompanyIds
  546. }
  547. }
  548. }
  549. }
  550. adminDataMap = CompanyReportDataMap{
  551. TryOutMap: tryOutMap,
  552. FormalMap: formalMap,
  553. ActiveMap: activeMap,
  554. StartDate: date.Format(utils.FormatDate),
  555. EndDate: date.Format(utils.FormatDate),
  556. TryStageFollowNum: tryStageFollowMap,
  557. TryStagePushNum: tryStagePushMap,
  558. TryStageReadyNum: tryStageReadyMap,
  559. TryStageInitNum: tryStageInitMap,
  560. }
  561. return
  562. }
  563. type IncrementalCompanyListReq struct {
  564. PageSize int `description:"每页数据条数"`
  565. CurrentIndex int `description:"当前页页码,从1开始"`
  566. SortParam string `description:"排序字段参数,用来排序的字段, 枚举值:'viewTotal':总阅读次数 、 'viewTime':阅读时间 、 'roadShowTotal':累计路演次数 、'expireDay':到期时间 、 'createTime':创建时间 、 'formalTime': 转正时间 、 'freezeTime':冻结时间 、'lossTime':流失时间" `
  567. SortType string `description:"如何排序,是正序还是倒序,枚举值:asc 正序,desc 倒叙 "`
  568. CompanyIds string `description:"标签,多个标签用、隔开;长度255"`
  569. }
  570. // GetWeekDataNum 周度数据
  571. func GetWeekDataNum(nowWeekMonday time.Time, productId, dataNum int) (adminDataList []CompanyReportDataMap, firstDate time.Time, err error) {
  572. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  573. chanList := make([]chan CompanyReportDataMap, 0)
  574. for i := 0; i < dataNum; i++ {
  575. tmpCh := make(chan CompanyReportDataMap, 0)
  576. chanList = append(chanList, tmpCh)
  577. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7*i)
  578. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7*i)
  579. // 第一个开始日期
  580. firstDate = lastWeekMonday
  581. go getSectionData(productId, lastWeekMonday, lastWeekSunday, tmpCh)
  582. }
  583. adminDataList = make([]CompanyReportDataMap, 0)
  584. for _, vChan := range chanList {
  585. var weekCompanyReportDataMap CompanyReportDataMap
  586. weekCompanyReportDataMap = <-vChan
  587. close(vChan)
  588. adminDataList = append(adminDataList, weekCompanyReportDataMap)
  589. }
  590. return
  591. }
  592. // GetMonthDataNum 月度数据
  593. func GetMonthDataNum(nowMonthFirstDay time.Time, productId, dataNum int) (adminDataList []CompanyReportDataMap, firstDate time.Time, err error) { //三个协程返回
  594. //nowMonthLastDay := nowMonthFirstDay.AddDate(0, 1, 0).Add(-1 * time.Second) //本月最后一天
  595. chanList := make([]chan CompanyReportDataMap, 0)
  596. for i := 0; i < dataNum; i++ {
  597. tmpCh := make(chan CompanyReportDataMap, 0)
  598. chanList = append(chanList, tmpCh)
  599. lastMonthFirstDay := nowMonthFirstDay.AddDate(0, -1*i, 0) //上个月第一天
  600. lastMonthLastDay := nowMonthFirstDay.AddDate(0, -1*(i-1), 0).Add(-1 * time.Second) //上个月最后一天
  601. // 第一个开始日期
  602. firstDate = lastMonthFirstDay
  603. go getSectionData(productId, lastMonthFirstDay, lastMonthLastDay, tmpCh)
  604. }
  605. adminDataList = make([]CompanyReportDataMap, 0)
  606. for _, vChan := range chanList {
  607. var weekCompanyReportDataMap CompanyReportDataMap
  608. weekCompanyReportDataMap = <-vChan
  609. close(vChan)
  610. adminDataList = append(adminDataList, weekCompanyReportDataMap)
  611. }
  612. return
  613. }
  614. // 获取互动数据
  615. func CompanyInteractiveTotal(date string, ch chan cygx.InteractiveList) (list cygx.InteractiveList, err error) {
  616. defer func() {
  617. ch <- list
  618. }()
  619. totalNumMap := make(map[int]int)
  620. var condition string
  621. if date != "" {
  622. date += " 23:59:59"
  623. condition += ` AND r.create_time <= '` + date + `' `
  624. }
  625. var errMsg string
  626. //condition := ` AND r.create_time < date_format(now(),'%Y-%m-%d') ` //过滤当天时间
  627. //totalEs, esList, err := services.SearcCygxArticleHistoryData(utils.IndexNameArticleHistory, "", "", date, "", 0, 0)
  628. //if err != nil {
  629. // errMsg = "获取Es文章阅读数量失败,Err:" + err.Error()
  630. // return
  631. //}
  632. //fmt.Println("len:", len(esList))
  633. //historyNum := int(totalEs)
  634. historyList, err := cygx.GetArticleHistoryGroupByCompany(condition)
  635. if err != nil {
  636. errMsg = "获取Es文章阅读数量失败,Err:" + err.Error()
  637. return
  638. }
  639. for _, item := range historyList {
  640. totalNumMap[item.CompanyId] += item.Count
  641. }
  642. //historyNum, err := cygx.GetCygxArticleHistoryCountByCompany(condition)
  643. //获取参会总数
  644. activityNumList, err := cygx.GetActivityMeetGroupByCompany(condition)
  645. if err != nil {
  646. errMsg = "获取参会总数失败,Err:" + err.Error()
  647. return
  648. }
  649. for _, item := range activityNumList {
  650. totalNumMap[item.CompanyId] += item.Count
  651. }
  652. //activityList, err := cygx.GetActivityMeetBySeller(condition)
  653. //if err != nil {
  654. // errMsg = "获取参会销售客户分组失败,Err:" + err.Error()
  655. // return
  656. //}
  657. //获取用户收藏记录总数
  658. articleCollectList, err := cygx.GetCygxArticleCollectCountGroupByCompany(condition)
  659. if err != nil {
  660. errMsg = "获取用户收藏记录总数失败,Err:" + err.Error()
  661. return
  662. }
  663. for _, item := range articleCollectList {
  664. totalNumMap[item.CompanyId] += item.Count
  665. }
  666. //collectList, err := cygx.GetCygxArticleCollectCountBySeller(condition)
  667. //if err != nil {
  668. // errMsg = "收藏列表数量销售分组,Err:" + err.Error()
  669. // return
  670. //}
  671. //获取用户关注总数
  672. industryFllowNumList, err := cygx.GetCygxIndustryFllowCountGroupByCompany(condition)
  673. if err != nil {
  674. errMsg = "获取用户关注总数失败,Err:" + err.Error()
  675. return
  676. }
  677. for _, item := range industryFllowNumList {
  678. totalNumMap[item.CompanyId] += item.Count
  679. }
  680. //industryFllowList, err := cygx.GetCygxArticleCollectCountBySeller(condition)
  681. //if err != nil {
  682. // errMsg = "关注产业数量销售分组,Err:" + err.Error()
  683. // return
  684. //}
  685. //获取关注作者总数
  686. departmentFollowNumList, err := cygx.GetCygArticleDepartmentFollowCountGroupByCompany(condition)
  687. if err != nil {
  688. errMsg = "获取关注作者总数失败,Err:" + err.Error()
  689. return
  690. }
  691. for _, item := range departmentFollowNumList {
  692. totalNumMap[item.CompanyId] += item.Count
  693. }
  694. //departmentFollowList, err := cygx.GetCygArticleDepartmentFollowCountBySeller(condition)
  695. //if err != nil {
  696. // errMsg = "获取关注作者总数销售分组,Err:" + err.Error()
  697. // return
  698. //}
  699. //获取搜索记录
  700. keyWordNumList, err := cygx.GetCygxSearchKeyWordCountGroupByCompany(condition)
  701. if err != nil {
  702. errMsg = "获取搜索记录失败,Err:" + err.Error()
  703. return
  704. }
  705. for _, item := range keyWordNumList {
  706. totalNumMap[item.CompanyId] += item.Count
  707. }
  708. //keyWordList, err := cygx.GetCygxSearchKeyWordCountBySeller(condition)
  709. //if err != nil {
  710. // errMsg = "获取关注作者总数销售分组,Err:" + err.Error()
  711. // return
  712. //}
  713. chartNumList, err := cygx.GetChartCountGroupByCompany(condition)
  714. if err != nil {
  715. errMsg = "获取总数失败,Err:" + err.Error()
  716. return
  717. }
  718. for _, item := range chartNumList {
  719. totalNumMap[item.CompanyId] += item.Count
  720. }
  721. //chartist, err := cygx.GetChartCountBySeller(condition)
  722. //if err != nil {
  723. // errMsg = "获取总数销售分组,Err:" + err.Error()
  724. // return
  725. //}
  726. fmt.Println(errMsg)
  727. //fmt.Println(historyNum)
  728. list = cygx.InteractiveList{
  729. TotalMap: totalNumMap,
  730. }
  731. return
  732. }
  733. // 获取互动数据列表
  734. //func CompanyInteractiveList(date string) (list cygx.InteractiveList, err error) {
  735. // totalNumMap := make(map[int]int)
  736. // var condition string
  737. // if date != "" {
  738. // date += " 23:59:59"
  739. // condition += ` AND r.create_time <= '` + date + `' `
  740. // }
  741. // var errMsg string
  742. // //condition := ` AND r.create_time < date_format(now(),'%Y-%m-%d') ` //过滤当天时间
  743. // totalEs, esList, err := services.SearcCygxArticleHistoryData(utils.IndexNameArticleHistory, "", "", date, "", 0, 0)
  744. // if err != nil {
  745. // errMsg = "获取Es文章阅读数量失败,Err:" + err.Error()
  746. // return
  747. // }
  748. // historyNum := int(totalEs)
  749. // //historyNum, err := cygx.GetCygxArticleHistoryCountByCompany(condition)
  750. // //获取参会总数
  751. // activityNumList, err := cygx.GetActivityMeetGroupByCompany(condition)
  752. // if err != nil {
  753. // errMsg = "获取参会总数失败,Err:" + err.Error()
  754. // return
  755. // }
  756. // for _, item := range activityNumList {
  757. // totalNumMap[item.CompanyId] += item.Count
  758. // }
  759. // activityList, err := cygx.GetActivityMeetBySeller(condition)
  760. // if err != nil {
  761. // errMsg = "获取参会销售客户分组失败,Err:" + err.Error()
  762. // return
  763. // }
  764. // //获取用户收藏记录总数
  765. // articleCollectList, err := cygx.GetCygxArticleCollectCountGroupByCompany(condition)
  766. // if err != nil {
  767. // errMsg = "获取用户收藏记录总数失败,Err:" + err.Error()
  768. // return
  769. // }
  770. // for _, item := range articleCollectList {
  771. // totalNumMap[item.CompanyId] += item.Count
  772. // }
  773. // collectList, err := cygx.GetCygxArticleCollectCountBySeller(condition)
  774. // if err != nil {
  775. // errMsg = "收藏列表数量销售分组,Err:" + err.Error()
  776. // return
  777. // }
  778. // //获取用户关注总数
  779. // industryFllowNumList, err := cygx.GetCygxIndustryFllowCountGroupByCompany(condition)
  780. // if err != nil {
  781. // errMsg = "获取用户关注总数失败,Err:" + err.Error()
  782. // return
  783. // }
  784. // for _, item := range industryFllowNumList {
  785. // totalNumMap[item.CompanyId] += item.Count
  786. // }
  787. // industryFllowList, err := cygx.GetCygxArticleCollectCountBySeller(condition)
  788. // if err != nil {
  789. // errMsg = "关注产业数量销售分组,Err:" + err.Error()
  790. // return
  791. // }
  792. // //获取关注作者总数
  793. // departmentFollowNumList, err := cygx.GetCygArticleDepartmentFollowCountGroupByCompany(condition)
  794. // if err != nil {
  795. // errMsg = "获取关注作者总数失败,Err:" + err.Error()
  796. // return
  797. // }
  798. // for _, item := range departmentFollowNumList {
  799. // totalNumMap[item.CompanyId] += item.Count
  800. // }
  801. // departmentFollowList, err := cygx.GetCygArticleDepartmentFollowCountBySeller(condition)
  802. // if err != nil {
  803. // errMsg = "获取关注作者总数销售分组,Err:" + err.Error()
  804. // return
  805. // }
  806. // //获取搜索记录
  807. // keyWordNumList, err := cygx.GetCygxSearchKeyWordCountGroupByCompany(condition)
  808. // if err != nil {
  809. // errMsg = "获取搜索记录失败,Err:" + err.Error()
  810. // return
  811. // }
  812. // for _, item := range keyWordNumList {
  813. // totalNumMap[item.CompanyId] += item.Count
  814. // }
  815. // keyWordList, err := cygx.GetCygxSearchKeyWordCountBySeller(condition)
  816. // if err != nil {
  817. // errMsg = "获取关注作者总数销售分组,Err:" + err.Error()
  818. // return
  819. // }
  820. //
  821. // chartNumList, err := cygx.GetChartCountGroupByCompany(condition)
  822. // if err != nil {
  823. // errMsg = "获取总数失败,Err:" + err.Error()
  824. // return
  825. // }
  826. // for _, item := range chartNumList {
  827. // totalNumMap[item.CompanyId] += item.Count
  828. // }
  829. // chartist, err := cygx.GetChartCountBySeller(condition)
  830. // if err != nil {
  831. // errMsg = "获取总数销售分组,Err:" + err.Error()
  832. // return
  833. // }
  834. // fmt.Println(errMsg)
  835. // fmt.Println(historyNum)
  836. // list = cygx.InteractiveList{
  837. // TotalMap: totalNumMap,
  838. // EsList: esList,
  839. // ActivityList: activityList,
  840. // CollectList: collectList,
  841. // IndustryFllowList: industryFllowList,
  842. // DepartmentFollowList: departmentFollowList,
  843. // KeyWordList: keyWordList,
  844. // Chartist: chartist,
  845. // }
  846. //
  847. // return
  848. //}