package pay

import (
	"common/remark"
	"common/resultStatus"
	"common/webServer"
	"goutil/logUtilPlus"
	"goutil/webUtil"
	"net/http"
	"paycenter/internal/alipay"
	"paycenter/internal/wxpay"
	"strconv"
)

func init() {

	//注册接口
	webServer.RegisterFunction(new(PayApi))
}

func init() {
	moduleName := "PayApi"
	desc := "用户接口"
	author := "tangping"
	mendor := ""
	date := "2025年1月8日15:49:03"
	remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
}

// PayApi 支付接口
type PayApi struct {
}

// ---------------------------------------- 接口 --------------------------------------------------
func init() {
	moduleName := "PayApi"
	methodName := "PlaceAnOrder"
	skipVerifyTokenPage := true
	methodDesc := "下单"
	methodAuthor := "tangping"
	methodMendor := ""
	methodDate := "2025年1月8日15:51:34"
	methodInParam := []string{"int64:订单id（由服务器生成）,int32:充值模版id,int64:充值金额,string：商品id, string:客户端IP地址,string:玩家id,int32:区服id"}
	methodOutParam := `
	{
		"Code '类型:int'": "响应结果的状态值",
		"Message '类型:string'": "响应结果的状态值所对应的描述信息",
		"Data '类型:interface{}'": "响应结果的数据"
		{
			"OrderID '类型:int64'": "订单id",
			"prepayId '类型:int64'": "预支付会话id",
		}
	}`
	remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
}

// 需要获取请求信息
func init() {
	moduleName := "PayApi"
	methodName := "PlaceAnOrder"
	webServer.AddNeedGetRequestInfo(moduleName, methodName)
}
func (a *PayApi) PlaceAnOrder(orderId int64, modelID int32, currency int64, storeId string, playerID string, serverID int64, r *http.Request) (responseObj *webServer.ResponseObject) {
	responseObj = webServer.GetInitResponseObj()

	//校验参数
	if modelID == 0 || playerID == "" || serverID == 0 {
		responseObj.SetResultStatus(resultStatus.APIDataError)
		return
	}

	//获取对应充值配置

	//客户端请求ip
	clientIp := webUtil.GetRequestIP(r)

	//下微信订单
	prepayId, err := wxpay.Prepay(orderId, currency, storeId, clientIp, "描述！！！！！！！！！！")
	if err != nil {
		responseObj.SetResultStatus(resultStatus.APIDataError)
		return
	}
	//处理数据
	order := NewOrder(orderId, prepayId, playerID, serverID, 1)

	//添加到数据库
	AddOrder(order)

	resultMap := make(map[string]any)
	resultMap["orderID"] = order.OrderID
	resultMap["prepayId"] = order.PrepayId
	responseObj.SetData(resultMap)
	return
}

func init() {
	moduleName := "PayApi"
	methodName := "CallBack"
	skipVerifyTokenPage := true
	methodDesc := "支付成功回调"
	methodAuthor := "tangping"
	methodMendor := ""
	methodDate := "2025年1月21日16:40:57"
	methodInParam := []string{"string:订单id"}
	methodOutParam := `
	{
		"Code '类型:int'": "响应结果的状态值",
		"Message '类型:string'": "响应结果的状态值所对应的描述信息",
		"Data '类型:interface{}'": "响应结果的数据"
		{
			"OrderID '类型:int64'": "订单id",
			"prepayId '类型:int64'": "预支付会话id",
		}
	}`
	remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
}
func (a *PayApi) CallBack(orderIDStr string) (responseObj *webServer.ResponseObject) {
	responseObj = webServer.GetInitResponseObj()

	//参数错误
	if orderIDStr == "" {
		responseObj.SetResultStatus(resultStatus.APIDataError)
		return
	}

	//orderID 转换成string
	orderID, err := strconv.ParseInt(orderIDStr, 10, 64)
	if err != nil {
		logUtilPlus.ErrorLog("订单id转换失败", err.Error())
		responseObj.SetResultStatus(resultStatus.APIParamError)
		return
	}

	//是否已经处理
	order, err := GetUserByID(orderID)

	//支付成功
	if err == nil && order != nil && order.OrderStatus == 1 {
		return
	}

	//查询订单状态
	statusStr, err := wxpay.QueryOrderByOutTradeNo(orderID)
	if err != nil {
		responseObj.SetResultStatus(resultStatus.DataError)
		return
	}

	//状态错误
	if statusStr != "SUCCESS" {
		logUtilPlus.WarnLog("订单状态错误", statusStr)
		return
	}

	err = ChangeOrderStatus(orderID, 1)
	if err != nil {
		responseObj.SetResultStatus(resultStatus.DataError)
		return
	}

	return responseObj
}

func init() {
	moduleName := "PayApi"
	methodName := "AliPayPlaceAnOrder"
	skipVerifyTokenPage := true
	methodDesc := "下单"
	methodAuthor := "tangping"
	methodMendor := ""
	methodDate := "2025年1月8日15:51:34"
	methodInParam := []string{"int64:订单id（由服务器生成）,int32:充值模版id,int64:充值金额,string：商品id, string:客户端IP地址,string:玩家id,int32:区服id"}
	methodOutParam := `
	{
		"Code '类型:int'": "响应结果的状态值",
		"Message '类型:string'": "响应结果的状态值所对应的描述信息",
		"Data '类型:interface{}'": "响应结果的数据"
		{
			"OrderID '类型:int64'": "订单id",
			"prepayId '类型:int64'": "预支付会话id",
		}
	}`
	remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam, skipVerifyTokenPage)
}

func (a *PayApi) AliPayPlaceAnOrder(orderId int64, modelID int32, currency int64, storeId string, playerID string, serverID int64, r *http.Request) (responseObj *webServer.ResponseObject) {
	responseObj = webServer.GetInitResponseObj()
	if orderId == 0 || modelID == 0 || playerID == "" || serverID == 0 {
		responseObj.SetResultStatus(resultStatus.APIDataError)
		return
	}

	//客户端请求ip
	clientIp := webUtil.GetRequestIP(r)

	//下微信订单
	prepayId, err := alipay.AliPayPlace(orderId, currency, storeId, clientIp, "描述！！！！！！！！！！")
	if err != nil {
		responseObj.SetResultStatus(resultStatus.APIDataError)
		return
	}

	//处理数据
	order := NewOrder(orderId, prepayId, playerID, serverID, 1)

	//添加到数据库
	AddOrder(order)

	resultMap := make(map[string]any)
	resultMap["orderID"] = order.OrderID
	resultMap["prepayId"] = order.PrepayId
	responseObj.SetData(resultMap)
	return responseObj
}
