package order

//用户虚拟资产表

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"hongze/hongze_cygx/utils"
	"time"
)

type CygxOrderVirtualAsset struct {
	OrderVirtualAssetsId int       `orm:"column(order_virtual_assets_id);pk";comment:"订单支付ID"`
	OrderCode            string    `comment:"订单编号"`
	UserId               int       `comment:"用户ID"`
	Mobile               string    `comment:"手机号"`
	Email                string    `comment:"邮箱"`
	CompanyId            int       `comment:"公司ID"`
	CompanyName          string    `comment:"公司名称"`
	RealName             string    `comment:"用户实际名称"`
	SellerName           string    `comment:"所属销售"`
	CreateTime           time.Time `comment:"创建时间"`
	ModifyTime           time.Time `comment:"修改时间"`
	SourceId             int       `comment:"来源ID"`
	Source               string    `comment:"来源\n报告 ：article\n活动 ：activity"`
}

// 添加
func AddCygxOrderVirtualAsset(item *CygxOrderVirtualAsset, itemOrder *CygxOrder) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()

	itemOrderAction := new(CygxOrderAction)

	switch itemOrder.TradeType {
	case "JSAPI":
		itemOrderAction.Action = "公众号支付"
	case "NATIVE":
		itemOrderAction.Action = "扫码支付"
	case "App":
		itemOrderAction.Action = "App支付"
	case "MICROPAY":
		itemOrderAction.Action = "付款码支付"
	case "MWEB":
		itemOrderAction.Action = "H5支付"
	case "FACEPAY":
		itemOrderAction.Action = "刷脸支付"
	}
	itemOrderAction.OrderStatus = 2
	itemOrderAction.OrderStatusText = "已支付"
	itemOrderAction.OrderCode = item.OrderCode
	itemOrderAction.UserId = item.UserId
	itemOrderAction.Mobile = item.Mobile
	itemOrderAction.Email = item.Email
	itemOrderAction.CompanyId = item.CompanyId
	itemOrderAction.CompanyName = item.CompanyName
	itemOrderAction.RealName = item.RealName
	itemOrderAction.SellerName = item.SellerName
	itemOrderAction.CreateTime = time.Now()
	itemOrderAction.ModifyTime = time.Now()
	itemOrderAction.RegisterPlatform = utils.REGISTER_PLATFORM

	updateParams := make(map[string]interface{})
	updateParams["PayTime"] = itemOrder.PayTime
	updateParams["PayMoney"] = itemOrder.PayMoney
	updateParams["OrderStatus"] = itemOrder.OrderStatus
	updateParams["OutTradeCode"] = itemOrder.OutTradeCode
	updateParams["ModifyTime"] = item.ModifyTime
	updateParams["PaymentType"] = 1
	updateParams["TradeType"] = itemOrder.TradeType
	ptrStructOrTableName := "cygx_order"
	whereParam := map[string]interface{}{"order_code": itemOrder.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(itemOrderAction) // 写入订单操作信息
	if err != nil {
		return
	}

	_, err = o.Insert(item) //写入用户用户虚拟资产表
	if err != nil {
		return
	}

	return
}

// 获取数量
func GetCygxOrderVirtualAssetdCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sqlCount := ` SELECT COUNT(1) AS  count  FROM cygx_order_virtual_asset WHERE   1= 1  ` + condition
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 移除用户活动权限记录
func RemoveCygxOrderVirtualAsset(item *CygxOrderVirtualAsset, itemOrder *CygxOrder) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()

	itemOrderAction := new(CygxOrderAction)

	itemOrderAction.Action = "退款成功"
	itemOrderAction.OrderStatus = 3
	itemOrderAction.OrderStatusText = "已退款"
	itemOrderAction.OrderCode = item.OrderCode
	itemOrderAction.UserId = item.UserId
	itemOrderAction.Mobile = item.Mobile
	itemOrderAction.Email = item.Email
	itemOrderAction.CompanyId = item.CompanyId
	itemOrderAction.CompanyName = item.CompanyName
	itemOrderAction.RealName = item.RealName
	itemOrderAction.SellerName = item.SellerName
	itemOrderAction.CreateTime = time.Now()
	itemOrderAction.ModifyTime = time.Now()
	itemOrderAction.RegisterPlatform = utils.REGISTER_PLATFORM

	updateParams := make(map[string]interface{})
	updateParams["OrderStatus"] = itemOrder.OrderStatus
	updateParams["ModifyTime"] = item.ModifyTime
	ptrStructOrTableName := "cygx_order"
	whereParam := map[string]interface{}{"order_code": itemOrder.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(itemOrderAction) // 写入订单操作信息
	if err != nil {
		return
	}

	//sql := ` DELETE FROM cygx_order_virtual_asset  WHERE order_code =? ` // 删除原有的持卡信息  (退款之后不删除相关权益权限)
	//_, err = o.Raw(sql, item.OrderCode).Exec()
	//if err != nil {
	//	return
	//}

	return
}
