company_statistic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongze_task/models"
  5. "hongze/hongze_task/utils"
  6. "time"
  7. "context"
  8. )
  9. //存量客户数据统计
  10. func StackCompanyStatistic(cont context.Context) (err error) {
  11. defer func() {
  12. if err != nil {
  13. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "存量客户数据统计 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  14. }
  15. }()
  16. dayStr := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) //截止到昨天的数据
  17. //查询昨天的数据有没有生成,如果没有生成的话,那么重新生成
  18. count, err := models.GetStackCompanyCount(dayStr)
  19. if err != nil {
  20. fmt.Println("查询昨天的数据是否生成语句 执行异常:", err.Error())
  21. return
  22. }
  23. //昨天数据有生成,那么就不往下执行了
  24. if count > 0 {
  25. fmt.Println("昨天的数据已经生成,不允许重复生成")
  26. return
  27. }
  28. total, list, err := models.GetStackCompanyListV1()
  29. if err != nil {
  30. fmt.Println("查询新签客户数(存量客户)异常:", err.Error())
  31. return
  32. }
  33. fmt.Println("total:", total)
  34. //fmt.Println(list)
  35. for _, company := range list {
  36. item := models.StackCompanyStatistic{
  37. CompanyId: company.CompanyId,
  38. CompanyName: company.CompanyName,
  39. ProductId: company.ProductId,
  40. ProductName: company.ProductName,
  41. ContractNum: company.Count,
  42. SellerId: company.SellerId,
  43. SellerName: company.SellerName,
  44. GroupId: company.GroupId,
  45. DepartmentId: company.DepartmentId,
  46. Date: dayStr, //截止到昨天的数据
  47. StartDate: company.StartDate,
  48. EndDate: company.EndDate,
  49. RegionType: company.RegionType,
  50. CreateTime: time.Now(),
  51. }
  52. if company.ProductStatus == "正式" {
  53. //正式客户
  54. if company.Count == 1 {
  55. //新签客户数
  56. item.Type = "新签客户"
  57. } else {
  58. item.Type = "续约客户"
  59. }
  60. } else {
  61. //未续约客户
  62. item.Type = "未续约客户"
  63. continue
  64. }
  65. addErr := models.AddStackCompanyStatistic(&item)
  66. if addErr != nil {
  67. fmt.Println("存量客户数据统计,插入数据异常:", addErr)
  68. }
  69. }
  70. total, notRenewalCompanyList, err := models.GetNotRenewalCompanyTotalV1(time.Now().Format(utils.FormatDateTime))
  71. if err != nil {
  72. fmt.Println("查询未续约客户数(存量客户)异常:", err.Error())
  73. return
  74. }
  75. fmt.Println("total:", total)
  76. //fmt.Println(list)
  77. for _, company := range notRenewalCompanyList {
  78. endDateTime, err := time.Parse(utils.FormatDateTime, company.CreateTime)
  79. if err != nil {
  80. fmt.Println("查询未续约客户数,插入数据异常:", err)
  81. }
  82. item := models.StackCompanyStatistic{
  83. CompanyId: company.CompanyId,
  84. CompanyName: company.CompanyName,
  85. ProductId: company.ProductId,
  86. ProductName: company.ProductName,
  87. //ContractNum: company.Count,
  88. SellerId: company.SellerId,
  89. SellerName: company.SellerName,
  90. GroupId: company.GroupId,
  91. DepartmentId: company.DepartmentId,
  92. Date: dayStr, //截止到昨天的数据
  93. StartDate: company.StartDate,
  94. EndDate: endDateTime.Format(utils.FormatDate),
  95. RegionType: company.RegionType,
  96. CreateTime: time.Now(),
  97. Type: "未续约客户",
  98. }
  99. addErr := models.AddStackCompanyStatistic(&item)
  100. if addErr != nil {
  101. fmt.Println("存量客户数据统计,插入数据异常:", addErr)
  102. }
  103. }
  104. return
  105. }