package contact

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

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

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

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

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

func (l *CreateWhatsappContactLogic) CreateWhatsappContact(req *types.CreateWhatsappContactReq) (*types.BaseMsgResp, error) {
	resp := types.BaseMsgResp{}
	organizationId := l.ctx.Value("organizationId").(uint64)

	uuidstr := uuidx.NewUUID().String()
	_, err := l.svcCtx.DB.Contact.Create().SetCtype(2).
		SetNotNilCc(req.Cc).
		SetNotNilPhone(req.Phone).
		SetWxWxid(uuidstr).
		SetWxid(uuidstr).
		SetType(5).
		SetNotNilMarkname(req.Markname).
		SetOrganizationID(organizationId).
		SetNotNilCarea(req.Carea).
		SetNotNilCage(req.Cage).
		SetNotNilSex(req.Sex).
		SetNotNilCbirtharea(req.Cbirtharea).
		SetNotNilCbirthday(req.Cbirthday).
		SetNotNilCtitle(req.Ctitle).
		SetNotNilCidcardNo(req.CidcardNo).
		SetNotNilCname(req.Cname).
		Save(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}
	resp.Msg = "创建成功"

	return &resp, nil
}
