package cast

import (
	"encoding/json"
	"errors"
	"fmt"
	"fonchain-fiee/cmd/config"
	"fonchain-fiee/pkg/e"
	modelCast "fonchain-fiee/pkg/model/cast"
	"fonchain-fiee/pkg/utils"

	"go.uber.org/zap"
)

type CastService struct {
}

func (c *CastService) ApprovalDetail(approvalIds []int) (data map[int]modelCast.Item, err error) {
	type ApprovalDetailRequest struct {
		ID []int `json:"ID"`
	}
	req := ApprovalDetailRequest{
		ID: approvalIds,
	}
	idsBytes, _ := json.Marshal(req)
	var respBody string
	url := fmt.Sprintf(config.AppConfig.System.FieeHost + "/approval/list/ex")
	respBody, err = utils.Post(url, string(idsBytes))
	if err != nil {
		zap.L().Error("ApprovalDetail error", zap.Error(err))
		return
	}
	var respDetail modelCast.ApprovalDetailResponse
	if err = json.Unmarshal([]byte(respBody), &respDetail); err != nil {
		err = errors.New(e.GetMsg(e.JsonUnmarshal))
		return
	}
	if respDetail.Status == 0 && len(respDetail.Data.Data) > 0 {
		data = make(map[int]modelCast.Item, len(respDetail.Data.Data))
		for _, v := range respDetail.Data.Data {
			data[v.ID] = v
		}
	} else {
		err = errors.New(e.GetMsg(e.ErrorHttp))
		return
	}
	return
}
