package cygx

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type CygxOrderRefund struct {
	//OrderRefundID   uint64    `description:"主键id"`
	OrderRefundId   int       `orm:"column(order_refund_id);pk";comment:"主键id"`
	OrderCode       string    `description:"订单编号"`
	RefundTradeCode string    `description:"退款交易号"`
	RefundMoney     float64   `description:"退款金额"`
	RefundWay       int       `description:"退款方式（1：微信商户，2：支付宝商户，3：线下）"`
	RefundTime      time.Time `description:"退款时间"`
	Remark          string    `description:"备注"`
	CreateTime      time.Time `description:"创建时间"`
	AdminId         int       `description:"管理员ID"`
	AdminName       string    `description:"管理员姓名"`
}

type CygxOrderRefundReq struct {
	OrderCode string `description:"订单编号"`
	Remark    string `description:"备注"`
}

// 添加CygxOrderRefund
func AddCygxOrderRefund(item *CygxOrderRefund, orderStatus int) (err error) {
	to := orm.NewOrmUsingDB("hz_cygx")
	o, err := to.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = o.Rollback()
		} else {
			_ = o.Commit()
		}
	}()
	updateParams := make(map[string]interface{})
	updateParams["OrderStatus"] = orderStatus
	updateParams["RefundMoney"] = item.RefundMoney
	updateParams["RefundRemark"] = item.Remark
	updateParams["RefundTime"] = time.Now()
	ptrStructOrTableName := "cygx_order"
	whereParam := map[string]interface{}{"order_code": item.OrderCode}
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	if err != nil {
		return
	}
	_, err = o.Insert(item)
	return
}
