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"
)

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

	if in.GetTitle() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	// TODO: add your logic code & delete this line.
	data := models.MallNotice{
		Title:      in.Title,
		Author:     in.Author,
		Content:    in.Content,
		Std_IICUDS: types.Std_IICUDS{Status: int8(in.GetStatus())},
	}
	err = impl.DBService.Model(&models.MallNotice{}).Select("title", "author", "content").Where("identity = ?", in.Identity).Updates(&data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

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

}
