|
@@ -0,0 +1,357 @@
|
|
|
+package data_manage_permission
|
|
|
+
|
|
|
+import (
|
|
|
+ "eta/eta_api/utils"
|
|
|
+ "fmt"
|
|
|
+ "github.com/beego/beego/v2/client/orm"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type ExcelInfoPermission struct {
|
|
|
+ ExcelInfoPermissionId int64 `json:"excel_info_permission_id" orm:"column(excel_info_permission_id);pk"`
|
|
|
+ ExcelInfoId int32 `json:"excel_info_id"`
|
|
|
+ Source int32 `json:"source"`
|
|
|
+ SysUserId int32 `json:"sys_user_id"`
|
|
|
+ ModifyTime time.Time `json:"modify_time"`
|
|
|
+ CreateTime time.Time `json:"create_time"`
|
|
|
+}
|
|
|
+
|
|
|
+type ExcelClassifyPermission struct {
|
|
|
+ ExcelClassifyPermissionId int64 `json:"excel_classify_permission_id" orm:"column(excel_classify_permission_id);pk"`
|
|
|
+ ExcelClassifyId int32 `json:"excel_classify_id"`
|
|
|
+ Source int32 `json:"source"`
|
|
|
+ SysUserId int32 `json:"sys_user_id"`
|
|
|
+ ModifyTime time.Time `json:"modify_time"`
|
|
|
+ CreateTime time.Time `json:"create_time"`
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func SetIsPermissionByExcelClassifyIdList(classifyIdList []int, excelClassifySource int) (err error) {
|
|
|
+ num := len(classifyIdList)
|
|
|
+ if num <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ o, err := orm.NewOrmUsingDB("data").Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = o.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND source = ?`
|
|
|
+ _, err = o.Raw(sql, 0, excelClassifySource).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(classifyIdList) > 0 {
|
|
|
+
|
|
|
+ sql = `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id in (` + utils.GetOrmInReplace(num) + `) `
|
|
|
+ _, err = o.Raw(sql, 1, excelClassifySource, classifyIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func SetPermissionByExcelIdList(excelIdList []string, userIdList []int, source int) (err error) {
|
|
|
+ excelNum := len(excelIdList)
|
|
|
+ if excelNum <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ o, err := orm.NewOrmUsingDB("data").Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = o.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+ excelInfoPermissionList := make([]*ExcelInfoPermission, 0)
|
|
|
+ sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
|
|
|
+ _, err = o.Raw(sql, source, excelIdList).QueryRows(&excelInfoPermissionList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ excelInfoPermissionMap := make(map[string]*ExcelInfoPermission)
|
|
|
+ for _, v := range excelInfoPermissionList {
|
|
|
+ excelInfoPermissionMap[fmt.Sprint(v.ExcelInfoId, "_", v.SysUserId)] = v
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ isJoinPermission := 1
|
|
|
+
|
|
|
+ if len(userIdList) <= 0 {
|
|
|
+
|
|
|
+ isJoinPermission = 0
|
|
|
+ }
|
|
|
+ sql = `UPDATE excel_info SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
|
|
|
+ _, err = o.Raw(sql, isJoinPermission, source, excelIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ addList := make([]*ExcelInfoPermission, 0)
|
|
|
+
|
|
|
+
|
|
|
+ for _, excelInfoIdStr := range excelIdList {
|
|
|
+ excelInfoId, tmpErr := strconv.ParseInt(excelInfoIdStr, 10, 64)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, userId := range userIdList {
|
|
|
+ key := fmt.Sprint(excelInfoId, "_", userId)
|
|
|
+ if _, ok := excelInfoPermissionMap[key]; ok {
|
|
|
+
|
|
|
+ delete(excelInfoPermissionMap, key)
|
|
|
+ } else {
|
|
|
+
|
|
|
+ addList = append(addList, &ExcelInfoPermission{
|
|
|
+
|
|
|
+ ExcelInfoId: int32(excelInfoId),
|
|
|
+ SysUserId: int32(userId),
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if len(addList) > 0 {
|
|
|
+ _, err = o.InsertMulti(500, addList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ deletePermissionIdList := make([]int64, 0)
|
|
|
+ for _, v := range excelInfoPermissionMap {
|
|
|
+ deletePermissionIdList = append(deletePermissionIdList, v.ExcelInfoPermissionId)
|
|
|
+ }
|
|
|
+
|
|
|
+ deletePermissionIdNum := len(deletePermissionIdList)
|
|
|
+ if deletePermissionIdNum > 0 {
|
|
|
+ sql = "DELETE FROM excel_info_permission WHERE excel_info_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
|
|
|
+ _, err = o.Raw(sql, deletePermissionIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func SetPermissionByExcelClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
|
|
|
+ userNum := len(userIdList)
|
|
|
+ if userNum <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ o, err := orm.NewOrmUsingDB("data").Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = o.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+ classifyPermissionList := make([]*ExcelClassifyPermission, 0)
|
|
|
+ sql := `SELECT * FROM excel_classify_permission WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
|
|
|
+ _, err = o.Raw(sql, classifyType, userIdList).QueryRows(&classifyPermissionList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ classifyPermissionMap := make(map[string]*ExcelClassifyPermission)
|
|
|
+ for _, v := range classifyPermissionList {
|
|
|
+ classifyPermissionMap[fmt.Sprint(v.ExcelClassifyId, "_", v.SysUserId)] = v
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ addList := make([]*ExcelClassifyPermission, 0)
|
|
|
+
|
|
|
+
|
|
|
+ for _, userId := range userIdList {
|
|
|
+ for _, classifyId := range classifyIdList {
|
|
|
+ key := fmt.Sprint(classifyId, "_", userId)
|
|
|
+ if _, ok := classifyPermissionMap[key]; ok {
|
|
|
+
|
|
|
+ delete(classifyPermissionMap, key)
|
|
|
+ } else {
|
|
|
+
|
|
|
+ addList = append(addList, &ExcelClassifyPermission{
|
|
|
+
|
|
|
+ ExcelClassifyId: int32(classifyId),
|
|
|
+ Source: int32(classifyType),
|
|
|
+ SysUserId: int32(userId),
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if len(addList) > 0 {
|
|
|
+ _, err = o.InsertMulti(500, addList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ deletePermissionIdList := make([]int64, 0)
|
|
|
+ for _, v := range classifyPermissionMap {
|
|
|
+ deletePermissionIdList = append(deletePermissionIdList, v.ExcelClassifyPermissionId)
|
|
|
+ }
|
|
|
+
|
|
|
+ deletePermissionIdNum := len(deletePermissionIdList)
|
|
|
+ if deletePermissionIdNum > 0 {
|
|
|
+ sql = "DELETE FROM excel_classify_permission WHERE excel_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
|
|
|
+ _, err = o.Raw(sql, deletePermissionIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func GetPermissionExcelClassifyIdListByUserId(userId int, classifyType int) (excelClassifyIdList []int, err error) {
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE source = ? AND sys_user_id = ? `
|
|
|
+ _, err = o.Raw(sql, classifyType, userId).QueryRows(&excelClassifyIdList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func GetPermissionExcelIdListByDataId(dataId int, source int) (excelIdList []int, err error) {
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT sys_user_id FROM excel_info_permission WHERE source = ? AND excel_info_id= ? `
|
|
|
+ _, err = o.Raw(sql, source, dataId).QueryRows(&excelIdList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func GetPermissionExcelIdList(userId, excelInfoId int) (idList []int, err error) {
|
|
|
+ pars := []interface{}{userId}
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT excel_info_id FROM excel_info_permission WHERE sys_user_id = ? `
|
|
|
+ if excelInfoId > 0 {
|
|
|
+ sql += ` AND excel_info_id = ? `
|
|
|
+ pars = append(pars, excelInfoId)
|
|
|
+ }
|
|
|
+ _, err = o.Raw(sql, pars).QueryRows(&idList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func GetPermissionExcelClassifyIdList(userId, classifyId int) (idList []int, err error) {
|
|
|
+ pars := []interface{}{userId}
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE sys_user_id = ? `
|
|
|
+ if classifyId > 0 {
|
|
|
+ sql += ` AND excel_classify_id = ? `
|
|
|
+ pars = append(pars, classifyId)
|
|
|
+ }
|
|
|
+ _, err = o.Raw(sql, pars).QueryRows(&idList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|