package Msg

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 CreateMsgLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *CreateMsgLogic) CreateMsg(req *types.MsgInfo) (*types.BaseMsgResp, error) {
    _, err := l.svcCtx.DB.Msg.Create().
			SetNotNilStatus(req.Status).
			SetNotNilFromwxid(req.Fromwxid).
			SetNotNilToid(req.Toid).
			SetNotNilMsgtype(req.Msgtype).
			SetNotNilMsg(req.Msg).
			SetNotNilBatchNo(req.BatchNo).
			Save(l.ctx)

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

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