package contact

import (
	"context"

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

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

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

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

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

func (l *CreateContactLogic) CreateContact(req *types.ContactInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	_, err := l.svcCtx.DB.Contact.Create().
		SetNotNilStatus(req.Status).
		SetNotNilWxWxid(req.WxWxid).
		SetNotNilType(req.Type).
		SetNotNilWxid(req.Wxid).
		SetNotNilAccount(req.Account).
		SetNotNilNickname(req.Nickname).
		SetNotNilMarkname(req.Markname).
		SetNotNilHeadimg(req.Headimg).
		SetNotNilSex(req.Sex).
		SetNotNilStarrole(req.Starrole).
		SetNotNilDontseeit(req.Dontseeit).
		SetNotNilDontseeme(req.Dontseeme).
		SetNotNilLag(req.Lag).
		SetNotNilGid(req.Gid).
		SetNotNilGname(req.Gname).
		SetNotNilV3(req.V3).
		SetOrganizationID(organizationId).
		SetNotNilCtype(req.Ctype).
		SetNotNilCname(req.Cname).
		SetNotNilCarea(req.Carea).
		SetNotNilCage(req.Cage).
		SetNotNilCbirthday(req.Cbirthday).
		SetNotNilCbirtharea(req.Cbirtharea).
		SetNotNilCc(req.Cc).
		SetNotNilPhone(req.Phone).
		SetNotNilCidcardNo(req.CidcardNo).
		SetNotNilCtitle(req.Ctitle).
		Save(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
}
