package contact

import (
	"context"
	"wechat-api/ent/contact"
	"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 UpdateContactLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *UpdateContactLogic) UpdateContact(req *types.ContactInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	err := l.svcCtx.DB.Contact.UpdateOneID(*req.Id).
		Where(contact.OrganizationID(organizationId)).
		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).
		Exec(l.ctx)

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

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