excel.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. 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, err := orm.NewOrmUsingDB("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.Raw(sql, 0, excelClassifySource).Exec()
  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.Raw(sql, 1, excelClassifySource, classifyIdList).Exec()
  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, err := orm.NewOrmUsingDB("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).QueryRows(&excelInfoPermissionList)
  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 = o.Raw(sql, isJoinPermission, source, excelIdList).Exec()
  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.InsertMulti(500, addList)
  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.Raw(sql, deletePermissionIdList).Exec()
  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, err := orm.NewOrmUsingDB("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).QueryRows(&classifyPermissionList)
  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.InsertMulti(500, addList)
  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.Raw(sql, deletePermissionIdList).Exec()
  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. o := orm.NewOrmUsingDB("data")
  266. sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE source = ? AND sys_user_id = ? `
  267. _, err = o.Raw(sql, classifyType, userId).QueryRows(&excelClassifyIdList)
  268. return
  269. }
  270. // GetPermissionUserIdListByExcelId
  271. // @Description: 根据表格ID获取已经配置的用户id列表
  272. // @author: Roc
  273. // @datetime 2024-03-29 16:24:46
  274. // @param dataId int
  275. // @param source int
  276. // @return userIdList []int
  277. // @return err error
  278. func GetPermissionUserIdListByExcelId(dataId int, source int) (userIdList []int, err error) {
  279. o := orm.NewOrmUsingDB("data")
  280. sql := `SELECT sys_user_id FROM excel_info_permission WHERE source = ? AND excel_info_id= ? `
  281. _, err = o.Raw(sql, source, dataId).QueryRows(&userIdList)
  282. return
  283. }
  284. // GetPermissionUserIdListByExcelClassifyId
  285. // @Description: 根据表格分类ID获取已经配置的用户id列表
  286. // @author: Roc
  287. // @datetime 2024-03-29 16:24:46
  288. // @param classifyId int
  289. // @param source int
  290. // @return userIdList []int
  291. // @return err error
  292. func GetPermissionUserIdListByExcelClassifyId(classifyId int, source int) (userIdList []int, err error) {
  293. o := orm.NewOrmUsingDB("data")
  294. sql := `SELECT sys_user_id FROM excel_classify_permission WHERE source = ? AND excel_classify_id= ? `
  295. _, err = o.Raw(sql, source, classifyId).QueryRows(&userIdList)
  296. return
  297. }
  298. // GetPermissionExcelIdList
  299. // @Description: 获取用户权限的表格列表
  300. // @author: Roc
  301. // @datetime 2024-03-28 16:50:47
  302. // @param userId int
  303. // @param excelInfoId int
  304. // @return idList []int
  305. // @return err error
  306. func GetPermissionExcelIdList(userId, excelInfoId int) (idList []int, err error) {
  307. pars := []interface{}{userId}
  308. o := orm.NewOrmUsingDB("data")
  309. sql := `SELECT excel_info_id FROM excel_info_permission WHERE sys_user_id = ? `
  310. if excelInfoId > 0 {
  311. sql += ` AND excel_info_id = ? `
  312. pars = append(pars, excelInfoId)
  313. }
  314. _, err = o.Raw(sql, pars).QueryRows(&idList)
  315. return
  316. }
  317. // GetPermissionExcelClassifyIdList
  318. // @Description: 获取用户权限的表格分类列表
  319. // @author: Roc
  320. // @datetime 2024-03-28 16:50:47
  321. // @param userId int
  322. // @param classifyId int
  323. // @return idList []int
  324. // @return err error
  325. func GetPermissionExcelClassifyIdList(userId, classifyId int) (idList []int, err error) {
  326. pars := []interface{}{userId}
  327. o := orm.NewOrmUsingDB("data")
  328. sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE sys_user_id = ? `
  329. if classifyId > 0 {
  330. sql += ` AND excel_classify_id = ? `
  331. pars = append(pars, classifyId)
  332. }
  333. _, err = o.Raw(sql, pars).QueryRows(&idList)
  334. return
  335. }
  336. // InheritParentClassifyByExcelClassifyId
  337. // @Description: 继承父级分类的ETA表格权限信息
  338. // @author: Roc
  339. // @datetime 2024-04-07 21:02:51
  340. // @param dataSource int
  341. // @param excelSource int
  342. // @param classifyId int
  343. // @param parentClassifyId int
  344. // @param classifyName string
  345. // @param uniqueCode string
  346. // @return err error
  347. func InheritParentClassifyByExcelClassifyId(dataSource, excelSource, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
  348. o, err := orm.NewOrmUsingDB("data").Begin()
  349. if err != nil {
  350. return
  351. }
  352. defer func() {
  353. if err != nil {
  354. _ = o.Rollback()
  355. } else {
  356. _ = o.Commit()
  357. }
  358. }()
  359. // 将对应的分类设置为涉密
  360. sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id = ? `
  361. _, err = o.Raw(sql, 1, excelSource, classifyId).Exec()
  362. if err != nil {
  363. return
  364. }
  365. // 添加未授权记录
  366. {
  367. // 获取父级未授权的用户记录
  368. var parentRecordItems []*ExcelInfoClassifyPermissionNoAuthRecord
  369. 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 ?,? `
  370. _, err = o.Raw(sql, parentClassifyId, dataSource, excelSource).QueryRows(&parentRecordItems)
  371. addNoAuthRecordItems := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  372. for _, v := range parentRecordItems {
  373. addNoAuthRecordItems = append(addNoAuthRecordItems, &ExcelInfoClassifyPermissionNoAuthRecord{
  374. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  375. Source: v.Source,
  376. OpUniqueCode: uniqueCode,
  377. ClassifyId: fmt.Sprint(classifyId),
  378. ClassifyName: classifyName,
  379. SysUserId: v.SysUserId,
  380. CreateTime: time.Now(),
  381. })
  382. }
  383. // 添加待配置项
  384. if len(addNoAuthRecordItems) > 0 {
  385. _, err = o.InsertMulti(500, addNoAuthRecordItems)
  386. if err != nil {
  387. return
  388. }
  389. }
  390. }
  391. // 添加授权记录
  392. {
  393. // 获取父级分类已经授权的用户
  394. parentClassifyPermissionList := make([]*ExcelClassifyPermission, 0)
  395. sql = `SELECT * FROM excel_classify_permission WHERE source = ? AND excel_classify_id = ? `
  396. _, err = o.Raw(sql, excelSource, parentClassifyId).QueryRows(&parentClassifyPermissionList)
  397. if err != nil {
  398. return
  399. }
  400. addList := make([]*ExcelClassifyPermission, 0)
  401. for _, v := range parentClassifyPermissionList {
  402. // 如果不存在,那么就提那家
  403. addList = append(addList, &ExcelClassifyPermission{
  404. //PermissionId: 0,
  405. ExcelClassifyId: int32(classifyId),
  406. Source: int32(excelSource),
  407. SysUserId: v.SysUserId,
  408. ModifyTime: time.Now(),
  409. CreateTime: time.Now(),
  410. })
  411. }
  412. // 添加待配置项
  413. if len(addList) > 0 {
  414. _, err = o.InsertMulti(500, addList)
  415. if err != nil {
  416. return
  417. }
  418. }
  419. }
  420. return
  421. }
  422. // ExcelInfoPermissionNoAuthRecord
  423. // @Description: ETA表格数据权限未授权记录表
  424. type ExcelInfoPermissionNoAuthRecord struct {
  425. ExcelInfoPermissionNoAuthRecordId int64 `json:"excel_info_permission_no_auth_record_id" orm:"column(excel_info_permission_no_auth_record_id);pk"` // 资产数据操作记录id
  426. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  427. Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
  428. ExcelInfoId int32 `json:"excel_info_id"` // 指标id
  429. ExcelName string `json:"excel_name"` // 图表名称
  430. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  431. CreateTime time.Time `json:"create_time"` // 创建时间
  432. }
  433. // AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList
  434. // @Description: 添加未授权用户记录
  435. // @author: Roc
  436. // @datetime 2024-04-07 15:25:49
  437. // @param source int
  438. // @param excelSource int
  439. // @param dataList []DataItem
  440. // @param noAuthUserIdList []int 未授权用户
  441. // @param authUserIdList []int 已授权用户
  442. // @param uniqueCode
  443. // @param content string
  444. // @param opUserId int
  445. // @return err error
  446. func AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(source, excelSource int, dataList []DataItem, noAuthUserIdList, authUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  447. num := len(dataList)
  448. if num <= 0 {
  449. return
  450. }
  451. dataIdList := make([]int, 0)
  452. for _, v := range dataList {
  453. dataIdList = append(dataIdList, v.DataId)
  454. }
  455. userNum := len(noAuthUserIdList)
  456. if userNum <= 0 {
  457. return
  458. }
  459. o, err := orm.NewOrmUsingDB("data").Begin()
  460. if err != nil {
  461. return
  462. }
  463. defer func() {
  464. if err != nil {
  465. _ = o.Rollback()
  466. } else {
  467. _ = o.Commit()
  468. }
  469. }()
  470. // 根据指标获取已经存在的未授权记录
  471. var existList []*ExcelInfoPermissionNoAuthRecord
  472. sql := `SELECT * FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
  473. _, err = o.Raw(sql, excelSource, dataIdList).QueryRows(&existList)
  474. if err != nil {
  475. return
  476. }
  477. // 已经标记了的数据
  478. existMap := make(map[int32]map[string]*ExcelInfoPermissionNoAuthRecord)
  479. for _, v := range existList {
  480. tmpUserExistMap, ok := existMap[v.SysUserId]
  481. if !ok {
  482. tmpUserExistMap = make(map[string]*ExcelInfoPermissionNoAuthRecord)
  483. }
  484. key := fmt.Sprint(v.Source, "_", v.ExcelInfoId)
  485. tmpUserExistMap[key] = v
  486. existMap[v.SysUserId] = tmpUserExistMap
  487. }
  488. addMessageList := make([]*DataPermissionMessage, 0)
  489. addRecordList := make([]*ExcelInfoPermissionNoAuthRecord, 0)
  490. for _, userId := range noAuthUserIdList {
  491. isAdd := false
  492. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  493. for _, dataItem := range dataList {
  494. // 判断是否已经存在,已经存在就过滤
  495. if userExistOk {
  496. key := fmt.Sprint(excelSource, "_", dataItem.DataId)
  497. _, ok := tmpUserExistMap[key]
  498. if ok {
  499. continue
  500. }
  501. }
  502. isAdd = true
  503. addRecordList = append(addRecordList, &ExcelInfoPermissionNoAuthRecord{
  504. ExcelInfoPermissionNoAuthRecordId: 0,
  505. OpUniqueCode: uniqueCode,
  506. Source: int32(excelSource),
  507. ExcelInfoId: int32(dataItem.DataId),
  508. ExcelName: dataItem.DataName,
  509. SysUserId: int32(userId),
  510. CreateTime: time.Now(),
  511. })
  512. }
  513. // 有记录的话,需要添加消息
  514. if isAdd {
  515. addMessageList = append(addMessageList, &DataPermissionMessage{
  516. DataPermissionMessageId: 0,
  517. SendUserId: int32(opUserId),
  518. ReceiveUserId: int32(userId),
  519. Content: title,
  520. Remark: content,
  521. OpType: 3,
  522. Source: int32(source),
  523. SubSource: int32(excelSource),
  524. OpUniqueCode: uniqueCode,
  525. IsRead: 0,
  526. CreateTime: time.Now(),
  527. ModifyTime: time.Now(),
  528. })
  529. }
  530. }
  531. // 添加消息
  532. if len(addMessageList) > 0 {
  533. _, err = o.InsertMulti(500, addMessageList)
  534. if err != nil {
  535. return
  536. }
  537. }
  538. // 添加记录
  539. if len(addRecordList) > 0 {
  540. _, err = o.InsertMulti(500, addRecordList)
  541. if err != nil {
  542. return
  543. }
  544. }
  545. // 已经授权了的用户,需要删除未授权记录
  546. authUserIdNum := len(authUserIdList)
  547. if authUserIdNum > 0 {
  548. 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) + `)`
  549. _, err = o.Raw(sql, excelSource, authUserIdList, dataIdList).Exec()
  550. }
  551. return
  552. }
  553. // DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList
  554. // @Description: 根据来源和数据id列表删除记录
  555. // @author: Roc
  556. // @datetime 2024-04-07 14:47:37
  557. // @param excelSource int
  558. // @param dataIdList []string
  559. // @return err error
  560. func DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(excelSource int, dataIdList []string) (err error) {
  561. num := len(dataIdList)
  562. if num <= 0 {
  563. return
  564. }
  565. o := orm.NewOrmUsingDB("data")
  566. sql := `DELETE FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
  567. _, err = o.Raw(sql, excelSource, dataIdList).Exec()
  568. return
  569. }
  570. func GetExcelInfoDataPermissionNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
  571. o := orm.NewOrmUsingDB("data")
  572. // 获取总数
  573. sql := `SELECT count(1) AS total FROM excel_info_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
  574. err = o.Raw(sql, userId, excelSource).QueryRow(&total)
  575. if err != nil {
  576. return
  577. }
  578. 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 ?,? `
  579. _, err = o.Raw(sql, userId, excelSource, startSize, pageSize).QueryRows(&items)
  580. return
  581. }
  582. type ExcelInfoClassifyPermissionNoAuthRecord struct {
  583. ExcelInfoClassifyPermissionNoAuthRecordId int64 `json:"excel_info_classify_permission_no_auth_record_id" orm:"column(excel_info_classify_permission_no_auth_record_id);pk"` // 资产分类数据操作记录id
  584. Source int32 `json:"source"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
  585. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  586. ClassifyId string `json:"classify_id"` // ETA表格资产分类id
  587. ClassifyName string `json:"classify_name"` // ETA表格资产分类名称
  588. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  589. CreateTime time.Time `json:"create_time"` // 创建时间
  590. }
  591. // AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList
  592. // @Description: 根据分类添加用户分类未授权记录
  593. // @author: Roc
  594. // @datetime 2024-04-07 16:44:21
  595. // @param source int
  596. // @param excelSource int
  597. // @param classifyInfoList []ClassifyDataItem
  598. // @param noAuthUserIdList []int
  599. // @param uniqueCode string
  600. // @param content string
  601. // @param opUserId int
  602. // @return err error
  603. func AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, excelSource int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  604. num := len(classifyInfoList)
  605. if num <= 0 {
  606. return
  607. }
  608. // 分类id
  609. classifyIdList := make([]int, 0)
  610. for _, v := range classifyInfoList {
  611. classifyIdList = append(classifyIdList, v.ClassifyId)
  612. }
  613. userNum := len(noAuthUserIdList)
  614. if userNum <= 0 {
  615. return
  616. }
  617. o, err := orm.NewOrmUsingDB("data").Begin()
  618. if err != nil {
  619. return
  620. }
  621. defer func() {
  622. if err != nil {
  623. _ = o.Rollback()
  624. } else {
  625. _ = o.Commit()
  626. }
  627. }()
  628. // 根据分类获取已经存在的未授权记录
  629. var existList []*ExcelInfoClassifyPermissionNoAuthRecord
  630. sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
  631. _, err = o.Raw(sql, excelSource, classifyIdList).QueryRows(&existList)
  632. if err != nil {
  633. return
  634. }
  635. // 已经标记了的数据
  636. existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  637. for _, v := range existList {
  638. tmpUserExistMap, ok := existMap[v.SysUserId]
  639. if !ok {
  640. tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  641. }
  642. key := fmt.Sprint(v.Source, "_", v.ClassifyId)
  643. tmpUserExistMap[key] = v
  644. existMap[v.SysUserId] = tmpUserExistMap
  645. }
  646. addMessageList := make([]*DataPermissionMessage, 0)
  647. addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  648. for _, userId := range noAuthUserIdList {
  649. isAdd := false
  650. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  651. for _, dataItem := range classifyInfoList {
  652. // 判断是否已经存在,已经存在就过滤
  653. if userExistOk {
  654. key := fmt.Sprint(excelSource, "_", dataItem.ClassifyId)
  655. _, ok := tmpUserExistMap[key]
  656. if ok {
  657. continue
  658. }
  659. }
  660. isAdd = true
  661. addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
  662. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  663. Source: int32(excelSource),
  664. OpUniqueCode: uniqueCode,
  665. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  666. ClassifyName: dataItem.ClassifyName,
  667. SysUserId: int32(userId),
  668. CreateTime: time.Now(),
  669. })
  670. }
  671. // 有记录的话,需要添加消息
  672. if isAdd {
  673. addMessageList = append(addMessageList, &DataPermissionMessage{
  674. DataPermissionMessageId: 0,
  675. SendUserId: int32(opUserId),
  676. ReceiveUserId: int32(userId),
  677. Content: title,
  678. Remark: content,
  679. OpType: 4,
  680. Source: int32(source),
  681. SubSource: int32(excelSource),
  682. OpUniqueCode: uniqueCode,
  683. IsRead: 0,
  684. CreateTime: time.Now(),
  685. ModifyTime: time.Now(),
  686. })
  687. }
  688. }
  689. // 添加消息
  690. if len(addMessageList) > 0 {
  691. _, err = o.InsertMulti(500, addMessageList)
  692. if err != nil {
  693. return
  694. }
  695. }
  696. // 添加记录
  697. if len(addRecordList) > 0 {
  698. _, err = o.InsertMulti(500, addRecordList)
  699. if err != nil {
  700. return
  701. }
  702. }
  703. // 移除已经公开了的分类权限,需要删除未授权记录
  704. authUserIdNum := len(classifyIdList)
  705. if authUserIdNum > 0 {
  706. sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
  707. _, err = o.Raw(sql, excelSource, classifyIdList).Exec()
  708. }
  709. return
  710. }
  711. // AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList
  712. // @Description: 根据用户添加用户分类未授权记录
  713. // @author: Roc
  714. // @datetime 2024-04-07 20:12:44
  715. // @param source int
  716. // @param excelSource int
  717. // @param noAuthClassifyMap map[int]ClassifyDataItem
  718. // @param configUserIdList []int
  719. // @param uniqueCode string
  720. // @param content string
  721. // @param opUserId int
  722. // @return err error
  723. func AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList(source, excelSource int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  724. // 当前配置用户
  725. configUserNum := len(configUserIdList)
  726. if configUserNum <= 0 {
  727. return
  728. }
  729. //// 总共的涉密分类
  730. //noAuthClassifyInfoNum := len(noAuthClassifyMap)
  731. //if noAuthClassifyInfoNum <= 0 {
  732. // return
  733. //}
  734. o, err := orm.NewOrmUsingDB("data").Begin()
  735. if err != nil {
  736. return
  737. }
  738. defer func() {
  739. if err != nil {
  740. _ = o.Rollback()
  741. } else {
  742. _ = o.Commit()
  743. }
  744. }()
  745. // 根据当前配置用户获取已经存在的未授权记录
  746. var existList []*ExcelInfoClassifyPermissionNoAuthRecord
  747. sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
  748. _, err = o.Raw(sql, excelSource, configUserIdList).QueryRows(&existList)
  749. if err != nil {
  750. return
  751. }
  752. // 已经标记了的数据
  753. existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  754. delRecordIdMap := make(map[int64]int64)
  755. for _, v := range existList {
  756. tmpUserExistMap, ok := existMap[v.SysUserId]
  757. if !ok {
  758. tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  759. }
  760. tmpUserExistMap[v.ClassifyId] = v
  761. existMap[v.SysUserId] = tmpUserExistMap
  762. // 已经配置了的记录id
  763. delRecordIdMap[v.ExcelInfoClassifyPermissionNoAuthRecordId] = v.ExcelInfoClassifyPermissionNoAuthRecordId
  764. }
  765. addMessageList := make([]*DataPermissionMessage, 0)
  766. addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  767. for _, userId := range configUserIdList {
  768. isAdd := false
  769. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  770. for _, dataItem := range noAuthClassifyMap {
  771. // 判断是否已经存在,已经存在就过滤
  772. if userExistOk {
  773. key := fmt.Sprint(dataItem.ClassifyId)
  774. tmpUserRecord, ok := tmpUserExistMap[key]
  775. if ok {
  776. delete(delRecordIdMap, tmpUserRecord.ExcelInfoClassifyPermissionNoAuthRecordId)
  777. continue
  778. }
  779. }
  780. isAdd = true
  781. addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
  782. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  783. Source: int32(excelSource),
  784. OpUniqueCode: uniqueCode,
  785. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  786. ClassifyName: dataItem.ClassifyName,
  787. SysUserId: int32(userId),
  788. CreateTime: time.Now(),
  789. })
  790. }
  791. // 有记录的话,需要添加消息
  792. if isAdd {
  793. addMessageList = append(addMessageList, &DataPermissionMessage{
  794. DataPermissionMessageId: 0,
  795. SendUserId: int32(opUserId),
  796. ReceiveUserId: int32(userId),
  797. Content: title,
  798. Remark: content,
  799. OpType: 4,
  800. Source: int32(source),
  801. SubSource: int32(excelSource),
  802. OpUniqueCode: uniqueCode,
  803. IsRead: 0,
  804. CreateTime: time.Now(),
  805. ModifyTime: time.Now(),
  806. })
  807. }
  808. }
  809. // 添加消息
  810. if len(addMessageList) > 0 {
  811. _, err = o.InsertMulti(500, addMessageList)
  812. if err != nil {
  813. return
  814. }
  815. }
  816. // 添加记录
  817. if len(addRecordList) > 0 {
  818. _, err = o.InsertMulti(500, addRecordList)
  819. if err != nil {
  820. return
  821. }
  822. }
  823. // 需要删除未授权记录
  824. delRecordIdNum := len(delRecordIdMap)
  825. if delRecordIdNum > 0 {
  826. delRecordIdList := make([]int64, 0)
  827. for _, v := range delRecordIdMap {
  828. delRecordIdList = append(delRecordIdList, v)
  829. }
  830. sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE excel_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
  831. _, err = o.Raw(sql, delRecordIdList).Exec()
  832. }
  833. return
  834. }
  835. // DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList
  836. // @Description: 根据来源和删除分类授权记录
  837. // @author: Roc
  838. // @datetime 2024-04-07 14:47:37
  839. // @param source int
  840. // @param excelSource int
  841. // @return err error
  842. func DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(excelSource int) (err error) {
  843. o := orm.NewOrmUsingDB("data")
  844. sql := `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ?`
  845. _, err = o.Raw(sql, excelSource).Exec()
  846. return
  847. }
  848. // GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId
  849. // @Description: 根据用户获取未授权的资产分类记录
  850. // @author: Roc
  851. // @datetime 2024-04-07 20:14:49
  852. // @param userId int
  853. // @param source int
  854. // @param subSource int
  855. // @param startSize int
  856. // @param pageSize int
  857. // @return total int
  858. // @return items []*DataPermissionClassifyNoAuthRecord
  859. // @return err error
  860. func GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
  861. o := orm.NewOrmUsingDB("data")
  862. // 获取总数
  863. sql := `SELECT count(1) AS total FROM excel_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
  864. err = o.Raw(sql, userId, excelSource).QueryRow(&total)
  865. if err != nil {
  866. return
  867. }
  868. 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 ?,? `
  869. _, err = o.Raw(sql, userId, excelSource, startSize, pageSize).QueryRows(&items)
  870. return
  871. }
  872. // ExcelInfoPermissionAdminAuth 含创建人的表格权限
  873. type ExcelInfoPermissionAdminAuth struct {
  874. ExcelInfoPermission
  875. ExcelName string `json:"excel_name"` // 表格名称
  876. UniqueCode string `json:"unique_code"` // 唯一编码
  877. CreateUserId int `json:"create_user_id"` // 创建人ID
  878. }
  879. // GetAdminAuthExcelInfoPermission 获取用户有权限的表格
  880. func GetAdminAuthExcelInfoPermission(source, adminId int, keywords string) (items []*ExcelInfoPermissionAdminAuth, err error) {
  881. o := orm.NewOrmUsingDB("data")
  882. sql := `SELECT a.*, b.sys_user_id AS create_user_id, b.excel_name, b.unique_code FROM excel_info_permission AS a
  883. JOIN excel_info AS b ON a.excel_info_id = b.excel_info_id
  884. WHERE a.source = ? AND (b.sys_user_id = ? OR a.sys_user_id = ?)`
  885. var pars []interface{}
  886. pars = append(pars, source, adminId, adminId)
  887. if keywords != "" {
  888. sql += ` AND b.excel_name LIKE ?`
  889. pars = append(pars, keywords)
  890. }
  891. sql += ` ORDER BY a.create_time ASC`
  892. _, err = o.Raw(sql, pars).QueryRows(&items)
  893. return
  894. }
  895. func ClearAndSetExcelInfoPermission(source, excelInfoId int, permissions []*ExcelInfoPermission) (err error) {
  896. if excelInfoId <= 0 {
  897. return
  898. }
  899. tx, e := orm.NewOrmUsingDB("data").Begin()
  900. if e != nil {
  901. err = fmt.Errorf("orm begin err: %v", e)
  902. return
  903. }
  904. defer func() {
  905. if err != nil {
  906. _ = tx.Rollback()
  907. return
  908. }
  909. _ = tx.Commit()
  910. }()
  911. sql := `DELETE FROM excel_info_permission WHERE excel_info_id = ? AND source = ?`
  912. _, e = tx.Raw(sql, excelInfoId, source).Exec()
  913. if e != nil {
  914. err = fmt.Errorf("clear permission err: %v", e)
  915. return
  916. }
  917. if len(permissions) > 0 {
  918. _, e = tx.InsertMulti(500, permissions)
  919. if e != nil {
  920. err = fmt.Errorf("insert permissions err: %v", e)
  921. return
  922. }
  923. }
  924. return
  925. }
  926. func GetExcelPermissionBySourceAndId(excelId, source int) (items []*ExcelInfoPermission, err error) {
  927. o := orm.NewOrmUsingDB("data")
  928. sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id = ?`
  929. _, err = o.Raw(sql, source, excelId).QueryRows(&items)
  930. return
  931. }
  932. func GetExcelPermissionByExcelIdAndUserId(excelId, userId int) (items []*ExcelInfoPermission, err error) {
  933. o := orm.NewOrmUsingDB("data")
  934. sql := `SELECT * FROM excel_info_permission WHERE excel_info_id = ? AND sys_user_id = ?`
  935. _, err = o.Raw(sql, excelId, userId).QueryRows(&items)
  936. return
  937. }