user_view_statistics.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "time"
  8. )
  9. // StatisticsUserView 汇总统计表
  10. func StatisticsUserViewBak(cont context.Context) (err error) {
  11. dateTime := time.Now().AddDate(0, 0, -1)
  12. dateStr := dateTime.Format(utils.FormatDate)
  13. count, err := models.GetUserViewStatisticsCount(dateStr)
  14. if err != nil {
  15. return
  16. }
  17. if count > 0 {
  18. return
  19. }
  20. {
  21. mobileViewMap := make(map[string]*models.UserViewMobileTotalSlice)
  22. list, tmpErr := models.GetCountUserViewHistoryByMobiles(dateStr)
  23. if tmpErr != nil {
  24. err = tmpErr
  25. return
  26. }
  27. for _, v := range list {
  28. mobileViewMap[v.Mobile] = v
  29. }
  30. list, tmpErr = models.GetReportViewMaxTimeByMobiles(dateStr)
  31. if tmpErr != nil {
  32. err = tmpErr
  33. return
  34. }
  35. for _, v := range list {
  36. if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
  37. userViewStatistics.Total += v.Total
  38. if v.ViewTime.After(userViewStatistics.ViewTime) {
  39. userViewStatistics.ViewTime = v.ViewTime
  40. }
  41. } else {
  42. mobileViewMap[v.Mobile] = v
  43. }
  44. }
  45. list, tmpErr = models.GetAdvisoryCountUserViewHistoryByMobiles(dateStr)
  46. if tmpErr != nil {
  47. err = tmpErr
  48. return
  49. }
  50. for _, v := range list {
  51. if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
  52. userViewStatistics.Total += v.Total
  53. if v.ViewTime.After(userViewStatistics.ViewTime) {
  54. userViewStatistics.ViewTime = v.ViewTime
  55. }
  56. } else {
  57. mobileViewMap[v.Mobile] = v
  58. }
  59. }
  60. if len(mobileViewMap) > 0 {
  61. tmpList := make([]*models.UserViewStatistics, 0)
  62. for mobile, userViewStatistics := range mobileViewMap {
  63. item := &models.UserViewStatistics{
  64. Mobile: mobile,
  65. RealName: userViewStatistics.RealName,
  66. CompanyName: userViewStatistics.CompanyName,
  67. ViewNum: userViewStatistics.Total,
  68. LastViewTime: userViewStatistics.ViewTime,
  69. Date: dateTime,
  70. CreateTime: time.Now(),
  71. }
  72. tmpList = append(tmpList, item)
  73. }
  74. //最后出来后,如果还有数据未插入,那么再去批量插入数据库
  75. if len(tmpList) > 0 {
  76. _ = models.AddMultiUserViewStatistics(tmpList)
  77. }
  78. }
  79. }
  80. //邮箱
  81. {
  82. emailViewMap := make(map[string]*models.UserViewEmailTotalSlice)
  83. list, tmpErr := models.GetCountUserViewHistoryByEmails(dateStr)
  84. if tmpErr != nil {
  85. err = tmpErr
  86. return
  87. }
  88. for _, v := range list {
  89. emailViewMap[v.Email] = v
  90. }
  91. list, tmpErr = models.GetReportViewMaxTimeByEmails(dateStr)
  92. if tmpErr != nil {
  93. err = tmpErr
  94. return
  95. }
  96. for _, v := range list {
  97. if userViewStatistics, ok := emailViewMap[v.Email]; ok {
  98. userViewStatistics.Total += v.Total
  99. if v.ViewTime.After(userViewStatistics.ViewTime) {
  100. userViewStatistics.ViewTime = v.ViewTime
  101. }
  102. } else {
  103. emailViewMap[v.Email] = v
  104. }
  105. }
  106. list, tmpErr = models.GetAdvisoryCountUserViewHistoryByEmails(dateStr)
  107. if tmpErr != nil {
  108. err = tmpErr
  109. return
  110. }
  111. for _, v := range list {
  112. if userViewStatistics, ok := emailViewMap[v.Email]; ok {
  113. userViewStatistics.Total += v.Total
  114. if v.ViewTime.After(userViewStatistics.ViewTime) {
  115. userViewStatistics.ViewTime = v.ViewTime
  116. }
  117. } else {
  118. emailViewMap[v.Email] = v
  119. }
  120. }
  121. if len(emailViewMap) > 0 {
  122. tmpList := make([]*models.UserViewStatistics, 0)
  123. for email, userViewStatistics := range emailViewMap {
  124. item := &models.UserViewStatistics{
  125. Email: email,
  126. RealName: userViewStatistics.RealName,
  127. CompanyName: userViewStatistics.CompanyName,
  128. ViewNum: userViewStatistics.Total,
  129. LastViewTime: userViewStatistics.ViewTime,
  130. Date: dateTime,
  131. CreateTime: time.Now(),
  132. }
  133. tmpList = append(tmpList, item)
  134. }
  135. //最后出来后,如果还有数据未插入,那么再去批量插入数据库
  136. if len(tmpList) > 0 {
  137. _ = models.AddMultiUserViewStatistics(tmpList)
  138. }
  139. }
  140. }
  141. StatisticsCompanyView(dateTime)
  142. return
  143. }
  144. // StatisticsCompanyView 客户统计
  145. func StatisticsCompanyViewBak(dateTime time.Time) (err error) {
  146. defer func() {
  147. if err != nil {
  148. fmt.Println("err:", err)
  149. }
  150. }()
  151. dateStr := dateTime.Format(utils.FormatDate)
  152. //fmt.Println(dateStr)
  153. count, err := models.GetCompanyViewStatisticsCount(dateStr)
  154. if err != nil {
  155. return
  156. }
  157. if count > 0 {
  158. return
  159. }
  160. companyViewMap := make(map[int]*models.CompanyViewTotalSlice)
  161. {
  162. list, tmpErr := models.GetCompanyViewMobileTotalList(dateStr)
  163. if tmpErr != nil {
  164. err = tmpErr
  165. return
  166. }
  167. for _, v := range list {
  168. companyViewMap[v.CompanyId] = v
  169. }
  170. }
  171. //fmt.Println(companyViewMap)
  172. //邮箱
  173. {
  174. list, tmpErr := models.GetCompanyViewEmailTotalList(dateStr)
  175. if tmpErr != nil {
  176. err = tmpErr
  177. return
  178. }
  179. for _, v := range list {
  180. if userViewStatistics, ok := companyViewMap[v.CompanyId]; ok {
  181. userViewStatistics.Total += v.Total
  182. } else {
  183. companyViewMap[v.CompanyId] = v
  184. }
  185. }
  186. }
  187. if len(companyViewMap) > 0 {
  188. tmpList := make([]*models.CompanyViewStatistics, 0)
  189. for _, companyView := range companyViewMap {
  190. item := &models.CompanyViewStatistics{
  191. CompanyId: companyView.CompanyId,
  192. ViewNum: companyView.Total,
  193. Date: dateTime,
  194. CreateTime: time.Now(),
  195. }
  196. tmpList = append(tmpList, item)
  197. }
  198. //最后出来后,如果还有数据未插入,那么再去批量插入数据库
  199. if len(tmpList) > 0 {
  200. err = models.AddMultiCompanyViewStatistics(tmpList)
  201. }
  202. }
  203. fmt.Println(dateStr, "end")
  204. return
  205. }
  206. // StatisticsUserView 汇总统计表(每天统一汇总一次)
  207. func StatisticsUserView(cont context.Context) (err error) {
  208. dateTime := time.Now().AddDate(0, 0, -1)
  209. statisticsUserView1Hour(dateTime)
  210. return
  211. }
  212. // StatisticsCompanyView 客户统计
  213. func StatisticsCompanyView(dateTime time.Time) (err error) {
  214. defer func() {
  215. if err != nil {
  216. fmt.Println("err:", err)
  217. }
  218. }()
  219. dateStr := dateTime.Format(utils.FormatDate)
  220. //fmt.Println(dateStr)
  221. count, err := models.GetCompanyViewStatisticsCount(dateStr)
  222. if err != nil {
  223. return
  224. }
  225. if count > 0 {
  226. return
  227. }
  228. companyViewMap := make(map[int]*models.CompanyViewTotalSlice)
  229. {
  230. list, tmpErr := models.GetCompanyViewMobileTotalList(dateStr)
  231. if tmpErr != nil {
  232. err = tmpErr
  233. return
  234. }
  235. for _, v := range list {
  236. companyViewMap[v.CompanyId] = v
  237. }
  238. }
  239. //fmt.Println(companyViewMap)
  240. //邮箱
  241. {
  242. list, tmpErr := models.GetCompanyViewEmailTotalList(dateStr)
  243. if tmpErr != nil {
  244. err = tmpErr
  245. return
  246. }
  247. for _, v := range list {
  248. if userViewStatistics, ok := companyViewMap[v.CompanyId]; ok {
  249. userViewStatistics.Total += v.Total
  250. } else {
  251. companyViewMap[v.CompanyId] = v
  252. }
  253. }
  254. }
  255. if len(companyViewMap) > 0 {
  256. for _, companyView := range companyViewMap {
  257. tmpCompanyViewStatistics, tmpErr := models.GetCompanyViewStatisticsByCompanyId(companyView.CompanyId, dateStr)
  258. if tmpErr != nil {
  259. if tmpErr.Error() == utils.ErrNoRow() {
  260. item := &models.CompanyViewStatistics{
  261. CompanyId: companyView.CompanyId,
  262. ViewNum: companyView.Total,
  263. Date: dateTime,
  264. CreateTime: time.Now(),
  265. }
  266. models.AddCompanyViewStatistics(item)
  267. }
  268. } else {
  269. tmpCompanyViewStatistics.ViewNum = companyView.Total
  270. tmpCompanyViewStatistics.Update([]string{"ViewNum"})
  271. }
  272. }
  273. }
  274. fmt.Println(dateStr, "end")
  275. return
  276. }
  277. // StatisticsUserView1Hour 每个小时汇总统计表
  278. func StatisticsUserView1Hour(cont context.Context) (err error) {
  279. //dateTime := time.Now().AddDate(0, 0, -1)
  280. dateTime := time.Now()
  281. statisticsUserView1Hour(dateTime)
  282. StatisticsCompanyView1Hour(dateTime)
  283. return
  284. }
  285. // statisticsUserView1Hour
  286. func statisticsUserView1Hour(dateTime time.Time) (err error) {
  287. //dateTime := time.Now().AddDate(0, 0, -1)
  288. dateStr := dateTime.Format(utils.FormatDate)
  289. {
  290. mobileViewMap := make(map[string]*models.UserViewMobileTotalSlice)
  291. list, tmpErr := models.GetCountUserViewHistoryByMobiles(dateStr)
  292. if tmpErr != nil {
  293. err = tmpErr
  294. return
  295. }
  296. for _, v := range list {
  297. mobileViewMap[v.Mobile] = v
  298. }
  299. list, tmpErr = models.GetReportViewMaxTimeByMobiles(dateStr)
  300. if tmpErr != nil {
  301. err = tmpErr
  302. return
  303. }
  304. for _, v := range list {
  305. if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
  306. userViewStatistics.Total += v.Total
  307. if v.ViewTime.After(userViewStatistics.ViewTime) {
  308. userViewStatistics.ViewTime = v.ViewTime
  309. }
  310. } else {
  311. mobileViewMap[v.Mobile] = v
  312. }
  313. }
  314. list, tmpErr = models.GetAdvisoryCountUserViewHistoryByMobiles(dateStr)
  315. if tmpErr != nil {
  316. err = tmpErr
  317. return
  318. }
  319. for _, v := range list {
  320. if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
  321. userViewStatistics.Total += v.Total
  322. if v.ViewTime.After(userViewStatistics.ViewTime) {
  323. userViewStatistics.ViewTime = v.ViewTime
  324. }
  325. } else {
  326. mobileViewMap[v.Mobile] = v
  327. }
  328. }
  329. if len(mobileViewMap) > 0 {
  330. for mobile, userViewStatistics := range mobileViewMap {
  331. tmpUserViewStatistics, tmpErr := models.GetUserViewStatisticsByMobile(mobile, dateStr)
  332. if tmpErr != nil {
  333. if tmpErr.Error() == utils.ErrNoRow() {
  334. item := &models.UserViewStatistics{
  335. Mobile: mobile,
  336. RealName: userViewStatistics.RealName,
  337. CompanyName: userViewStatistics.CompanyName,
  338. ViewNum: userViewStatistics.Total,
  339. LastViewTime: userViewStatistics.ViewTime,
  340. Date: dateTime,
  341. CreateTime: time.Now(),
  342. }
  343. models.AddUserViewStatistics(item)
  344. }
  345. } else {
  346. tmpUserViewStatistics.ViewNum = userViewStatistics.Total
  347. tmpUserViewStatistics.LastViewTime = userViewStatistics.ViewTime
  348. tmpUserViewStatistics.Update([]string{"ViewNum", "LastViewTime"})
  349. }
  350. }
  351. }
  352. }
  353. //邮箱
  354. {
  355. emailViewMap := make(map[string]*models.UserViewEmailTotalSlice)
  356. list, tmpErr := models.GetCountUserViewHistoryByEmails(dateStr)
  357. if tmpErr != nil {
  358. err = tmpErr
  359. return
  360. }
  361. for _, v := range list {
  362. emailViewMap[v.Email] = v
  363. }
  364. list, tmpErr = models.GetReportViewMaxTimeByEmails(dateStr)
  365. if tmpErr != nil {
  366. err = tmpErr
  367. return
  368. }
  369. for _, v := range list {
  370. if userViewStatistics, ok := emailViewMap[v.Email]; ok {
  371. userViewStatistics.Total += v.Total
  372. if v.ViewTime.After(userViewStatistics.ViewTime) {
  373. userViewStatistics.ViewTime = v.ViewTime
  374. }
  375. } else {
  376. emailViewMap[v.Email] = v
  377. }
  378. }
  379. list, tmpErr = models.GetAdvisoryCountUserViewHistoryByEmails(dateStr)
  380. if tmpErr != nil {
  381. err = tmpErr
  382. return
  383. }
  384. for _, v := range list {
  385. if userViewStatistics, ok := emailViewMap[v.Email]; ok {
  386. userViewStatistics.Total += v.Total
  387. if v.ViewTime.After(userViewStatistics.ViewTime) {
  388. userViewStatistics.ViewTime = v.ViewTime
  389. }
  390. } else {
  391. emailViewMap[v.Email] = v
  392. }
  393. }
  394. if len(emailViewMap) > 0 {
  395. for email, userViewStatistics := range emailViewMap {
  396. tmpUserViewStatistics, tmpErr := models.GetUserViewStatisticsByEmail(email, dateStr)
  397. if tmpErr != nil {
  398. if tmpErr.Error() == utils.ErrNoRow() {
  399. item := &models.UserViewStatistics{
  400. Email: email,
  401. RealName: userViewStatistics.RealName,
  402. CompanyName: userViewStatistics.CompanyName,
  403. ViewNum: userViewStatistics.Total,
  404. LastViewTime: userViewStatistics.ViewTime,
  405. Date: dateTime,
  406. CreateTime: time.Now(),
  407. }
  408. models.AddUserViewStatistics(item)
  409. }
  410. } else {
  411. tmpUserViewStatistics.ViewNum = userViewStatistics.Total
  412. tmpUserViewStatistics.LastViewTime = userViewStatistics.ViewTime
  413. tmpUserViewStatistics.Update([]string{"ViewNum", "LastViewTime"})
  414. }
  415. }
  416. }
  417. }
  418. StatisticsCompanyView1Hour(dateTime)
  419. return
  420. }
  421. // StatisticsCompanyView1Hour 每个小时客户统计
  422. func StatisticsCompanyView1Hour(dateTime time.Time) (err error) {
  423. defer func() {
  424. if err != nil {
  425. fmt.Println("err:", err)
  426. }
  427. }()
  428. dateStr := dateTime.Format(utils.FormatDate)
  429. //fmt.Println(dateStr)
  430. companyViewMap := make(map[int]*models.CompanyViewTotalSlice)
  431. {
  432. list, tmpErr := models.GetCompanyViewMobileTotalList(dateStr)
  433. if tmpErr != nil {
  434. err = tmpErr
  435. return
  436. }
  437. for _, v := range list {
  438. companyViewMap[v.CompanyId] = v
  439. }
  440. }
  441. //fmt.Println(companyViewMap)
  442. //邮箱
  443. {
  444. list, tmpErr := models.GetCompanyViewEmailTotalList(dateStr)
  445. if tmpErr != nil {
  446. err = tmpErr
  447. return
  448. }
  449. for _, v := range list {
  450. if userViewStatistics, ok := companyViewMap[v.CompanyId]; ok {
  451. userViewStatistics.Total += v.Total
  452. } else {
  453. companyViewMap[v.CompanyId] = v
  454. }
  455. }
  456. }
  457. if len(companyViewMap) > 0 {
  458. for _, companyView := range companyViewMap {
  459. tmpCompanyViewStatistics, tmpErr := models.GetCompanyViewStatisticsByCompanyId(companyView.CompanyId, dateStr)
  460. if tmpErr != nil {
  461. if tmpErr.Error() == utils.ErrNoRow() {
  462. item := &models.CompanyViewStatistics{
  463. CompanyId: companyView.CompanyId,
  464. ViewNum: companyView.Total,
  465. Date: dateTime,
  466. CreateTime: time.Now(),
  467. }
  468. models.AddCompanyViewStatistics(item)
  469. }
  470. } else {
  471. tmpCompanyViewStatistics.ViewNum = companyView.Total
  472. tmpCompanyViewStatistics.Update([]string{"ViewNum"})
  473. }
  474. }
  475. }
  476. fmt.Println(dateStr, "end")
  477. return
  478. }
  479. //func Fix() {
  480. // StatisticsUserView1Hour(nil)
  481. //
  482. // //dateTime, _ := time.ParseInLocation(utils.FormatDate, "2021-03-04", time.Local)
  483. // //lastDateTime, _ := time.ParseInLocation(utils.FormatDate, "2022-04-08", time.Local)
  484. // //i := 0
  485. // //for {
  486. // // tmpDateTime := dateTime.AddDate(0, 0, i)
  487. // // if tmpDateTime.After(lastDateTime) { //晚于昨天,那么就结束循环
  488. // // break
  489. // // }
  490. // // StatisticsCompanyView(tmpDateTime)
  491. // // i++
  492. // //}
  493. //
  494. //}