12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- 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"` // ETA表格id
- Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- ModifyTime time.Time `json:"modify_time"` // 变更时间
- CreateTime time.Time `json:"create_time"` // 关系建立时间
- PermissionType int `json:"permission_type"` // 权限类型: 0-默认; 1-查看; 2-编辑
- }
- type ExcelClassifyPermission struct {
- ExcelClassifyPermissionId int64 `json:"excel_classify_permission_id" orm:"column(excel_classify_permission_id);pk"`
- ExcelClassifyId int32 `json:"excel_classify_id"` // 分类id
- Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- ModifyTime time.Time `json:"modify_time"` // 变更时间
- CreateTime time.Time `json:"create_time"` // 关系建立时间
- }
- // SetIsPermissionByExcelClassifyIdList
- // @Description: 设置表格分类是否涉密
- // @author: Roc
- // @datetime 2024-04-01 10:53:23
- // @param classifyIdList []int
- // @param excelClassifySource int
- // @return err error
- func SetIsPermissionByExcelClassifyIdList(classifyIdList []int, excelClassifySource int) (err error) {
- num := len(classifyIdList)
- o, err := orm.NewOrmUsingDB("data").Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = o.Rollback()
- } else {
- _ = o.Commit()
- }
- }()
- //// 获取已经配置涉密的分类权限
- //excelClassifyList := make([]*ExcelClassify, 0)
- //sql := `SELECT * FROM excel_classify WHERE is_join_permission = ? `
- //_, err = o.Raw(sql, 1).QueryRows(&excelClassifyList)
- //if err != nil {
- // return
- //}
- //excelClassifyMap := make(map[int]*ExcelClassify)
- //for _, v := range excelClassifyList {
- // excelClassifyMap[v.ClassifyId] = v
- //}
- // 先将所有已经设置了涉密的分类设置为不涉密
- 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 num > 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
- }
- // SetPermissionByExcelIdList
- // @Description: 根据表格ID列表设置表格的用户权限
- // @author: Roc
- // @datetime 2024-04-01 10:26:17
- // @param excelIdList []string
- // @param userIdList []int
- // @param source int
- // @return err error
- 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{
- //PermissionId: 0,
- ExcelInfoId: int32(excelInfoId),
- SysUserId: int32(userId),
- Source: int32(source),
- 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
- }
- // SetPermissionByExcelClassifyIdList
- // @Description: 根据表格分类ID列表设置分类的用户权限
- // @author: Roc
- // @datetime 2024-03-28 14:53:04
- // @param classifyIdList []int
- // @param userIdList []int
- // @return err error
- 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{
- //PermissionId: 0,
- 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
- }
- // GetPermissionExcelClassifyIdListByUserId
- // @Description: 根据用户ID获取已经配置的分类id列表
- // @author: Roc
- // @datetime 2024-03-29 16:24:46
- // @param userId int
- // @param classifyType int
- // @return excelClassifyIdList []int
- // @return err error
- 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
- }
- // GetPermissionUserIdListByExcelId
- // @Description: 根据表格ID获取已经配置的用户id列表
- // @author: Roc
- // @datetime 2024-03-29 16:24:46
- // @param dataId int
- // @param source int
- // @return userIdList []int
- // @return err error
- func GetPermissionUserIdListByExcelId(dataId int, source int) (userIdList []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(&userIdList)
- return
- }
- // GetPermissionUserIdListByExcelClassifyId
- // @Description: 根据表格分类ID获取已经配置的用户id列表
- // @author: Roc
- // @datetime 2024-03-29 16:24:46
- // @param classifyId int
- // @param source int
- // @return userIdList []int
- // @return err error
- func GetPermissionUserIdListByExcelClassifyId(classifyId int, source int) (userIdList []int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `SELECT sys_user_id FROM excel_classify_permission WHERE source = ? AND excel_classify_id= ? `
- _, err = o.Raw(sql, source, classifyId).QueryRows(&userIdList)
- return
- }
- // GetPermissionExcelIdList
- // @Description: 获取用户权限的表格列表
- // @author: Roc
- // @datetime 2024-03-28 16:50:47
- // @param userId int
- // @param excelInfoId int
- // @return idList []int
- // @return err error
- 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
- }
- // GetPermissionExcelClassifyIdList
- // @Description: 获取用户权限的表格分类列表
- // @author: Roc
- // @datetime 2024-03-28 16:50:47
- // @param userId int
- // @param classifyId int
- // @return idList []int
- // @return err error
- 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
- }
- // InheritParentClassifyByExcelClassifyId
- // @Description: 继承父级分类的ETA表格权限信息
- // @author: Roc
- // @datetime 2024-04-07 21:02:51
- // @param dataSource int
- // @param excelSource int
- // @param classifyId int
- // @param parentClassifyId int
- // @param classifyName string
- // @param uniqueCode string
- // @return err error
- func InheritParentClassifyByExcelClassifyId(dataSource, excelSource, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
- 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 source = ? AND excel_classify_id = ? `
- _, err = o.Raw(sql, 1, excelSource, classifyId).Exec()
- if err != nil {
- return
- }
- // 添加未授权记录
- {
- // 获取父级未授权的用户记录
- var parentRecordItems []*ExcelInfoClassifyPermissionNoAuthRecord
- sql = `SELECT * FROM data_permission_classify_no_auth_record WHERE classify_id = ? AND source = ? AND sub_source = ? ORDER BY data_permission_classify_no_auth_record_id desc LIMIT ?,? `
- _, err = o.Raw(sql, parentClassifyId, dataSource, excelSource).QueryRows(&parentRecordItems)
- addNoAuthRecordItems := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
- for _, v := range parentRecordItems {
- addNoAuthRecordItems = append(addNoAuthRecordItems, &ExcelInfoClassifyPermissionNoAuthRecord{
- ExcelInfoClassifyPermissionNoAuthRecordId: 0,
- Source: v.Source,
- OpUniqueCode: uniqueCode,
- ClassifyId: fmt.Sprint(classifyId),
- ClassifyName: classifyName,
- SysUserId: v.SysUserId,
- CreateTime: time.Now(),
- })
- }
- // 添加待配置项
- if len(addNoAuthRecordItems) > 0 {
- _, err = o.InsertMulti(500, addNoAuthRecordItems)
- if err != nil {
- return
- }
- }
- }
- // 添加授权记录
- {
- // 获取父级分类已经授权的用户
- parentClassifyPermissionList := make([]*ExcelClassifyPermission, 0)
- sql = `SELECT * FROM excel_classify_permission WHERE source = ? AND excel_classify_id = ? `
- _, err = o.Raw(sql, excelSource, parentClassifyId).QueryRows(&parentClassifyPermissionList)
- if err != nil {
- return
- }
- addList := make([]*ExcelClassifyPermission, 0)
- for _, v := range parentClassifyPermissionList {
- // 如果不存在,那么就提那家
- addList = append(addList, &ExcelClassifyPermission{
- //PermissionId: 0,
- ExcelClassifyId: int32(classifyId),
- Source: int32(excelSource),
- SysUserId: v.SysUserId,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- })
- }
- // 添加待配置项
- if len(addList) > 0 {
- _, err = o.InsertMulti(500, addList)
- if err != nil {
- return
- }
- }
- }
- return
- }
- // ExcelInfoPermissionNoAuthRecord
- // @Description: ETA表格数据权限未授权记录表
- type ExcelInfoPermissionNoAuthRecord struct {
- ExcelInfoPermissionNoAuthRecordId int64 `json:"excel_info_permission_no_auth_record_id" orm:"column(excel_info_permission_no_auth_record_id);pk"` // 资产数据操作记录id
- OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
- Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
- ExcelInfoId int32 `json:"excel_info_id"` // 指标id
- ExcelName string `json:"excel_name"` // 图表名称
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- CreateTime time.Time `json:"create_time"` // 创建时间
- }
- // AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList
- // @Description: 添加未授权用户记录
- // @author: Roc
- // @datetime 2024-04-07 15:25:49
- // @param source int
- // @param excelSource int
- // @param dataList []DataItem
- // @param noAuthUserIdList []int 未授权用户
- // @param authUserIdList []int 已授权用户
- // @param uniqueCode
- // @param content string
- // @param opUserId int
- // @return err error
- func AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(source, excelSource 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
- }
- o, err := orm.NewOrmUsingDB("data").Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = o.Rollback()
- } else {
- _ = o.Commit()
- }
- }()
- // 根据指标获取已经存在的未授权记录
- var existList []*ExcelInfoPermissionNoAuthRecord
- sql := `SELECT * FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
- _, err = o.Raw(sql, excelSource, dataIdList).QueryRows(&existList)
- if err != nil {
- return
- }
- // 已经标记了的数据
- existMap := make(map[int32]map[string]*ExcelInfoPermissionNoAuthRecord)
- for _, v := range existList {
- tmpUserExistMap, ok := existMap[v.SysUserId]
- if !ok {
- tmpUserExistMap = make(map[string]*ExcelInfoPermissionNoAuthRecord)
- }
- key := fmt.Sprint(v.Source, "_", v.ExcelInfoId)
- tmpUserExistMap[key] = v
- existMap[v.SysUserId] = tmpUserExistMap
- }
- addMessageList := make([]*DataPermissionMessage, 0)
- addRecordList := make([]*ExcelInfoPermissionNoAuthRecord, 0)
- for _, userId := range noAuthUserIdList {
- isAdd := false
- tmpUserExistMap, userExistOk := existMap[int32(userId)]
- for _, dataItem := range dataList {
- // 判断是否已经存在,已经存在就过滤
- if userExistOk {
- key := fmt.Sprint(excelSource, "_", dataItem.DataId)
- _, ok := tmpUserExistMap[key]
- if ok {
- continue
- }
- }
- isAdd = true
- addRecordList = append(addRecordList, &ExcelInfoPermissionNoAuthRecord{
- ExcelInfoPermissionNoAuthRecordId: 0,
- OpUniqueCode: uniqueCode,
- Source: int32(excelSource),
- ExcelInfoId: int32(dataItem.DataId),
- ExcelName: 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(excelSource),
- OpUniqueCode: uniqueCode,
- IsRead: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- // 添加消息
- if len(addMessageList) > 0 {
- _, err = o.InsertMulti(500, addMessageList)
- if err != nil {
- return
- }
- }
- // 添加记录
- if len(addRecordList) > 0 {
- _, err = o.InsertMulti(500, addRecordList)
- if err != nil {
- return
- }
- }
- // 已经授权了的用户,需要删除未授权记录
- authUserIdNum := len(authUserIdList)
- if authUserIdNum > 0 {
- sql = `DELETE FROM excel_info_permission_no_auth_record WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(authUserIdNum) + `) AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
- _, err = o.Raw(sql, excelSource, authUserIdList, dataIdList).Exec()
- }
- return
- }
- // DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList
- // @Description: 根据来源和数据id列表删除记录
- // @author: Roc
- // @datetime 2024-04-07 14:47:37
- // @param excelSource int
- // @param dataIdList []string
- // @return err error
- func DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(excelSource int, dataIdList []string) (err error) {
- num := len(dataIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := `DELETE FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
- _, err = o.Raw(sql, excelSource, dataIdList).Exec()
- return
- }
- func GetExcelInfoDataPermissionNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
- o := orm.NewOrmUsingDB("data")
- // 获取总数
- sql := `SELECT count(1) AS total FROM excel_info_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
- err = o.Raw(sql, userId, excelSource).QueryRow(&total)
- if err != nil {
- return
- }
- sql = `SELECT excel_info_permission_no_auth_record_id as data_permission_no_auth_record_id,op_unique_code,source as sub_source,excel_info_id as data_id,excel_name as data_name,sys_user_id,create_time FROM excel_info_permission_no_auth_record WHERE sys_user_id = ? AND source = ? ORDER BY excel_info_permission_no_auth_record_id desc LIMIT ?,? `
- _, err = o.Raw(sql, userId, excelSource, startSize, pageSize).QueryRows(&items)
- return
- }
- type ExcelInfoClassifyPermissionNoAuthRecord struct {
- ExcelInfoClassifyPermissionNoAuthRecordId int64 `json:"excel_info_classify_permission_no_auth_record_id" orm:"column(excel_info_classify_permission_no_auth_record_id);pk"` // 资产分类数据操作记录id
- Source int32 `json:"source"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
- OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
- ClassifyId string `json:"classify_id"` // ETA表格资产分类id
- ClassifyName string `json:"classify_name"` // ETA表格资产分类名称
- SysUserId int32 `json:"sys_user_id"` // 系统用户id
- CreateTime time.Time `json:"create_time"` // 创建时间
- }
- // AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList
- // @Description: 根据分类添加用户分类未授权记录
- // @author: Roc
- // @datetime 2024-04-07 16:44:21
- // @param source int
- // @param excelSource int
- // @param classifyInfoList []ClassifyDataItem
- // @param noAuthUserIdList []int
- // @param uniqueCode string
- // @param content string
- // @param opUserId int
- // @return err error
- func AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, excelSource int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
- num := len(classifyInfoList)
- if num <= 0 {
- return
- }
- // 分类id
- classifyIdList := make([]int, 0)
- for _, v := range classifyInfoList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- userNum := len(noAuthUserIdList)
- if userNum <= 0 {
- return
- }
- o, err := orm.NewOrmUsingDB("data").Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = o.Rollback()
- } else {
- _ = o.Commit()
- }
- }()
- // 根据分类获取已经存在的未授权记录
- var existList []*ExcelInfoClassifyPermissionNoAuthRecord
- sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
- _, err = o.Raw(sql, excelSource, classifyIdList).QueryRows(&existList)
- if err != nil {
- return
- }
- // 已经标记了的数据
- existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
- for _, v := range existList {
- tmpUserExistMap, ok := existMap[v.SysUserId]
- if !ok {
- tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
- }
- key := fmt.Sprint(v.Source, "_", v.ClassifyId)
- tmpUserExistMap[key] = v
- existMap[v.SysUserId] = tmpUserExistMap
- }
- addMessageList := make([]*DataPermissionMessage, 0)
- addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
- for _, userId := range noAuthUserIdList {
- isAdd := false
- tmpUserExistMap, userExistOk := existMap[int32(userId)]
- for _, dataItem := range classifyInfoList {
- // 判断是否已经存在,已经存在就过滤
- if userExistOk {
- key := fmt.Sprint(excelSource, "_", dataItem.ClassifyId)
- _, ok := tmpUserExistMap[key]
- if ok {
- continue
- }
- }
- isAdd = true
- addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
- ExcelInfoClassifyPermissionNoAuthRecordId: 0,
- Source: int32(excelSource),
- 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(excelSource),
- OpUniqueCode: uniqueCode,
- IsRead: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- // 添加消息
- if len(addMessageList) > 0 {
- _, err = o.InsertMulti(500, addMessageList)
- if err != nil {
- return
- }
- }
- // 添加记录
- if len(addRecordList) > 0 {
- _, err = o.InsertMulti(500, addRecordList)
- if err != nil {
- return
- }
- }
- // 移除已经公开了的分类权限,需要删除未授权记录
- authUserIdNum := len(classifyIdList)
- if authUserIdNum > 0 {
- sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
- _, err = o.Raw(sql, excelSource, classifyIdList).Exec()
- }
- return
- }
- // AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList
- // @Description: 根据用户添加用户分类未授权记录
- // @author: Roc
- // @datetime 2024-04-07 20:12:44
- // @param source int
- // @param excelSource int
- // @param noAuthClassifyMap map[int]ClassifyDataItem
- // @param configUserIdList []int
- // @param uniqueCode string
- // @param content string
- // @param opUserId int
- // @return err error
- func AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList(source, excelSource int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
- // 当前配置用户
- configUserNum := len(configUserIdList)
- if configUserNum <= 0 {
- return
- }
- //// 总共的涉密分类
- //noAuthClassifyInfoNum := len(noAuthClassifyMap)
- //if noAuthClassifyInfoNum <= 0 {
- // return
- //}
- o, err := orm.NewOrmUsingDB("data").Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = o.Rollback()
- } else {
- _ = o.Commit()
- }
- }()
- // 根据当前配置用户获取已经存在的未授权记录
- var existList []*ExcelInfoClassifyPermissionNoAuthRecord
- sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
- _, err = o.Raw(sql, excelSource, configUserIdList).QueryRows(&existList)
- if err != nil {
- return
- }
- // 已经标记了的数据
- existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
- delRecordIdMap := make(map[int64]int64)
- for _, v := range existList {
- tmpUserExistMap, ok := existMap[v.SysUserId]
- if !ok {
- tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
- }
- tmpUserExistMap[v.ClassifyId] = v
- existMap[v.SysUserId] = tmpUserExistMap
- // 已经配置了的记录id
- delRecordIdMap[v.ExcelInfoClassifyPermissionNoAuthRecordId] = v.ExcelInfoClassifyPermissionNoAuthRecordId
- }
- addMessageList := make([]*DataPermissionMessage, 0)
- addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 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.ExcelInfoClassifyPermissionNoAuthRecordId)
- continue
- }
- }
- isAdd = true
- addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
- ExcelInfoClassifyPermissionNoAuthRecordId: 0,
- Source: int32(excelSource),
- 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(excelSource),
- OpUniqueCode: uniqueCode,
- IsRead: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- // 添加消息
- if len(addMessageList) > 0 {
- _, err = o.InsertMulti(500, addMessageList)
- if err != nil {
- return
- }
- }
- // 添加记录
- if len(addRecordList) > 0 {
- _, err = o.InsertMulti(500, addRecordList)
- 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 excel_info_classify_permission_no_auth_record WHERE excel_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
- _, err = o.Raw(sql, delRecordIdList).Exec()
- }
- return
- }
- // DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList
- // @Description: 根据来源和删除分类授权记录
- // @author: Roc
- // @datetime 2024-04-07 14:47:37
- // @param source int
- // @param excelSource int
- // @return err error
- func DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(excelSource int) (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ?`
- _, err = o.Raw(sql, excelSource).Exec()
- return
- }
- // GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId
- // @Description: 根据用户获取未授权的资产分类记录
- // @author: Roc
- // @datetime 2024-04-07 20:14:49
- // @param userId int
- // @param source int
- // @param subSource int
- // @param startSize int
- // @param pageSize int
- // @return total int
- // @return items []*DataPermissionClassifyNoAuthRecord
- // @return err error
- func GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
- o := orm.NewOrmUsingDB("data")
- // 获取总数
- sql := `SELECT count(1) AS total FROM excel_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
- err = o.Raw(sql, userId, excelSource).QueryRow(&total)
- if err != nil {
- return
- }
- sql = `SELECT excel_info_classify_permission_no_auth_record_id as data_permission_classify_no_auth_record_id,source as sub_source,op_unique_code,classify_id,classify_name,sys_user_id,create_time FROM excel_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND source = ? ORDER BY excel_info_classify_permission_no_auth_record_id desc LIMIT ?,? `
- _, err = o.Raw(sql, userId, excelSource, startSize, pageSize).QueryRows(&items)
- return
- }
- // ExcelInfoPermissionAdminAuth 含创建人的表格权限
- type ExcelInfoPermissionAdminAuth struct {
- ExcelInfoPermission
- ExcelName string `json:"excel_name"` // 表格名称
- UniqueCode string `json:"unique_code"` // 唯一编码
- CreateUserId int `json:"create_user_id"` // 创建人ID
- }
- // GetAdminAuthExcelInfoPermission 获取用户有权限的表格
- func GetAdminAuthExcelInfoPermission(source, adminId int, keywords string) (items []*ExcelInfoPermissionAdminAuth, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `SELECT a.*, b.sys_user_id AS create_user_id, b.excel_name, b.unique_code FROM excel_info_permission AS a
- JOIN excel_info AS b ON a.excel_info_id = b.excel_info_id
- WHERE a.source = ? AND (b.sys_user_id = ? OR a.sys_user_id = ?)`
- var pars []interface{}
- pars = append(pars, source, adminId, adminId)
- if keywords != "" {
- sql += ` AND b.excel_name LIKE ?`
- pars = append(pars, keywords)
- }
- sql += ` ORDER BY a.create_time ASC`
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- func ClearAndSetExcelInfoPermission(source, excelInfoId int, permissions []*ExcelInfoPermission) (err error) {
- if excelInfoId <= 0 {
- return
- }
- tx, e := orm.NewOrmUsingDB("data").Begin()
- if e != nil {
- err = fmt.Errorf("orm begin err: %v", e)
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- sql := `DELETE FROM excel_info_permission WHERE excel_info_id = ? AND source = ?`
- _, e = tx.Raw(sql, excelInfoId, source).Exec()
- if e != nil {
- err = fmt.Errorf("clear permission err: %v", e)
- return
- }
- if len(permissions) > 0 {
- _, e = tx.InsertMulti(500, permissions)
- if e != nil {
- err = fmt.Errorf("insert permissions err: %v", e)
- return
- }
- }
- return
- }
- func GetExcelPermissionBySourceAndId(excelId, source int) (items []*ExcelInfoPermission, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id = ?`
- _, err = o.Raw(sql, source, excelId).QueryRows(&items)
- return
- }
- func GetExcelPermissionByExcelIdAndUserId(excelId, userId int) (items []*ExcelInfoPermission, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := `SELECT * FROM excel_info_permission WHERE excel_info_id = ? AND sys_user_id = ?`
- _, err = o.Raw(sql, excelId, userId).QueryRows(&items)
- return
- }
|