package gome_live_show

import (
	"encoding/json"
	"errors"
	"git.rosy.net.cn/baseapi/utils"
	"net/http"
)

// 查询售后单
func (a *API) QueryAfterSaleList(req *QueryAfterSaleListReq) (*QueryAfterSaleListRes, error) {
	byteReq, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, QueryAfterSaleList, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteReq)))
	if err != nil {
		return nil, err
	}

	result := &QueryAfterSaleListRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}

// 创建售后单 此接口主要基于售后场景，订单处于待收货或已完成，商户在后台针对订单的单商品主动发起售后
func (a *API) CreateAfterSaleOrderApi(req *CreateOrderAfterSaleReq) (*SystemParameterRes, error) {
	byteReq, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, CreateAfterSaleOrder, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteReq)))
	if err != nil {
		return nil, err
	}

	result := &SystemParameterRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}

// 售后单详情接口
func (a *API) QueryAfterSaleOrderDetail(afterSaleNo string) (*QueryAfterSaleListRes, error) {
	byteReq, err := json.Marshal(map[string]string{"afterSaleNo": afterSaleNo})
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, QueryAfterSaleOrderDetail, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteReq)))
	if err != nil {
		return nil, err
	}

	result := &QueryAfterSaleListRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}

// 此接口主当售中（待收货、已完成）用户发起单sku的售后申请，申请后进行商家审核场景，
//商家审核通过售后单进退退款流程，商家拒绝售后单关闭，用户可进行重新申请或申请平台介入
func (a *API) AgreeOrRefuseAfterApplyOrder(req *CheckOutAfterOrderReq) (*SystemParameterRes, error) {
	byteReq, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, AgreeOrRefuseAfterApplyOrder, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteReq)))
	if err != nil {
		return nil, err
	}

	result := &SystemParameterRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}

// 售后单商户收货确认
func (a *API) MerchantConfirmOrder(afterSaleNo string) (*SystemParameterRes, error) {
	byteReq, err := json.Marshal(map[string]string{"afterSaleNo": afterSaleNo})
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, MerchantAgreeOkAfterApplyOrder, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteReq)))
	if err != nil {
		return nil, err
	}

	result := &SystemParameterRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}

// 合作方图片上传 materialType(枚举值1 图片2视频)
func (a *API) UploadPicture(data string, materialType int, materialName string) (*GomeiUploadImgRes, error) {
	byteReq, err := json.Marshal(map[string]interface{}{"data": data, "materialType": materialType, "materialName": materialName})
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, UploadImg, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteReq)))
	if err != nil {
		return nil, err
	}

	result := &GomeiUploadImgRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}
