package logic

import (
	"context"
	"encoding/json"
	"fmt"
	. "jyBXSubscribe/entity"
	IC "jyBXSubscribe/rpc/init"
	"jyBXSubscribe/rpc/internal/svc"
	"jyBXSubscribe/rpc/model/service"
	"jyBXSubscribe/rpc/type/bxsubscribe"

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

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

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

// 修改订阅信息接口
func (l *UpdateSubScribeInfoLogic) UpdateSubScribeInfo(inc *bxsubscribe.UpdateSubScribeInfoReq) (*bxsubscribe.StatusResp, error) {
	// todo: add your logic here and delete this line
	resp := &bxsubscribe.StatusResp{}
	in := &SubscribeUpdate{}
	err := json.Unmarshal(inc.SubSet, &in)
	if err != nil {
		return resp, nil
	}
	subService := &service.SubseribeService{
		UserId:          inc.UserId,
		Mgo:             IC.Mgo,
		SubscribeUpdate: in,
	}
	status, err := subService.Update()
	if err != nil || status == 0 {
		l.Error(fmt.Sprintf("%+v", in), err.Error())
		resp.ErrorMsg = err.Error()
		resp.ErrorCode = -1
	} else {
		resp.Status = status
	}

	return resp, nil
}
