package services import ( "context" "fmt" "hongze/hongze_task/models" "hongze/hongze_task/utils" "time" ) // StackCompanyStatisticOld 存量客户数据统计(2021-10-26 14:12:27 过期失效) func StackCompanyStatisticOld(cont context.Context) (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 } // StackCompanyStatistic 存量客户数据统计 func StackCompanyStatistic(cont context.Context) (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 } //新签客户 { var condition1 string var pars1 []interface{} condition1 += ` AND a.start_date <= ? AND a.end_date >= ? ` pars1 = append(pars1, dayStr, dayStr) condition1 += ` AND a.contract_type = ? ` pars1 = append(pars1, "新签合同") list, countErr := models.GetTodayStackCompanyList(condition1, pars1) if countErr != nil { err = countErr return } for _, companyInfo := range list { item := models.StackCompanyStatistic{ CompanyId: companyInfo.CompanyId, CompanyName: companyInfo.CompanyName, ProductId: companyInfo.ProductId, ProductName: companyInfo.ProductName, ContractId: companyInfo.CompanyContractId, SellerId: companyInfo.SellerId, SellerName: companyInfo.SellerName, GroupId: companyInfo.GroupId, DepartmentId: companyInfo.DepartmentId, Date: dayStr, //截止到昨天的数据 StartDate: companyInfo.StartDate, EndDate: companyInfo.EndDate, RegionType: companyInfo.RegionType, CreateTime: time.Now(), Type: "新签客户", } addErr := models.AddStackCompanyStatistic(&item) if addErr != nil { fmt.Println("存量新签客户数据统计,插入数据异常:", addErr) } } } //续约客户 { var condition1 string var pars1 []interface{} condition1 += ` AND a.start_date <= ? AND a.end_date >= ? ` pars1 = append(pars1, dayStr, dayStr) condition1 += ` AND a.contract_type = ? ` pars1 = append(pars1, "续约合同") //额外条件(续约合同的起始日期包含在所选时间段内且不包含在新签合同存续期内的客户) pars1 = append(pars1, dayStr) list, countErr := models.GetTodayStackCompanyListV2(condition1, pars1) if countErr != nil { err = countErr return } for _, companyInfo := range list { item := models.StackCompanyStatistic{ CompanyId: companyInfo.CompanyId, CompanyName: companyInfo.CompanyName, ProductId: companyInfo.ProductId, ProductName: companyInfo.ProductName, ContractId: companyInfo.CompanyContractId, SellerId: companyInfo.SellerId, SellerName: companyInfo.SellerName, GroupId: companyInfo.GroupId, DepartmentId: companyInfo.DepartmentId, Date: dayStr, //截止到昨天的数据 StartDate: companyInfo.StartDate, EndDate: companyInfo.EndDate, RegionType: companyInfo.RegionType, CreateTime: time.Now(), Type: "续约客户", } addErr := models.AddStackCompanyStatistic(&item) if addErr != nil { fmt.Println("存量续约客户数据统计,插入数据异常:", addErr) } } } //未续约客户 { var condition1 string var pars1 []interface{} condition1 += ` AND c.status not in ("永续","正式") AND a.create_time <= ? ` pars1 = append(pars1, time.Now().Format(utils.FormatDateTime)) condition1 += ` AND a.operation = 'try_out' ` list, countErr := models.GetIncrementalCompanyListByOperationRecord(condition1, pars1) if countErr != nil { err = countErr return } for _, companyInfo := range list { item := models.StackCompanyStatistic{ CompanyId: companyInfo.CompanyId, CompanyName: companyInfo.CompanyName, ProductId: companyInfo.ProductId, ProductName: companyInfo.ProductName, ContractId: companyInfo.CompanyOperationRecordId, SellerId: companyInfo.SellerId, SellerName: companyInfo.SellerName, GroupId: companyInfo.GroupId, DepartmentId: companyInfo.DepartmentId, Date: dayStr, //截止到昨天的数据 StartDate: companyInfo.StartDate, EndDate: companyInfo.EndDate, RegionType: companyInfo.RegionType, CreateTime: time.Now(), Type: "未续约客户", } addErr := models.AddStackCompanyStatistic(&item) if addErr != nil { fmt.Println("存量未续约客户数据统计,插入数据异常:", addErr) } } } return }