package card_redeem_account

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

	"github.com/duke-git/lancet/v2/slice"
	"github.com/gogf/gf/v2/database/gdb"
)

func (a *sCardRedeemAccount) GetByCK(ctx context.Context, ck string) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	err = dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).WhereLike(dao.V1CardRedeemAccountInfo.Columns().Cookie, utils.OrmLike(ck)).Scan(&data)
	return
}

// GetOneById 获取一个账号信息
func (a *sCardRedeemAccount) GetOneById(ctx context.Context, id string, tx gdb.TX) (data *entity.V1CardRedeemAccountInfo, err error) {
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Where(dao.V1CardRedeemAccountInfo.Columns().Id, id).Scan(&data)
	err = utils.HandleNoRowsError(err)
	return
}

func (a *sCardRedeemAccount) GetByNickname(ctx context.Context, nickName string) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	//m := dao.V1CardJdAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1CardJdAccountInfo.Columns().NickName, )
	return
}

func (a *sCardRedeemAccount) GetByName(ctx context.Context, username string) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	err = dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).WhereLike(dao.V1CardRedeemAccountInfo.Columns().Name, utils.OrmLike(username)).Scan(&data)
	return
}

func (a *sCardRedeemAccount) GetByGroup(ctx context.Context, groupId int) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	err = dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1CardRedeemAccountInfo.Columns().GroupId, groupId).Scan(&data)
	return
}

func (a *sCardRedeemAccount) Query(ctx context.Context, input *model.RedeemAccountQueryInput, tx gdb.TX) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	if input.Ck != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Cookie, utils.OrmLike(input.Ck))
	}
	if input.NickName != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Nickname, utils.OrmLike(input.NickName))
	}
	if input.Name != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Name, utils.OrmLike(input.Name))
	}
	if input.GroupId != 0 {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().GroupId, input.GroupId)
	}
	err = m.Scan(&data)
	return
}

func (a *sCardRedeemAccount) GetNormalOneByUsername(ctx context.Context, username string, category consts.CardRedeemAccountCategory) (data *entity.V1CardRedeemAccountInfo, err error) {
	err = dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1CardRedeemAccountInfo.Columns().Username, username).
		Where(dao.V1CardRedeemAccountInfo.Columns().Category, category).
		Where(dao.V1CardRedeemAccountInfo.Columns().Status, consts.RedeemAccountNormal).
		Scan(&data)
	return
}

func (a *sCardRedeemAccount) GetNormalOneByCk(ctx context.Context, category consts.CardRedeemAccountCategory, username string) (data *entity.V1CardRedeemAccountInfo, err error) {
	err = dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).Where(dao.V1CardRedeemAccountInfo.Columns().Username, username).
		Where(dao.V1CardRedeemAccountInfo.Columns().Category, category).
		Where(dao.V1CardRedeemAccountInfo.Columns().Status, consts.RedeemAccountNormal).
		Scan(&data)
	return
}

// List 获取所有苹果账户
func (a *sCardRedeemAccount) List(ctx context.Context, input *model.RedeemAccountListInput) (total int, data []*model.RedeemAccountOutput, err error) {
	data = make([]*model.RedeemAccountOutput, 0)
	total = 0
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if input.NickName != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Nickname, utils.OrmLike(input.NickName))
	}
	if input.UserId != "" {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().CreateUserId, input.UserId)
	}
	if input.Cookie != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Cookie, utils.OrmLike(input.Cookie))
	}
	if input.Name != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Name, utils.OrmLike(input.Name))
	}
	if input.Category != "" {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().Category, input.Category)
	}
	if input.CreatedUserName != "" {
		userInfo, _ := service.SysUser().GetByUserName(ctx, input.CreatedUserName)
		accountList := slice.Map(userInfo, func(index int, item *entity.V1SysUser) string {
			return item.Id
		})
		m = m.WhereIn(dao.V1CardRedeemAccountInfo.Columns().CreateUserId, accountList)
	}
	if input.GroupId != 0 {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().GroupId, input.GroupId)
	}
	if input.PageSize != 0 && input.Current != 0 {
		m = m.Page(input.Current, input.PageSize)
	}

	err = m.WithAll().OrderDesc(dao.V1CardRedeemAccountInfo.Columns().CreatedAt).
		ScanAndCount(&data, &total, true)
	return
}

