package tao_vegetable

import (
	"encoding/json"
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability3156"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability3156/domain"
	request2 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability3156/request"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability591"
	domain591 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability591/domain"
	request591 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability591/request"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/util"
	"git.rosy.net.cn/baseapi/utils"
	"io/ioutil"
	"net/http"
	"time"
)

// AgreeUserCancel 同意用户售后申请
func (a *API) AgreeUserCancel(req *request2.AlibabaTclsAelophyRefundAgreeRequest) error {
	client := ability3156.NewAbility3156(&a.client)

	data, _ := client.AlibabaTclsAelophyRefundAgree(req, a.token)

	if !*data.Result.IsSuccess {
		return fmt.Errorf(*data.Result.ReturnMsg)
	}

	return nil
}

// DisAgreeUserCancel 拒绝用户售后申请
func (a *API) DisAgreeUserCancel(req *request2.AlibabaTclsAelophyRefundDisagreeRequest) error {
	client := ability3156.NewAbility3156(&a.client)

	data, _ := client.AlibabaTclsAelophyRefundDisagree(req, a.token)

	if !*data.Result.IsSuccess {
		return fmt.Errorf(*data.Result.ReturnMsg)
	}

	return nil
}

// PartialRefundReason 商户逆向取消订单数据渲染(获取订单商品列表已经取消原因:部分退款)
func (a *API) PartialRefundReason(req *request2.AlibabaTclsAelophyRefundCsapplyrenderRequest) (*domain.AlibabaTclsAelophyRefundCsapplyrenderRefundCsApplyRenderResponseDto, error) {
	client := ability3156.NewAbility3156(&a.client)

	data, _ := client.AlibabaTclsAelophyRefundCsapplyrender(req, a.token)

	if !*data.ApiResult.Success {
		return nil, fmt.Errorf(*data.ApiResult.ErrMsg)
	}

	return data.ApiResult.Model, nil
}

// PartialRefund 商户逆向取消订单
func (a *API) PartialRefund(req *request2.AlibabaTclsAelophyRefundCsapplyNewRequest) error {
	client := ability3156.NewAbility3156(&a.client)

	data, _ := client.AlibabaTclsAelophyRefundCsapplyNew(req, a.token)

	if !*data.ApiResult.Success {
		return fmt.Errorf(*data.ApiResult.ErrMsg)
	}

	return nil
}

// UserApplyRefund 用户申请售后单消息通知
func (a *API) UserApplyRefund(c *http.Request) (*UserApplyRefundCallBack, string, error) {
	data, err := ioutil.ReadAll(c.Body)
	if err != nil {
		return nil, "", err
	}

	if len(data) == 0 {
		return nil, "", err
	}
	var result *UserApplyRefundCallBack
	if err := json.Unmarshal(data, &result); err != nil {
		return nil, "", err
	}
	return result, string(data), nil
}

// UserCancelRefundApply 用户取消售后申请
func (a *API) UserCancelRefundApply(c *http.Request) (*UserCancelRefundApply, string, error) {
	data, err := ioutil.ReadAll(c.Body)
	if err != nil {
		return nil, "", err
	}

	if len(data) == 0 {
		return nil, "", err
	}
	var result *UserCancelRefundApply
	if err := json.Unmarshal(data, &result); err != nil {
		return nil, "", err
	}
	return result, string(data), nil
}

// OnSaleRefundOrder 用户售中取消
func (a *API) OnSaleRefundOrder(c *http.Request) (*OnSaleCancel, string, error) {
	body, _ := ioutil.ReadAll(c.Body)

	if len(body) == 0 {
		return nil, "", nil
	}
	var onSaleOrder *OnSaleCancel
	if err := json.Unmarshal(body, &onSaleOrder); err != nil {
		return nil, "", err
	}
	return onSaleOrder, string(body), nil
}

