excel.go 34 KB

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