package logic

import (
	"context"
	"fmt"

	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
	. "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 CheckPowerLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

// 检查用户权益
func (l *CheckPowerLogic) CheckPower(in *pb.CheckPowerReq) (*pb.Resp, error) {
	resp := &pb.Resp{}
	if in.Appid == "" {
		l.Error(fmt.Sprintf("%+v", in), "无效的参数appid")
		return resp, nil
	} else if in.FunctionCode == "" {
		l.Error(fmt.Sprintf("%+v", in), "无效的参数function_code")
		return resp, nil
	} else if in.AccountId == 0 && in.EntId == 0 && in.EntUserId == 0 {
		l.Error(fmt.Sprintf("%+v", in), "无效的参数account_id、ent_id、ent_user_id")
		return resp, nil
	}
	function := Base_function.FindByCode(in.Appid, in.FunctionCode)
	if function == nil {
		l.Error(fmt.Sprintf("%+v", in), "功能原始表中没有找到该功能")
		return resp, nil
	}
	status, use_count, surplus_count, start_time, end_time, err := Surplus(in.Appid, in.FunctionCode, in.AccountId, in.EntAccountId, in.EntId, in.EntUserId)
	resp.Status = status
	resp.UseCount = use_count
	resp.SurplusCount = surplus_count
	resp.StartTime = start_time
	resp.EndTime = end_time
	if err != nil {
		l.Error(fmt.Sprintf("%+v", in), err.Error())
	}
	return resp, nil
}
