package trade

import (
	"Wine-Server/utils"
	"Wine-Server/utils/tables"
	"github.com/gorilla/websocket"
)

// QueryUser @admin.trade.queryUser
func QueryUser(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeQueryUserRes", utils.Fail("权限不足")))
		return
	}
	var param queryUserParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeQueryUserRes", utils.Fail("参数错误")))
		return
	}
	total, users, err := tables.UserQuery(param.Id, param.Vip, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeQueryUserRes", utils.Fail("查询用户失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"superOrderTradeQueryUserRes",
		utils.Success(utils.JsonType{"total": total, "list": users}),
	))
}

// QueryTrade @admin.trade.queryTrade
func QueryTrade(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeQueryTradeRes", utils.Fail("权限不足")))
		return
	}
	var param queryTradeParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeQueryTradeRes", utils.Fail("参数错误")))
		return
	}
	total, trades, err := tables.TradeQuery(param.Cond, param.Uid, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeQueryTradeRes", utils.Fail("查询订单失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"superOrderTradeQueryTradeRes",
		utils.Success(utils.JsonType{"total": total, "list": trades}),
	))
}

// Refund @admin.trade.refund
func Refund(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeRefundRes", utils.Fail("权限不足")))
		return
	}
	var param refundParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeRefundRes", utils.Fail("参数错误")))
		return
	}
	trade := tables.TradeTable{Id: param.Tid}
	err = trade.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeRefundRes", utils.Fail("订单查询失败")))
		return
	}
	if trade.Cash < param.Cash {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superOrderTradeRefundRes", utils.Fail("超出原订单金额")))
		return
	}
	refund := tables.RefundTable{Tid: param.Tid, Manager: manager.Id, Cash: param.Cash, Reason: param.Reason}
	err = refund.Insert()
	if err != nil {
		err = refund.Get()
		refund.Manager, refund.Cash, refund.Reason = manager.Id, param.Cash, param.Reason
		err = refund.UpdateSelf()
	}
	_ = conn.WriteJSON(utils.WsEvent("superOrderTradeRefundRes", utils.Success(nil)))
	// update user table
	user := tables.UserTable{Id: trade.Payer}
	err = user.Get()
	if err != nil {
		utils.Logger.Println(err)
	}
	user.RefundCount++
	user.RefundCost += uint64(param.Cash)
	_ = user.UpdateSelf()
}
