package svc import ( "applet/app/comm/db" "applet/app/comm/md" "applet/app/comm/utils" "applet/app/comm/utils/logx" "github.com/gin-gonic/gin" "github.com/tidwall/gjson" ) // UserProfileDashbord is 我的面板属性 func UserProfileDashbord(c *gin.Context, uid string, r *md.UserProfileResponse, style string, earning string) error { result, err := statUserOrderMoney(c, uid) if err != nil { return err } for _, item := range gjson.Get(style, "list_style").Array() { switch item.Get("type").String() { case "earning": r.GridViews = append(r.GridViews, md.UserProfileResponseGridView{ Name: item.Get("name").String(), Value: utils.Float64ToStr(utils.StrToFloat64(earning)), }) case "todayIncome": // 今日预估 r.GridViews = append(r.GridViews, md.UserProfileResponseGridView{ Name: item.Get("name").String(), Value: utils.Float64ToStr(utils.StrToFloat64(result["today"])), }) case "monthIncome": // 本月预估 r.GridViews = append(r.GridViews, md.UserProfileResponseGridView{ Name: item.Get("name").String(), Value: utils.Float64ToStr(utils.StrToFloat64(result["thisMonth"])), }) case "lastMonthIncome": // 上月预估 r.GridViews = append(r.GridViews, md.UserProfileResponseGridView{ Name: item.Get("name").String(), Value: utils.Float64ToStr(utils.StrToFloat64(result["lastMonth"])), }) case "lastMonthSettle": // 上月结算 r.GridViews = append(r.GridViews, md.UserProfileResponseGridView{ Name: item.Get("name").String(), Value: utils.Float64ToStr(utils.StrToFloat64(result["lastMonthSettle"])), }) case "waitSettle": // 待结算 r.GridViews = append(r.GridViews, md.UserProfileResponseGridView{ Name: item.Get("name").String(), Value: utils.Float64ToStr(utils.StrToFloat64(result["waitSettle"])), }) } } return nil } func statUserOrderMoney(c *gin.Context, uid string) (map[string]string, error) { result := make(map[string]string, 0) // 获取时间范围 todayRange := utils.GetTimeRange("today") thisMonthRange := utils.GetTimeRange("current_month") lastMonthRange := utils.GetTimeRange("last_month") // 统计预估 sqlTpl := `SELECT SUM(olr.amount) AS amount FROM ord_list_relate olr JOIN ord_list ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND olr.create_at >= ? AND olr.create_at < ? AND ol.state IN (0,1,2,3) AND ol.settle_at=0; ` todayResult, err := db.QueryNativeString(MasterDb(c), sqlTpl, uid, todayRange["start"], todayRange["end"]) if err != nil { _ = logx.Warn(err) result["today"] = "0.00" } else { result["today"] = todayResult[0]["amount"] } thisMonthResult, err := db.QueryNativeString(MasterDb(c), sqlTpl, uid, thisMonthRange["start"], thisMonthRange["end"]) if err != nil { _ = logx.Warn(err) result["thisMonth"] = "0.00" } else { result["thisMonth"] = thisMonthResult[0]["amount"] } // 统计上月预估和结算 sqlTpl2 := `SELECT SUM(IF(ol.state IN (0, 1, 2, 3) AND ol.settle_at = 0, olr.amount, 0)) AS amount, SUM(IF(ol.settle_at != 0, olr.amount, 0)) AS settle_amount FROM ord_list_relate olr JOIN ord_list ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND olr.create_at >= ? AND olr.create_at < ? ` lastMonthResult, err := db.QueryNativeString(MasterDb(c), sqlTpl2, uid, lastMonthRange["start"], lastMonthRange["end"]) if err != nil { _ = logx.Warn(err) result["lastMonth"] = "0.00" result["lastMonthSettle"] = "0.00" } else { result["lastMonth"] = lastMonthResult[0]["amount"] result["lastMonthSettle"] = lastMonthResult[0]["settle_amount"] } // 统计全部预估 sqlTpl3 := `SELECT SUM(olr.amount) AS amount FROM ord_list_relate olr JOIN ord_list ol ON olr.oid = ol.ord_id WHERE olr.uid = ? AND ol.state IN (0,1,2,3) AND ol.settle_at=0; ` waitResult, err := db.QueryNativeString(MasterDb(c), sqlTpl3, uid) if err != nil { _ = logx.Warn(err) result["waitSettle"] = "0.00" } else { result["waitSettle"] = waitResult[0]["amount"] } return result, nil }