浏览代码

feat:权益查询

wangchuanjin 3 年之前
父节点
当前提交
8ba5d936dd

+ 1 - 1
rpc/etc/resource.yaml

@@ -2,7 +2,7 @@ Name: resource.rpc
 ListenOn: 0.0.0.0:1005
 Etcd:
   Hosts:
-  - 127.0.0.1:2379
+  - 192.168.3.240:2379
   Key: resource.rpc
 Mysql:
   BaseService:

+ 23 - 0
rpc/internal/entity/base_ent_empower.go

@@ -0,0 +1,23 @@
+package entity
+
+import (
+	. "app.yhyue.com/moapp/jybase/common"
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/db"
+)
+
+var Base_ent_empower = base_ent_empower{}
+
+//企业授权表
+type base_ent_empower struct {
+	Id            int
+	Appid         string
+	Ent_id        int64  //企业id
+	Ent_user_id   int64  //企业员工id
+	Function_code string //功能代码
+}
+
+//授权记录
+func (b *base_ent_empower) FindMyEntId(appid, function_code string, ent_id int64) *[]*base_ent_empower {
+	list := Mysql_BaseService.SelectBySql(`select * from base_ent_empower where appid=? and function_code=? and ent_id=?`, appid, function_code, ent_id)
+	return JsonUnmarshal(list, &[]base_ent_empower{}).(*[]*base_ent_empower)
+}

+ 28 - 0
rpc/internal/entity/base_function.go

@@ -0,0 +1,28 @@
+package entity
+
+import (
+	. "app.yhyue.com/moapp/jybase/common"
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/db"
+)
+
+var Base_function = base_function{}
+
+//功能原始表
+type base_function struct {
+	Id         int
+	Appid      string
+	Name       string //功能名称
+	Code       string //功能代码
+	Status     int    //0:不可用 1:可用
+	Version    string //版本号
+	Power_rule int    //校验权限规则;1:仅周期 2:周期+数量 3:周期+频率+数量
+}
+
+//根据代码查找功能详情
+func (b *base_function) FindByCode(appid, code string) *base_function {
+	list := Mysql_BaseService.SelectBySql(`select * from base_function where appid=? and code=? and status=1 limit 1`, appid, code)
+	if list != nil && len(*list) == 1 {
+		return JsonUnmarshal((*list)[0], &base_function{}).(*base_function)
+	}
+	return nil
+}

+ 11 - 0
rpc/internal/entity/base_goods_function.go

@@ -1,5 +1,10 @@
 package entity
 
+import (
+	. "app.yhyue.com/moapp/jybase/common"
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/db"
+)
+
 var Base_goods_function = base_goods_function{}
 
 //商品功能表
@@ -12,3 +17,9 @@ type base_goods_function struct {
 	Strategy_count string //频率数量
 	Power_type     string //权益所属类型;1:个人 2:企业
 }
+
+//根据商品代码查找商品的功能明细
+func (b *base_goods_function) FindByGoodsCode(appid, goods_code string) *[]*base_goods_function {
+	list := Mysql_BaseService.SelectBySql(`select * from base_goods_function where appid=? and goods_code=?`, appid, goods_code)
+	return JsonUnmarshal(list, &[]base_goods_function{}).(*[]*base_goods_function)
+}

+ 5 - 4
rpc/internal/entity/base_power.go

@@ -12,7 +12,7 @@ type base_power struct {
 	Id             int
 	Appid          string
 	Function_code  string //功能代码
-	Power_type     string //权益所属类型;1:个人 2:企业
+	Power_type     int    //权益所属类型;1:个人 2:企业
 	Power_ofid     int    //权益所属id;个人id/企业id/部门id/员工id
 	Start_time     string //开始时间
 	End_time       string //结束时间
@@ -21,7 +21,8 @@ type base_power struct {
 	Strategy_count int    //频率数量
 }
 
-func (b *base_power) FindMyPower(appid, function_code string, power_ofid int) []base_power {
-	list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and power_ofid=? and status=1`, appid, function_code, power_ofid)
-	return JsonUnmarshal(list, []base_power{}).([]base_power)
+//查找我的权益
+func (b *base_power) FindMyPower(appid, function_code string, ent_id, user_id int64) *[]*base_power {
+	list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and (power_ofid=? or power_ofid=?) and status=1`, appid, function_code, ent_id, user_id)
+	return JsonUnmarshal(list, &[]base_power{}).(*[]*base_power)
 }

