package notice

import (
	"context"
	"time"

	"bsm/full/module/ec/mall/internal/impl"
	"bsm/full/module/ec/mall/internal/models"
	pb "bsm/full/module/ec/mall/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"git.apinb.com/bsm-sdk/core/types"
	"git.apinb.com/bsm-sdk/core/utils"
)

// 新建公告
func Create(ctx context.Context, in *pb.NoticeItem) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	AUTH, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	if in.GetTitle() == "" {
		return nil, errcode.ErrInvalidArgument
	}

	// 参数转换
	data := models.MallNotice{
		Title:      in.Title,
		Author:     in.Author,
		Content:    in.Content,
		Std_IICUDS: types.Std_IICUDS{Identity: utils.UUID(), Status: 1},
	}

	owner := AUTH.Owner.(map[string]any)
	data.Store_ID = uint(owner["store_id"].(float64))
	data.Store_Identity = owner["store_identity"].(string)

	err = impl.DBService.Create(&data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	return &pb.StatusReply{
		Code:    0,
		Message: "OK",
		Timeseq: time.Now().UnixMilli(),
	}, nil

}
