excel.go 32 KB

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