Ver código fonte

feature(首页统计): 加上对于年季月的统计,加上回款率的统计

likai 2 anos atrás
pai
commit
5bd23e8e25

+ 1 - 0
opms_admin/.gitignore

@@ -3,6 +3,7 @@
 *.o
 *.a
 *.so
+log
 
 # Folders
 _obj

+ 1 - 0
opms_parent/app/model/home/report.go

@@ -4,4 +4,5 @@ type ReportData struct {
 	XData       []string  `json:"xData"`
 	YDataTarget []float64 `json:"yDataTarget"`
 	YDataReal   []float64 `json:"yDataReal"`
+	PercentData []float64 `json:"percentData"`
 }

+ 61 - 8
opms_parent/app/service/home/report.go

@@ -89,21 +89,43 @@ func getPersonalContractReportData(ctx context.Context, dataType string, params
 		} else {
 			reportData.YDataReal = append(reportData.YDataReal, 0)
 		}
+		if reportData.YDataTarget[index-1] == 0 {
+			reportData.PercentData = append(reportData.PercentData, 0)
+		} else {
+			reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index-1]*100/reportData.YDataTarget[index-1])
+		}
 	}
 
 	return &reportData, nil
 }
 
-// getCompanyContractReportData 获取个人数据
+// getCompanyContractReportData 获取总部数据
 // dataType:collection为回款;其他为合同
 func getCompanyContractReportData(ctx context.Context, dataType string, params *map[string]interface{}) (*home.ReportData, error) {
 	var reportData home.ReportData
 	realMap := make(map[int]gdb.Record, 0)
 	targetField := "sales_target"
 	realField := "contract_amount"
-	year := gtime.Now().Format("Y")
-	if params != nil && (*params)["year"] != nil {
-		year = gconv.String((*params)["year"])
+	dateWhere1 := "" // 查询条件
+	dateWhere2 := "" // 查询条件
+	dateWhere3 := ""
+	date := gtime.Now()
+	if params != nil && (*params)["date"] != nil {
+		date = gconv.GTime((*params)["date"])
+	}
+	dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y")+"-%")
+	dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y")+"-%")
+	if params != nil && (*params)["searchType"] != nil {
+		searchType := gconv.String((*params)["searchType"])
+		if searchType == "month" {
+			dateWhere1 = fmt.Sprintf("ctr_contract.contract_start_time LIKE '%v'", date.Format("Y-m")+"-%")
+			dateWhere2 = fmt.Sprintf("ctr_contract_collection.collection_datetime LIKE '%v'", date.Format("Y-m")+"-%")
+			dateWhere3 = fmt.Sprintf("monthly=%v", date.Month())
+		} else if searchType == "quarter" {
+			dateWhere1 = getQuarterWhere(date, "ctr_contract.contract_start_time")
+			dateWhere2 = getQuarterWhere(date, "ctr_contract_collection.collection_datetime")
+			dateWhere3 = getQuarterMonthWhere(date, "monthly")
+		}
 	}
 	// 统计字段
 	if dataType == "COLLECTION" {
@@ -116,14 +138,14 @@ func getCompanyContractReportData(ctx context.Context, dataType string, params *
 		return nil, err
 	}
 
-	targets, err := srv.Dao.DB.Model("rpt_sales_target").Order("sale_user_id ASC").Group("sale_user_id").Fields(fmt.Sprintf("sale_user_id, sale_user_name, SUM(%v) %v", targetField, targetField)).FindAll()
+	targets, err := srv.Dao.DB.Model("rpt_sales_target").Where(dateWhere3).Order("sale_user_id ASC").Group("sale_user_id").Fields(fmt.Sprintf("sale_user_id, sale_user_name, SUM(%v) %v", targetField, targetField)).FindAll()
 	if err != nil && err != sql.ErrNoRows {
 		return nil, err
 	}
 	// 统计合同值
 	contractModel := srv.Dao.DB.Model("ctr_contract").Group("ctr_contract.incharge_id").Order("ctr_contract.incharge_id ASC")
 	if dataType != "COLLECTION" {
-		contracts, err := contractModel.Where("ctr_contract.contract_start_time LIKE ?", year+"-%").Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract.contract_amount) contract_amount").FindAll()
+		contracts, err := contractModel.Where(dateWhere1).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract.contract_amount) contract_amount").FindAll()
 		if err != nil && err != sql.ErrNoRows {
 			return nil, err
 		}
@@ -132,7 +154,7 @@ func getCompanyContractReportData(ctx context.Context, dataType string, params *
 		}
 	} else {
 		// 回款数据统计
-		collections, err := contractModel.InnerJoin("ctr_contract_collection", "ctr_contract.id=ctr_contract_collection.contract_id").Where("ctr_contract_collection.appro_status", "20").Where("ctr_contract_collection.collection_datetime LIKE ?", year+"-%").Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract_collection.collection_amount) collection_amount").FindAll()
+		collections, err := contractModel.InnerJoin("ctr_contract_collection", "ctr_contract.id=ctr_contract_collection.contract_id").Where("ctr_contract_collection.appro_status", "20").Where(dateWhere2).Fields("ctr_contract.incharge_id, ctr_contract.incharge_name, SUM(ctr_contract_collection.collection_amount) collection_amount").FindAll()
 		if err != nil && err != sql.ErrNoRows {
 			return nil, err
 		}
@@ -142,7 +164,7 @@ func getCompanyContractReportData(ctx context.Context, dataType string, params *
 	}
 
 	// 统计目标值和实际值
-	for _, target := range targets {
+	for index, target := range targets {
 		reportData.XData = append(reportData.XData, target["sale_user_name"].String())
 		reportData.YDataTarget = append(reportData.YDataTarget, target[targetField].Float64())
 		if realData, ok := realMap[target["sale_user_id"].Int()]; ok {
@@ -150,7 +172,38 @@ func getCompanyContractReportData(ctx context.Context, dataType string, params *
 		} else {
 			reportData.YDataReal = append(reportData.YDataReal, 0)
 		}
+		if reportData.YDataTarget[index] == 0 {
+			reportData.PercentData = append(reportData.PercentData, 0)
+		} else {
+			reportData.PercentData = append(reportData.PercentData, reportData.YDataReal[index]*100/reportData.YDataTarget[index])
+		}
 	}
 
 	return &reportData, nil
 }
+
+func getQuarterWhere(date *gtime.Time, field string) string {
+	if date.Month() >= 1 && date.Month() <= 3 {
+		return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"01-01 00:00:00", field, date.Format("Y-")+"04-01 00:00:00")
+	} else if date.Month() >= 4 && date.Month() <= 6 {
+		return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"04-01 00:00:00", field, date.Format("Y-")+"07-01 00:00:00")
+	} else if date.Month() >= 7 && date.Month() <= 9 {
+		return fmt.Sprintf("%v >= '%v' AND %v < '%v'", field, date.Format("Y-")+"07-01 00:00:00", field, date.Format("Y-")+"10-01 00:00:00")
+	} else if date.Month() >= 10 && date.Month() <= 12 {
+		return fmt.Sprintf("%v >= '%v' AND %v <= '%v'", field, date.Format("Y-")+"10-01 00:00:00", field, date.Format("Y-")+"12-31 23:59:59")
+	}
+	return ""
+}
+
+func getQuarterMonthWhere(date *gtime.Time, field string) string {
+	if date.Month() >= 1 && date.Month() <= 3 {
+		return fmt.Sprintf("%v >= %v AND %v < %v", field, 1, field, 4)
+	} else if date.Month() >= 4 && date.Month() <= 6 {
+		return fmt.Sprintf("%v >= %v AND %v < %v", field, 4, field, 7)
+	} else if date.Month() >= 7 && date.Month() <= 9 {
+		return fmt.Sprintf("%v >= %v AND %v < %v", field, 7, field, 10)
+	} else if date.Month() >= 10 && date.Month() <= 12 {
+		return fmt.Sprintf("%v >= %v AND %v <= %v", field, 10, field, 12)
+	}
+	return ""
+}