package logic import ( "context" "fmt" "time" . "app.yhyue.com/moapp/jybase/date" . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/entity" "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.UserId == 0 && in.EntId == 0 && in.EntUserId == 0 { l.Error(fmt.Sprintf("%+v", in), "无效的参数user_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 } myPowers := Base_power.FindMyPower(in.Appid, in.FunctionCode, in.UserId, in.EntId) if myPowers == nil || len(*myPowers) == 0 { resp.Status = -1 l.Info(fmt.Sprintf("%+v", in), "不在有效期内") return resp, nil } if function.Power_rule == 1 { //只对周期进行校验 resp.Status = 1 return resp, nil } else { for _, v := range *myPowers { if v.Power_type == 2 { hasEmpower := false empower := Base_ent_empower.FindMyEntId(in.Appid, in.FunctionCode, in.EntId) if empower != nil { if len(*empower) == 1 { if (*empower)[0].Ent_user_id == 0 { hasEmpower = true } } else { for _, vv := range *empower { if vv.Ent_id == in.EntId && vv.Ent_user_id == in.EntUserId { hasEmpower = true break } } } } if !hasEmpower { resp.Status = -3 l.Info(fmt.Sprintf("%+v", in), "没有对该用户进行授权") return resp, nil } } if function.Power_rule == 2 { //周期+数量校验 if v.Surplus_count > 0 { resp.Status = 1 return resp, nil } } else if function.Power_rule == 3 { //周期+频率+数量校验 use, err := Base_resource_use.FindLastOne(in.Appid, in.FunctionCode, in.UserId, in.EntId, v.Power_type) if err != nil { l.Error(fmt.Sprintf("%+v", in), "查询资源使用记录失败", err) return resp, nil } else if use == nil { resp.Status = 1 return resp, nil } create_time, err := time.ParseInLocation(Date_Full_Layout, use.Create_time, time.Local) if err != nil { l.Error(fmt.Sprintf("%+v", in), "资源充值/消耗表创建时间错误", create_time, err) return resp, nil } now := time.Now() if (*myPowers)[0].Limit_strategy == "1d" { if create_time.Year() == now.Year() && create_time.Month() == now.Month() && create_time.Day() == now.Day() { if use.Surplus_count > 0 { resp.Status = 1 return resp, nil } } else { resp.Status = 1 return resp, nil } } else if (*myPowers)[0].Limit_strategy == "1m" { if create_time.Year() == now.Year() && create_time.Month() == now.Month() { if use.Surplus_count > 0 { resp.Status = 1 return resp, nil } } else { resp.Status = 1 return resp, nil } } else { l.Error(fmt.Sprintf("%+v", in), "limit_strategy格式错误", (*myPowers)[0].Limit_strategy) return resp, nil } } } resp.Status = -2 return resp, nil } return resp, nil }