package logic

import (
	"context"
	"encoding/json"
	"jyBXSubscribe/rpc/type/bxsubscribe"

	"jyBXSubscribe/api/internal/svc"
	"jyBXSubscribe/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetStaffSubscribeDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetStaffSubscribeDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStaffSubscribeDetailLogic {
	return &GetStaffSubscribeDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetStaffSubscribeDetailLogic) GetStaffSubscribeDetail(req *types.GetStaffSubscribeDetailReq) (resp *types.StaffSubscribeCommonResp, err error) {
	res, err := l.svcCtx.Suscribe.GetStaffSubscribeDetail(l.ctx, &bxsubscribe.StaffSubscribeDetailReq{
		EntId:     req.EntId,
		EntUserId: req.EntUserId,
		Token:     req.Token,
	})
	if err != nil {
		return nil, err
	}
	if res.ErrCode != 0 {
		return &types.StaffSubscribeCommonResp{
			Err_code: res.ErrCode,
			Err_msg:  res.ErrMsg,
			Data:     nil,
		}, nil
	}
	subscribeDetail := map[string]interface{}{}
	if err := json.Unmarshal(res.Data, &subscribeDetail); err != nil {
		return &types.StaffSubscribeCommonResp{
			Err_code: res.ErrCode,
			Err_msg:  "返回异常",
			Data:     nil,
		}, nil
	}
	return &types.StaffSubscribeCommonResp{
		Err_code: res.ErrCode,
		Err_msg:  res.ErrMsg,
		Data:     subscribeDetail,
	}, nil
}
