package card_redeem_account

import (
	"context"
	v1 "kami/api/card_info_walmart/v1"
	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"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"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/os/gtime"
)

func (a *sCardRedeemAccount) DailySummary(ctx context.Context) (err error) {
	if err = config.GetDatabaseV1().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		accountInfo, err2 := service.CardRedeemOrder().DailySummary(ctx, gtime.Now().Add(-gtime.D), tx)
		if err2 != nil {
			glog.Error(ctx, "充值订单统计错误", err2)
			return err2
		}
		data := make([]*do.V1CardRedeemAccountSummary, 0)
		for _, output := range accountInfo {
			data = append(data, &do.V1CardRedeemAccountSummary{
				AccountId:        output.AccountId,
				AmountTotalSum:   output.AmountTotalSum,
				AmountTodaySum:   output.Amount,
				AmountTotalCount: output.AmountTotalCount,
				AmountTodayCount: output.Count,
				Date:             gtime.Now().Add(-gtime.D),
				Category:         output.Category,
			})
		}
		if len(data) == 0 {
			return nil
		}
		_, err2 = dao.V1CardRedeemAccountSummary.Ctx(ctx).DB(config.GetDatabaseV1()).TX(tx).Data(data).Insert()
		return err2
	}); err != nil {
		glog.Error(ctx, "充值订单统计错误", err)
	}
	return
}

func (a *sCardRedeemAccount) GetSummary(ctx context.Context, input *model.AccountDailySummaryGetInput, tx gdb.TX) (total int, output []*model.AccountDailySummaryGetOutput, err error) {
	//查询当前用户上传账户

	orderSummary, err := service.CardRedeemOrder().Summary(ctx, input.CommonPageReq, input.Category, input.Username, input.Date, tx)
	if err != nil {
		return
	}

	// 查询所有账户
	var accountInfos []*v1.AccountInfo
	m := dao.V1CardRedeemAccountInfo.Ctx(ctx).DB(config.GetDatabaseV1()).Unscoped()
	if tx != nil {
		m = m.TX(tx)
	}
	accountIds := slice.Map(orderSummary, func(index int, item *model.RedeemCardSummaryOutput) string {
		return item.AccountId
	})
	m = m.WhereIn(dao.V1CardRedeemAccountInfo.Columns().Id, accountIds)
	if input.CreatedUserId != "" {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().CreateUserId, input.CreatedUserId)
	}
	if input.Category != "" {
		m = m.Where(dao.V1CardRedeemAccountInfo.Columns().Category, input.Category)
	}
	if input.AccountName != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Name, utils.OrmLike(input.AccountName))
	}
	if input.Username != "" {
		m = m.WhereLike(dao.V1CardRedeemAccountInfo.Columns().Username, utils.OrmLike(input.Username))
	}
	_ = m.WithAll().Scan(&accountInfos)

	// 查询所有分组
	total, groups, err := service.CardRedeemAccount().GroupAll(ctx, &model.RedeemAccountGroupAll{
		Category:      input.Category,
		CreatedUserId: input.CreatedUserId,
		GroupIds: slice.Map(accountInfos, func(index int, item *v1.AccountInfo) int {
			return item.GroupId
		}),
	}, tx)
	if err != nil {
		return
	}

	slice.ForEachConcurrent(orderSummary, func(index int, item *model.RedeemCardSummaryOutput) {
		accountInfo, ok := slice.FindBy(accountInfos, func(index int, item2 *v1.AccountInfo) bool {
			return item2.Id == item.AccountId
		})
		if !ok {
			accountInfo = &v1.AccountInfo{
				Id: item.AccountId,
			}
		}
		group, ok := slice.FindBy(groups, func(index int, item2 *entity.V1CardRedeemAccountGroup) bool {
			return item2.Id == accountInfo.GroupId
		})
		if !ok {
			group = &entity.V1CardRedeemAccountGroup{
				Id: accountInfo.GroupId,
			}
		}
		output = append(output, &model.AccountDailySummaryGetOutput{
			CardRedeemAccountSummary: &v1.CardRedeemAccountSummary{
				AccountId:        item.AccountId,
				AmountTodaySum:   item.Amount,
				AmountTodayCount: item.Count,
				Date:             item.Date.StartOfDay(),
				AccountInfo:      accountInfo,
				AmountTotalSum:   accountInfo.AmountTotalSum,
				AmountTotalCount: accountInfo.AmountTotalCount,
				CreatedAt:        accountInfo.CreatedAt,
				UpdatedAt:        accountInfo.UpdatedAt,
				DeletedAt:        accountInfo.DeletedAt,
				GroupName:        group.Name,
			},
		})
	}, 5)
	return
}