+ 14 - 4
rpc/internal/entity/base_resource_use.go

@@ -1,6 +1,8 @@
 package entity
 
 import (
+	"errors"
+
 	. "app.yhyue.com/moapp/jybase/common"
 	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/db"
 )
@@ -11,14 +13,22 @@ var Base_resource_use = base_resource_use{}
 type base_resource_use struct {
 	Id              int
 	Appid           string
-	Use_id          int    //使用人id
+	Ent_id          int64  //企业id
+	User_id         int64  //用户id
 	Function_code   string //功能代码
 	Add_count       int    //新增数量
 	Surplus_count   int    //剩余数量
 	Deduction_count int    //扣除数量
 }
 
-func (b *base_resource_use) FindLastOne(appid, function_code string, use_id int) []base_power {
-	list := Mysql_BaseService.SelectBySql(`select * from base_resource_use where appid=? and function_code=? and use_id=? and status=1 limit 1`, appid, function_code, use_id)
-	return JsonUnmarshal(list, []base_power{}).([]base_power)
+//查找最新一条的权益使用记录
+func (b *base_resource_use) FindLastOneByEntId(appid, function_code string, ent_id int64) (*base_resource_use, error) {
+	list := Mysql_BaseService.SelectBySql(`select * from base_resource_use where appid=? and function_code=? and ent_id=? limit 1`, appid, function_code, ent_id)
+	if list == nil {
+		return nil, errors.New("find error")
+	}
+	if list != nil && len(*list) == 1 {
+		return JsonUnmarshal((*list)[0], &base_resource_use{}).(*base_resource_use), nil
+	}
+	return nil, nil
 }

+ 81 - 2
rpc/internal/logic/checkpowerlogic.go

@@ -2,7 +2,10 @@ package logic
 
 import (
 	"context"
+	"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"
 
@@ -24,6 +27,82 @@ func NewCheckPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckP
 }
 
 func (l *CheckPowerLogic) CheckPower(in *pb.CheckPowerReq) (*pb.Resp, error) {
-
-	return &pb.Resp{}, nil
+	resp := &pb.Resp{}
+	function := Base_function.FindByCode(in.Appid, in.FunctionCode)
+	if function == nil {
+		l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "功能原始表中没有找到该功能")
+		return resp, nil
+	}
+	myPowers := Base_power.FindMyPower(in.Appid, in.FunctionCode, in.EntId, in.UserId)
+	if myPowers == nil || len(*myPowers) == 0 {
+		l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "功能权益表中没有找到权益记录")
+		return resp, nil
+	}
+	isPass := false
+	for _, v := range *myPowers {
+		start, err := time.ParseInLocation(Date_Full_Layout, v.Start_time, time.Local)
+		if err != nil {
+			l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "开始时间转化错误", err)
+			return resp, nil
+		}
+		end, err := time.ParseInLocation(Date_Full_Layout, v.End_time, time.Local)
+		if err != nil {
+			l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "结束时间转化错误", err)
+			return resp, nil
+		}
+		if time.Now().After(start) && time.Now().Before(end) {
+			isPass = true
+		}
+	}
+	if !isPass {
+		resp.Status = -1
+		l.Info(in.Appid, in.FunctionCode, in.EntId, in.UserId, "不在有效期内")
+		return resp, nil
+	}
+	if function.Power_rule == 1 {
+		resp.Status = 1
+		return resp, nil
+	} else {
+		if function.Power_rule == 3 {
+			hasEmpower := false
+			if (*myPowers)[0].Power_type == 2 {
+				empower := Base_ent_empower.FindMyEntId(in.Appid, in.FunctionCode, in.EntId)
+				if empower == nil || len(*empower) == 0 {
+					resp.Status = -3
+					l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "没有授权记录")
+					return resp, nil
+				}
+				if len(*empower) == 1 {
+					if (*empower)[0].Ent_user_id == 0 {
+						hasEmpower = true
+					}
+				} else {
+					for _, v := range *empower {
+						if v.Ent_user_id == in.UserId {
+							hasEmpower = true
+							break
+						}
+					}
+				}
+			} else {
+				hasEmpower = true
+			}
+			if !hasEmpower {
+				resp.Status = -3
+				l.Info(in.Appid, in.FunctionCode, in.EntId, in.UserId, "没有对该用户进行授权")
+				return resp, nil
+			}
+			use, err := Base_resource_use.FindLastOneByEntId(in.Appid, in.FunctionCode, in.EntId)
+			if err != nil {
+				l.Error(in.Appid, in.FunctionCode, in.EntId, in.UserId, "查询资源使用记录失败")
+				return resp, nil
+			} else if use == nil {
+				resp.Status = 1
+				return resp, nil
+			}
+			//limit_strategy := (*myPowers)[0].Limit_strategy
+		}
+		return resp, nil
+	}
+	return resp, nil
 }

