package wxcarduser

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/errorx"
	"wechat-api/ent"
	"wechat-api/ent/wxcarduser"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *GetWxCardUserByIdLogic) GetWxCardUserById(req *types.IDReq) (*types.WxCardUserInfoResp, error) {
	data, err := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(req.Id)).Only(l.ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return nil, errorx.NewInvalidArgumentError("记录不存在")
		}
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.WxCardUserInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.WxCardUserInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			OpenId:   &data.OpenID,
			Account:  &data.Account,
			Avatar:   &data.Avatar,
			Nickname: &data.Nickname,
			Remark:   &data.Remark,
			Phone:    &data.Phone,
			Wxid:     &data.Wxid,
		},
	}, nil
}
