package xunji

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

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

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

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

func (l *GetXunjiByIdLogic) GetXunjiById(req *types.IDReq) (*types.XunjiInfoResp, error) {
	data, err := l.svcCtx.DB.Xunji.Get(l.ctx, req.Id)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.XunjiInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.XunjiInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			Status:         &data.Status,
			AppKey:         &data.AppKey,
			AppSecret:      &data.AppSecret,
			Token:          &data.Token,
			EncodingKey:    &data.EncodingKey,
			OrganizationId: &data.OrganizationID,
		},
	}, nil
}
