excel.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. func SetIsPermissionByExcelClassifyIdList(classifyIdList []int, excelClassifySource int) (err error) {
  27. num := len(classifyIdList)
  28. o := global.DmSQL["data"].Begin()
  29. defer func() {
  30. if err != nil {
  31. _ = o.Rollback()
  32. } else {
  33. _ = o.Commit()
  34. }
  35. }()
  36. sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND source = ?`
  37. err = o.Exec(sql, 0, excelClassifySource).Error
  38. if err != nil {
  39. return
  40. }
  41. if num > 0 {
  42. sql = `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id in (` + utils.GetOrmInReplace(num) + `) `
  43. err = o.Exec(sql, 1, excelClassifySource, classifyIdList).Error
  44. if err != nil {
  45. return
  46. }
  47. }
  48. return
  49. }
  50. func SetPermissionByExcelIdList(excelIdList []string, userIdList []int, source int) (err error) {
  51. excelNum := len(excelIdList)
  52. if excelNum <= 0 {
  53. return
  54. }
  55. o := global.DmSQL["data"].Begin()
  56. defer func() {
  57. if err != nil {
  58. _ = o.Rollback()
  59. } else {
  60. _ = o.Commit()
  61. }
  62. }()
  63. excelInfoPermissionList := make([]*ExcelInfoPermission, 0)
  64. sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
  65. err = o.Raw(sql, source, excelIdList).Scan(&excelInfoPermissionList).Error
  66. if err != nil {
  67. return
  68. }
  69. excelInfoPermissionMap := make(map[string]*ExcelInfoPermission)
  70. for _, v := range excelInfoPermissionList {
  71. excelInfoPermissionMap[fmt.Sprint(v.ExcelInfoId, "_", v.SysUserId)] = v
  72. }
  73. {
  74. isJoinPermission := 1
  75. if len(userIdList) <= 0 {
  76. isJoinPermission = 0
  77. }
  78. sql = `UPDATE excel_info SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
  79. err = global.DmSQL["data"].Exec(sql, isJoinPermission, source, excelIdList).Error
  80. if err != nil {
  81. return
  82. }
  83. }
  84. addList := make([]*ExcelInfoPermission, 0)
  85. for _, excelInfoIdStr := range excelIdList {
  86. excelInfoId, tmpErr := strconv.ParseInt(excelInfoIdStr, 10, 64)
  87. if tmpErr != nil {
  88. err = tmpErr
  89. return
  90. }
  91. for _, userId := range userIdList {
  92. key := fmt.Sprint(excelInfoId, "_", userId)
  93. if _, ok := excelInfoPermissionMap[key]; ok {
  94. delete(excelInfoPermissionMap, key)
  95. } else {
  96. addList = append(addList, &ExcelInfoPermission{
  97. ExcelInfoId: int32(excelInfoId),
  98. SysUserId: int32(userId),
  99. Source: int32(source),
  100. ModifyTime: time.Now(),
  101. CreateTime: time.Now(),
  102. })
  103. }
  104. }
  105. }
  106. if len(addList) > 0 {
  107. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  108. if err != nil {
  109. return
  110. }
  111. }
  112. {
  113. deletePermissionIdList := make([]int64, 0)
  114. for _, v := range excelInfoPermissionMap {
  115. deletePermissionIdList = append(deletePermissionIdList, v.ExcelInfoPermissionId)
  116. }
  117. deletePermissionIdNum := len(deletePermissionIdList)
  118. if deletePermissionIdNum > 0 {
  119. sql = "DELETE FROM excel_info_permission WHERE excel_info_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  120. err = o.Exec(sql, deletePermissionIdList).Error
  121. if err != nil {
  122. return
  123. }
  124. }
  125. }
  126. return
  127. }
  128. func SetPermissionByExcelClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
  129. userNum := len(userIdList)
  130. if userNum <= 0 {
  131. return
  132. }
  133. o := global.DmSQL["data"].Begin()
  134. defer func() {
  135. if err != nil {
  136. _ = o.Rollback()
  137. } else {
  138. _ = o.Commit()
  139. }
  140. }()
  141. classifyPermissionList := make([]*ExcelClassifyPermission, 0)
  142. sql := `SELECT * FROM excel_classify_permission WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
  143. err = o.Raw(sql, classifyType, userIdList).Scan(&classifyPermissionList).Error
  144. if err != nil {
  145. return
  146. }
  147. classifyPermissionMap := make(map[string]*ExcelClassifyPermission)
  148. for _, v := range classifyPermissionList {
  149. classifyPermissionMap[fmt.Sprint(v.ExcelClassifyId, "_", v.SysUserId)] = v
  150. }
  151. addList := make([]*ExcelClassifyPermission, 0)
  152. for _, userId := range userIdList {
  153. for _, classifyId := range classifyIdList {
  154. key := fmt.Sprint(classifyId, "_", userId)
  155. if _, ok := classifyPermissionMap[key]; ok {
  156. delete(classifyPermissionMap, key)
  157. } else {
  158. addList = append(addList, &ExcelClassifyPermission{
  159. ExcelClassifyId: int32(classifyId),
  160. Source: int32(classifyType),
  161. SysUserId: int32(userId),
  162. ModifyTime: time.Now(),
  163. CreateTime: time.Now(),
  164. })
  165. }
  166. }
  167. }
  168. if len(addList) > 0 {
  169. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  170. if err != nil {
  171. return
  172. }
  173. }
  174. {
  175. deletePermissionIdList := make([]int64, 0)
  176. for _, v := range classifyPermissionMap {
  177. deletePermissionIdList = append(deletePermissionIdList, v.ExcelClassifyPermissionId)
  178. }
  179. deletePermissionIdNum := len(deletePermissionIdList)
  180. if deletePermissionIdNum > 0 {
  181. sql = "DELETE FROM excel_classify_permission WHERE excel_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  182. err = o.Exec(sql, deletePermissionIdList).Error
  183. if err != nil {
  184. return
  185. }
  186. }
  187. }
  188. return
  189. }
  190. func GetPermissionExcelClassifyIdListByUserId(userId int, classifyType int) (excelClassifyIdList []int, err error) {
  191. sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE source = ? AND sys_user_id = ? `
  192. err = global.DmSQL["data"].Raw(sql, classifyType, userId).Scan(&excelClassifyIdList).Error
  193. return
  194. }
  195. func GetPermissionUserIdListByExcelId(dataId int, source int) (userIdList []int, err error) {
  196. sql := `SELECT sys_user_id FROM excel_info_permission WHERE source = ? AND excel_info_id= ? `
  197. err = global.DmSQL["data"].Raw(sql, source, dataId).Scan(&userIdList).Error
  198. return
  199. }
  200. func GetPermissionUserIdListByExcelClassifyId(classifyId int, source int) (userIdList []int, err error) {
  201. sql := `SELECT sys_user_id FROM excel_classify_permission WHERE source = ? AND excel_classify_id= ? `
  202. err = global.DmSQL["data"].Raw(sql, source, classifyId).Scan(&userIdList).Error
  203. return
  204. }
  205. func GetPermissionExcelIdList(userId, excelInfoId int) (idList []int, err error) {
  206. pars := []interface{}{userId}
  207. sql := `SELECT excel_info_id FROM excel_info_permission WHERE sys_user_id = ? `
  208. if excelInfoId > 0 {
  209. sql += ` AND excel_info_id = ? `
  210. pars = append(pars, excelInfoId)
  211. }
  212. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&idList).Error
  213. return
  214. }
  215. func GetPermissionExcelClassifyIdList(userId, classifyId int) (idList []int, err error) {
  216. pars := []interface{}{userId}
  217. sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE sys_user_id = ? `
  218. if classifyId > 0 {
  219. sql += ` AND excel_classify_id = ? `
  220. pars = append(pars, classifyId)
  221. }
  222. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&idList).Error
  223. return
  224. }
  225. func InheritParentClassifyByExcelClassifyId(dataSource, excelSource, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
  226. o := global.DmSQL["data"].Begin()
  227. defer func() {
  228. if err != nil {
  229. _ = o.Rollback()
  230. } else {
  231. _ = o.Commit()
  232. }
  233. }()
  234. sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id = ? `
  235. err = o.Exec(sql, 1, excelSource, classifyId).Error
  236. if err != nil {
  237. return
  238. }
  239. {
  240. var parentRecordItems []*ExcelInfoClassifyPermissionNoAuthRecord
  241. 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 ?,? `
  242. err = o.Raw(sql, parentClassifyId, dataSource, excelSource).Scan(&parentRecordItems).Error
  243. addNoAuthRecordItems := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  244. for _, v := range parentRecordItems {
  245. addNoAuthRecordItems = append(addNoAuthRecordItems, &ExcelInfoClassifyPermissionNoAuthRecord{
  246. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  247. Source: v.Source,
  248. OpUniqueCode: uniqueCode,
  249. ClassifyId: fmt.Sprint(classifyId),
  250. ClassifyName: classifyName,
  251. SysUserId: v.SysUserId,
  252. CreateTime: time.Now(),
  253. })
  254. }
  255. if len(addNoAuthRecordItems) > 0 {
  256. err = o.CreateInBatches(addNoAuthRecordItems, utils.MultiAddNum).Error
  257. if err != nil {
  258. return
  259. }
  260. }
  261. }
  262. {
  263. parentClassifyPermissionList := make([]*ExcelClassifyPermission, 0)
  264. sql = `SELECT * FROM excel_classify_permission WHERE source = ? AND excel_classify_id = ? `
  265. err = o.Raw(sql, excelSource, parentClassifyId).Scan(&parentClassifyPermissionList).Error
  266. if err != nil {
  267. return
  268. }
  269. addList := make([]*ExcelClassifyPermission, 0)
  270. for _, v := range parentClassifyPermissionList {
  271. addList = append(addList, &ExcelClassifyPermission{
  272. ExcelClassifyId: int32(classifyId),
  273. Source: int32(excelSource),
  274. SysUserId: v.SysUserId,
  275. ModifyTime: time.Now(),
  276. CreateTime: time.Now(),
  277. })
  278. }
  279. if len(addList) > 0 {
  280. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  281. if err != nil {
  282. return
  283. }
  284. }
  285. }
  286. return
  287. }
  288. type ExcelInfoPermissionNoAuthRecord struct {
  289. ExcelInfoPermissionNoAuthRecordId int64 `json:"excel_info_permission_no_auth_record_id" orm:"column(excel_info_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产数据操作记录id
  290. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  291. Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
  292. ExcelInfoId int32 `json:"excel_info_id"` // 指标id
  293. ExcelName string `json:"excel_name"` // 图表名称
  294. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  295. CreateTime time.Time `json:"create_time"` // 创建时间
  296. }
  297. func AddExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(source, excelSource int, dataList []DataItem, noAuthUserIdList, authUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  298. num := len(dataList)
  299. if num <= 0 {
  300. return
  301. }
  302. dataIdList := make([]int, 0)
  303. for _, v := range dataList {
  304. dataIdList = append(dataIdList, v.DataId)
  305. }
  306. userNum := len(noAuthUserIdList)
  307. if userNum <= 0 {
  308. return
  309. }
  310. o := global.DmSQL["data"].Begin()
  311. defer func() {
  312. if err != nil {
  313. _ = o.Rollback()
  314. } else {
  315. _ = o.Commit()
  316. }
  317. }()
  318. var existList []*ExcelInfoPermissionNoAuthRecord
  319. sql := `SELECT * FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
  320. err = o.Raw(sql, excelSource, dataIdList).Scan(&existList).Error
  321. if err != nil {
  322. return
  323. }
  324. existMap := make(map[int32]map[string]*ExcelInfoPermissionNoAuthRecord)
  325. for _, v := range existList {
  326. tmpUserExistMap, ok := existMap[v.SysUserId]
  327. if !ok {
  328. tmpUserExistMap = make(map[string]*ExcelInfoPermissionNoAuthRecord)
  329. }
  330. key := fmt.Sprint(v.Source, "_", v.ExcelInfoId)
  331. tmpUserExistMap[key] = v
  332. existMap[v.SysUserId] = tmpUserExistMap
  333. }
  334. addMessageList := make([]*DataPermissionMessage, 0)
  335. addRecordList := make([]*ExcelInfoPermissionNoAuthRecord, 0)
  336. for _, userId := range noAuthUserIdList {
  337. isAdd := false
  338. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  339. for _, dataItem := range dataList {
  340. if userExistOk {
  341. key := fmt.Sprint(excelSource, "_", dataItem.DataId)
  342. _, ok := tmpUserExistMap[key]
  343. if ok {
  344. continue
  345. }
  346. }
  347. isAdd = true
  348. addRecordList = append(addRecordList, &ExcelInfoPermissionNoAuthRecord{
  349. ExcelInfoPermissionNoAuthRecordId: 0,
  350. OpUniqueCode: uniqueCode,
  351. Source: int32(excelSource),
  352. ExcelInfoId: int32(dataItem.DataId),
  353. ExcelName: dataItem.DataName,
  354. SysUserId: int32(userId),
  355. CreateTime: time.Now(),
  356. })
  357. }
  358. if isAdd {
  359. addMessageList = append(addMessageList, &DataPermissionMessage{
  360. DataPermissionMessageId: 0,
  361. SendUserId: int32(opUserId),
  362. ReceiveUserId: int32(userId),
  363. Content: title,
  364. Remark: content,
  365. OpType: 3,
  366. Source: int32(source),
  367. SubSource: int32(excelSource),
  368. OpUniqueCode: uniqueCode,
  369. IsRead: 0,
  370. CreateTime: time.Now(),
  371. ModifyTime: time.Now(),
  372. })
  373. }
  374. }
  375. if len(addMessageList) > 0 {
  376. err = o.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  377. if err != nil {
  378. return
  379. }
  380. }
  381. if len(addRecordList) > 0 {
  382. err = o.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  383. if err != nil {
  384. return
  385. }
  386. }
  387. authUserIdNum := len(authUserIdList)
  388. if authUserIdNum > 0 {
  389. 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) + `)`
  390. err = o.Exec(sql, excelSource, authUserIdList, dataIdList).Error
  391. }
  392. return
  393. }
  394. func DeleteExcelInfoPermissionNoAuthRecordBySourceAndDataIdList(excelSource int, dataIdList []string) (err error) {
  395. num := len(dataIdList)
  396. if num <= 0 {
  397. return
  398. }
  399. sql := `DELETE FROM excel_info_permission_no_auth_record WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(num) + `)`
  400. err = global.DmSQL["data"].Exec(sql, excelSource, dataIdList).Error
  401. return
  402. }
  403. func GetExcelInfoDataPermissionNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
  404. sql := `SELECT count(1) AS total FROM excel_info_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
  405. err = global.DmSQL["data"].Raw(sql, userId, excelSource).Scan(&total).Error
  406. if err != nil {
  407. return
  408. }
  409. 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 ?,? `
  410. err = global.DmSQL["data"].Raw(sql, userId, excelSource, startSize, pageSize).Scan(&items).Error
  411. return
  412. }
  413. type ExcelInfoClassifyPermissionNoAuthRecord struct {
  414. 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
  415. Source int32 `json:"source"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
  416. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  417. ClassifyId string `json:"classify_id"` // ETA表格资产分类id
  418. ClassifyName string `json:"classify_name"` // ETA表格资产分类名称
  419. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  420. CreateTime time.Time `json:"create_time"` // 创建时间
  421. }
  422. func AddExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, excelSource int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  423. num := len(classifyInfoList)
  424. if num <= 0 {
  425. return
  426. }
  427. classifyIdList := make([]int, 0)
  428. for _, v := range classifyInfoList {
  429. classifyIdList = append(classifyIdList, v.ClassifyId)
  430. }
  431. userNum := len(noAuthUserIdList)
  432. if userNum <= 0 {
  433. return
  434. }
  435. o := global.DmSQL["data"].Begin()
  436. defer func() {
  437. if err != nil {
  438. _ = o.Rollback()
  439. } else {
  440. _ = o.Commit()
  441. }
  442. }()
  443. var existList []*ExcelInfoClassifyPermissionNoAuthRecord
  444. sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
  445. err = o.Raw(sql, excelSource, classifyIdList).Scan(&existList).Error
  446. if err != nil {
  447. return
  448. }
  449. existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  450. for _, v := range existList {
  451. tmpUserExistMap, ok := existMap[v.SysUserId]
  452. if !ok {
  453. tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  454. }
  455. key := fmt.Sprint(v.Source, "_", v.ClassifyId)
  456. tmpUserExistMap[key] = v
  457. existMap[v.SysUserId] = tmpUserExistMap
  458. }
  459. addMessageList := make([]*DataPermissionMessage, 0)
  460. addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  461. for _, userId := range noAuthUserIdList {
  462. isAdd := false
  463. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  464. for _, dataItem := range classifyInfoList {
  465. if userExistOk {
  466. key := fmt.Sprint(excelSource, "_", dataItem.ClassifyId)
  467. _, ok := tmpUserExistMap[key]
  468. if ok {
  469. continue
  470. }
  471. }
  472. isAdd = true
  473. addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
  474. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  475. Source: int32(excelSource),
  476. OpUniqueCode: uniqueCode,
  477. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  478. ClassifyName: dataItem.ClassifyName,
  479. SysUserId: int32(userId),
  480. CreateTime: time.Now(),
  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: 4,
  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. if len(addMessageList) > 0 {
  501. err = o.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  502. if err != nil {
  503. return
  504. }
  505. }
  506. if len(addRecordList) > 0 {
  507. err = o.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  508. if err != nil {
  509. return
  510. }
  511. }
  512. authUserIdNum := len(classifyIdList)
  513. if authUserIdNum > 0 {
  514. sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
  515. err = o.Exec(sql, excelSource, classifyIdList).Error
  516. }
  517. return
  518. }
  519. func AddExcelInfoClassifyNoAuthRecordBySourceAndUserIdList(source, excelSource int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  520. configUserNum := len(configUserIdList)
  521. if configUserNum <= 0 {
  522. return
  523. }
  524. o := global.DmSQL["data"].Begin()
  525. defer func() {
  526. if err != nil {
  527. _ = o.Rollback()
  528. } else {
  529. _ = o.Commit()
  530. }
  531. }()
  532. var existList []*ExcelInfoClassifyPermissionNoAuthRecord
  533. sql := `SELECT * FROM excel_info_classify_permission_no_auth_record WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
  534. err = o.Raw(sql, excelSource, configUserIdList).Scan(&existList).Error
  535. if err != nil {
  536. return
  537. }
  538. existMap := make(map[int32]map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  539. delRecordIdMap := make(map[int64]int64)
  540. for _, v := range existList {
  541. tmpUserExistMap, ok := existMap[v.SysUserId]
  542. if !ok {
  543. tmpUserExistMap = make(map[string]*ExcelInfoClassifyPermissionNoAuthRecord)
  544. }
  545. tmpUserExistMap[v.ClassifyId] = v
  546. existMap[v.SysUserId] = tmpUserExistMap
  547. delRecordIdMap[v.ExcelInfoClassifyPermissionNoAuthRecordId] = v.ExcelInfoClassifyPermissionNoAuthRecordId
  548. }
  549. addMessageList := make([]*DataPermissionMessage, 0)
  550. addRecordList := make([]*ExcelInfoClassifyPermissionNoAuthRecord, 0)
  551. for _, userId := range configUserIdList {
  552. isAdd := false
  553. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  554. for _, dataItem := range noAuthClassifyMap {
  555. if userExistOk {
  556. key := fmt.Sprint(dataItem.ClassifyId)
  557. tmpUserRecord, ok := tmpUserExistMap[key]
  558. if ok {
  559. delete(delRecordIdMap, tmpUserRecord.ExcelInfoClassifyPermissionNoAuthRecordId)
  560. continue
  561. }
  562. }
  563. isAdd = true
  564. addRecordList = append(addRecordList, &ExcelInfoClassifyPermissionNoAuthRecord{
  565. ExcelInfoClassifyPermissionNoAuthRecordId: 0,
  566. Source: int32(excelSource),
  567. OpUniqueCode: uniqueCode,
  568. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  569. ClassifyName: dataItem.ClassifyName,
  570. SysUserId: int32(userId),
  571. CreateTime: time.Now(),
  572. })
  573. }
  574. if isAdd {
  575. addMessageList = append(addMessageList, &DataPermissionMessage{
  576. DataPermissionMessageId: 0,
  577. SendUserId: int32(opUserId),
  578. ReceiveUserId: int32(userId),
  579. Content: title,
  580. Remark: content,
  581. OpType: 4,
  582. Source: int32(source),
  583. SubSource: int32(excelSource),
  584. OpUniqueCode: uniqueCode,
  585. IsRead: 0,
  586. CreateTime: time.Now(),
  587. ModifyTime: time.Now(),
  588. })
  589. }
  590. }
  591. if len(addMessageList) > 0 {
  592. err = o.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  593. if err != nil {
  594. return
  595. }
  596. }
  597. if len(addRecordList) > 0 {
  598. err = o.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  599. if err != nil {
  600. return
  601. }
  602. }
  603. delRecordIdNum := len(delRecordIdMap)
  604. if delRecordIdNum > 0 {
  605. delRecordIdList := make([]int64, 0)
  606. for _, v := range delRecordIdMap {
  607. delRecordIdList = append(delRecordIdList, v)
  608. }
  609. sql = `DELETE FROM excel_info_classify_permission_no_auth_record WHERE excel_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
  610. err = o.Exec(sql, delRecordIdList).Error
  611. }
  612. return
  613. }
  614. func DeleteExcelInfoClassifyNoAuthRecordBySourceAndClassifyIdList(excelSource int) (err error) {
  615. sql := `DELETE FROM excel_info_classify_permission_no_auth_record WHERE source = ?`
  616. err = global.DmSQL["data"].Exec(sql, excelSource).Error
  617. return
  618. }
  619. func GetExcelInfoDataPermissionClassifyNoAuthRecordListByUserId(userId, excelSource int32, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
  620. sql := `SELECT count(1) AS total FROM excel_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND source = ? `
  621. err = global.DmSQL["data"].Raw(sql, userId, excelSource).Scan(&total).Error
  622. if err != nil {
  623. return
  624. }
  625. 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 ?,? `
  626. err = global.DmSQL["data"].Raw(sql, userId, excelSource, startSize, pageSize).Scan(&items).Error
  627. return
  628. }
  629. type ExcelInfoPermissionAdminAuth struct {
  630. ExcelInfoPermission
  631. ExcelName string `json:"excel_name"` // 表格名称
  632. UniqueCode string `json:"unique_code"` // 唯一编码
  633. CreateUserId int `json:"create_user_id"` // 创建人ID
  634. }
  635. func GetAdminAuthExcelInfoPermission(source, adminId int, keywords string) (items []*ExcelInfoPermissionAdminAuth, err error) {
  636. sql := `SELECT a.*, b.sys_user_id AS create_user_id, b.excel_name, b.unique_code FROM excel_info_permission AS a
  637. JOIN excel_info AS b ON a.excel_info_id = b.excel_info_id
  638. WHERE a.source = ? AND (b.sys_user_id = ? OR a.sys_user_id = ?)`
  639. var pars []interface{}
  640. pars = append(pars, source, adminId, adminId)
  641. if keywords != "" {
  642. sql += ` AND b.excel_name LIKE ?`
  643. pars = append(pars, keywords)
  644. }
  645. sql += ` ORDER BY a.create_time ASC`
  646. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error
  647. return
  648. }
  649. func ClearAndSetExcelInfoPermission(source, excelInfoId int, permissions []*ExcelInfoPermission) (err error) {
  650. if excelInfoId <= 0 {
  651. return
  652. }
  653. tx := global.DmSQL["data"].Begin()
  654. defer func() {
  655. if err != nil {
  656. _ = tx.Rollback()
  657. return
  658. }
  659. _ = tx.Commit()
  660. }()
  661. sql := `DELETE FROM excel_info_permission WHERE excel_info_id = ? AND source = ?`
  662. err = tx.Exec(sql, excelInfoId, source).Error
  663. if err != nil {
  664. err = fmt.Errorf("clear permission err: %v", err)
  665. return
  666. }
  667. if len(permissions) > 0 {
  668. err = tx.CreateInBatches(permissions, utils.MultiAddNum).Error
  669. if err != nil {
  670. err = fmt.Errorf("insert permissions err: %v", err)
  671. return
  672. }
  673. }
  674. return
  675. }
  676. func GetExcelPermissionBySourceAndId(excelId, source int) (items []*ExcelInfoPermission, err error) {
  677. sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id = ?`
  678. err = global.DmSQL["data"].Raw(sql, source, excelId).Scan(&items).Error
  679. return
  680. }
  681. func GetExcelPermissionByExcelIdAndUserId(excelId, userId int) (items []*ExcelInfoPermission, err error) {
  682. sql := `SELECT * FROM excel_info_permission WHERE excel_info_id = ? AND sys_user_id = ?`
  683. err = global.DmSQL["data"].Raw(sql, excelId, userId).Scan(&items).Error
  684. return
  685. }