package hdl import ( "applet/app/admin/md" "applet/app/admin/svc" "applet/app/db" "applet/app/e" "applet/app/enum" "applet/app/utils" "applet/app/utils/cache" "encoding/json" "fmt" "github.com/gin-gonic/gin" "time" ) func HomePageIndex(c *gin.Context) { now := time.Now() todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05") //今日开始时间 todayEnd := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05") //今日结束时间 sql1 := fmt.Sprintf("SELECT sum(trade_amount) as total FROM self_support_for_school_ord where order_status = 2 and face_time >= '%s' and face_time < '%s'", todayStart, todayEnd) //统计今日"自营-学校"收益 sql2 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and create_at >= '%s' and create_at < '%s'", todayStart, todayEnd) //统计今日"央厨-学校"收益 mapArr1, err := db.QueryNativeString(db.Db, sql1) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } mapArr2, err := db.QueryNativeString(db.Db, sql2) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } var result1, result2, todayIncome float64 if mapArr1 != nil { result1 = utils.StrToFloat64(mapArr1[0]["total"]) } if mapArr2 != nil { result2 = utils.StrToFloat64(mapArr2[0]["total"]) } todayIncome = result1 + result2 sql3 := fmt.Sprintf("SELECT COUNT(*) as total,kind FROM `enterprise` GROUP BY kind") //统计校企类型数量 mapArr3, err := db.QueryNativeString(db.Db, sql3) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } var totalCentralKitchenForSchoolNums, totalCentralKitchenForFactoryNums, totalSelfSupportForSchoolNums, totalSelfSupportForFactoryNums int if mapArr3 != nil { for _, v := range mapArr3 { if v["kind"] == utils.IntToStr(enum.EnterpriseKindByCentralKitchenForSchool) { totalCentralKitchenForSchoolNums = utils.StrToInt(v["total"]) } if v["kind"] == utils.IntToStr(enum.EnterpriseKindByCentralKitchenForFactory) { totalCentralKitchenForFactoryNums = utils.StrToInt(v["total"]) } if v["kind"] == utils.IntToStr(enum.EnterpriseKindBySelfSupportForSchool) { totalSelfSupportForSchoolNums = utils.StrToInt(v["total"]) } if v["kind"] == utils.IntToStr(enum.EnterpriseKindBySelfSupportForFactory) { totalSelfSupportForFactoryNums = utils.StrToInt(v["total"]) } } } var totalCentralKitchenForSchoolUserIdentityNums, totalCentralKitchenForFactoryUserIdentityNums, totalSelfSupportForSchoolUserIdentityNums, totalSelfSupportForFactoryUserIdentityNums int sql4 := fmt.Sprintf("select count(*) as total from `user_identity` where identity in (%d, %d) ", enum.UserIdentityForCentralKitchenForStudent, enum.UserIdentityForCentralKitchenForTeacher) //统计"央厨-学校"用户数 sql5 := fmt.Sprintf("select count(*) as total from `user_identity` where identity in (%d, %d, %d) ", enum.UserIdentityForSelfSupportForStudent, enum.UserIdentityForSelfSupportForTeacher, enum.UserIdentityForSelfSupportForWorker) //统计"自营-学校"用户数 mapArr4, err := db.QueryNativeString(db.Db, sql4) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } if mapArr4 != nil { totalCentralKitchenForSchoolUserIdentityNums = utils.StrToInt(mapArr4[0]["total"]) } mapArr5, err := db.QueryNativeString(db.Db, sql5) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } if mapArr5 != nil { totalSelfSupportForSchoolUserIdentityNums = utils.StrToInt(mapArr5[0]["total"]) } e.OutSuc(c, map[string]interface{}{ "data": map[string]interface{}{ "todayIncome": utils.Float64ToStr(todayIncome), "totalCentralKitchenForSchoolNums": totalCentralKitchenForSchoolNums, "totalCentralKitchenForFactoryNums": totalCentralKitchenForFactoryNums, "totalSelfSupportForSchoolNums": totalSelfSupportForSchoolNums, "totalSelfSupportForFactoryNums": totalSelfSupportForFactoryNums, "totalCentralKitchenForSchoolUserIdentityNums": totalCentralKitchenForSchoolUserIdentityNums, "totalCentralKitchenForFactoryUserIdentityNums": totalCentralKitchenForFactoryUserIdentityNums, "totalSelfSupportForSchoolUserIdentityNums": totalSelfSupportForSchoolUserIdentityNums, "totalSelfSupportForFactoryUserIdentityNums": totalSelfSupportForFactoryUserIdentityNums, }, }, nil) return } func ThisWeekData(c *gin.Context) { now := time.Now() var list []map[string]interface{} for i := -6; i <= 0; i++ { day := now.AddDate(0, 0, i).Format("2006-01-02") //7日前 dayStart := now.AddDate(0, 0, i).Format("2006-01-02") dayEnd := now.AddDate(0, 0, i+1).Format("2006-01-02") sql1 := fmt.Sprintf("SELECT sum(trade_amount) as total FROM self_support_for_school_ord where order_status = 2 and face_time >= '%s' and face_time < '%s'", dayStart, dayEnd) //统计今日"自营-学校"收益 sql2 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and create_at >= '%s' and create_at < '%s'", dayStart, dayEnd) //统计今日"央厨-学校"收益 mapArr1, err := db.QueryNativeString(db.Db, sql1) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } mapArr2, err := db.QueryNativeString(db.Db, sql2) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } var result1, result2, todayIncome float64 if mapArr1 != nil { result1 = utils.StrToFloat64(mapArr1[0]["total"]) } if mapArr2 != nil { result2 = utils.StrToFloat64(mapArr2[0]["total"]) } todayIncome = result1 + result2 list = append(list, map[string]interface{}{ "date": day, "income": todayIncome, }) } e.OutSuc(c, map[string]interface{}{ "data": list, }, nil) return } func ThisMonthData(c *gin.Context) { var list []map[string]interface{} admin := svc.GetUser(c) cacheKey := fmt.Sprintf(md.AdminHomePageDataKey, utils.GetIP(c.Request), utils.AnyToString(admin.AdmId)) var adminHomePageDataString string adminHomePageDataString, _ = cache.GetString(cacheKey) if adminHomePageDataString != "" { if err := json.Unmarshal([]byte(adminHomePageDataString), &list); err != nil { e.OutErr(c, e.ERR, err.Error()) return } } else { now := time.Now() for i := -29; i <= 0; i++ { day := now.AddDate(0, 0, i).Format("2006-01-02") //7日前 dayStart := now.AddDate(0, 0, i).Format("2006-01-02 15:04:05") dayEnd := now.AddDate(0, 0, i+1).Format("2006-01-02 15:04:05") sql1 := fmt.Sprintf("SELECT count(*) as total FROM self_support_for_school_ord where order_status = 2 and face_time >= '%s' and face_time < '%s'", dayStart, dayEnd) //统计今日"自营-学校"收益 sql2 := fmt.Sprintf("SELECT count(*) as total FROM central_kitchen_for_school_package_ord where state = 1 and create_at >= '%s' and create_at < '%s'", dayStart, dayEnd) //统计今日"央厨-学校"收益 mapArr1, err := db.QueryNativeString(db.Db, sql1) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } mapArr2, err := db.QueryNativeString(db.Db, sql2) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } var result1, result2 int if mapArr1 != nil { result1 = utils.StrToInt(mapArr1[0]["total"]) } if mapArr2 != nil { result2 = utils.StrToInt(mapArr2[0]["total"]) } list = append(list, map[string]interface{}{ "date": day, "self_support_for_school": result1, "central_kitchen_for_school": result2, }) } marshal, err := json.Marshal(list) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } adminHomePageDataString = string(marshal) _, err = cache.SetEx(cacheKey, adminHomePageDataString, md.AdminHomePageDataCacheTime) } e.OutSuc(c, map[string]interface{}{ "data": list, }, nil) return }