// GetRandomOne 随机获取一个京东账号
func (a *sCardRedeemAccount) GetRandomOne(ctx context.Context, tx gdb.TX) (data *entity.V1CardRedeemAccountInfo, err error) {
	data = &entity.V1CardRedeemAccountInfo{}
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Where(dao.V1CardRedeemAccountInfo.Columns().Status, consts.RedeemAccountNormal).
		OrderRandom().Scan(&data)
	err = utils.HandleNoRowsError(err)
	return
}

// CheckCurrentIsAvailable 判断当前账号是否可用
func (a *sCardRedeemAccount) CheckCurrentIsAvailable(ctx context.Context, id string, chargeAmount float64, tx gdb.TX) (isExist bool, err error) {
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	isExist, err = m.Where(dao.V1CardRedeemAccountInfo.Columns().Id, id).Where(dao.V1CardRedeemAccountInfo.Columns().Status, consts.RedeemAccountNormal).
		WhereGT(dao.V1CardRedeemAccountInfo.Columns().MaxCountLimit, dao.V1CardRedeemAccountInfo.Columns().AmountTodayCount).
		WhereNot(dao.V1CardRedeemAccountInfo.Columns().MaxCountLimit, 0).
		WhereGTE(fmt.Sprintf("`%s`-`%s`", dao.V1CardRedeemAccountInfo.Columns().MaxAmountLimit, dao.V1CardRedeemAccountInfo.Columns().AmountTotalSum), chargeAmount).
		WhereOr(dao.V1CardRedeemAccountInfo.Columns().MaxAmountLimit, 0).
		Exist()
	return
}

// GetAllAvailableAccount 获取所有可用的账号
func (a *sCardRedeemAccount) GetAllAvailableAccount(ctx context.Context, category consts.CardRedeemAccountCategory, chargeAmount float64, tx gdb.TX) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Where(dao.V1CardRedeemAccountInfo.Columns().Status, consts.RedeemAccountNormal).
		Where(dao.V1CardRedeemAccountInfo.Columns().Category, category).WhereGT(dao.V1CardRedeemAccountInfo.Columns().MaxCountLimit, dao.V1CardRedeemAccountInfo.Columns().AmountTodayCount).
		WhereOr(dao.V1CardRedeemAccountInfo.Columns().MaxCountLimit, 0).
		WhereGTE(fmt.Sprintf("`%s`-`%s`", dao.V1CardRedeemAccountInfo.Columns().MaxAmountLimit, dao.V1CardRedeemAccountInfo.Columns().AmountTotalSum), chargeAmount).
		WhereOr(dao.V1CardRedeemAccountInfo.Columns().MaxAmountLimit, 0).OrderAsc(dao.V1CardRedeemAccountInfo.Columns().CreatedAt).
		Scan(&data)
	return
}

// GetAllNormalAccount 获取所有正常账户
func (a *sCardRedeemAccount) GetAllNormalAccount(ctx context.Context, tx gdb.TX) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Where(dao.V1CardRedeemAccountInfo.Columns().Status, consts.RedeemAccountNormal).Scan(&data)
	return
}

// GetCountByStatus 获取指定状态的账号数量
func (a *sCardRedeemAccount) GetCountByStatus(ctx context.Context, status consts.RedeemAccountStatus) (count int, err error) {
	count = 0
	count, err = dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardRedeemAccountInfo.Columns().Status, status).Count()
	return count, utils.HandleNoRowsError(err)
}

// GetAllByUserId 下载当前上传账户所有数据
func (a *sCardRedeemAccount) GetAllByUserId(ctx context.Context, userId string, category consts.CardRedeemAccountCategory, tx gdb.TX) (data []*entity.V1CardRedeemAccountInfo, err error) {
	data = make([]*entity.V1CardRedeemAccountInfo, 0)
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardRedeemAccountInfo.Columns().Category, category)
	if tx != nil {
		m = m.TX(tx)
	}
	if userId != "" {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().CreateUserId, userId)
	}
	err = m.Scan(&data)
	err = utils.HandleNoRowsError(err)
	return
}

// GetAll 获取所有账户
func (a *sCardRedeemAccount) GetAll(ctx context.Context, tx gdb.TX) (data []*entity.V1CardRedeemAccountInfo, err error) {
	return
}
