excel.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. package data_manage_permission
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. )
  9. type ExcelInfoPermission struct {
  10. ExcelInfoPermissionId int64 `json:"excel_info_permission_id" orm:"column(excel_info_permission_id);pk" gorm:"primaryKey" `
  11. ExcelInfoId int32 `json:"excel_info_id"` // ETA表格id
  12. Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
  13. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  14. ModifyTime time.Time `json:"modify_time"` // 变更时间
  15. CreateTime time.Time `json:"create_time"` // 关系建立时间
  16. PermissionType int `json:"permission_type"` // 权限类型: 0-默认; 1-查看; 2-编辑
  17. }
  18. type ExcelClassifyPermission struct {
  19. ExcelClassifyPermissionId int64 `json:"excel_classify_permission_id" orm:"column(excel_classify_permission_id);pk" gorm:"primaryKey" `
  20. ExcelClassifyId int32 `json:"excel_classify_id"` // 分类id
  21. Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
  22. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  23. ModifyTime time.Time `json:"modify_time"` // 变更时间
  24. CreateTime time.Time `json:"create_time"` // 关系建立时间
  25. }
  26. // SetIsPermissionByExcelClassifyIdList
  27. // @Description: 设置表格分类是否涉密
  28. // @author: Roc
  29. // @datetime 2024-04-01 10:53:23
  30. // @param classifyIdList []int
  31. // @param excelClassifySource int
  32. // @return err error
  33. func SetIsPermissionByExcelClassifyIdList(classifyIdList []int, excelClassifySource int) (err error) {
  34. num := len(classifyIdList)
  35. o := global.DmSQL["data"].Begin()
  36. defer func() {
  37. if err != nil {
  38. _ = o.Rollback()
  39. } else {
  40. _ = o.Commit()
  41. }
  42. }()
  43. //// 获取已经配置涉密的分类权限
  44. //excelClassifyList := make([]*ExcelClassify, 0)
  45. //sql := `SELECT * FROM excel_classify WHERE is_join_permission = ? `
  46. //_, err = o.Raw(sql, 1).QueryRows(&excelClassifyList)
  47. //if err != nil {
  48. // return
  49. //}
  50. //excelClassifyMap := make(map[int]*ExcelClassify)
  51. //for _, v := range excelClassifyList {
  52. // excelClassifyMap[v.ClassifyId] = v
  53. //}
  54. // 先将所有已经设置了涉密的分类设置为不涉密
  55. sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND source = ?`
  56. err = o.Exec(sql, 0, excelClassifySource).Error
  57. if err != nil {
  58. return
  59. }
  60. if num > 0 {
  61. // 将对应的分类设置为涉密
  62. sql = `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id in (` + utils.GetOrmInReplace(num) + `) `
  63. err = o.Exec(sql, 1, excelClassifySource, classifyIdList).Error
  64. if err != nil {
  65. return
  66. }
  67. }
  68. return
  69. }
  70. // SetPermissionByExcelIdList
  71. // @Description: 根据表格ID列表设置表格的用户权限
  72. // @author: Roc
  73. // @datetime 2024-04-01 10:26:17
  74. // @param excelIdList []string
  75. // @param userIdList []int
  76. // @param source int
  77. // @return err error
  78. func SetPermissionByExcelIdList(excelIdList []string, userIdList []int, source int) (err error) {
  79. excelNum := len(excelIdList)
  80. if excelNum <= 0 {
  81. return
  82. }
  83. o := global.DmSQL["data"].Begin()
  84. defer func() {
  85. if err != nil {
  86. _ = o.Rollback()
  87. } else {
  88. _ = o.Commit()
  89. }
  90. }()
  91. // 获取已经配置的表格权限用户
  92. excelInfoPermissionList := make([]*ExcelInfoPermission, 0)
  93. sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
  94. err = o.Raw(sql, source, excelIdList).Scan(&excelInfoPermissionList).Error
  95. if err != nil {
  96. return
  97. }
  98. excelInfoPermissionMap := make(map[string]*ExcelInfoPermission)
  99. for _, v := range excelInfoPermissionList {
  100. excelInfoPermissionMap[fmt.Sprint(v.ExcelInfoId, "_", v.SysUserId)] = v
  101. }
  102. // 标记表格是否纳入权限管控
  103. {
  104. // 默认 标记表格为纳入权限管控
  105. isJoinPermission := 1
  106. // 用户不选的情况下,说明是要给这些表格移除权限管控
  107. if len(userIdList) <= 0 {
  108. // 标记表格为不纳入权限管控
  109. isJoinPermission = 0
  110. }
  111. sql = `UPDATE excel_info SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
  112. err = global.DmSQL["data"].Exec(sql, isJoinPermission, source, excelIdList).Error
  113. if err != nil {
  114. return
  115. }
  116. }
  117. // 待添加的配置项
  118. addList := make([]*ExcelInfoPermission, 0)
  119. // 遍历待配置的表格和用户,筛选出需要添加的配置项
  120. for _, excelInfoIdStr := range excelIdList {
  121. excelInfoId, tmpErr := strconv.ParseInt(excelInfoIdStr, 10, 64)
  122. if tmpErr != nil {
  123. err = tmpErr
  124. return
  125. }
  126. for _, userId := range userIdList {
  127. key := fmt.Sprint(excelInfoId, "_", userId)
  128. if _, ok := excelInfoPermissionMap[key]; ok {
  129. // 如果存在那么就移除,说明不需要处理了
  130. delete(excelInfoPermissionMap, key)
  131. } else {
  132. // 如果不存在,那么就添加
  133. addList = append(addList, &ExcelInfoPermission{
  134. //PermissionId: 0,
  135. ExcelInfoId: int32(excelInfoId),
  136. SysUserId: int32(userId),
  137. Source: int32(source),
  138. ModifyTime: time.Now(),
  139. CreateTime: time.Now(),
  140. })
  141. }
  142. }
  143. }
  144. // 添加待配置项
  145. if len(addList) > 0 {
  146. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  147. if err != nil {
  148. return
  149. }
  150. }
  151. // 移除废弃的配置项
  152. {
  153. // 待移除的配置项
  154. deletePermissionIdList := make([]int64, 0)
  155. for _, v := range excelInfoPermissionMap {
  156. deletePermissionIdList = append(deletePermissionIdList, v.ExcelInfoPermissionId)
  157. }
  158. deletePermissionIdNum := len(deletePermissionIdList)
  159. if deletePermissionIdNum > 0 {
  160. sql = "DELETE FROM excel_info_permission WHERE excel_info_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  161. err = o.Exec(sql, deletePermissionIdList).Error
  162. if err != nil {
  163. return
  164. }
  165. }
  166. }
  167. return
  168. }
  169. // SetPermissionByExcelClassifyIdList
  170. // @Description: 根据表格分类ID列表设置分类的用户权限
  171. // @author: Roc
  172. // @datetime 2024-03-28 14:53:04
  173. // @param classifyIdList []int
  174. // @param userIdList []int
  175. // @return err error
  176. func SetPermissionByExcelClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
  177. userNum := len(userIdList)
  178. if userNum <= 0 {
  179. return
  180. }
  181. o := global.DmSQL["data"].Begin()
  182. defer func() {
  183. if err != nil {
  184. _ = o.Rollback()
  185. } else {
  186. _ = o.Commit()
  187. }
  188. }()
  189. // 获取当前选择用户已经配置的表格分类权限
  190. classifyPermissionList := make([]*ExcelClassifyPermission, 0)
  191. sql := `SELECT * FROM excel_classify_permission WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
  192. err = o.Raw(sql, classifyType, userIdList).Scan(&classifyPermissionList).Error
  193. if err != nil {
  194. return
  195. }
  196. classifyPermissionMap := make(map[string]*ExcelClassifyPermission)
  197. for _, v := range classifyPermissionList {
  198. classifyPermissionMap[fmt.Sprint(v.ExcelClassifyId, "_", v.SysUserId)] = v
  199. }
  200. // 待添加的配置项
  201. addList := make([]*ExcelClassifyPermission, 0)
  202. // 遍历待配置的表格和用户,筛选出需要添加的配置项
  203. for _, userId := range userIdList {
  204. for _, classifyId := range classifyIdList {
  205. key := fmt.Sprint(classifyId, "_", userId)
  206. if _, ok := classifyPermissionMap[key]; ok {
  207. // 如果存在那么就移除,说明不需要处理了
  208. delete(classifyPermissionMap, key)
  209. } else {
  210. // 如果不存在,那么就提那家
  211. addList = append(addList, &ExcelClassifyPermission{
  212. //PermissionId: 0,
  213. ExcelClassifyId: int32(classifyId),
  214. Source: int32(classifyType),
  215. SysUserId: int32(userId),
  216. ModifyTime: time.Now(),
  217. CreateTime: time.Now(),
  218. })
  219. }
  220. }
  221. }
  222. // 添加待配置项
  223. if len(addList) > 0 {
  224. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  225. if err != nil {
  226. return
  227. }
  228. }
  229. // 移除废弃的配置项
  230. {
  231. // 获取移除的配置项
  232. deletePermissionIdList := make([]int64, 0)
  233. for _, v := range classifyPermissionMap {
  234. deletePermissionIdList = append(deletePermissionIdList, v.ExcelClassifyPermissionId)
  235. }
  236. deletePermissionIdNum := len(deletePermissionIdList)
  237. if deletePermissionIdNum > 0 {
  238. sql = "DELETE FROM excel_classify_permission WHERE excel_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  239. err = o.Exec(sql, deletePermissionIdList).Error
  240. if err != nil {
  241. return
  242. }
  243. }
  244. }
  245. return
  246. }
  247. // GetPermissionExcelClassifyIdListByUserId
  248. // @Description: 根据用户ID获取已经配置的分类id列表
  249. // @author: Roc
  250. // @datetime 2024-03-29 16:24:46
  251. // @param userId int
  252. // @param classifyType int
  253. // @return excelClassifyIdList []int
  254. // @return err error
  255. func GetPermissionExcelClassifyIdListByUserId(userId int, classifyType int) (excelClassifyIdList []int, err error) {
  256. sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE source = ? AND sys_user_id = ? `
  257. err = global.DmSQL["data"].Raw(sql, classifyType, userId).Scan(&excelClassifyIdList).Error
  258. return
  259. }
  260. // GetPermissionUserIdListByExcelId
  261. // @Description: 根据表格ID获取已经配置的用户id列表
  262. // @author: Roc
  263. // @datetime 2024-03-29 16:24:46
  264. // @param dataId int
  265. // @param source int
  266. // @return userIdList []int
  267. // @return err error
  268. func GetPermissionUserIdListByExcelId(dataId int, source int) (userIdList []int, err error) {
  269. sql := `SELECT sys_user_id FROM excel_info_permission WHERE source = ? AND excel_info_id= ? `
  270. err = global.DmSQL["data"].Raw(sql, source, dataId).Scan(&userIdList).Error
  271. return
  272. }
  273. // GetPermissionUserIdListByExcelClassifyId
  274. // @Description: 根据表格分类ID获取已经配置的用户id列表
  275. // @author: Roc
  276. // @datetime 2024-03-29 16:24:46
  277. // @param classifyId int
  278. // @param source int
  279. // @return userIdList []int
  280. // @return err error
  281. func GetPermissionUserIdListByExcelClassifyId(classifyId int, source int) (userIdList []int, err error) {
  282. sql := `SELECT sys_user_id FROM excel_classify_permission WHERE source = ? AND excel_classify_id= ? `
  283. err = global.DmSQL["data"].Raw(sql, source, classifyId).Scan(&userIdList).Error
  284. return
  285. }
  286. // GetPermissionExcelIdList
  287. // @Description: 获取用户权限的表格列表
  288. // @author: Roc
  289. // @datetime 2024-03-28 16:50:47
  290. // @param userId int
  291. // @param excelInfoId int
  292. // @return idList []int
  293. // @return err error
  294. func GetPermissionExcelIdList(userId, excelInfoId int) (idList []int, err error) {
  295. pars := []interface{}{userId}
  296. sql := `SELECT excel_info_id FROM excel_info_permission WHERE sys_user_id = ? `
  297. if excelInfoId > 0 {
  298. sql += ` AND excel_info_id = ? `
  299. pars = append(pars, excelInfoId)
  300. }
  301. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&idList).Error
  302. return
  303. }
  304. // GetPermissionExcelClassifyIdList
  305. // @Description: 获取用户权限的表格分类列表
  306. // @author: Roc
  307. // @datetime 2024-03-28 16:50:47
  308. // @param userId int
  309. // @param classifyId int
  310. // @return idList []int
  311. // @return err error
  312. func GetPermissionExcelClassifyIdList(userId, classifyId int) (idList []int, err error) {
  313. pars := []interface{}{userId}
  314. sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE sys_user_id = ? `
  315. if classifyId > 0 {
  316. sql += ` AND excel_classify_id = ? `
  317. pars = append(pars, classifyId)
  318. }
  319. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&idList).Error
  320. return
  321. }
  322. // InheritParentClassifyByExcelClassifyId
  323. // @Description: 继承父级分类的ETA表格权限信息
  324. // @author: Roc
  325. // @datetime 2024-04-07 21:02:51
  326. // @param dataSource int
  327. // @param excelSource int
  328. // @param classifyId int
  329. // @param parentClassifyId int
  330. // @param classifyName string
  331. // @param uniqueCode string
  332. // @return err error
  333. func InheritParentClassifyByExcelClassifyId(dataSource, excelSource, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
  334. o := global.DmSQL["data"].Begin()
  335. defer func() {
  336. if err != nil {
  337. _ = o.Rollback()
  338. } else {
  339. _ = o.Commit()
  340. }
  341. }()
  342. // 将对应的分类设置为涉密
  343. sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id = ? `
  344. err = o.Exec(sql, 1, excelSource, classifyId).Error
  345. if err != nil {
  346. return
  347. }
  348. // 添加未授权记录
  349. {
  350. // 获取父级未授权的用户记录
  351. var parentRecordItems []*ExcelInfoClassifyPermissionNoAuthRecord
  352. 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 ?,? `
  353. err = o.Raw(sql, parentClassifyId, dataSource, excelSource).Scan(&parentRecordItems).Error
  354. addNoAuthRecordItems := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  355. for _, v := range parentRecordItems {
  356. addNoAuthRecordItems = append(addNoAuthRecordItems, &ExcelInfoClassifyPermissionNoAuthRecord{
  357. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  358. Source: v.Source,
  359. OpUniqueCode: uniqueCode,
  360. ClassifyId: fmt.Sprint(classifyId),
  361. ClassifyName: classifyName,
  362. SysUserId: v.SysUserId,
  363. CreateTime: time.Now(),
  364. })
  365. }
  366. // 添加待配置项
  367. if len(addNoAuthRecordItems) > 0 {
  368. err = o.CreateInBatches(addNoAuthRecordItems, utils.MultiAddNum).Error
  369. if err != nil {
  370. return
  371. }
  372. }
  373. }
  374. // 添加授权记录
  375. {
  376. // 获取父级分类已经授权的用户
  377. parentClassifyPermissionList := make([]*ExcelClassifyPermission, 0)
  378. sql = `SELECT * FROM excel_classify_permission WHERE source = ? AND excel_classify_id = ? `
  379. err = o.Raw(sql, excelSource, parentClassifyId).Scan(&parentClassifyPermissionList).Error
  380. if err != nil {
  381. return
  382. }
  383. addList := make([]*ExcelClassifyPermission, 0)
  384. for _, v := range parentClassifyPermissionList {
  385. // 如果不存在,那么就提那家
  386. addList = append(addList, &ExcelClassifyPermission{
  387. //PermissionId: 0,
  388. ExcelClassifyId: int32(classifyId),
  389. Source: int32(excelSource),
  390. SysUserId: v.SysUserId,
  391. ModifyTime: time.Now(),
  392. CreateTime: time.Now(),
  393. })
  394. }
  395. // 添加待配置项
  396. if len(addList) > 0 {
  397. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  398. if err != nil {
  399. return
  400. }
  401. }
  402. }
  403. return
  404. }
  405. // ExcelInfoPermissionNoAuthRecord
  406. // @Description: ETA表格数据权限未授权记录表
  407. type ExcelInfoPermissionNoAuthRecord struct {
  408. ExcelInfoPermissionNoAuthRecordId int64 `json:"excel_info_permission_no_auth_record_id" orm:"column(excel_info_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产数据操作记录id
  409. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  410. Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
  411. ExcelInfoId int32 `json:"excel_info_id"` // 指标id
  412. ExcelName string `json:"excel_name"` // 图表名称
  413. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  414. CreateTime time.Time `json:"create_time"` // 创建时间
  415. }
  416. // AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList
  417. // @Description: 添加未授权用户记录
  418. // @author: Roc
  419. // @datetime 2024-04-07 15:25:49
  420. // @param source int
  421. // @param excelSource int
  422. // @param dataList []DataItem
  423. // @param noAuthUserIdList []int 未授权用户
  424. // @param authUserIdList []int 已授权用户
  425. // @param uniqueCode
  426. // @param content string
  427. // @param opUserId int
  428. // @return err error
  429. func AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(source, excelSource int, dataList []DataItem, noAuthUserIdList, authUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  430. num := len(dataList)
  431. if num <= 0 {
  432. return
  433. }
  434. dataIdList := make([]int, 0)
  435. for _, v := range dataList {
  436. dataIdList = append(dataIdList, v.DataId)
  437. }
  438. userNum := len(noAuthUserIdList)
  439. if userNum <= 0 {
  440. return
  441. }
  442. o := global.DmSQL["data"].Begin()
  443. defer func() {
  444. if err != nil {
  445. _ = o.Rollback()
  446. } else {
  447. _ = o.Commit()
  448. }
  449. }()
  450. // 根据指标获取已经存在的未授权记录
  451. var existList []*ExcelInfoPermissionNoAuthRecord
  452. sql := `SELECT * FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
  453. err = o.Raw(sql, excelSource, dataIdList).Scan(&existList).Error
  454. if err != nil {
  455. return
  456. }
  457. // 已经标记了的数据
  458. existMap := make(map[int32]map[string]*ExcelInfoPermissionNoAuthRecord)
  459. for _, v := range existList {
  460. tmpUserExistMap, ok := existMap[v.SysUserId]
  461. if !ok {
  462. tmpUserExistMap = make(map[string]*ExcelInfoPermissionNoAuthRecord)
  463. }
  464. key := fmt.Sprint(v.Source, "_", v.ExcelInfoId)
  465. tmpUserExistMap[key] = v
  466. existMap[v.SysUserId] = tmpUserExistMap
  467. }
  468. addMessageList := make([]*DataPermissionMessage, 0)
  469. addRecordList := make([]*ExcelInfoPermissionNoAuthRecord, 0)
  470. for _, userId := range noAuthUserIdList {
  471. isAdd := false
  472. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  473. for _, dataItem := range dataList {
  474. // 判断是否已经存在,已经存在就过滤
  475. if userExistOk {
  476. key := fmt.Sprint(excelSource, "_", dataItem.DataId)
  477. _, ok := tmpUserExistMap[key]
  478. if ok {
  479. continue
  480. }
  481. }
  482. isAdd = true
  483. addRecordList = append(addRecordList, &ExcelInfoPermissionNoAuthRecord{
  484. ExcelInfoPermissionNoAuthRecordId: 0,
  485. OpUniqueCode: uniqueCode,
  486. Source: int32(excelSource),
  487. ExcelInfoId: int32(dataItem.DataId),
  488. ExcelName: dataItem.DataName,
  489. SysUserId: int32(userId),
  490. CreateTime: time.Now(),
  491. })
  492. }
  493. // 有记录的话,需要添加消息
  494. if isAdd {
  495. addMessageList = append(addMessageList, &DataPermissionMessage{
  496. DataPermissionMessageId: 0,
  497. SendUserId: int32(opUserId),
  498. ReceiveUserId: int32(userId),
  499. Content: title,
  500. Remark: content,
  501. OpType: 3,
  502. Source: int32(source),
  503. SubSource: int32(excelSource),
  504. OpUniqueCode: uniqueCode,
  505. IsRead: 0,
  506. CreateTime: time.Now(),
  507. ModifyTime: time.Now(),
  508. })
  509. }
  510. }
  511. // 添加消息
  512. if len(addMessageList) > 0 {
  513. err = o.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  514. if err != nil {
  515. return
  516. }
  517. }
  518. // 添加记录
  519. if len(addRecordList) > 0 {
  520. err = o.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  521. if err != nil {
  522. return
  523. }
  524. }
  525. // 已经授权了的用户,需要删除未授权记录
  526. authUserIdNum := len(authUserIdList)
  527. if authUserIdNum > 0 {
  528. 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) + `)`
  529. err = o.Exec(sql, excelSource, authUserIdList, dataIdList).Error
  530. }
  531. return
  532. }
  533. // DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList
  534. // @Description: 根据来源和数据id列表删除记录
  535. // @author: Roc
  536. // @datetime 2024-04-07 14:47:37
  537. // @param excelSource int
  538. // @param dataIdList []string
  539. // @return err error
  540. func DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(excelSource int, dataIdList []string) (err error) {
  541. num := len(dataIdList)
  542. if num <= 0 {
  543. return
  544. }
  545. sql := `DELETE FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
  546. err = global.DmSQL["data"].Exec(sql, excelSource, dataIdList).Error
  547. return
  548. }
  549. func GetExcelInfoDataPermissionNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
  550. // 获取总数
  551. sql := `SELECT count(1) AS total FROM excel_info_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
  552. err = global.DmSQL["data"].Raw(sql, userId, excelSource).Scan(&total).Error
  553. if err != nil {
  554. return
  555. }
  556. 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 ?,? `
  557. err = global.DmSQL["data"].Raw(sql, userId, excelSource, startSize, pageSize).Scan(&items).Error
  558. return
  559. }
  560. type ExcelInfoClassifyPermissionNoAuthRecord struct {
  561. ExcelInfoClassifyPermissionNoAuthRecordId int64 `json:"excel_info_classify_permission_no_auth_record_id" orm:"column(excel_info_classify_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产分类数据操作记录id
  562. Source int32 `json:"source"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
  563. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  564. ClassifyId string `json:"classify_id"` // ETA表格资产分类id
  565. ClassifyName string `json:"classify_name"` // ETA表格资产分类名称
  566. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  567. CreateTime time.Time `json:"create_time"` // 创建时间
  568. }
  569. // AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList
  570. // @Description: 根据分类添加用户分类未授权记录
  571. // @author: Roc
  572. // @datetime 2024-04-07 16:44:21
  573. // @param source int
  574. // @param excelSource int
  575. // @param classifyInfoList []ClassifyDataItem
  576. // @param noAuthUserIdList []int
  577. // @param uniqueCode string
  578. // @param content string
  579. // @param opUserId int
  580. // @return err error
  581. func AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, excelSource int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  582. num := len(classifyInfoList)
  583. if num <= 0 {
  584. return
  585. }
  586. // 分类id
  587. classifyIdList := make([]int, 0)
  588. for _, v := range classifyInfoList {
  589. classifyIdList = append(classifyIdList, v.ClassifyId)
  590. }
  591. userNum := len(noAuthUserIdList)
  592. if userNum <= 0 {
  593. return
  594. }
  595. o := global.DmSQL["data"].Begin()
  596. defer func() {
  597. if err != nil {
  598. _ = o.Rollback()
  599. } else {
  600. _ = o.Commit()
  601. }
  602. }()
  603. // 根据分类获取已经存在的未授权记录
  604. var existList []*ExcelInfoClassifyPermissionNoAuthRecord
  605. sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
  606. err = o.Raw(sql, excelSource, classifyIdList).Scan(&existList).Error
  607. if err != nil {
  608. return
  609. }
  610. // 已经标记了的数据
  611. existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  612. for _, v := range existList {
  613. tmpUserExistMap, ok := existMap[v.SysUserId]
  614. if !ok {
  615. tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  616. }
  617. key := fmt.Sprint(v.Source, "_", v.ClassifyId)
  618. tmpUserExistMap[key] = v
  619. existMap[v.SysUserId] = tmpUserExistMap
  620. }
  621. addMessageList := make([]*DataPermissionMessage, 0)
  622. addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  623. for _, userId := range noAuthUserIdList {
  624. isAdd := false
  625. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  626. for _, dataItem := range classifyInfoList {
  627. // 判断是否已经存在,已经存在就过滤
  628. if userExistOk {
  629. key := fmt.Sprint(excelSource, "_", dataItem.ClassifyId)
  630. _, ok := tmpUserExistMap[key]
  631. if ok {
  632. continue
  633. }
  634. }
  635. isAdd = true
  636. addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
  637. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  638. Source: int32(excelSource),
  639. OpUniqueCode: uniqueCode,
  640. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  641. ClassifyName: dataItem.ClassifyName,
  642. SysUserId: int32(userId),
  643. CreateTime: time.Now(),
  644. })
  645. }
  646. // 有记录的话,需要添加消息
  647. if isAdd {
  648. addMessageList = append(addMessageList, &DataPermissionMessage{
  649. DataPermissionMessageId: 0,
  650. SendUserId: int32(opUserId),
  651. ReceiveUserId: int32(userId),
  652. Content: title,
  653. Remark: content,
  654. OpType: 4,
  655. Source: int32(source),
  656. SubSource: int32(excelSource),
  657. OpUniqueCode: uniqueCode,
  658. IsRead: 0,
  659. CreateTime: time.Now(),
  660. ModifyTime: time.Now(),
  661. })
  662. }
  663. }
  664. // 添加消息
  665. if len(addMessageList) > 0 {
  666. err = o.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  667. if err != nil {
  668. return
  669. }
  670. }
  671. // 添加记录
  672. if len(addRecordList) > 0 {
  673. err = o.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  674. if err != nil {
  675. return
  676. }
  677. }
  678. // 移除已经公开了的分类权限,需要删除未授权记录
  679. authUserIdNum := len(classifyIdList)
  680. if authUserIdNum > 0 {
  681. sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
  682. err = o.Exec(sql, excelSource, classifyIdList).Error
  683. }
  684. return
  685. }
  686. // AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList
  687. // @Description: 根据用户添加用户分类未授权记录
  688. // @author: Roc
  689. // @datetime 2024-04-07 20:12:44
  690. // @param source int
  691. // @param excelSource int
  692. // @param noAuthClassifyMap map[int]ClassifyDataItem
  693. // @param configUserIdList []int
  694. // @param uniqueCode string
  695. // @param content string
  696. // @param opUserId int
  697. // @return err error
  698. func AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList(source, excelSource int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  699. // 当前配置用户
  700. configUserNum := len(configUserIdList)
  701. if configUserNum <= 0 {
  702. return
  703. }
  704. //// 总共的涉密分类
  705. //noAuthClassifyInfoNum := len(noAuthClassifyMap)
  706. //if noAuthClassifyInfoNum <= 0 {
  707. // return
  708. //}
  709. o := global.DmSQL["data"].Begin()
  710. defer func() {
  711. if err != nil {
  712. _ = o.Rollback()
  713. } else {
  714. _ = o.Commit()
  715. }
  716. }()
  717. // 根据当前配置用户获取已经存在的未授权记录
  718. var existList []*ExcelInfoClassifyPermissionNoAuthRecord
  719. sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
  720. err = o.Raw(sql, excelSource, configUserIdList).Scan(&existList).Error
  721. if err != nil {
  722. return
  723. }
  724. // 已经标记了的数据
  725. existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  726. delRecordIdMap := make(map[int64]int64)
  727. for _, v := range existList {
  728. tmpUserExistMap, ok := existMap[v.SysUserId]
  729. if !ok {
  730. tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  731. }
  732. tmpUserExistMap[v.ClassifyId] = v
  733. existMap[v.SysUserId] = tmpUserExistMap
  734. // 已经配置了的记录id
  735. delRecordIdMap[v.ExcelInfoClassifyPermissionNoAuthRecordId] = v.ExcelInfoClassifyPermissionNoAuthRecordId
  736. }
  737. addMessageList := make([]*DataPermissionMessage, 0)
  738. addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  739. for _, userId := range configUserIdList {
  740. isAdd := false
  741. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  742. for _, dataItem := range noAuthClassifyMap {
  743. // 判断是否已经存在,已经存在就过滤
  744. if userExistOk {
  745. key := fmt.Sprint(dataItem.ClassifyId)
  746. tmpUserRecord, ok := tmpUserExistMap[key]
  747. if ok {
  748. delete(delRecordIdMap, tmpUserRecord.ExcelInfoClassifyPermissionNoAuthRecordId)
  749. continue
  750. }
  751. }
  752. isAdd = true
  753. addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
  754. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  755. Source: int32(excelSource),
  756. OpUniqueCode: uniqueCode,
  757. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  758. ClassifyName: dataItem.ClassifyName,
  759. SysUserId: int32(userId),
  760. CreateTime: time.Now(),
  761. })
  762. }
  763. // 有记录的话,需要添加消息
  764. if isAdd {
  765. addMessageList = append(addMessageList, &DataPermissionMessage{
  766. DataPermissionMessageId: 0,
  767. SendUserId: int32(opUserId),
  768. ReceiveUserId: int32(userId),
  769. Content: title,
  770. Remark: content,
  771. OpType: 4,
  772. Source: int32(source),
  773. SubSource: int32(excelSource),
  774. OpUniqueCode: uniqueCode,
  775. IsRead: 0,
  776. CreateTime: time.Now(),
  777. ModifyTime: time.Now(),
  778. })
  779. }
  780. }
  781. // 添加消息
  782. if len(addMessageList) > 0 {
  783. err = o.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  784. if err != nil {
  785. return
  786. }
  787. }
  788. // 添加记录
  789. if len(addRecordList) > 0 {
  790. err = o.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  791. if err != nil {
  792. return
  793. }
  794. }
  795. // 需要删除未授权记录
  796. delRecordIdNum := len(delRecordIdMap)
  797. if delRecordIdNum > 0 {
  798. delRecordIdList := make([]int64, 0)
  799. for _, v := range delRecordIdMap {
  800. delRecordIdList = append(delRecordIdList, v)
  801. }
  802. sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE excel_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
  803. err = o.Exec(sql, delRecordIdList).Error
  804. }
  805. return
  806. }
  807. // DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList
  808. // @Description: 根据来源和删除分类授权记录
  809. // @author: Roc
  810. // @datetime 2024-04-07 14:47:37
  811. // @param source int
  812. // @param excelSource int
  813. // @return err error
  814. func DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(excelSource int) (err error) {
  815. sql := `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ?`
  816. err = global.DmSQL["data"].Exec(sql, excelSource).Error
  817. return
  818. }
  819. // GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId
  820. // @Description: 根据用户获取未授权的资产分类记录
  821. // @author: Roc
  822. // @datetime 2024-04-07 20:14:49
  823. // @param userId int
  824. // @param source int
  825. // @param subSource int
  826. // @param startSize int
  827. // @param pageSize int
  828. // @return total int
  829. // @return items []*DataPermissionClassifyNoAuthRecord
  830. // @return err error
  831. func GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
  832. // 获取总数
  833. sql := `SELECT count(1) AS total FROM excel_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
  834. err = global.DmSQL["data"].Raw(sql, userId, excelSource).Scan(&total).Error
  835. if err != nil {
  836. return
  837. }
  838. 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 ?,? `
  839. err = global.DmSQL["data"].Raw(sql, userId, excelSource, startSize, pageSize).Scan(&items).Error
  840. return
  841. }
  842. // ExcelInfoPermissionAdminAuth 含创建人的表格权限
  843. type ExcelInfoPermissionAdminAuth struct {
  844. ExcelInfoPermission
  845. ExcelName string `json:"excel_name"` // 表格名称
  846. UniqueCode string `json:"unique_code"` // 唯一编码
  847. CreateUserId int `json:"create_user_id"` // 创建人ID
  848. }
  849. // GetAdminAuthExcelInfoPermission 获取用户有权限的表格
  850. func GetAdminAuthExcelInfoPermission(source, adminId int, keywords string) (items []*ExcelInfoPermissionAdminAuth, err error) {
  851. sql := `SELECT a.*, b.sys_user_id AS create_user_id, b.excel_name, b.unique_code FROM excel_info_permission AS a
  852. JOIN excel_info AS b ON a.excel_info_id = b.excel_info_id
  853. WHERE a.source = ? AND (b.sys_user_id = ? OR a.sys_user_id = ?)`
  854. var pars []interface{}
  855. pars = append(pars, source, adminId, adminId)
  856. if keywords != "" {
  857. sql += ` AND b.excel_name LIKE ?`
  858. pars = append(pars, keywords)
  859. }
  860. sql += ` ORDER BY a.create_time ASC`
  861. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error
  862. return
  863. }
  864. func ClearAndSetExcelInfoPermission(source, excelInfoId int, permissions []*ExcelInfoPermission) (err error) {
  865. if excelInfoId <= 0 {
  866. return
  867. }
  868. tx := global.DmSQL["data"].Begin()
  869. defer func() {
  870. if err != nil {
  871. _ = tx.Rollback()
  872. return
  873. }
  874. _ = tx.Commit()
  875. }()
  876. sql := `DELETE FROM excel_info_permission WHERE excel_info_id = ? AND source = ?`
  877. err = tx.Exec(sql, excelInfoId, source).Error
  878. if err != nil {
  879. err = fmt.Errorf("clear permission err: %v", err)
  880. return
  881. }
  882. if len(permissions) > 0 {
  883. err = tx.CreateInBatches(permissions, utils.MultiAddNum).Error
  884. if err != nil {
  885. err = fmt.Errorf("insert permissions err: %v", err)
  886. return
  887. }
  888. }
  889. return
  890. }
  891. func GetExcelPermissionBySourceAndId(excelId, source int) (items []*ExcelInfoPermission, err error) {
  892. sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id = ?`
  893. err = global.DmSQL["data"].Raw(sql, source, excelId).Scan(&items).Error
  894. return
  895. }
  896. func GetExcelPermissionByExcelIdAndUserId(excelId, userId int) (items []*ExcelInfoPermission, err error) {
  897. sql := `SELECT * FROM excel_info_permission WHERE excel_info_id = ? AND sys_user_id = ?`
  898. err = global.DmSQL["data"].Raw(sql, excelId, userId).Scan(&items).Error
  899. return
  900. }