wangchuanjin 3 éve
szülő
commit
69e9cdd52a

+ 2 - 2
api/etc/resource.yaml

@@ -8,9 +8,9 @@ Etcd:
 Mysql:
   BaseService:
     DBName: base_service
-    Address: 192.168.3.11:3366
+    Address: 192.168.3.217:4000
     UserName: root
-    PassWord: Topnet123
+    PassWord: "=PDT49#80Z!RVv52_z"
     MaxOpenConns: 5
     MaxIdleConns: 5
 Logx:

+ 28 - 0
api/internal/handler/haspowershandler.go

@@ -0,0 +1,28 @@
+package handler
+
+import (
+	"net/http"
+
+	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/logic"
+	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/svc"
+	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/types"
+	"github.com/zeromicro/go-zero/rest/httpx"
+)
+
+func haspowersHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.Req
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewHaspowersLogic(r.Context(), svcCtx)
+		resp, err := l.Haspowers(&req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 5 - 0
api/internal/handler/routes.go

@@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				Path:    "/resourceCenter/surplus",
 				Handler: surplusDetailHandler(serverCtx),
 			},
+			{
+				Method:  http.MethodPost,
+				Path:    "/resourceCenter/haspowers",
+				Handler: haspowersHandler(serverCtx),
+			},
 		},
 	)
 }

+ 1 - 1
api/internal/handler/surplusdetailhandler.go

