diff --git a/app/admin/hdl/hdl_home_page.go b/app/admin/hdl/hdl_home_page.go new file mode 100644 index 0000000..e2aa4eb --- /dev/null +++ b/app/admin/hdl/hdl_home_page.go @@ -0,0 +1,191 @@ +package hdl + +import ( + "applet/app/db" + "applet/app/e" + "applet/app/utils" + "fmt" + "github.com/gin-gonic/gin" + "time" +) + +func HomeIndex(c *gin.Context) { + now := time.Now() + + //1、统计公司数量 + mapArr1, err := db.QueryNativeString(db.Db, "SELECT COUNT(*) AS total FROM `company`") + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //2、统计校企数量 + mapArr2, err := db.QueryNativeString(db.Db, "SELECT COUNT(*) AS total FROM `enterprise`") + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //3、统计订单付款金额 + mapArr3, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) AS total FROM `order` where create_at >='%s' and state != 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"))) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //4、统计订单取消金额 + mapArr4, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and state = 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"))) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + var result1, result2, result3, result4 float64 + if mapArr1 != nil { + result1 = utils.StrToFloat64(mapArr1[0]["total"]) + } + if mapArr2 != nil { + result2 = utils.StrToFloat64(mapArr2[0]["total"]) + } + if mapArr3 != nil { + result3 = utils.StrToFloat64(mapArr3[0]["total"]) + } + if mapArr4 != nil { + result4 = utils.StrToFloat64(mapArr4[0]["total"]) + } + e.OutSuc(c, map[string]interface{}{ + "company_total_nums": result1, + "enterprise_total_nums": result2, + "order_total_amount_for_success": result3, + "order_total_amount_for_cancel": result4, + }, nil) + return +} + +func RepayBehaviorAnalysis(c *gin.Context) { + now := time.Now() + + //1、统计订单数据 + mapArr1, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total, enterprise_id FROM `order` where create_at >='%s' and create_at < '%s' GROUP BY enterprise_id", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //2、数据整理 + var dataMap = map[int]float64{} + var totalNUms = len(mapArr1) + for _, v := range mapArr1 { + dataMap[utils.StrToInt(v["total"])]++ + } + + //3、数据计算 + var result []map[string]interface{} + for k, v := range dataMap { + result = append(result, map[string]interface{}{ + "key": utils.IntToStr(k) + "次购买", + "value": v / float64(totalNUms), + }) + } + e.OutSuc(c, map[string]interface{}{ + "result": result, + }, nil) + return +} + +func OperationalDataStatistics(c *gin.Context) { + startDate := c.DefaultQuery("start_date", "") + endDate := c.DefaultQuery("end_date", "") + if startDate == "" || endDate == "" { + e.OutErr(c, e.ERR_INVALID_ARGS, "时间范围不能为空") + return + } + + now := time.Now() + //1、商品数量 + mapArr1, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `goods` where create_at >='%s' and create_at < '%s'", startDate, endDate)) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + mapArr11, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `goods`")) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //2、订单数量 + mapArr2, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `order` where create_at >='%s' and create_at < '%s'", startDate, endDate)) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + mapArr22, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT COUNT(*) AS total FROM `order` where create_at >='%s' and create_at < '%s'", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //3、统计订单付款金额 + mapArr3, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) AS total FROM `order` where create_at >='%s' and create_at < '%s' and state != 5", startDate, endDate)) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + mapArr33, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and create_at < '%s' and state != 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))) + fmt.Println(fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and create_at < '%s' and state != 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + //4、统计订单取消金额 + mapArr4, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) AS total FROM `order` where create_at >='%s' and create_at < '%s' and state = 5", startDate, endDate)) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + mapArr44, err := db.QueryNativeString(db.Db, fmt.Sprintf("SELECT SUM(amount) as total FROM `order` AS total where create_at >='%s' and create_at < '%s' and state = 5", now.AddDate(0, -3, 0).Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + var result1, result2, result3, result4, result11, result22, result33, result44 float64 + if mapArr1 != nil { + result1 = utils.StrToFloat64(mapArr1[0]["total"]) + } + if mapArr2 != nil { + result2 = utils.StrToFloat64(mapArr2[0]["total"]) + } + if mapArr3 != nil { + result3 = utils.StrToFloat64(mapArr3[0]["total"]) + } + if mapArr4 != nil { + result4 = utils.StrToFloat64(mapArr4[0]["total"]) + } + if mapArr11 != nil { + result11 = utils.StrToFloat64(mapArr11[0]["total"]) + } + if mapArr22 != nil { + result22 = utils.StrToFloat64(mapArr22[0]["total"]) + } + if mapArr33 != nil { + result33 = utils.StrToFloat64(mapArr33[0]["total"]) + } + if mapArr44 != nil { + result44 = utils.StrToFloat64(mapArr44[0]["total"]) + } + + e.OutSuc(c, map[string]interface{}{ + "goods_increase_value": result1, + "goods_total_value": result11, + "order_increase_value": result2, + "order_total_value": result22, + "success_order_total_amount_increase_value": result3, + "success_order_total_amount_total_value": result33, + "cancel_order_total_amount_increase_value": result4, + "cancel_order_total_amount_total_value": result44, + }, nil) + return +} diff --git a/app/router/admin_router.go b/app/router/admin_router.go index d1df9f5..9006252 100644 --- a/app/router/admin_router.go +++ b/app/router/admin_router.go @@ -50,6 +50,9 @@ func rCompany(r *gin.RouterGroup) { } func rHomePage(r *gin.RouterGroup) { + r.GET("/index", hdl.HomeIndex) + r.GET("/operationalDataStatistics", hdl.OperationalDataStatistics) + r.GET("/repayBehaviorAnalysis", hdl.RepayBehaviorAnalysis) }