package chatrecords

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

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

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

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

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

func (l *CreateChatRecordsLogic) CreateChatRecords(req *types.ChatRecordsInfo) (*types.BaseMsgResp, error) {
	_, err := l.svcCtx.DB.ChatRecords.Create().
		SetNotNilSessionID(req.SessionId).
		SetNotNilUserID(req.UserId).
		SetNotNilContent(req.Content).
		SetNotNilContentType(req.ContentType).
		SetNotNilBotID(req.BotId).
		SetNotNilBotType(req.BotType).
		Save(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.BaseMsgResp{
		Code: 0,
		Msg:  errormsg.Success,
	}, nil
}
