excel.go 34 KB

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