// RefundOrderFinish 同城零售逆向订单状态完成接口(退款完成)
func (a *API) RefundOrderFinish(c *http.Request) (*RefundOrderFinish, string, error) {
	data, err := ioutil.ReadAll(c.Body)
	if err != nil {
		return nil, "", err
	}

	if len(data) == 0 {
		return nil, "", nil
	}
	var result *RefundOrderFinish
	if err := json.Unmarshal(data, &result); err != nil {
		return nil, "", err
	}
	return result, string(data), nil
}

// QueryAfsOrderDetail 获取订单退款详情
func (a *API) QueryAfsOrderDetail(req *request591.AlibabaWdkOrderRefundGetRequest) (*domain591.AlibabaWdkOrderRefundGetOrderSyncRefundListResult, error) {
	client := ability591.NewAbility591(&a.client)

	data, _ := client.AlibabaWdkOrderRefundGet(req, a.token)

	if !*data.Result.Success {
		return nil, fmt.Errorf(*data.Result.ReturnMsg)
	}

	return &data.Result, nil
}

// QueryAfsOrderDetailList 批量获取订单退款详情
func (a *API) QueryAfsOrderDetailList(req *request591.AlibabaWdkOrderRefundListRequest) (*domain591.AlibabaWdkOrderRefundListOrderSyncRefundListResult, error) {
	client := ability591.NewAbility591(&a.client)

	data, _ := client.AlibabaWdkOrderRefundList(req, a.token)

	if !*data.Result.Success {
		return nil, fmt.Errorf(*data.Result.ReturnMsg)
	}

	return &data.Result, nil
}

// GetTransactionOrderListByTime 获取门店当天订单
func (a *API) GetTransactionOrderListByTime(storeId string, queryData time.Time) ([]string, error) {
	createTime := util.LocalTime(time.Date(queryData.Year(), queryData.Month(), queryData.Day(), 0, 0, 0, 0, queryData.Location()))
	endTime := util.LocalTime(time.Date(queryData.Year(), queryData.Month(), queryData.Day(), 23, 59, 59, 59, queryData.Location()))
	var pageIndex int64 = 1
	var PageSize int64 = 200
	param := &request591.AlibabaWdkOrderListRequest{
		BatchQueryRequest: &domain591.AlibabaWdkOrderListBatchQueryRequest{
			StoreIds:  &[]string{storeId},
			StartTime: &createTime,
			EndTime:   &endTime,
			OrderFrom: utils.Int64ToPointer(ChannelCome),
			ShopId:    nil,
			StoreId:   nil,
			BizType:   nil,
		},
	}
	vendorOrderList := make([]string, 0, 0)

	for {
		param.BatchQueryRequest.SetPageSize(PageSize)
		param.BatchQueryRequest.SetPageIndex(pageIndex)
		result, err := a.GetTransactionOrderList(param)
		if err != nil {
			return nil, err
		}

		for _, v := range *result.Orders {
			vendorOrderList = append(vendorOrderList, utils.Int64ToStr(*v.BizOrderId))
		}
		if *result.NextIndex != -1 {
			pageIndex += 1
		} else {
			break
		}
	}

	return vendorOrderList, nil
}

// GetTransactionOrderList 实时拉取交易列表
func (a *API) GetTransactionOrderList(req *request591.AlibabaWdkOrderListRequest) (*domain591.AlibabaWdkOrderListResult, error) {
	client := ability591.NewAbility591(&a.client)

	data, _ := client.AlibabaWdkOrderList(req, a.token)

	if data.Result.Orders == nil {
		return nil, fmt.Errorf("未查询到订单信息")
	}

	return &data.Result, nil
}

// QueryBillList 获取账单信息
func (a *API) QueryBillList(req *request591.AlibabaWdkBillListRequest) (*domain591.AlibabaWdkBillListTxdBillListGetResult, error) {
	client := ability591.NewAbility591(&a.client)

	data, _ := client.AlibabaWdkBillList(req, a.token)
	if data != nil && !*data.ApiResult.Success {
		return nil, fmt.Errorf(*data.ApiResult.ErrMsg)
	}

	return data.ApiResult.Model, nil
}