@@ -11,7 +11,7 @@ import (
 
 func surplusDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
-		var req types.SurplusReq
+		var req types.Req
 		if err := httpx.Parse(r, &req); err != nil {
 			httpx.Error(w, err)
 			return

+ 41 - 0
api/internal/logic/haspowerslogic.go

@@ -0,0 +1,41 @@
+package logic
+
+import (
+	"context"
+	"fmt"
+
+	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/svc"
+	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/api/internal/types"
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/service"
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type HaspowersLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewHaspowersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HaspowersLogic {
+	return &HaspowersLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *HaspowersLogic) Haspowers(req *types.Req) (resp *types.Reply, err error) {
+	resp = &types.Reply{}
+	if req.AppId == "" {
+		resp.Error_msg = "无效的参数appid"
+		l.Error(fmt.Sprintf("%+v", req), resp.Error_msg)
+		return resp, nil
+	} else if req.User_id == 0 && req.Ent_id == 0 && req.Ent_user_id == 0 {
+		resp.Error_msg = "无效的参数user_id、ent_id、ent_user_id"
+		l.Error(fmt.Sprintf("%+v", req), resp.Error_msg)
+		return resp, nil
+	}
+	resp.Error_code = 0
+	resp.Data = HasPowers(req.AppId, req.User_id, req.Ent_id, req.Ent_user_id)
+	return
+}

+ 1 - 1
api/internal/logic/surplusdetaillogic.go

@@ -25,7 +25,7 @@ func NewSurplusDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sur
 }
 
 //资源剩余详情
-func (l *SurplusDetailLogic) SurplusDetail(req *types.SurplusReq) (resp *types.Reply, err error) {
+func (l *SurplusDetailLogic) SurplusDetail(req *types.Req) (resp *types.Reply, err error) {
 	resp = &types.Reply{}
 	if req.AppId == "" {
 		resp.Error_msg = "无效的参数appid"

+ 8 - 9
api/internal/types/types.go

@@ -1,22 +1,21 @@
 // Code generated by goctl. DO NOT EDIT.
 package types
 
-type SurplusReq struct {
+type Req struct {
 	AppId         string `header:"appId,default=10000"`
-	Function_code string `header:"functionCode,default=xxfb_gyxx_add"`
-	Muser_id      string `header:"userId,optional"`
+	Function_code string `header:"functionCode,optional"`
 	User_id       int64  `header:"newUserId,optional"`
-	Ent_id        int64  `header:"entId,optional,default=55"`
+	Ent_id        int64  `header:"entId,optional"`
 	Ent_user_id   int64  `header:"entUserId,optional"`
 }
 
+type SurplusData struct {
+	Surplus int64 `json:"surplus"`
+	Total   int64 `json:"total"`
+}
+
 type Reply struct {
 	Error_code int64       `json:"error_code"`
 	Error_msg  string      `json:"error_msg"`
 	Data       interface{} `json:"data"`
 }
-
-type SurplusData struct {
-	Surplus int64 `json:"surplus"`
-	Total   int64 `json:"total"`
-}

+ 12 - 10
api/resource.api

@@ -7,29 +7,31 @@ info(
 )
 
 type (
-	surplusReq {
+	Req {
 		AppId         string `header:"appId,default=10000"`
-		Function_code string `header:"functionCode,default=xxfb_gyxx_add"`
-		Muser_id      string `header:"userId,optional"`
+		Function_code string `header:"functionCode,optional"`
 		User_id       int64  `header:"newUserId,optional"`
-		Ent_id        int64  `header:"entId,optional,default=55"`
+		Ent_id        int64  `header:"entId,optional"`
 		Ent_user_id   int64  `header:"entUserId,optional"`
 	}
 
+	surplusData {
+		Surplus int64 `json:"surplus"`
+		Total   int64 `json:"total"`
+	}
+
 	Reply {
 		Error_code int64       `json:"error_code"`
 		Error_msg  string      `json:"error_msg"`
 		Data       interface{} `json:"data"`
 	}
-
-	surplusData {
-		Surplus int64 `json:"surplus"`
-		Total   int64 `json:"total"`
-	}
 )
 
 service resource {
 	@doc "获取资源剩余详情"
 	@handler surplusDetail
-	post /resourceCenter/surplus (surplusReq) returns (Reply)
+	post /resourceCenter/surplus (Req) returns (Reply)
+	@doc "获取所有的权益"
+	@handler haspowers
+	post /resourceCenter/haspowers (Req) returns (Reply)
 }

+ 2 - 2
go.mod

@@ -3,8 +3,8 @@ module bp.jydev.jianyu360.cn/BaseService/resourceCenter
 go 1.18
 
 require (
-	app.yhyue.com/moapp/jybase v0.0.0-20220420032112-668025915ee4
-	bp.jydev.jianyu360.cn/BaseService/gateway v0.0.0-20220419090715-88ddb32961be
+	app.yhyue.com/moapp/jybase v0.0.0-20220421060131-a1001013ba46
+	bp.jydev.jianyu360.cn/BaseService/gateway v1.3.0
 	github.com/zeromicro/go-zero v1.3.2
 	google.golang.org/grpc v1.45.0
 	google.golang.org/protobuf v1.28.0

+ 8 - 2
go.sum

@@ -1,12 +1,18 @@
 app.yhyue.com/moapp/esv1 v0.0.0-20220414031211-3da4123e648d/go.mod h1:91/lSD/hS+ckMVP3WdidRzDhC60lLMdyce9QHy0cSMA=
 app.yhyue.com/moapp/jybase v0.0.0-20220415064050-37ce64b3e2d4/go.mod h1:qNRA0sHuYqcLoYoP8irpaWnW9YsXixe6obBIkwaXpD0=
-app.yhyue.com/moapp/jybase v0.0.0-20220420032112-668025915ee4 h1:GD3/xYydpFEHe3qQ1pXTTucuhxDP8eEdZIrbu2Iz10I=
+app.yhyue.com/moapp/jybase v0.0.0-20220418104200-46c3fff161c7/go.mod h1:qNRA0sHuYqcLoYoP8irpaWnW9YsXixe6obBIkwaXpD0=
 app.yhyue.com/moapp/jybase v0.0.0-20220420032112-668025915ee4/go.mod h1:qNRA0sHuYqcLoYoP8irpaWnW9YsXixe6obBIkwaXpD0=
+app.yhyue.com/moapp/jybase v0.0.0-20220421060131-a1001013ba46 h1:ME9XyG40U8JQh70lmQnzACCPNaDyBQ5BaCVTIxPu6rk=
+app.yhyue.com/moapp/jybase v0.0.0-20220421060131-a1001013ba46/go.mod h1:qNRA0sHuYqcLoYoP8irpaWnW9YsXixe6obBIkwaXpD0=
 bp.jydev.jianyu360.cn/BP/jynsq v0.0.0-20220222052708-ebc43af90698/go.mod h1:ojo/AUH9Yr1wzarEjOaNMkj1Cet/9r8IgLyba64Z52E=
-bp.jydev.jianyu360.cn/BaseService/gateway v0.0.0-20220419090715-88ddb32961be h1:YCPWJEM8JHtV5ush0OEB85mKKsn2hpzKNmFhnbXyMRY=
 bp.jydev.jianyu360.cn/BaseService/gateway v0.0.0-20220419090715-88ddb32961be/go.mod h1:Yj4oabIGItuMoF0BXYLz2XAnF581kxgXBrvlUtIJrkI=
+bp.jydev.jianyu360.cn/BaseService/gateway v1.3.0 h1:N7oYOQkv/7V6agwu8/M3UhARnV7ztBaaS8sTjfcjDyA=
+bp.jydev.jianyu360.cn/BaseService/gateway v1.3.0/go.mod h1:BMLd/5wb3BIEGhnEgF9y1sJN9P5/Dw9kYsoiE9V8I9g=
 bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220418005748-8ba5d936dd53/go.mod h1:E5lcDI3k4FESLxiAetCfWQTq8qfpy9cv0yN1oKoEO34=
+bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220419023723-0b32d4a41751/go.mod h1:6KL5LMEku83uRbre0W/bj5kXG2I6pJGBFtktmtp51yM=
+bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.0.0-20220420075831-0b59892e9982/go.mod h1:wsHNO91h37H+xE4ZNny0yd7mtpODeDJxbVYhIRMR+qw=
 bp.jydev.jianyu360.cn/BaseService/userCenter v0.0.0-20220418072311-2062bed1e700/go.mod h1:KjcrxTzM96tBc6G4B8tlLBn1lrVy5UJYF8+eTdP4xAE=
+bp.jydev.jianyu360.cn/BaseService/userCenter v0.0.0-20220421015128-4a36f3eac5c5/go.mod h1:GT0QC4aaKDuXxAvaU4G02XjCc31TU1ctqBGqxQYOfC4=
 cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
 cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
 cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=

+ 17 - 0
public/entity/base_ent_empower.go

@@ -26,3 +26,20 @@ func (b *base_ent_empower) FindMyEntId(appid, function_code string, ent_id int64
 func (b *base_ent_empower) Count(appid, function_code string, ent_id int64) int64 {
 	return Mysql_BaseService.CountBySql(`select count(1) as count from base_ent_empower where appid=? and function_code=? and ent_id=?`, appid, function_code, ent_id)
 }
+
+//是否授权
+func (b *base_ent_empower) HasEmpower(appid, function_code string, ent_id, ent_user_id int64) bool {
+	empower := b.FindMyEntId(appid, function_code, ent_id)
+	if empower != nil {
+		if len(*empower) == 1 && (*empower)[0].Ent_user_id == 0 {
+			return true
+		} else {
+			for _, vv := range *empower {
+				if vv.Ent_id == ent_id && vv.Ent_user_id == ent_user_id {
+					return true
+				}
+			}
+		}
+	}
+	return false
+}

+ 9 - 2
public/entity/base_power.go

@@ -28,6 +28,13 @@ type base_power struct {
 	Strategy_count int64  //频率数量
 }
 
+//获取所有权益
+func (b *base_power) FindMyPowers(appid string, user_id, ent_id int64) *[]*base_power {
+	now := NowFormat(Date_Full_Layout)
+	list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and ((power_type=1 and power_ofid=?) or (power_type=2 and power_ofid=?)) and end_time>? and status=1 order by power_type`, appid, user_id, ent_id, now)
+	return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
+}
+
 //查找我的权益
 func (b *base_power) FindMyPower(appid, function_code string, user_id, ent_id int64) *[]*base_power {
 	now := NowFormat(Date_Full_Layout)
@@ -88,7 +95,7 @@ func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, us
 					logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的cycle", v.Cycle)
 					continue
 				}
-				starat_time, end_time := NowFormat(Date_Full_Layout), FormatDate(&end, Date_Full_Layout)
+				start_time, end_time := NowFormat(Date_Full_Layout), FormatDate(&end, Date_Full_Layout)
 				power_values = append(power_values, start_time, end_time)
 				use_count := buy_num
 				if bgs.Calculation_type == 2 {
@@ -99,7 +106,7 @@ func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, us
 				if v.Power_type == 2 && v.Power_count == -1 && Base_ent_empower.Count(appid, v.Function_code, ent_id) == 0 {
 					empower_values = append(empower_values, appid, ent_id, 0, v.Function_code, NowFormat(Date_Full_Layout))
 				} else if v.Power_type == 2 && v.Power_count > 0 {
-					wait_empower_values = append(wait_empower_values, appid, v.Id, ent_id, starat_time, end_time, use_count, v.Power_count, v.Limit_strategy, NowFormat(Date_Full_Layout))
+					wait_empower_values = append(wait_empower_values, appid, v.Id, ent_id, start_time, end_time, use_count, v.Power_count, v.Limit_strategy, NowFormat(Date_Full_Layout))
 				}
 			}
 		}

+ 57 - 0
public/service/power.go

@@ -0,0 +1,57 @@
+package service
+
+import (
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
+)
+
+/*
+ * 获取已有的权益
+ * @param appid
+ * @param function_code 功能代码
+ * @param user_id 用户id
+ * @param ent_id 企业id
+ * @return 所有已开通的权益
+ */
+func HasPowers(appid string, user_id, ent_id, ent_user_id int64) []string {
+	result := []string{}
+	m := map[string]bool{}
+	bps := Base_power.FindMyPowers(appid, user_id, ent_id)
+	if bps != nil {
+		for _, v := range *bps {
+			if v.Power_type == 1 || (v.Power_type == 2 && Base_ent_empower.HasEmpower(appid, v.Function_code, ent_id, ent_user_id)) {
+				if m[v.Function_code] {
+					continue
+				}
+				m[v.Function_code] = true
+				result = append(result, v.Function_code)
+			}
+		}
+	}
+	return result
+}
+
+/*
+ * 获取已有的权益
+ * @param appid
+ * @param function_code 功能代码
+ * @param user_id 用户id
+ * @param ent_id 企业id
+ * @return 所有已开通的权益
+ */
+func ReDistribution(appid string, ent_id int64, ent_user_id []int) []string {
+	result := []string{}
+	m := map[string]bool{}
+	bps := Base_power.FindMyPowers(appid, user_id, ent_id)
+	if bps != nil {
+		for _, v := range *bps {
+			if v.Power_type == 1 || (v.Power_type == 2 && Base_ent_empower.HasEmpower(appid, v.Function_code, ent_id, ent_user_id)) {
+				if m[v.Function_code] {
+					continue
+				}
+				m[v.Function_code] = true
+				result = append(result, v.Function_code)
+			}
+		}
+	}
+	return result
+}

+ 23 - 0
public/service/power_test.go

@@ -0,0 +1,23 @@
+package service
+
+import (
+	"testing"
+
+	. "app.yhyue.com/moapp/jybase/mysql"
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
+)
+
+func init() {
+	InitMysql(&Mysql{
+		Address:      "192.168.3.217:4000", //数据库地址:端口
+		UserName:     "root",               //用户名
+		PassWord:     "=PDT49#80Z!RVv52_z", //密码
+		DBName:       "base_service",       //数据库名
+		MaxOpenConns: 5,
+		MaxIdleConns: 5,
+	})
+}
+
+func TestHasPowers(t *testing.T) {
+	t.Log("我的权益", HasPowers("10000", 0, 14184, 4271))
+}

+ 2 - 19
public/service/surplus.go

@@ -31,27 +31,10 @@ func Surplus(appid, function_code string, user_id, ent_id, ent_user_id int64) (i
 	if function.Power_rule == 1 { //只对周期进行校验
 		return 1, 0, 0, nil
 	}
-	hasEmpower := func() bool {
-		empower := Base_ent_empower.FindMyEntId(appid, function_code, ent_id)
-		if empower != nil {
-			if len(*empower) == 1 {
-				if (*empower)[0].Ent_user_id == 0 {
-					return true
-				}
-			} else {
-				for _, vv := range *empower {
-					if vv.Ent_id == ent_id && vv.Ent_user_id == ent_user_id {
-						return true
-					}
-				}
-			}
-		}
-		return false
-	}
 	if function.Power_rule == 2 { //周期+数量校验
 		use_count, surplus_count := int64(0), int64(0)
 		for _, v := range *myPowers {
-			if v.Power_type == 2 && !hasEmpower() {
+			if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
 				return -3, 0, 0, errors.New("没有对该用户进行授权")
 			}
 			use_count += v.Use_count
@@ -64,7 +47,7 @@ func Surplus(appid, function_code string, user_id, ent_id, ent_user_id int64) (i
 		}
 	} else if function.Power_rule == 3 { //周期+频率+数量校验
 		for _, v := range *myPowers {
-			if v.Power_type == 2 && !hasEmpower() {
+			if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
 				return -3, 0, 0, errors.New("没有对该用户进行授权")
 			}
 			use, err := Base_resource_use.FindLastOne(appid, function_code, user_id, ent_id, v.Power_type)

+ 2 - 2
rpc/etc/resource.yaml

@@ -7,9 +7,9 @@ Etcd:
 Mysql:
   BaseService:
     DBName: base_service
-    Address: 192.168.3.11:3366
+    Address: 192.168.3.217:4000
     UserName: root
-    PassWord: Topnet123
+    PassWord: "=PDT49#80Z!RVv52_z"
     MaxOpenConns: 5
     MaxIdleConns: 5
 Logx:

+ 9 - 0
rpc/resource.proto

@@ -29,6 +29,13 @@ message DeductionReq {
   repeated string ids = 6;  //id列表
 }
 
+message ReDisReq {
+  string appid = 1;
+  string function_code = 2; //功能代码
+  int64 ent_id = 3;         //企业id
+  repeated ent_user_id = 4; //企业用户id
+}
+
 message Resp {
   int64 status = 1;        //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
 }
@@ -40,4 +47,6 @@ service Resource {
   rpc CheckPower(CheckPowerReq) returns(Resp);
   //资源扣减
   rpc Deduction(DeductionReq) returns(Resp);
+  //重新分配
+  rpc ReDistribution() returns(Resp);
 }