package ads

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.AdsItem) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	// valid must params.
	if in.GetId() == 0 {
		return nil, errcode.ErrInvalidArgument
	}

	data := models.MallAds{
		Title:      in.Title,
		PosKey:     in.PosKey,
		Content:    in.Content,
		Type:       models.ContentType(in.Type),
		ToUrl:      in.ToUrl,
		Std_IICUDS: types.Std_IICUDS{Status: int8(in.GetStatus())},
	}

	err = impl.DBService.Select("title", "pos_key", "content", "type", "to_url").Where("id=?", in.GetId()).Updates(&data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

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

}
