فهرست منبع

feat:授权数量控制

wangchuanjin 1 سال پیش
والد
کامیت
84d67bd4d1
3فایلهای تغییر یافته به همراه49 افزوده شده و 23 حذف شده
  1. 30 0
      public/entity/base_ent_empower.go
  2. 9 18
      public/entity/base_ent_wait_empower.go
  3. 10 5
      public/service/power.go

+ 30 - 0
public/entity/base_ent_empower.go

@@ -2,6 +2,8 @@ package entity
 
 import (
 	"database/sql"
+	"errors"
+	"fmt"
 	"strings"
 
 	. "app.yhyue.com/moapp/jybase/common"
@@ -131,3 +133,31 @@ func (b *base_ent_empower) CancelAllEmpower(tx *sql.Tx, appid string, function_m
 	inner join base_function b on (a.appid=? and a.name in (`+strings.Join(wh1, ",")+`) and a.id=b.pid and b.appid=?)
 	inner join base_ent_empower c on (b.code=c.function_code and c.appid=? and c.ent_id=? and c.ent_user_id in (`+strings.Join(wh2, ",")+`))`, args...) >= 0
 }
+
+//校验功能代码
+func (b *base_ent_empower) CheckEmpowerCount(tx *sql.Tx, appid string, function_code []string, ent_id int64) (int64, error) {
+	m := map[string]int64{}
+	for _, v := range *Base_ent_wait_empower.AllWaitEmpowers(tx, appid, ent_id) {
+		if v.Empower_count < 0 {
+			m[v.Function_code] = v.Empower_count
+		} else {
+			m[v.Function_code] = m[v.Function_code] + v.Empower_count
+		}
+	}
+	c := map[string]int64{}
+	list := Mysql_BaseService.SelectBySqlByTx(tx, `SELECT function_code,SUM(1) AS SUM FROM base_ent_empower WHERE appid=? and ent_id=? GROUP BY function_code`, appid, ent_id)
+	if list != nil {
+		return -3, errors.New(fmt.Sprintf("企业%d查询已授权数量失败", ent_id))
+	}
+	for _, v := range *list {
+		c[ObjToString(v["function_code"])] = Int64All(v["sum"])
+	}
+	for _, v := range function_code {
+		if count, ok := m[v]; !ok {
+			return -1, errors.New(fmt.Sprintf("企业%d没有该待授权%s功能代码", ent_id, v))
+		} else if count >= 0 && c[v] > count {
+			return -2, errors.New(fmt.Sprintf("企业%d功能代码%s可授权数量不足", ent_id, v))
+		}
+	}
+	return 1, nil
+}

+ 9 - 18
public/entity/base_ent_wait_empower.go

@@ -1,6 +1,7 @@
 package entity
 
 import (
+	"database/sql"
 	"errors"
 	"fmt"
 
@@ -39,30 +40,20 @@ func (b *base_ent_wait_empower) WaitEmpowers(appid, function_code string, ent_id
 }
 
 //所有待授权记录
-func (b *base_ent_wait_empower) AllWaitEmpowers(appid string, ent_id int64) *[]*base_ent_wait_empower {
-	list := Mysql_BaseService.SelectBySql(`select * from base_ent_wait_empower where appid=? and ent_id=? and end_time>?`, appid, ent_id, NowFormat(Date_Full_Layout))
+func (b *base_ent_wait_empower) AllWaitEmpowers(tx *sql.Tx, appid string, ent_id int64) *[]*base_ent_wait_empower {
+	list := Mysql_BaseService.SelectBySqlByTx(tx, `select * from base_ent_wait_empower where appid=? and ent_id=? and end_time>?`, appid, ent_id, NowFormat(Date_Full_Layout))
 	return JsonUnmarshal(list, &[]*base_ent_wait_empower{}).(*[]*base_ent_wait_empower)
 }
 
 //校验功能代码
-func (b *base_ent_wait_empower) CheckBeforeEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
-	list := b.AllWaitEmpowers(appid, ent_id)
-	if list == nil {
-		return -1, errors.New(fmt.Sprintf("企业%d没有任何待授权的功能代码", ent_id))
-	}
-	m := map[string]int64{}
-	for _, v := range *list {
-		if v.Empower_count < 0 {
-			m[v.Function_code] = v.Empower_count
-		} else {
-			m[v.Function_code] = m[v.Function_code] + v.Empower_count
-		}
+func (b *base_ent_wait_empower) CheckFunctionCode(appid string, function_code []string, ent_id int64) (int64, error) {
+	m := map[string]bool{}
+	for _, v := range *b.AllWaitEmpowers(nil, appid, ent_id) {
+		m[v.Function_code] = true
 	}
 	for _, v := range function_code {
-		if count, ok := m[v]; !ok {
+		if !m[v] {
 			return -1, errors.New(fmt.Sprintf("企业%d没有该待授权%s功能代码", ent_id, v))
-		} else if count >= 0 && int64(len(ent_user_id)) > count {
-			return -2, errors.New(fmt.Sprintf("企业%d功能代码%s可授权数量不足", ent_id, v))
 		}
 	}
 	return 1, nil
@@ -70,5 +61,5 @@ func (b *base_ent_wait_empower) CheckBeforeEmpower(appid string, function_code [
 
 //是否全部授权
 func (b *base_ent_wait_empower) IsAllEmpower(appid, function_code string, ent_id int64) bool {
-	return Mysql_BaseService.CountBySql(`select count(1) as count from base_ent_wait_empower where appid=? and function_code=? and ent_id=? and end_time>? and empower_count=-1`, appid, function_code, ent_id, NowFormat(Date_Full_Layout)) > 0
+	return Mysql_BaseService.CountBySql(`select count(1) as count from base_ent_wait_empower where appid=? and function_code=? and ent_id=? and end_time>? and empower_count<0`, appid, function_code, ent_id, NowFormat(Date_Full_Layout)) > 0
 }

+ 10 - 5
public/service/power.go

@@ -180,7 +180,7 @@ func ReEmpower(appid string, function_code []string, ent_id int64, ent_user_id [
 		return 0, errors.New("无效的参数ent_id")
 	} else if len(ent_user_id) == 0 {
 		return 0, errors.New("无效的参数ent_user_id")
-	} else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
+	} else if status, err := Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id); err != nil {
 		return status, err
 	}
 	if Base_ent_empower.ReEmpower(appid, function_code, ent_id, ent_user_id) {
@@ -209,17 +209,22 @@ func CrReEmpower(appid string, function_module, function_code []string, ent_id i
 		return 0, errors.New("无效的参数ent_id")
 	} else if len(ent_user_id) == 0 {
 		return 0, errors.New("无效的参数ent_user_id")
-	} else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
+	} else if status, err := Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id); err != nil {
 		return status, err
 	}
+	var err error
+	var status int64
 	if Mysql_BaseService.ExecTx("根据功能分类取消所有授权", func(tx *sql.Tx) bool {
 		ok1 := Base_ent_empower.CancelAllEmpower(tx, appid, function_module, ent_id, ent_user_id)
 		ok2 := Base_ent_empower.Empower(tx, appid, function_code, ent_id, ent_user_id)
+		if status, err = Base_ent_empower.CheckEmpowerCount(tx, appid, function_code, ent_id); err != nil {
+			return false
+		}
 		return ok1 && ok2
 	}) {
-		return 1, nil
+		status = 1
 	}
-	return 0, nil
+	return status, err
 }
 
 /*
@@ -239,7 +244,7 @@ func Empower(appid string, function_code []string, ent_id int64, ent_user_id []i
 		return 0, errors.New("无效的参数ent_id")
 	} else if len(ent_user_id) == 0 {
 		return 0, errors.New("无效的参数ent_user_id")
-	} else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
+	} else if status, err := Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id); err != nil {
 		return status, err
 	}
 	if Mysql_BaseService.ExecTx("授权", func(tx *sql.Tx) bool {