Kaynağa Gözat

feat:批量删除

wangchuanjin 1 yıl önce
ebeveyn
işleme
6dc638a9f7

+ 17 - 15
public/entity/base_ent_empower.go

@@ -58,11 +58,11 @@ func (b *base_ent_empower) HasEmpower(appid, function_code string, ent_id, ent_u
 }
 
 //重新授权
-func (b *base_ent_empower) ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) bool {
+func (b *base_ent_empower) ReEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
 	return Mysql_BaseService.ExecTx("重新授权", func(tx *sql.Tx) bool {
 		args := []interface{}{appid, ent_id}
 		wh := []string{}
-		for _, v := range strings.Split(function_code, ",") {
+		for _, v := range function_code {
 			wh = append(wh, "?")
 			args = append(args, v)
 		}
@@ -70,7 +70,7 @@ func (b *base_ent_empower) ReEmpower(appid, function_code string, ent_id int64,
 		nowFormat := NowFormat(Date_Full_Layout)
 		values := []interface{}{}
 		for _, v := range ent_user_id {
-			for _, vv := range strings.Split(function_code, ",") {
+			for _, vv := range function_code {
 				values = append(values, appid, ent_id, v, vv, nowFormat)
 			}
 		}
@@ -78,32 +78,32 @@ func (b *base_ent_empower) ReEmpower(appid, function_code string, ent_id int64,
 		if len(values) > 0 {
 			r2, r3 = Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, values)
 		}
-		return r1 >= 0 && r2 == int64(len(ent_user_id)) && r3 > 0
+		return r1 >= 0 && r2 == int64(len(function_code)*len(ent_user_id)) && r3 > 0
 	})
 }
 
 //授权
-func (b *base_ent_empower) Empower(appid, function_code string, ent_id int64, ent_user_id []int64) bool {
+func (b *base_ent_empower) Empower(appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
 	fields := []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}
 	nowFormat := NowFormat(Date_Full_Layout)
 	values := []interface{}{}
-	functionCodes := strings.Split(function_code, ",")
 	for _, v := range ent_user_id {
-		for _, vv := range functionCodes {
+		for _, vv := range function_code {
 			values = append(values, appid, ent_id, v, vv, nowFormat)
 		}
 	}
-	b.CancelEmpower(appid, function_code, ent_id, ent_user_id)
-	r1, _ := Mysql_BaseService.InsertBatch("base_ent_empower", fields, values)
-	return r1 == int64(len(functionCodes)*len(ent_user_id))
+	return Mysql_BaseService.ExecTx("授权", func(tx *sql.Tx) bool {
+		b.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
+		r1, _ := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", fields, values)
+		return r1 == int64(len(function_code)*len(ent_user_id))
+	})
 }
 
 //取消授权
-func (b *base_ent_empower) CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) bool {
+func (b *base_ent_empower) CancelEmpower(tx *sql.Tx, appid string, function_code []string, ent_id int64, ent_user_id []int64) bool {
 	args := []interface{}{appid, ent_id}
 	wh1 := []string{}
-	functionCodes := strings.Split(function_code, ",")
-	for _, v := range functionCodes {
+	for _, v := range function_code {
 		wh1 = append(wh1, "?")
 		args = append(args, v)
 	}
@@ -112,6 +112,8 @@ func (b *base_ent_empower) CancelEmpower(appid, function_code string, ent_id int
 		wh2 = append(wh2, "?")
 		args = append(args, v)
 	}
-	r1 := Mysql_BaseService.UpdateOrDeleteBySql(`delete from base_ent_empower where appid=? and ent_id=? and function_code in (`+strings.Join(wh1, ",")+`) and ent_user_id in (`+strings.Join(wh2, ",")+`)`, args...)
-	return r1 == int64(len(functionCodes)*len(ent_user_id))
+	return Mysql_BaseService.ExecTx("取消授权", func(tx *sql.Tx) bool {
+		r1 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `delete from base_ent_empower where appid=? and ent_id=? and function_code in (`+strings.Join(wh1, ",")+`) and ent_user_id in (`+strings.Join(wh2, ",")+`)`, args...)
+		return r1 == int64(len(function_code)*len(ent_user_id))
+	})
 }

+ 16 - 9
public/entity/base_ent_wait_empower.go

@@ -1,7 +1,8 @@
 package entity
 
 import (
-	"strings"
+	"errors"
+	"fmt"
 
 	. "app.yhyue.com/moapp/jybase/common"
 	. "app.yhyue.com/moapp/jybase/date"
@@ -44,21 +45,27 @@ func (b *base_ent_wait_empower) AllWaitEmpowers(appid string, ent_id int64) *[]*
 }
 
 //校验功能代码
-func (b *base_ent_wait_empower) CheckFunctionCode(appid, function_code string, ent_id int64) bool {
+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 false
+		return -1, errors.New(fmt.Sprintf("企业%d没有任何待授权的功能代码", ent_id))
 	}
-	m := map[string]bool{}
+	m := map[string]int64{}
 	for _, v := range *list {
-		m[v.Function_code] = true
+		if v.Empower_count < 0 {
+			m[v.Function_code] = v.Empower_count
+		} else {
+			m[v.Function_code] = m[v.Function_code] + v.Empower_count
+		}
 	}
-	for _, v := range strings.Split(function_code, ",") {
-		if !m[v] {
-			return false
+	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 && int64(len(ent_user_id)) > count {
+			return -2, errors.New(fmt.Sprintf("企业%d功能代码%s可授权数量不足", ent_id, v))
 		}
 	}
-	return true
+	return 1, nil
 }
 
 //是否全部授权

+ 15 - 28
public/service/power.go

@@ -1,10 +1,12 @@
 package service
 
 import (
+	"database/sql"
 	"errors"
 	"time"
 
 	. "app.yhyue.com/moapp/jybase/date"
+	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
 	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
 	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
 )
@@ -169,32 +171,17 @@ func HasPowers(appid string, account_id, ent_account_id, ent_id, ent_user_id int
  * @param ent_user_id 企业用户id
  * @return 0:失败 1:成功 -2:数量不足
  */
-func ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
+func ReEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
 	if appid == "" {
 		return 0, errors.New("无效的参数appid")
-	} else if function_code == "" {
+	} else if len(function_code) == 0 {
 		return 0, errors.New("无效的参数function_code")
 	} else if ent_id == 0 {
 		return 0, errors.New("无效的参数ent_id")
 	} else if len(ent_user_id) == 0 {
 		return 0, errors.New("无效的参数ent_user_id")
-	} else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
-		return 0, errors.New("无效的参数function_code")
-	}
-	bewes := Base_ent_wait_empower.WaitEmpowers(appid, function_code, ent_id)
-	if bewes == nil {
-		return -1, nil
-	}
-	var count int64
-	for _, v := range *bewes {
-		if v.Empower_count == -1 {
-			count = -1
-			break
-		}
-		count += v.Empower_count
-	}
-	if count != -1 && int64(len(ent_user_id)) > count {
-		return -2, nil
+	} else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
+		return status, err
 	}
 	if Base_ent_empower.ReEmpower(appid, function_code, ent_id, ent_user_id) {
 		return 1, nil
@@ -210,17 +197,17 @@ func ReEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (
  * @param ent_user_id 企业用户id
  * @return 0:失败 1:成功
  */
-func Empower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
+func Empower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
 	if appid == "" {
 		return 0, errors.New("无效的参数appid")
-	} else if function_code == "" {
+	} else if len(function_code) == 0 {
 		return 0, errors.New("无效的参数function_code")
 	} else if ent_id == 0 {
 		return 0, errors.New("无效的参数ent_id")
 	} else if len(ent_user_id) == 0 {
 		return 0, errors.New("无效的参数ent_user_id")
-	} else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
-		return 0, errors.New("无效的参数function_code")
+	} else if status, err := Base_ent_wait_empower.CheckBeforeEmpower(appid, function_code, ent_id, ent_user_id); err != nil {
+		return status, err
 	}
 	if Base_ent_empower.Empower(appid, function_code, ent_id, ent_user_id) {
 		return 1, nil
@@ -236,19 +223,19 @@ func Empower(appid, function_code string, ent_id int64, ent_user_id []int64) (in
  * @param ent_user_id 企业用户id
  * @return 0:失败 1:成功
  */
-func CancelEmpower(appid, function_code string, ent_id int64, ent_user_id []int64) (int64, error) {
+func CancelEmpower(appid string, function_code []string, ent_id int64, ent_user_id []int64) (int64, error) {
 	if appid == "" {
 		return 0, errors.New("无效的参数appid")
-	} else if function_code == "" {
+	} else if len(function_code) == 0 {
 		return 0, errors.New("无效的参数function_code")
 	} else if ent_id == 0 {
 		return 0, errors.New("无效的参数ent_id")
 	} else if len(ent_user_id) == 0 {
 		return 0, errors.New("无效的参数ent_user_id")
-	} else if !Base_ent_wait_empower.CheckFunctionCode(appid, function_code, ent_id) {
-		return 0, errors.New("无效的参数function_code")
 	}
-	if Base_ent_empower.CancelEmpower(appid, function_code, ent_id, ent_user_id) {
+	if Mysql_BaseService.ExecTx("取消授权", func(tx *sql.Tx) bool {
+		return Base_ent_empower.CancelEmpower(tx, appid, function_code, ent_id, ent_user_id)
+	}) {
 		return 1, nil
 	}
 	return 0, nil

+ 7 - 7
public/service/power_test.go

@@ -27,7 +27,7 @@ func TestCancelPower(t *testing.T) {
 
 func TestHasPowers(t *testing.T) {
 	//企业
-	powers, err := HasPowers("10000", 13488, 64, 221, 195)
+	powers, err := HasPowers("10000", 180192, 12276, 221, 195)
 	//个人
 	//powers, err := HasPowers("10000", 13485, 0, 0, 0)
 	assert.Nil(t, err)
@@ -35,22 +35,22 @@ func TestHasPowers(t *testing.T) {
 }
 
 func TestReEmpower(t *testing.T) {
-	v, err := ReEmpower("10000", "znsj_kf_use", 14184, []int64{4271, 123})
-	assert.Nil(t, err)
-	assert.Equal(t, int64(1), v)
-	v, err = ReEmpower("10000", "znsj_jqr_use", 14184, []int64{4271})
+	v, err := ReEmpower("10000", []string{"bi_dx_yjtj", "bi_dx_zxdx", "bi_dx_rcgl", "bi_dx_wjsgjkh", "bi_dx_shxs", "bi_dx_xszl"}, 1, []int64{3870})
 	assert.Nil(t, err)
 	assert.Equal(t, int64(1), v)
+	//v, err = ReEmpower("10000", "znsj_jqr_use", 14184, []int64{4271})
+	//assert.Nil(t, err)
+	//assert.Equal(t, int64(1), v)
 }
 
 func TestEmpower(t *testing.T) {
-	v, err := Empower("10000", "bi_dx_rcgl,bi_dx_shxs", 1, []int64{3870, 22})
+	v, err := Empower("10000", []string{"bi_dx_yjtj", "bi_dx_zxdx", "bi_dx_rcgl", "bi_dx_wjsgjkh", "bi_dx_shxs", "bi_dx_xszl"}, 1, []int64{3870})
 	assert.Nil(t, err)
 	assert.Equal(t, int64(1), v)
 }
 
 func TestCancelEmpower(t *testing.T) {
-	v, err := CancelEmpower("10000", "bi_dx_rcgl,bi_dx_shxs", 1, []int64{3870})
+	v, err := CancelEmpower("10000", []string{"bi_dx_rcgl", "bi_dx_shxs"}, 1, []int64{3870})
 	assert.Nil(t, err)
 	assert.Equal(t, int64(1), v)
 }

+ 2 - 1
rpc/internal/logic/cancelempowerlogic.go

@@ -3,6 +3,7 @@ package logic
 import (
 	"context"
 	"fmt"
+	"strings"
 
 	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/service"
 	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/svc"
@@ -28,7 +29,7 @@ func NewCancelEmpowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Can
 // 取消授权
 func (l *CancelEmpowerLogic) CancelEmpower(in *pb.EmpowerReq) (*pb.Resp, error) {
 	resp := &pb.Resp{}
-	status, err := CancelEmpower(in.Appid, in.FunctionCode, in.EntId, in.EntUserId)
+	status, err := CancelEmpower(in.Appid, strings.Split(in.FunctionCode, ","), in.EntId, in.EntUserId)
 	if err != nil {
 		l.Error(fmt.Sprintf("%+v", in), err)
 	}

+ 2 - 1
rpc/internal/logic/empowerlogic.go

@@ -3,6 +3,7 @@ package logic
 import (
 	"context"
 	"fmt"
+	"strings"
 
 	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/service"
 	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/svc"
@@ -28,7 +29,7 @@ func NewEmpowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EmpowerLo
 // 授权
 func (l *EmpowerLogic) Empower(in *pb.EmpowerReq) (*pb.Resp, error) {
 	resp := &pb.Resp{}
-	status, err := Empower(in.Appid, in.FunctionCode, in.EntId, in.EntUserId)
+	status, err := Empower(in.Appid, strings.Split(in.FunctionCode, ","), in.EntId, in.EntUserId)
 	if err != nil {
 		l.Error(fmt.Sprintf("%+v", in), err)
 	}

+ 2 - 1
rpc/internal/logic/reempowerlogic.go

@@ -3,6 +3,7 @@ package logic
 import (
 	"context"
 	"fmt"
+	"strings"
 
 	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/service"
 	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/svc"
@@ -28,7 +29,7 @@ func NewReEmpowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReEmpow
 // 重新授权
 func (l *ReEmpowerLogic) ReEmpower(in *pb.EmpowerReq) (*pb.Resp, error) {
 	resp := &pb.Resp{}
-	status, err := ReEmpower(in.Appid, in.FunctionCode, in.EntId, in.EntUserId)
+	status, err := ReEmpower(in.Appid, strings.Split(in.FunctionCode, ","), in.EntId, in.EntUserId)
 	if err != nil {
 		l.Error(fmt.Sprintf("%+v", in), err)
 	}