package services import ( "fmt" "hongze/hongze_task/models" "hongze/hongze_task/utils" "time" ) //存量客户数据统计 func StackCompanyStatistic() (err error) { defer func() { if err != nil { go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "存量客户数据统计 ErrMsg:"+err.Error(), utils.EmailSendToUsers) } }() dayStr := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) //截止到昨天的数据 //查询昨天的数据有没有生成,如果没有生成的话,那么重新生成 count, err := models.GetStackCompanyCount(dayStr) if err != nil { fmt.Println("查询昨天的数据是否生成语句 执行异常:", err.Error()) return } //昨天数据有生成,那么就不往下执行了 if count > 0 { fmt.Println("昨天的数据已经生成,不允许重复生成") return } total, list, err := models.GetStackCompanyListV1() if err != nil { fmt.Println("查询新签客户数(存量客户)异常:", err.Error()) return } fmt.Println("total:", total) //fmt.Println(list) for _, company := range list { item := models.StackCompanyStatistic{ CompanyId: company.CompanyId, CompanyName: company.CompanyName, ProductId: company.ProductId, ProductName: company.ProductName, ContractNum: company.Count, SellerId: company.SellerId, SellerName: company.SellerName, GroupId: company.GroupId, DepartmentId: company.DepartmentId, Date: dayStr, //截止到昨天的数据 StartDate: company.StartDate, EndDate: company.EndDate, RegionType: company.RegionType, CreateTime: time.Now(), } if company.ProductStatus == "正式" { //正式客户 if company.Count == 1 { //新签客户数 item.Type = "新签客户" } else { item.Type = "续约客户" } } else { //未续约客户 item.Type = "未续约客户" continue } addErr := models.AddStackCompanyStatistic(&item) if addErr != nil { fmt.Println("存量客户数据统计,插入数据异常:", addErr) } } total, notRenewalCompanyList, err := models.GetNotRenewalCompanyTotalV1(time.Now().Format(utils.FormatDateTime)) if err != nil { fmt.Println("查询未续约客户数(存量客户)异常:", err.Error()) return } fmt.Println("total:", total) //fmt.Println(list) for _, company := range notRenewalCompanyList { endDateTime, err := time.Parse(utils.FormatDateTime, company.CreateTime) if err != nil { fmt.Println("查询未续约客户数,插入数据异常:", err) } item := models.StackCompanyStatistic{ CompanyId: company.CompanyId, CompanyName: company.CompanyName, ProductId: company.ProductId, ProductName: company.ProductName, //ContractNum: company.Count, SellerId: company.SellerId, SellerName: company.SellerName, GroupId: company.GroupId, DepartmentId: company.DepartmentId, Date: dayStr, //截止到昨天的数据 StartDate: company.StartDate, EndDate: endDateTime.Format(utils.FormatDate), RegionType: company.RegionType, CreateTime: time.Now(), Type: "未续约客户", } addErr := models.AddStackCompanyStatistic(&item) if addErr != nil { fmt.Println("存量客户数据统计,插入数据异常:", addErr) } } return }