package card_t_mall_order

import (
	"context"
	"kami/internal/consts"
	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/utility/config"
	"kami/utility/utils"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/os/gtime"
)

// AddAgisoCallBackBaseInfo 返回阿奇索基基本回调数据
func (h *sRechargeTMallGameOrder) AddAgisoCallBackBaseInfo(ctx context.Context, input *model.AddAgisoCallBackBaseInfoInput, tx gdb.TX) (id int64, err error) {
	m := dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	} else {
		m = config.GetDatabaseV1().Model()
	}
	err = config.GetDatabaseV1().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		//创建淘宝订单
		id_, err2 := dao.V1RechargeTMallShop.Ctx(ctx).TX(tx).InsertAndGetId(do.V1RechargeTMallShop{
			OrderNo:      input.OrderNo,
			TMallOrderNo: input.TMallOrderId,
			BuyerNick:    input.BuyerNick,
			Status:       input.Status,
			Payment:      input.Payment,
		})
		id = id_
		if err2 != nil {
			return err2
		}
		err2 = h.AddShopHistory(ctx, &model.AddOrderShopHistoryInput{
			ShopId:  int(id_),
			RawData: input.OriginalData,
			Remark:  "",
			Status:  consts.EnumShopStatusReceiveCallback,
		}, tx)
		return err2
	})
	return
}

// UpdateTMallCallBackInfo 返回淘宝详细订单数据
func (h *sRechargeTMallGameOrder) UpdateTMallCallBackInfo(ctx context.Context, input *model.UpdateAgisoCallBackInfoInput, tx gdb.TX) (err error) {
	m := dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		_, err2 := dao.V1RechargeTMallShop.Ctx(ctx).TX(tx).DB(config.GetDatabaseV1()).
			Where(dao.V1RechargeTMallShop.Columns().Id, input.Id).
			Update(do.V1RechargeTMallShop{
				OrderNo:      input.OrderNo,
				TMallOrderNo: input.TMallOrderId,
				CreateTime:   input.Created,
				BuyerNick:    input.BuyerNick,
				BuyerRate:    input.BuyerRate,
				TradeStatus:  input.TradeStatus,
				Status:       input.Status,
				Payment:      input.Payment,
				TotalFee:     input.TotalFee,
				PayTime:      input.PayTime,
				EndTime:      input.EndTime,
				BuyerMemo:    input.BuyerMemo,
				AlipayNo:     input.AlipayNo,
				BuyerMessage: input.BuyerMessage,
			})
		if err2 != nil {
			return err2
		}
		err2 = h.AddShopHistory(ctx, &model.AddOrderShopHistoryInput{
			ShopId:  int(input.Id),
			RawData: input.OriginalData,
			Remark:  "",
			Status:  input.Status,
		}, tx)
		return err2
	})
	return
}

func (h *sRechargeTMallGameOrder) ShopList(ctx context.Context, input *model.RechargeTMallGameShopOrderListInput) (total int, data []*entity.V1RechargeTMallShop, err error) {
	total = 0
	data = make([]*entity.V1RechargeTMallShop, 0)
	m := dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1()).Page(input.Current, input.PageSize)
	if input.Account != "" {
		m = m.Where(dao.V1RechargeTMallShop.Columns().Account, input.Account)
	}
	if input.TMallOrderNo != "" {
		m = m.Where(dao.V1RechargeTMallShop.Columns().TMallOrderNo, input.TMallOrderNo)
	}
	if input.OrderNo != "" {
		m = m.Where(dao.V1RechargeTMallShop.Columns().OrderNo, input.OrderNo)
	}
	if len(input.DateRange) == 2 {
		m = m.WhereBetween(dao.V1RechargeTMallShop.Columns().CreatedAt, input.DateRange[0], input.DateRange[1])
	}
	if input.BuyerNickName != "" {
		m = m.WhereLike(dao.V1RechargeTMallShop.Columns().BuyerNick, utils.OrmLike(input.BuyerNickName))
	}
	err = m.OrderDesc(dao.V1RechargeTMallShop.Columns().CreatedAt).ScanAndCount(&data, &total, false)
	return
}

// GetShopOne 获取订单详情
func (h *sRechargeTMallGameOrder) GetShopOne(ctx context.Context, shopOrderId int64) (res *entity.V1RechargeTMallShop, err error) {
	res = &entity.V1RechargeTMallShop{}
	err = dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1RechargeTMallShop.Columns().Id, shopOrderId).Scan(&res)
	return
}

// GetShopOneByTid 根据天猫ID获取订单
func (h *sRechargeTMallGameOrder) GetShopOneByTid(ctx context.Context, iId string, tx gdb.TX) (data *entity.V1RechargeTMallShop, err error) {
	data = &entity.V1RechargeTMallShop{}
	m := dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Where(dao.V1RechargeTMallShop.Columns().TMallOrderNo, iId).Scan(&data)
	return
}

// IsExist 判断当前订单是否存在
func (h *sRechargeTMallGameOrder) IsExist(ctx context.Context, tMallOrderNo string) (bool, error) {
	count, err := dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1RechargeTMallShop.Columns().TMallOrderNo, tMallOrderNo).Count()
	err = utils.HandleNoRowsError(err)
	return count > 0, err
}

// UpdateEndTime 更新订单确认收货时间
func (h *sRechargeTMallGameOrder) UpdateEndTime(ctx context.Context, shopId int64) {
	_, _ = dao.V1RechargeTMallShop.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1RechargeTMallShop.Columns().Id, shopId).Update(do.V1RechargeTMallShop{
		EndTime: gtime.Now(),
	})
	_ = h.AddShopHistory(ctx, &model.AddOrderShopHistoryInput{
		ShopId:  int(shopId),
		RawData: "",
		Remark:  "",
		Status:  consts.EnumShopStatusConfirmOrder,
	}, nil)
}
