package statistics import ( "fmt" "server/call" "server/common" "server/db" "server/modules/backend/app" "server/modules/backend/models" utils "server/modules/backend/util" "server/modules/backend/values" "sort" "github.com/gin-gonic/gin" "github.com/liangdas/mqant/log" ) func WithdrawOrderList(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.WithdrawOrderListReq) if !a.S(req) { return } st, et := utils.GetQueryUnix(req.Start, req.End) queryOrder := " SELECT * FROM withdraw_order AS re where" queryCount := " SELECT COUNT(*) as count FROM withdraw_order AS re where" str := fmt.Sprintf(" `event` = %d AND create_time >= %d AND create_time < %d ", common.CurrencyEventWithDraw, st, et) if req.Channel != nil { str += fmt.Sprintf(" AND channel_id = %d ", *req.Channel) } if req.Status != nil { str += fmt.Sprintf(" AND `status` = %d ", *req.Status) } if req.Uid != nil { str += fmt.Sprintf(" AND uid = %d ", *req.Uid) } // queryOrder += " LEFT JOIN ( SELECT uid, SUM(amount) AS totalAmount FROM withdraw_order WHERE " + str + " GROUP BY uid) rm ON rm.uid = re.uid WHERE rm.uid = re.uid AND " // queryCount += " LEFT JOIN ( SELECT uid, SUM(amount) AS totalAmount FROM withdraw_order WHERE " + str + " GROUP BY uid) rm ON rm.uid = re.uid WHERE rm.uid = re.uid AND " var ret []common.WithdrawOrder var count int64 // 查询总数量 err := db.Mysql().QueryBySql(queryCount+str, &count) if err != nil { log.Error("err:%v", err) } var desc string switch req.Sort { case 1: desc += " ORDER BY re.create_time DESC " case 2: desc += " ORDER BY rm.totalAmount DESC " default: desc += " ORDER BY re.create_time DESC " } limit := fmt.Sprintf(" LIMIT %d, %d ", (req.Page-1)*req.Num, req.Num) // 查询订单 err = db.Mysql().QueryBySql(queryOrder+str+desc+limit, &ret) if err != nil { log.Error("err:%v", err) } resp := values.WithdrawOrderListResp{Count: count} for _, v := range ret { user, _ := call.GetUserXInfo(v.UID, "birth") var totalWithdraw int64 err = db.Mysql().QueryBySql(fmt.Sprintf("SELECT IFNULL(SUM(amount),0) AS totalAmount FROM withdraw_order WHERE uid = %d AND ", v.UID)+str, &totalWithdraw) if err != nil { log.Error("err:%v", err) } resp.List = append([]values.OneWithdrawOrderList{{ Channel: v.ChannelID, UID: v.UID, Time: v.CreateTime, FinishTime: v.CallbackTime, Amount: v.Amount, Status: int(v.Status), PayAccount: v.PayAccount, OrderID: v.APIPayID, MyOrderID: v.OrderID, FailReason: v.FailReason, Birth: user.Birth, TotalWithdraw: totalWithdraw, WithDrawPer: utils.GetPer(models.GetWithdrawTotalByUid(&v.UID), models.GetRechargeTotalByUid(&v.UID)), PayChannel: v.PayChannel, PaySource: v.PaySource, }}, resp.List...) } sort.SliceStable(resp.List, func(i, j int) bool { return resp.List[i].Time > resp.List[j].Time }) // total s, e := utils.GetQueryUnix(req.Start, req.End) sql := fmt.Sprintf("`event` = %v and `status` = %v and create_time >= %d and create_time < %d", common.CurrencyEventWithDraw, common.StatusROrderFinish, s, e) if req.Channel != nil { sql += fmt.Sprintf(" and channel_id = %v", *req.Channel) } resp.WithdrawTotal = db.Mysql().SumTable("withdraw_order", sql, "amount") resp.WithdrawCount = db.Mysql().CountTable("withdraw_order", sql) sql = fmt.Sprintf("`event` = %v and create_time >= %d and create_time < %d", common.CurrencyEventWithDraw, s, e) if req.Channel != nil { sql += fmt.Sprintf(" and channel_id = %v", *req.Channel) } withdrawTotalCount := db.Mysql().CountTable("withdraw_order", sql) resp.WithdrawSuccess = utils.GetPer(resp.WithdrawCount, withdrawTotalCount) a.Data = resp } func QueryWithdrawOrder(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.WithdrawOrderReq) if !a.S(req) { return } var ret []common.WithdrawOrder queryOrder := fmt.Sprintf(" SELECT * FROM withdraw_order WHERE `event` = %d AND (apipayid = '%s' or orderid = '%s') ", common.CurrencyEventWithDraw, req.OrderId, req.OrderId) // 查询订单 err := db.Mysql().QueryBySql(queryOrder, &ret) if err != nil { log.Error("err:%v", err) } resp := values.WithdrawOrderListResp{Count: 1} for _, v := range ret { user, _ := call.GetUserInfo(v.UID) var totalWithdraw int64 err = db.Mysql().QueryBySql(fmt.Sprintf("SELECT total_withdraw FROM recharge_info WHERE uid = %d ", v.UID), &totalWithdraw) if err != nil { log.Error("err:%v", err) } resp.List = append([]values.OneWithdrawOrderList{{ Channel: v.ChannelID, UID: v.UID, Time: v.CreateTime, Amount: v.Amount, Status: int(v.Status), PayAccount: v.PayAccount, OrderID: v.APIPayID, FailReason: v.FailReason, Birth: user.Birth, TotalWithdraw: totalWithdraw, WithDrawPer: utils.GetPer(models.GetWithdrawTotalByUid(&v.UID), models.GetRechargeTotalByUid(&v.UID)), PayChannel: v.PayChannel, PaySource: v.PaySource, }}, resp.List...) } if len(ret) > 0 { // total sql := fmt.Sprintf("`event` = %v and `status` = %v and uid = %d", common.CurrencyEventWithDraw, common.StatusROrderFinish, ret[0].UID) resp.WithdrawTotal = db.Mysql().Sum(&common.WithdrawOrder{}, sql, "amount") resp.WithdrawCount = db.Mysql().Count(&common.WithdrawOrder{}, sql) sql = fmt.Sprintf("event = %v and uid = %d", common.CurrencyEventWithDraw, ret[0].UID) withdrawTotalCount := db.Mysql().Count(&common.WithdrawOrder{}, sql) resp.WithdrawSuccess = utils.GetPer(resp.WithdrawCount, withdrawTotalCount) } a.Data = resp }