package contact_field_template

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

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

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

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

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

func (l *UpdateContactFieldTemplateLogic) UpdateContactFieldTemplate(req *types.ContactFieldTemplateInfo) (resp *types.BaseMsgResp, err error) {
	organizationId := l.ctx.Value("organizationId").(uint64)

	template := ConvertContactFieldTemplates(req.Template)

	err = l.svcCtx.DB.ContactFieldTemplate.
		Update().
		Where(contactfieldtemplate.OrganizationID(organizationId)).
		SetTemplate(template).
		Exec(l.ctx)

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

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