package pposRequest

import (
	"PPOSUtils/utils"
	"errors"
	"net/url"
	"strings"
)

//参数：子系统ID
func (that *PPOSClient) NewSkuInfoRequest(sysID int64) (*SkuInfoRequest, error) {
	switch sysID {
	case 11:
		return nil, errors.New("该业务接口暂未实现") //会员..
	case 12:
		return that.NewTicketSkuInfoRequest(), nil //门票
	case 13:
		return that.NewFieldSkuInfoRequest(), nil //场地
	case 14:
		return nil, errors.New("该业务接口暂未实现") //商品..
	case 15:
		return that.NewCosSkuInfoRequest(), nil //通用收款
	case 16:
		return nil, errors.New("该业务接口暂未实现") //赛事..
	case 18:
		return that.NewLeaseSkuInfoRequest(), nil //租赁
	default:
		return nil, errors.New("暂未实现该业务类型创单接口")
	}
}

//NewCosSkuInfoRequest 产品信息（场地）
func (that *PPOSClient) NewFieldSkuInfoRequest() *SkuInfoRequest {
	return &SkuInfoRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/fieldBuyPlan/skuInfoList",
		},
		SysID: 13,
	}
}

//NewTicketSkuInfoRequest 产品信息（门票）
func (that *PPOSClient) NewTicketSkuInfoRequest() *SkuInfoRequest {
	return &SkuInfoRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/ticketSale/skuInfoList",
		},
		SysID: 12,
	}
}

//NewCosSkuInfoRequest 产品信息（通用收款）
func (that *PPOSClient) NewCosSkuInfoRequest() *SkuInfoRequest {
	return &SkuInfoRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/product/find",
		},
		SysID: 15,
	}
}

//NewLeaseSkuInfoRequest 产品信息（租赁）
func (that *PPOSClient) NewLeaseSkuInfoRequest() *SkuInfoRequest {
	return &SkuInfoRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/leaseSale/skuInfoList",
		},
		SysID: 18,
	}
}

type SkuInfoRequest struct {
	Request
	SkuSlice []string
	SysID    int64
	//
	skuType int64
}

func (that *SkuInfoRequest) SetSku(sku string) *SkuInfoRequest {
	if that.SkuSlice == nil {
		that.SkuSlice = []string{}
	}
	that.SkuSlice = append(that.SkuSlice, sku)
	that.skuType = 2
	return that
}
func (that *SkuInfoRequest) SetSkuStr(value string) *SkuInfoRequest {
	that.initParams()
	that.params.Set("sku_slice", value)
	return that
}

//Params ...
func (that *SkuInfoRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.skuType == 2 {
		_params.Set("sku_slice", strings.Join(that.SkuSlice, ","))
	}
	_params.Set("sys_id", utils.ToString(that.SysID))
	return _params, nil
}

//Do ...
func (that *SkuInfoRequest) Do() ([]*SkuInfoResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//data为 分页结构
	//_listData, _err := that.ParseList(_data)
	//if _err != nil {
	//	return nil, _err
	//}
	//返回结果
	_rs := []*SkuInfoResponse{}
	_sliceData, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	for _, _v := range _sliceData {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}
		_tmp := new(SkuInfoResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