+ 4 - 4
rpc/pb/resource.pb.go

@@ -262,7 +262,7 @@ type Resp struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Status string `protobuf:"bytes,1,opt,name=status,proto3" json:"status,omitempty"` //0:失败 1:成功 -1:不在有效期内 -2:数量不足
+	Status int64 `protobuf:"varint,1,opt,name=status,proto3" json:"status,omitempty"` //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
 }
 
 func (x *Resp) Reset() {
@@ -297,11 +297,11 @@ func (*Resp) Descriptor() ([]byte, []int) {
 	return file_resource_proto_rawDescGZIP(), []int{3}
 }
 
-func (x *Resp) GetStatus() string {
+func (x *Resp) GetStatus() int64 {
 	if x != nil {
 		return x.Status
 	}
-	return ""
+	return 0
 }
 
 var File_resource_proto protoreflect.FileDescriptor
@@ -335,7 +335,7 @@ var file_resource_proto_rawDesc = []byte{
 	0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x63,
 	0x6f, 0x75, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x69, 0x64, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28,
 	0x09, 0x52, 0x03, 0x69, 0x64, 0x73, 0x22, 0x1e, 0x0a, 0x04, 0x52, 0x65, 0x73, 0x70, 0x12, 0x16,
-	0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06,
+	0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06,
 	0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x32, 0x73, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72,
 	0x63, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x50, 0x6f, 0x77, 0x65, 0x72, 0x48, 0x61, 0x6e, 0x64, 0x6c,
 	0x65, 0x12, 0x09, 0x2e, 0x50, 0x6f, 0x77, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x05, 0x2e, 0x52,

+ 0 - 1
rpc/resource.go

@@ -9,7 +9,6 @@ import (
 
 	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/config"
 	_ "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/db"
-	_ "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/entity"
 	"github.com/zeromicro/go-zero/core/service"
 	"github.com/zeromicro/go-zero/zrpc"
 	"google.golang.org/grpc"

+ 1 - 1
rpc/resource.proto

@@ -27,7 +27,7 @@ message DeductionReq {
 }
 
 message Resp {
-  string status = 1;        //0:失败 1:成功 -1:不在有效期内 -2:数量不足
+  int64 status = 1;        //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
 }
 
 service Resource {

+ 1 - 1
rpc/resource/resource.go

@@ -6,7 +6,7 @@ package resource
 import (
 	"context"
 
-	"bp.jydev.jianyu360.cn/resourceCenter/rpc/pb"
+	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
 
 	"github.com/zeromicro/go-zero/zrpc"
 	"google.golang.org/grpc"