package tiktok_api

import (
	"errors"
	order_getSettleBillDetailV3_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/order_getSettleBillDetailV3/request"
)

// GetSettleBillDetailV3 商家结算账单
// 已结算的订单才会有数据，数据T+1生成，建议第二天15点之后查询。如因任务积压导致延迟的情况，建议重试。
// 1、推荐使用start_index方式查询
func (a *API) GetSettleBillDetailV3(param *order_getSettleBillDetailV3_request.OrderGetSettleBillDetailV3Param) (int64, string, error) {
	request := order_getSettleBillDetailV3_request.New()
	request.Param = param

	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return 0, "", err
	}
	if result.Code != RequestSuccessCode {
		return 0, "", errors.New(result.SubMsg + ":" + result.LogId)
	}

	if len(result.Data.Data) == 0 {
		return 0, "", nil
	}

	var totalShopMoney int64 = 0
	var postAmount int64 = 0
	for _, v := range result.Data.Data {
		totalShopMoney += v.SettleAmount
		postAmount += v.PostAmount
	}

	return totalShopMoney - postAmount, result.Data.NextStartIndex, nil
}
