package logic

import (
	"context"
	"errors"
	"fmt"

	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/service"
	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/svc"
	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

// 开通或者取消权益
func (l *PowerHandleLogic) PowerHandle(in *pb.PowerReq) (*pb.Resp, error) {
	resp := &pb.Resp{}
	var err error
	if in.Type == 1 { //开通
		if err = OpenPower(in.Appid, in.GoodsCode, in.GoodsSpecId, in.AccountId, in.EntAccountId, in.EntId, in.BuyNum, in.StartTime, in.EndTime); err == nil {
			resp = &pb.Resp{Status: 1}
		}
	} else if in.Type == -1 { //取消
		var ok bool
		ok, err = CancelPower(in.Appid, in.GoodsCode, in.GoodsSpecId, in.AccountId, in.EntAccountId, in.EntId)
		if ok {
			resp = &pb.Resp{Status: 1}
		}
	} else {
		err = errors.New("无效的参数type")
	}
	if err != nil {
		l.Error(fmt.Sprintf("%+v", in), err)
	}
	return resp, nil
}
