123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- package data_manage_permission
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "strconv"
- "time"
- )
- type EdbInfoPermission struct {
- EdbInfoPermissionId int64 `json:"edb_info_permission_id" orm:"column(edb_info_permission_id);pk" gorm:"primaryKey" `
- EdbInfoId int32 `json:"edb_info_id"` // 指标id
- EdbInfoType int32 `json:"edb_info_type"` // 指标类型,0:普通指标,1:预测指标
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- ModifyTime time.Time `json:"modify_time"` // 变更时间
- CreateTime time.Time `json:"create_time"` // 关系建立时间
- }
- type EdbClassifyPermission struct {
- EdbClassifyPermissionId int64 `json:"edb_classify_permission_id" orm:"column(edb_classify_permission_id);pk" gorm:"primaryKey" `
- EdbClassifyId int32 `json:"edb_classify_id"` // 分类id
- EdbClassifyType int32 `json:"edb_classify_type"` // 分类类型,0:普通指标分类,1:预测指标分类
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- ModifyTime time.Time `json:"modify_time"` // 变更时间
- CreateTime time.Time `json:"create_time"` // 关系建立时间
- }
- func SetIsPermissionEdbChartByEdbClassifyIdList(classifyIdList []int, classifyType int) (err error) {
- num := len(classifyIdList)
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- sql := `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND classify_type = ?`
- err = to.Exec(sql, 0, classifyType).Error
- if err != nil {
- return
- }
- if num > 0 {
- sql = `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE classify_type = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `) `
- err = to.Exec(sql, 1, classifyType, classifyIdList).Error
- if err != nil {
- return
- }
- }
- return
- }
- func SetPermissionByEdbIdList(edbIdList []string, userIdList []int, edbInfoType int) (err error) {
- edbNum := len(edbIdList)
- if edbNum <= 0 {
- return
- }
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- edbInfoPermissionList := make([]*EdbInfoPermission, 0)
- sql := `SELECT * FROM edb_info_permission WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(edbNum) + `) `
- err = to.Raw(sql, edbInfoType, edbIdList).Find(&edbInfoPermissionList).Error
- if err != nil {
- return
- }
- edbInfoPermissionMap := make(map[string]*EdbInfoPermission)
- for _, v := range edbInfoPermissionList {
- edbInfoPermissionMap[fmt.Sprint(v.EdbInfoId, "_", v.SysUserId)] = v
- }
- {
- isJoinPermission := 1
- if len(userIdList) <= 0 {
- isJoinPermission = 0
- }
- sql = `UPDATE edb_info SET is_join_permission=?,modify_time=now() WHERE edb_info_type = ? AND edb_info_id in (?) `
- err = to.Exec(sql, isJoinPermission, edbInfoType, edbIdList).Error
- if err != nil {
- return
- }
- }
- addList := make([]*EdbInfoPermission, 0)
- for _, edbInfoIdStr := range edbIdList {
- edbInfoId, tmpErr := strconv.ParseInt(edbInfoIdStr, 10, 64)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, userId := range userIdList {
- key := fmt.Sprint(edbInfoId, "_", userId)
- if _, ok := edbInfoPermissionMap[key]; ok {
- delete(edbInfoPermissionMap, key)
- } else {
- addList = append(addList, &EdbInfoPermission{
- EdbInfoId: int32(edbInfoId),
- SysUserId: int32(userId),
- EdbInfoType: int32(edbInfoType),
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- }
- }
- }
- if len(addList) > 0 {
- err = to.CreateInBatches(addList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- {
- deletePermissionIdList := make([]int64, 0)
- for _, v := range edbInfoPermissionMap {
- deletePermissionIdList = append(deletePermissionIdList, v.EdbInfoPermissionId)
- }
- deletePermissionIdNum := len(deletePermissionIdList)
- if deletePermissionIdNum > 0 {
- sql = `DELETE FROM edb_info_permission WHERE edb_info_permission_id in (?)`
- err = to.Exec(sql, deletePermissionIdList).Error
- if err != nil {
- return
- }
- }
- }
- return
- }
- func SetPermissionByEdbClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
- userNum := len(userIdList)
- if userNum <= 0 {
- return
- }
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- classifyPermissionList := make([]*EdbClassifyPermission, 0)
- sql := `SELECT * FROM edb_classify_permission WHERE edb_classify_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
- err = to.Raw(sql, classifyType, userIdList).Find(&classifyPermissionList).Error
- if err != nil {
- return
- }
- classifyPermissionMap := make(map[string]*EdbClassifyPermission)
- for _, v := range classifyPermissionList {
- classifyPermissionMap[fmt.Sprint(v.EdbClassifyId, "_", v.SysUserId)] = v
- }
- addList := make([]*EdbClassifyPermission, 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, &EdbClassifyPermission{
- EdbClassifyId: int32(classifyId),
- EdbClassifyType: int32(classifyType),
- SysUserId: int32(userId),
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- }
- }
- }
- if len(addList) > 0 {
- err = to.CreateInBatches(addList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- {
- deletePermissionIdList := make([]int64, 0)
- for _, v := range classifyPermissionMap {
- deletePermissionIdList = append(deletePermissionIdList, v.EdbClassifyPermissionId)
- }
- deletePermissionIdNum := len(deletePermissionIdList)
- if deletePermissionIdNum > 0 {
- sql = "DELETE FROM edb_classify_permission WHERE edb_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
- err = to.Exec(sql, deletePermissionIdList).Error
- if err != nil {
- return
- }
- }
- }
- return
- }
- func GetPermissionEdbClassifyIdListByUserId(userId int, classifyType int) (edbClassifyIdList []int, err error) {
- sql := `SELECT edb_classify_id FROM edb_classify_permission WHERE edb_classify_type = ? AND sys_user_id = ? `
- err = global.DmSQL["data"].Raw(sql, classifyType, userId).Find(&edbClassifyIdList).Error
- return
- }
- func GetPermissionUserIdListByEdbId(dataId int, edbInfoType int) (userIdList []int, err error) {
- sql := `SELECT sys_user_id FROM edb_info_permission WHERE edb_info_type = ? AND edb_info_id= ? `
- err = global.DmSQL["data"].Raw(sql, edbInfoType, dataId).Find(&userIdList).Error
- return
- }
- func GetPermissionUserIdListByEdbClassifyId(classifyId int, edbClassifyType int) (userIdList []int, err error) {
- sql := `SELECT sys_user_id FROM edb_classify_permission WHERE edb_classify_type = ? AND edb_classify_id= ? `
- err = global.DmSQL["data"].Raw(sql, edbClassifyType, classifyId).Find(&userIdList).Error
- return
- }
- func GetPermissionEdbIdList(userId, edbInfoId int) (idList []int, err error) {
- pars := []interface{}{userId}
- sql := `SELECT edb_info_id FROM edb_info_permission WHERE sys_user_id = ? `
- if edbInfoId > 0 {
- sql += ` AND edb_info_id = ? `
- pars = append(pars, edbInfoId)
- }
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&idList).Error
- return
- }
- func GetPermissionEdbClassifyIdList(userId, classifyId int) (idList []int, err error) {
- pars := []interface{}{userId}
- sql := `SELECT edb_classify_id FROM edb_classify_permission WHERE sys_user_id = ? `
- if classifyId > 0 {
- sql += ` AND edb_classify_id = ? `
- pars = append(pars, classifyId)
- }
- err = global.DmSQL["data"].Raw(sql, pars...).Find(&idList).Error
- return
- }
- func InheritParentClassifyByEdbClassifyId(source, classifyType, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- sql := `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE classify_type = ? AND classify_id = ? `
- err = to.Exec(sql, 1, classifyType, classifyId).Error
- if err != nil {
- return
- }
- {
- var parentRecordItems []*EdbInfoClassifyPermissionNoAuthRecord
- sql = `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE classify_id = ? AND edb_classify_type = ? ORDER BY edb_info_classify_permission_no_auth_record_id desc LIMIT ?,? `
- err = to.Raw(sql, parentClassifyId, source, classifyType).Find(&parentRecordItems).Error
- addNoAuthRecordItems := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
- for _, v := range parentRecordItems {
- addNoAuthRecordItems = append(addNoAuthRecordItems, &EdbInfoClassifyPermissionNoAuthRecord{
- EdbInfoClassifyPermissionNoAuthRecordId: 0,
- EdbClassifyType: v.EdbClassifyType,
- OpUniqueCode: uniqueCode,
- ClassifyId: fmt.Sprint(classifyId),
- ClassifyName: classifyName,
- SysUserId: v.SysUserId,
- CreateTime: time.Now(),
- })
- }
- if len(addNoAuthRecordItems) > 0 {
- err = to.CreateInBatches(addNoAuthRecordItems, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- }
- {
- parentClassifyPermissionList := make([]*EdbClassifyPermission, 0)
- sql = `SELECT * FROM edb_classify_permission WHERE edb_classify_type = ? AND edb_classify_id = ? `
- err = to.Raw(sql, classifyType, parentClassifyId).Find(&parentClassifyPermissionList).Error
- if err != nil {
- return
- }
- addList := make([]*EdbClassifyPermission, 0)
- for _, v := range parentClassifyPermissionList {
- addList = append(addList, &EdbClassifyPermission{
- EdbClassifyId: int32(classifyId),
- EdbClassifyType: int32(classifyType),
- SysUserId: v.SysUserId,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- }
- if len(addList) > 0 {
- err = to.CreateInBatches(addList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- }
- return
- }
- type EdbInfoPermissionNoAuthRecord struct {
- EdbInfoPermissionNoAuthRecordId int64 `json:"edb_info_permission_no_auth_record_id" orm:"column(edb_info_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产数据操作记录id
- OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
- EdbInfoType int32 `json:"edb_info_type"` // 指标类型,0:普通指标,1:预测指标
- EdbInfoId int32 `json:"edb_info_id"` // 指标id
- EdbCode string `json:"edb_code"` // 指标编码
- EdbName string `json:"edb_name"` // 指标名称
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- CreateTime time.Time `json:"create_time"` // 创建时间
- }
- func AddEdbInfoPermissionNoAuthRecordBySourceAndDataIdList(source, edbInfoType int, dataList []DataItem, noAuthUserIdList, authUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
- num := len(dataList)
- if num <= 0 {
- return
- }
- dataIdList := make([]int, 0)
- for _, v := range dataList {
- dataIdList = append(dataIdList, v.DataId)
- }
- userNum := len(noAuthUserIdList)
- if userNum <= 0 {
- return
- }
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- var existList []*EdbInfoPermissionNoAuthRecord
- sql := `SELECT * FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
- err = to.Raw(sql, edbInfoType, dataIdList).Find(&existList).Error
- if err != nil {
- return
- }
- existMap := make(map[int32]map[string]*EdbInfoPermissionNoAuthRecord)
- for _, v := range existList {
- tmpUserExistMap, ok := existMap[v.SysUserId]
- if !ok {
- tmpUserExistMap = make(map[string]*EdbInfoPermissionNoAuthRecord)
- }
- key := fmt.Sprint(v.EdbInfoType, "_", v.EdbInfoId)
- tmpUserExistMap[key] = v
- existMap[v.SysUserId] = tmpUserExistMap
- }
- addMessageList := make([]*DataPermissionMessage, 0)
- addRecordList := make([]*EdbInfoPermissionNoAuthRecord, 0)
- for _, userId := range noAuthUserIdList {
- isAdd := false
- tmpUserExistMap, userExistOk := existMap[int32(userId)]
- for _, dataItem := range dataList {
- if userExistOk {
- key := fmt.Sprint(edbInfoType, "_", dataItem.DataId)
- _, ok := tmpUserExistMap[key]
- if ok {
- continue
- }
- }
- isAdd = true
- addRecordList = append(addRecordList, &EdbInfoPermissionNoAuthRecord{
- EdbInfoPermissionNoAuthRecordId: 0,
- OpUniqueCode: uniqueCode,
- EdbInfoType: int32(edbInfoType),
- EdbInfoId: int32(dataItem.DataId),
- EdbCode: dataItem.DataCode,
- EdbName: dataItem.DataName,
- SysUserId: int32(userId),
- CreateTime: time.Now(),
- })
- }
- if isAdd {
- addMessageList = append(addMessageList, &DataPermissionMessage{
- DataPermissionMessageId: 0,
- SendUserId: int32(opUserId),
- ReceiveUserId: int32(userId),
- Content: title,
- Remark: content,
- OpType: 3,
- Source: int32(source),
- SubSource: int32(edbInfoType),
- OpUniqueCode: uniqueCode,
- IsRead: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- if len(addMessageList) > 0 {
- err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- if len(addRecordList) > 0 {
- err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- authUserIdNum := len(authUserIdList)
- if authUserIdNum > 0 {
- sql = `DELETE FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(authUserIdNum) + `) AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
- err = to.Exec(sql, edbInfoType, authUserIdList, dataIdList).Error
- }
- return
- }
- func DeleteEdbInfoPermissionNoAuthRecordBySourceAndDataIdList(edbInfoType int, dataIdList []string) (err error) {
- num := len(dataIdList)
- if num <= 0 {
- return
- }
- sql := `DELETE FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
- err = global.DmSQL["data"].Exec(sql, edbInfoType, dataIdList).Error
- return
- }
- func GetEdbInfoDataPermissionNoAuthRecordListByUserId(userId int32, edbInfoType, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
- sql := `SELECT count(1) AS total FROM edb_info_permission_no_auth_record WHERE sys_user_id = ? AND edb_info_type = ? `
- err = global.DmSQL["data"].Raw(sql, userId, edbInfoType).First(&total).Error
- if err != nil {
- return
- }
- sql = `SELECT edb_info_permission_no_auth_record_id as data_permission_no_auth_record_id,op_unique_code,edb_info_type as sub_source,edb_info_id as data_id,edb_code as data_code,edb_name as data_name,sys_user_id,create_time FROM edb_info_permission_no_auth_record WHERE sys_user_id = ? AND edb_info_type = ? ORDER BY edb_info_permission_no_auth_record_id desc LIMIT ?,? `
- err = global.DmSQL["data"].Raw(sql, userId, edbInfoType, startSize, pageSize).Find(&items).Error
- return
- }
- type EdbInfoClassifyPermissionNoAuthRecord struct {
- EdbInfoClassifyPermissionNoAuthRecordId int64 `json:"edb_info_classify_permission_no_auth_record_id" orm:"column(edb_info_classify_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产分类数据操作记录id
- EdbClassifyType int32 `json:"edb_classify_type"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
- OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
- ClassifyId string `json:"classify_id"` // 资产分类id(指标、图表、表格)
- ClassifyName string `json:"classify_name"` // 资产分类名称(指标、图表、表格)
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- CreateTime time.Time `json:"create_time"` // 创建时间
- }
- func AddEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, edbClassifyType int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
- num := len(classifyInfoList)
- if num <= 0 {
- return
- }
- classifyIdList := make([]int, 0)
- for _, v := range classifyInfoList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- userNum := len(noAuthUserIdList)
- if userNum <= 0 {
- return
- }
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- var existList []*EdbInfoClassifyPermissionNoAuthRecord
- sql := `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
- err = to.Raw(sql, edbClassifyType, classifyIdList).Find(&existList).Error
- if err != nil {
- return
- }
- existMap := make(map[int32]map[string]*EdbInfoClassifyPermissionNoAuthRecord)
- for _, v := range existList {
- tmpUserExistMap, ok := existMap[v.SysUserId]
- if !ok {
- tmpUserExistMap = make(map[string]*EdbInfoClassifyPermissionNoAuthRecord)
- }
- key := fmt.Sprint(v.EdbClassifyType, "_", v.ClassifyId)
- tmpUserExistMap[key] = v
- existMap[v.SysUserId] = tmpUserExistMap
- }
- addMessageList := make([]*DataPermissionMessage, 0)
- addRecordList := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
- for _, userId := range noAuthUserIdList {
- isAdd := false
- tmpUserExistMap, userExistOk := existMap[int32(userId)]
- for _, dataItem := range classifyInfoList {
- if userExistOk {
- key := fmt.Sprint(edbClassifyType, "_", dataItem.ClassifyId)
- _, ok := tmpUserExistMap[key]
- if ok {
- continue
- }
- }
- isAdd = true
- addRecordList = append(addRecordList, &EdbInfoClassifyPermissionNoAuthRecord{
- EdbInfoClassifyPermissionNoAuthRecordId: 0,
- EdbClassifyType: int32(edbClassifyType),
- OpUniqueCode: uniqueCode,
- ClassifyId: fmt.Sprint(dataItem.ClassifyId),
- ClassifyName: dataItem.ClassifyName,
- SysUserId: int32(userId),
- CreateTime: time.Now(),
- })
- }
- if isAdd {
- addMessageList = append(addMessageList, &DataPermissionMessage{
- DataPermissionMessageId: 0,
- SendUserId: int32(opUserId),
- ReceiveUserId: int32(userId),
- Content: title,
- Remark: content,
- OpType: 4,
- Source: int32(source),
- SubSource: int32(edbClassifyType),
- OpUniqueCode: uniqueCode,
- IsRead: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- if len(addMessageList) > 0 {
- err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- if len(addRecordList) > 0 {
- err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- authUserIdNum := len(classifyIdList)
- if authUserIdNum > 0 {
- sql = `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
- err = to.Exec(sql, edbClassifyType, classifyIdList).Error
- }
- return
- }
- func AddEdbInfoClassifyNoAuthRecordBySourceAndUserIdList(source, edbClassifyType int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
- configUserNum := len(configUserIdList)
- if configUserNum <= 0 {
- return
- }
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- var existList []*EdbInfoClassifyPermissionNoAuthRecord
- sql := `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
- err = to.Raw(sql, edbClassifyType, configUserIdList).Find(&existList).Error
- if err != nil {
- return
- }
- existMap := make(map[int32]map[string]*EdbInfoClassifyPermissionNoAuthRecord)
- delRecordIdMap := make(map[int64]int64)
- for _, v := range existList {
- tmpUserExistMap, ok := existMap[v.SysUserId]
- if !ok {
- tmpUserExistMap = make(map[string]*EdbInfoClassifyPermissionNoAuthRecord)
- }
- tmpUserExistMap[v.ClassifyId] = v
- existMap[v.SysUserId] = tmpUserExistMap
- delRecordIdMap[v.EdbInfoClassifyPermissionNoAuthRecordId] = v.EdbInfoClassifyPermissionNoAuthRecordId
- }
- addMessageList := make([]*DataPermissionMessage, 0)
- addRecordList := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
- for _, userId := range configUserIdList {
- isAdd := false
- tmpUserExistMap, userExistOk := existMap[int32(userId)]
- for _, dataItem := range noAuthClassifyMap {
- if userExistOk {
- key := fmt.Sprint(dataItem.ClassifyId)
- tmpUserRecord, ok := tmpUserExistMap[key]
- if ok {
- delete(delRecordIdMap, tmpUserRecord.EdbInfoClassifyPermissionNoAuthRecordId)
- continue
- }
- }
- isAdd = true
- addRecordList = append(addRecordList, &EdbInfoClassifyPermissionNoAuthRecord{
- EdbInfoClassifyPermissionNoAuthRecordId: 0,
- EdbClassifyType: int32(edbClassifyType),
- OpUniqueCode: uniqueCode,
- ClassifyId: fmt.Sprint(dataItem.ClassifyId),
- ClassifyName: dataItem.ClassifyName,
- SysUserId: int32(userId),
- CreateTime: time.Now(),
- })
- }
- if isAdd {
- addMessageList = append(addMessageList, &DataPermissionMessage{
- DataPermissionMessageId: 0,
- SendUserId: int32(opUserId),
- ReceiveUserId: int32(userId),
- Content: title,
- Remark: content,
- OpType: 4,
- Source: int32(source),
- SubSource: int32(edbClassifyType),
- OpUniqueCode: uniqueCode,
- IsRead: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- if len(addMessageList) > 0 {
- err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- if len(addRecordList) > 0 {
- err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- delRecordIdNum := len(delRecordIdMap)
- if delRecordIdNum > 0 {
- delRecordIdList := make([]int64, 0)
- for _, v := range delRecordIdMap {
- delRecordIdList = append(delRecordIdList, v)
- }
- sql = `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
- err = to.Exec(sql, delRecordIdList).Error
- }
- return
- }
- func DeleteEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList(edbClassifyType int) (err error) {
- sql := `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ?`
- err = global.DmSQL["data"].Exec(sql, edbClassifyType).Error
- return
- }
- func GetEdbInfoDataPermissionClassifyNoAuthRecordListByUserId(userId int32, edbClassifyType, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
- sql := `SELECT count(1) AS total FROM edb_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND edb_classify_type = ? `
- err = global.DmSQL["data"].Raw(sql, userId, edbClassifyType).Scan(&total).Error
- if err != nil {
- return
- }
- sql = `SELECT edb_info_classify_permission_no_auth_record_id as data_permission_classify_no_auth_record_id,edb_classify_type as sub_source,op_unique_code,classify_id,classify_name,sys_user_id,create_time FROM edb_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND edb_classify_type = ? ORDER BY edb_info_classify_permission_no_auth_record_id desc LIMIT ?,? `
- err = global.DmSQL["data"].Raw(sql, userId, edbClassifyType, startSize, pageSize).Find(&items).Error
- return
- }
|