Browse Source

feat(存量客户统计):调整未续约客户统计

Roc 3 years ago
parent
commit
40cbc315c0
2 changed files with 99 additions and 9 deletions
  1. 50 2
      models/stack_company_statistic.go
  2. 49 7
      services/company_statistic.go

+ 50 - 2
models/stack_company_statistic.go

@@ -33,12 +33,12 @@ func AddStackCompanyStatistic(item *StackCompanyStatistic) (err error) {
 }
 
 //获取某天的存量客户统计数据数量
-func GetStackCompanyCount(dayStr string) (count int,  err error) {
+func GetStackCompanyCount(dayStr string) (count int, err error) {
 	o := orm.NewOrm()
 	//产品权限
 	sql := `SELECT count(1) count FROM stack_company_statistic WHERE date = ?`
 
-	err = o.Raw(sql,dayStr).QueryRow(&count)
+	err = o.Raw(sql, dayStr).QueryRow(&count)
 	return
 }
 
@@ -84,3 +84,51 @@ GROUP BY company_id,product_id `
 	total, err = o.Raw(sql).QueryRows(&items)
 	return
 }
+
+//增量客户统计报表列表数据结构
+type IncrementalList struct {
+	CompanyContractId int     `description:"合同id"`
+	ContractType      string  `description:"合同类型"`
+	CompanyId         int     `description:"企业客户id"`
+	CompanyName       string  `description:"企业客户名称"`
+	ProductId         int     `description:"产品id"`
+	ProductName       string  `description:"产品名称"`
+	CompanyProductId  int     `description:"客户购买产品授权id"`
+	ContractCode      string  `description:"合同编码"`
+	StartDate         string  `description:"合同开始日期"`
+	EndDate           string  `description:"合同结束日期"`
+	Money             float64 `description:"合同金额"`
+	PayMethod         string  `description:"付款方式"`
+	PayChannel        string  `description:"付款渠道"`
+	ImgUrl            string  `description:"合同图片"`
+	CreateTime        string  `description:"合同创建时间"`
+	ModifyTime        string  `description:"合同修改时间"`
+	Status            string  `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
+	RegionType        string  `description:"企业客户所属区域;可选范围:国内,海外"`
+	SellerId          int     `description:"归属销售id"`
+	GroupId           int     `description:"归属分组id"`
+	DepartmentId      int     `description:"归属部门id"`
+	SellerName        string  `description:"归属销售名称"`
+	ExpireDay         string  `description:"剩余可用天数"`
+}
+
+//未续约客户数(存量客户)
+func GetNotRenewalCompanyTotalV1(dateTime string) (total int64, items []*IncrementalList, err error) {
+	o := orm.NewOrm()
+	//产品权限
+	sql := `SELECT *  FROM
+	( SELECT a.id,a.company_id,a.company_name,c.seller_id,c.seller_name,c.group_id,c.department_id,a.product_id,a.product_name,a.create_time,b.region_type 
+	FROM company_operation_record a
+		RIGHT JOIN company b ON a.company_id = b.company_id
+		JOIN company_product c ON b.company_id = c.company_id AND a.product_id = c.product_id 
+	WHERE
+		1 = 1  AND c.STATUS NOT IN ( "永续", "正式" )  AND a.create_time <= ? 
+		AND a.operation = 'try_out' 
+	ORDER BY create_time ASC 
+	) f 
+GROUP BY company_id, product_id 
+ORDER BY create_time DESC , company_id DESC  `
+
+	total, err = o.Raw(sql, dateTime).QueryRows(&items)
+	return
+}

+ 49 - 7
services/company_statistic.go

@@ -8,11 +8,16 @@ import (
 )
 
 //存量客户数据统计
-func StackCompanyStatistic()(err error) {
-	dayStr := time.Now().AddDate(0,0,-1).Format(utils.FormatDate)	//截止到昨天的数据
+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{
+	count, err := models.GetStackCompanyCount(dayStr)
+	if err != nil {
 		fmt.Println("查询昨天的数据是否生成语句 执行异常:", err.Error())
 		return
 	}
@@ -41,7 +46,7 @@ func StackCompanyStatistic()(err error) {
 			SellerName:   company.SellerName,
 			GroupId:      company.GroupId,
 			DepartmentId: company.DepartmentId,
-			Date:         dayStr,	//截止到昨天的数据
+			Date:         dayStr, //截止到昨天的数据
 			StartDate:    company.StartDate,
 			EndDate:      company.EndDate,
 			RegionType:   company.RegionType,
@@ -59,10 +64,47 @@ func StackCompanyStatistic()(err error) {
 		} 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)
+		if addErr != nil {
+			fmt.Println("存量客户数据统计,插入数据异常:", addErr)
 		}
 	}
 	return