data_move.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. package data_manage_permission
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/data_manage/data_manage_permission"
  7. "eta_gn/eta_api/models/data_manage/excel"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/rdlucklib/rdluck_tools/uuid"
  15. )
  16. // GetEdbChartClassifyList
  17. // @Description: 获取数据权限那边的分类列表
  18. // @author: Roc
  19. // @datetime 2024-04-01 09:46:29
  20. // @param source int
  21. // @param subSource int
  22. // @return resp data_manage.EdbChartClassifyResp
  23. // @return err error
  24. func GetEdbChartClassifyList(source, subSource, sysUserId int) (resp data_manage.EdbChartClassifyResp, err error) {
  25. switch source {
  26. case 1: //手工数据指标
  27. list, e := models.GetEdbdataClassify(0)
  28. if e != nil {
  29. err = e
  30. return
  31. }
  32. for _, v := range list {
  33. item := data_manage.EdbChartClassify{
  34. ClassifyId: v.ClassifyId,
  35. ClassifyName: v.ClassifyName,
  36. ParentId: v.ParentId,
  37. }
  38. for _, v2 := range v.Child {
  39. child := data_manage.EdbChartClassify{
  40. ClassifyId: v2.ClassifyId,
  41. ClassifyName: v2.ClassifyName,
  42. ParentId: v2.ParentId,
  43. }
  44. item.Child = append(item.Child, &child)
  45. }
  46. resp.List = append(resp.List, &item)
  47. }
  48. case 3, 7:
  49. // 这里拆分指标库和计算指标
  50. classifyType := utils.EdbClassifyTypeBase
  51. if source == 7 {
  52. classifyType = utils.EdbClassifyTypeCalculate
  53. }
  54. if classifyType == utils.EdbClassifyTypeBase {
  55. sysUserId = 0
  56. }
  57. // 考虑到后面可以会迭代到10层, 这里直接用递归处理
  58. classifyAll, e := data_manage.GetAllEdbClassifyByType(classifyType, sysUserId)
  59. if e != nil && !utils.IsErrNoRow(e) {
  60. err = e
  61. return
  62. }
  63. resp.List = GetEdbMenuTreeRecursive(classifyAll, 0)
  64. case 4:
  65. //ETA预测指标
  66. // 考虑到后面可以会迭代到10层, 这里直接用递归
  67. classifyAll, e := data_manage.GetAllEdbClassifyByType(1, sysUserId)
  68. if e != nil && !utils.IsErrNoRow(e) {
  69. err = e
  70. return
  71. }
  72. resp.List = GetEdbMenuTreeRecursive(classifyAll, 0)
  73. case 5: //图库
  74. // 考虑到后面可以会迭代到10层, 这里直接用递归处理
  75. classifyAll, e := data_manage.GetAllChartClassifyItemsBySource(1)
  76. if e != nil && !utils.IsErrNoRow(e) {
  77. err = e
  78. return
  79. }
  80. resp.List = GetChartMenuTreeRecursive(classifyAll, 0)
  81. case 6:
  82. // ETA表格
  83. // 考虑到后面可以会迭代到10层, 这里直接用递归处理
  84. classifyAll, e := excel.GetExcelClassifyBySourceOrderByLevel(subSource)
  85. if e != nil && !utils.IsErrNoRow(e) {
  86. err = e
  87. return
  88. }
  89. resp.List = GetExcelMenuTreeRecursive(classifyAll, 0)
  90. default:
  91. return
  92. }
  93. return
  94. }
  95. // GetEdbMenuTreeRecursive 递归指标库菜单树
  96. func GetEdbMenuTreeRecursive(list []*data_manage.EdbClassifyItems, parentId int) []*data_manage.EdbChartClassify {
  97. res := make([]*data_manage.EdbChartClassify, 0)
  98. for _, v := range list {
  99. t := new(data_manage.EdbChartClassify)
  100. t.ClassifyId = v.ClassifyId
  101. t.ClassifyName = v.ClassifyName
  102. t.ParentId = v.ParentId
  103. t.IsJoinPermission = v.IsJoinPermission
  104. if v.ParentId == parentId {
  105. t.Child = GetEdbMenuTreeRecursive(list, v.ClassifyId)
  106. res = append(res, t)
  107. }
  108. }
  109. return res
  110. }
  111. // GetChartMenuTreeRecursive 递归指标库菜单树
  112. func GetChartMenuTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.EdbChartClassify {
  113. res := make([]*data_manage.EdbChartClassify, 0)
  114. for _, v := range list {
  115. t := new(data_manage.EdbChartClassify)
  116. t.ClassifyId = v.ChartClassifyId
  117. t.ClassifyName = v.ChartClassifyName
  118. t.ParentId = v.ParentId
  119. t.IsJoinPermission = v.IsJoinPermission
  120. if v.ParentId == parentId {
  121. t.Child = GetChartMenuTreeRecursive(list, v.ChartClassifyId)
  122. res = append(res, t)
  123. }
  124. }
  125. return res
  126. }
  127. // GetExcelMenuTreeRecursive
  128. // @Description: 递归ETA表格菜单树
  129. // @author: Roc
  130. // @datetime 2024-03-27 10:30:32
  131. // @param list []*data_manage.ChartClassifyItems
  132. // @param parentId int
  133. // @return []*data_manage.EdbChartClassify
  134. func GetExcelMenuTreeRecursive(list []*excel.ExcelClassifyItems, parentId int) []*data_manage.EdbChartClassify {
  135. res := make([]*data_manage.EdbChartClassify, 0)
  136. for _, v := range list {
  137. t := new(data_manage.EdbChartClassify)
  138. t.ClassifyId = v.ExcelClassifyId
  139. t.ClassifyName = v.ExcelClassifyName
  140. t.ParentId = v.ParentId
  141. t.IsJoinPermission = v.IsJoinPermission
  142. if v.ParentId == parentId {
  143. t.Child = GetExcelMenuTreeRecursive(list, v.ExcelClassifyId)
  144. res = append(res, t)
  145. }
  146. }
  147. return res
  148. }
  149. // GetMoveEdbChartList 获取待转移的指标/图表列表
  150. // @param source 来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格
  151. func GetMoveEdbChartList(source, subSource, userId int, keyword, classify string, startSize, pageSize int) (list []data_manage.MoveEdbChartList, total int, err error) {
  152. var condition string
  153. var pars []interface{}
  154. switch source {
  155. case 1: //手工数据指标
  156. if keyword != "" {
  157. condition += ` AND (a.SEC_NAME LIKE ? OR a.TRADE_CODE like ? ) `
  158. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  159. }
  160. if userId > 0 {
  161. condition += ` AND a.user_id = ? `
  162. pars = append(pars, userId)
  163. }
  164. if classify != "" {
  165. condition += ` AND a.classify_id IN (` + classify + `) `
  166. }
  167. total, err = models.GetEdbinfoListCount(condition, pars, "", 0)
  168. if err != nil {
  169. return
  170. }
  171. tmpList, tmpErr := models.GetEdbinfoItemList(condition, pars, startSize, pageSize, "", 0)
  172. if tmpErr != nil {
  173. err = tmpErr
  174. return
  175. }
  176. if len(tmpList) > 0 {
  177. adminIdList := make([]int, 0)
  178. for _, v := range tmpList {
  179. adminIdList = append(adminIdList, v.UserId)
  180. }
  181. adminList, tmpErr := system.GetAdminListByIdListWithoutEnable(adminIdList)
  182. if tmpErr != nil {
  183. err = tmpErr
  184. return
  185. }
  186. adminMap := make(map[int]string)
  187. for _, adminInfo := range adminList {
  188. adminMap[adminInfo.AdminId] = adminInfo.RealName
  189. }
  190. for _, v := range tmpList {
  191. list = append(list, data_manage.MoveEdbChartList{
  192. DataId: v.TradeCode,
  193. Code: v.TradeCode,
  194. Name: v.SecName,
  195. ClassifyName: v.ClassifyName,
  196. NameEn: v.SecName,
  197. ClassifyNameEn: v.ClassifyName,
  198. CreateUserId: v.UserId,
  199. CreateUserName: adminMap[v.UserId],
  200. })
  201. }
  202. }
  203. case 3, 4, utils.EdbPermissionSourceCalculate: //ETA指标库、ETA预测指标、指标加工
  204. if keyword != `` {
  205. condition += " AND (edb_code like ? OR edb_name like ? OR edb_name_en like ? OR sys_user_real_name like ? ) "
  206. pars = utils.GetLikeKeywordPars(pars, keyword, 4)
  207. }
  208. if userId > 0 {
  209. condition += ` AND sys_user_id = ? `
  210. pars = append(pars, userId)
  211. }
  212. if classify != "" {
  213. condition += ` AND classify_id IN (` + classify + `) `
  214. }
  215. edbInfoType := 0 //ETA指标库
  216. if source == 4 { //ETA预测指标
  217. edbInfoType = 1
  218. }
  219. condition += ` AND edb_info_type = ? `
  220. pars = append(pars, edbInfoType)
  221. // 指标加工
  222. if source != 4 {
  223. edbType := utils.EdbTypeBase
  224. if source == utils.EdbPermissionSourceCalculate {
  225. edbType = utils.EdbTypeCalculate
  226. }
  227. condition += ` AND edb_type = ? `
  228. pars = append(pars, edbType)
  229. }
  230. total, err = data_manage.GetEdbInfoByConditionCount(condition, pars)
  231. if err != nil {
  232. return
  233. }
  234. tmpList, tmpErr := data_manage.GetEdbInfoListByCondition(condition, pars, startSize, pageSize, "DESC")
  235. if tmpErr != nil {
  236. err = tmpErr
  237. return
  238. }
  239. for _, v := range tmpList {
  240. list = append(list, data_manage.MoveEdbChartList{
  241. DataId: strconv.Itoa(v.EdbInfoId),
  242. Code: v.EdbCode,
  243. Name: v.EdbName,
  244. NameEn: v.EdbNameEn,
  245. //ClassifyName: v.,
  246. CreateUserId: v.SysUserId,
  247. CreateUserName: v.SysUserRealName,
  248. })
  249. }
  250. case 5: //图库
  251. // 只展示eta图库
  252. condition += ` AND source = ? `
  253. pars = append(pars, utils.CHART_SOURCE_DEFAULT)
  254. // 关键字搜索
  255. if keyword != `` {
  256. condition += " AND (chart_name like ? or chart_name_en like ? OR sys_user_real_name like ? ) "
  257. pars = utils.GetLikeKeywordPars(pars, keyword, 3)
  258. }
  259. if userId > 0 {
  260. condition += ` AND sys_user_id = ? `
  261. pars = append(pars, userId)
  262. }
  263. if classify != "" {
  264. condition += ` AND chart_classify_id IN (` + classify + `) `
  265. }
  266. total, err = data_manage.GetChartInfoCountByCondition(condition, pars)
  267. if err != nil {
  268. return
  269. }
  270. tmpList, tmpErr := data_manage.GetChartInfoListByCondition(condition, pars, startSize, pageSize)
  271. if tmpErr != nil {
  272. err = tmpErr
  273. return
  274. }
  275. // 获取所有的图表分类
  276. firstClassifyList, tmpErr := data_manage.GetChartClassifyByParentId(0, 1, userId)
  277. if tmpErr != nil {
  278. err = tmpErr
  279. return
  280. }
  281. firstClassifyNameMap := make(map[int]string)
  282. firstClassifyNameEnMap := make(map[int]string)
  283. for _, v := range firstClassifyList {
  284. firstClassifyNameMap[v.ChartClassifyId] = v.ChartClassifyName
  285. firstClassifyNameEnMap[v.ChartClassifyId] = v.ChartClassifyNameEn
  286. }
  287. secondClassList, tmpErr := data_manage.GetChartClassifyAll(1, userId)
  288. if tmpErr != nil {
  289. err = tmpErr
  290. return
  291. }
  292. secondClassifyNameMap := make(map[int]string)
  293. secondClassifyNameEnMap := make(map[int]string)
  294. for _, v := range secondClassList {
  295. firstName := firstClassifyNameMap[v.ParentId]
  296. firstNameEn := firstClassifyNameEnMap[v.ParentId]
  297. if firstName != `` {
  298. firstName = firstName + " / "
  299. }
  300. if firstNameEn != `` {
  301. firstNameEn = firstNameEn + " / "
  302. }
  303. secondClassifyNameMap[v.ChartClassifyId] = firstName + v.ChartClassifyName
  304. secondClassifyNameEnMap[v.ChartClassifyId] = firstNameEn + v.ChartClassifyNameEn
  305. }
  306. for _, v := range tmpList {
  307. list = append(list, data_manage.MoveEdbChartList{
  308. DataId: strconv.Itoa(v.ChartInfoId),
  309. Name: v.ChartName,
  310. ClassifyName: secondClassifyNameMap[v.ChartClassifyId],
  311. NameEn: v.ChartNameEn,
  312. ClassifyNameEn: secondClassifyNameEnMap[v.ChartClassifyId],
  313. CreateUserId: v.SysUserId,
  314. CreateUserName: v.SysUserRealName,
  315. })
  316. }
  317. case 6: //ETA表格
  318. if keyword != `` {
  319. condition += " AND (excel_name like ? OR sys_user_real_name like ? ) "
  320. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  321. }
  322. if userId > 0 {
  323. condition += ` AND sys_user_id = ? `
  324. pars = append(pars, userId)
  325. }
  326. if classify != "" {
  327. condition += ` AND excel_classify_id IN (` + classify + `) `
  328. }
  329. condition += " AND source = ? "
  330. pars = append(pars, subSource)
  331. if subSource == utils.BALANCE_TABLE {
  332. condition += " AND parent_id = 0 AND balance_type = 0 "
  333. }
  334. total, err = excel.GetExcelInfoCountByCondition(condition, pars)
  335. if err != nil {
  336. return
  337. }
  338. tmpList, tmpErr := excel.GetNoContentExcelInfoListByCondition(condition, pars, startSize, pageSize)
  339. if tmpErr != nil {
  340. err = tmpErr
  341. return
  342. }
  343. // 获取所有的图表分类
  344. firstClassifyList, tmpErr := excel.GetExcelClassifyByParentId(0, subSource)
  345. if tmpErr != nil {
  346. err = tmpErr
  347. return
  348. }
  349. firstClassifyNameMap := make(map[int]string)
  350. for _, v := range firstClassifyList {
  351. firstClassifyNameMap[v.ExcelClassifyId] = v.ExcelClassifyName
  352. }
  353. secondClassList, tmpErr := excel.GetExcelClassifyBySource(subSource)
  354. if tmpErr != nil {
  355. err = tmpErr
  356. return
  357. }
  358. secondClassifyNameMap := make(map[int]string)
  359. for _, v := range secondClassList {
  360. firstName := firstClassifyNameMap[v.ParentId]
  361. if firstName != `` {
  362. firstName = firstName + " / "
  363. }
  364. secondClassifyNameMap[v.ExcelClassifyId] = firstName + v.ExcelClassifyName
  365. }
  366. for _, v := range tmpList {
  367. list = append(list, data_manage.MoveEdbChartList{
  368. DataId: strconv.Itoa(v.ExcelInfoId),
  369. Name: v.ExcelClassifyName,
  370. ClassifyName: secondClassifyNameMap[v.ExcelClassifyId],
  371. NameEn: v.ExcelClassifyName,
  372. ClassifyNameEn: secondClassifyNameMap[v.ExcelClassifyId],
  373. CreateUserId: v.SysUserId,
  374. CreateUserName: v.SysUserRealName,
  375. })
  376. }
  377. default:
  378. return
  379. }
  380. return
  381. }
  382. // MoveEdbChart 转移指标/图表创建人
  383. // @param source 来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格;7:计算指标库
  384. func MoveEdbChart(source, subSource, oldUserId, newUserId int, isSelectAll bool, dataId, noDataId []string, keyword, classify string, opUserId int, opUserName string) (err error, errMsg string) {
  385. adminInfo, err := system.GetSysAdminById(newUserId)
  386. if err != nil {
  387. return
  388. }
  389. // 如果勾选全部数据,那么
  390. if isSelectAll {
  391. // 找出不要的指标ID列表
  392. noDataIdMap := make(map[string]string, 0)
  393. for _, v := range noDataId {
  394. noDataIdMap[v] = v
  395. }
  396. // 需要转义的指标/图表ID列表
  397. dataId = make([]string, 0)
  398. // 获取所有指标/图表
  399. list, _, tmpErr := GetMoveEdbChartList(source, subSource, oldUserId, keyword, classify, 0, 100000)
  400. if tmpErr != nil {
  401. err = tmpErr
  402. return
  403. }
  404. for _, v := range list {
  405. if _, ok := noDataIdMap[v.DataId]; !ok {
  406. dataId = append(dataId, v.DataId)
  407. }
  408. }
  409. }
  410. // 操作记录
  411. dataPermissionMoveRecordList := make([]*data_manage_permission.DataPermissionMoveRecord, 0)
  412. uniqueCode := uuid.NewUUID().Hex32()
  413. title := `资产转移`
  414. content := `资产转入`
  415. switch source {
  416. case 1: //手工数据指标
  417. content += `(手工指标数据)`
  418. tmpList, tmpErr := models.GetEdbinfoListByCodeListByCodeIdList(dataId)
  419. if tmpErr != nil {
  420. err = tmpErr
  421. return
  422. }
  423. if len(tmpList) > 0 {
  424. for _, v := range tmpList {
  425. //if v.UserId == newUserId {
  426. // errMsg = "新创建人不可和原创建人一致"
  427. // err = errors.New(errMsg)
  428. // return
  429. //}
  430. // 操作记录
  431. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  432. DataPermissionMoveRecordId: 0,
  433. Source: int32(source),
  434. SubSource: int32(subSource),
  435. OpUniqueCode: uniqueCode,
  436. DataId: v.TradeCode,
  437. DataCode: v.TradeCode,
  438. DataName: v.SecName,
  439. OriginalUserId: int32(v.UserId),
  440. OriginalUserName: v.UserName,
  441. NewUserId: int32(adminInfo.AdminId),
  442. NewUserName: adminInfo.RealName,
  443. CreateTime: time.Now(),
  444. })
  445. }
  446. // 修改创建人
  447. err = models.ModifyEdbinfoUserIdByCodeList(dataId, newUserId, adminInfo.RealName)
  448. // 如果移动成功了,需要记录该指标的操作记录
  449. if err == nil && len(tmpList) > 0 {
  450. go afterMoveManualEdb(tmpList, adminInfo, opUserId, opUserName)
  451. }
  452. }
  453. case 3, 4, utils.EdbPermissionSourceCalculate: //ETA指标库、ETA预测指标
  454. if source == 3 {
  455. content += `(数据查看)`
  456. }
  457. if source == 4 {
  458. content += `(衍生指标)`
  459. }
  460. if source == utils.EdbPermissionSourceCalculate {
  461. content += `(指标加工)`
  462. }
  463. edbInfoIdList := make([]int, 0)
  464. for _, id := range dataId {
  465. edbInfoId, tmpErr := strconv.Atoi(id)
  466. if tmpErr != nil {
  467. err = tmpErr
  468. return
  469. }
  470. edbInfoIdList = append(edbInfoIdList, edbInfoId)
  471. }
  472. tmpList, tmpErr := data_manage.GetEdbInfoListByEdbInfoId(edbInfoIdList)
  473. if tmpErr != nil {
  474. err = tmpErr
  475. return
  476. }
  477. if len(tmpList) > 0 {
  478. for _, v := range tmpList {
  479. //if v.SysUserId == newUserId {
  480. // errMsg = "新创建人不可和原创建人一致"
  481. // err = errors.New(errMsg)
  482. // return
  483. //}
  484. // 操作记录
  485. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  486. DataPermissionMoveRecordId: 0,
  487. Source: int32(source),
  488. SubSource: int32(subSource),
  489. OpUniqueCode: uniqueCode,
  490. DataId: fmt.Sprint(v.EdbInfoId),
  491. DataCode: v.EdbCode,
  492. DataName: v.EdbName,
  493. OriginalUserId: int32(v.SysUserId),
  494. OriginalUserName: v.SysUserRealName,
  495. NewUserId: int32(adminInfo.AdminId),
  496. NewUserName: adminInfo.RealName,
  497. CreateTime: time.Now(),
  498. })
  499. }
  500. // 修改创建人
  501. err = data_manage.ModifyEdbInfoUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName)
  502. // 移除当前用户的不可操作权限
  503. {
  504. obj := data_manage.EdbInfoNoPermissionAdmin{}
  505. for _, v := range dataId {
  506. edbInfoId, _ := strconv.Atoi(v)
  507. go obj.DeleteByEdbInfoIdAndAdminId(edbInfoId, adminInfo.AdminId)
  508. }
  509. }
  510. }
  511. case 5: //图库
  512. content += `(图库)`
  513. // 只展示eta图库
  514. subSource = utils.CHART_SOURCE_DEFAULT
  515. tmpList, tmpErr := data_manage.GetChartInfoListByChartIdList(dataId)
  516. if tmpErr != nil {
  517. err = tmpErr
  518. return
  519. }
  520. if len(tmpList) > 0 {
  521. for _, v := range tmpList {
  522. //if v.SysUserId == newUserId {
  523. // errMsg = "新创建人不可和原创建人一致"
  524. // err = errors.New(errMsg)
  525. // return
  526. //}
  527. // 操作记录
  528. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  529. DataPermissionMoveRecordId: 0,
  530. Source: int32(source),
  531. SubSource: int32(subSource),
  532. OpUniqueCode: uniqueCode,
  533. DataId: fmt.Sprint(v.ChartInfoId),
  534. DataCode: fmt.Sprint(v.ChartInfoId),
  535. DataName: v.ChartName,
  536. OriginalUserId: int32(v.SysUserId),
  537. OriginalUserName: v.SysUserRealName,
  538. NewUserId: int32(adminInfo.AdminId),
  539. NewUserName: adminInfo.RealName,
  540. CreateTime: time.Now(),
  541. })
  542. }
  543. // 修改创建人
  544. err = data_manage.ModifyChartInfoUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName)
  545. }
  546. case 6: // ETA表格
  547. content += `(表格)`
  548. tmpList, tmpErr := excel.GetNoContentExcelListByExcelInfoIdList(dataId)
  549. if tmpErr != nil {
  550. err = tmpErr
  551. return
  552. }
  553. var customAnalysisIds []int
  554. if len(tmpList) > 0 {
  555. for _, v := range tmpList {
  556. //if v.SysUserId == newUserId {
  557. // errMsg = "新创建人不可和原创建人一致"
  558. // err = errors.New(errMsg)
  559. // return
  560. //}
  561. // 操作记录
  562. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  563. DataPermissionMoveRecordId: 0,
  564. Source: int32(source),
  565. SubSource: int32(subSource),
  566. OpUniqueCode: uniqueCode,
  567. DataId: fmt.Sprint(v.ExcelInfoId),
  568. DataCode: fmt.Sprint(v.ExcelInfoId),
  569. DataName: v.ExcelName,
  570. OriginalUserId: int32(v.SysUserId),
  571. OriginalUserName: v.SysUserRealName,
  572. NewUserId: int32(adminInfo.AdminId),
  573. NewUserName: adminInfo.RealName,
  574. CreateTime: time.Now(),
  575. })
  576. if v.Source == utils.CUSTOM_ANALYSIS_TABLE {
  577. customAnalysisIds = append(customAnalysisIds, v.ExcelInfoId)
  578. }
  579. }
  580. // 修改创建人
  581. err = excel.ModifyExcelInfoUserIdByCodeList(dataId, adminInfo.AdminId, adminInfo.RealName)
  582. if err != nil {
  583. return
  584. }
  585. // 由于自定义分析表分类私有化, 如果转移的是自定义分析表, 那么需要将excel转移至未分类下
  586. if len(customAnalysisIds) > 0 {
  587. err = MoveCustomAnalysisExcel2DefaultClassify(customAnalysisIds, adminInfo.AdminId, adminInfo.RealName)
  588. }
  589. }
  590. default:
  591. return
  592. }
  593. // 转移失败了那么就直接返回了
  594. if err != nil {
  595. return
  596. }
  597. // 添加资产转移的记录
  598. dataPermissionMessage := &data_manage_permission.DataPermissionMessage{
  599. DataPermissionMessageId: 0,
  600. SendUserId: int32(opUserId),
  601. ReceiveUserId: int32(adminInfo.AdminId),
  602. Content: title,
  603. Remark: content,
  604. OpType: 1,
  605. Source: int32(source),
  606. SubSource: int32(subSource),
  607. OpUniqueCode: uniqueCode,
  608. IsRead: 0,
  609. CreateTime: time.Now(),
  610. ModifyTime: time.Now(),
  611. }
  612. tmpErr := data_manage_permission.AddDataPermissionMoveRecord(dataPermissionMoveRecordList, dataPermissionMessage)
  613. // 失败的话,日志记录
  614. if tmpErr != nil {
  615. // 序列化结构体为 JSON 数据
  616. recordListJsonBytes, tmpErr2 := json.Marshal(dataPermissionMoveRecordList)
  617. if tmpErr2 != nil {
  618. utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;list转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage))
  619. return
  620. }
  621. // 序列化结构体为 JSON 数据
  622. messageJsonBytes, tmpErr2 := json.Marshal(dataPermissionMessage)
  623. if tmpErr2 != nil {
  624. utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;message转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage))
  625. return
  626. }
  627. utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;操作记录:%s;消息记录:%s", tmpErr.Error(), string(recordListJsonBytes), string(messageJsonBytes)))
  628. }
  629. return
  630. }
  631. // MoveAllEdbChart
  632. // @Description: 通过原创建人转移指标/图表创建人
  633. // @author: Roc
  634. // @datetime 2024-03-26 15:11:12
  635. // @param sourceList []int 1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格
  636. // @param oldUserId []int
  637. // @param userId int
  638. // @param opUserId int
  639. // @return err error
  640. // @return errMsg string
  641. func MoveAllEdbChart(sourceList, oldUserIdList []int, userId, opUserId int) (err error, errMsg string) {
  642. adminInfo, err := system.GetSysAdminById(userId)
  643. if err != nil {
  644. return
  645. }
  646. // 操作记录
  647. dataPermissionMoveRecordList := make([]*data_manage_permission.DataPermissionMoveRecord, 0)
  648. uniqueCode := uuid.NewUUID().Hex32()
  649. title := `资产转移`
  650. content := `资产转入`
  651. sourceStrList := make([]string, 0)
  652. var isMoveManual, isMoveMysteelChemical, isMoveEdb, isMovePredictEdb, isMoveChart, isMoveExcel bool
  653. var customAnalysisIds []int
  654. // 遍历需要转移的模块,1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格,并找出当前需要转移的资产
  655. for _, source := range sourceList {
  656. switch source {
  657. case 1: //手工数据指标
  658. sourceStrList = append(sourceStrList, "手工指标数据")
  659. tmpList, tmpErr := models.GetEdbinfoListByCodeListByUserId(oldUserIdList)
  660. if tmpErr != nil {
  661. err = tmpErr
  662. return
  663. }
  664. if len(tmpList) > 0 {
  665. for _, v := range tmpList {
  666. // 操作记录
  667. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  668. DataPermissionMoveRecordId: 0,
  669. Source: int32(source),
  670. SubSource: 0,
  671. OpUniqueCode: uniqueCode,
  672. DataId: v.TradeCode,
  673. DataCode: v.TradeCode,
  674. DataName: v.SecName,
  675. OriginalUserId: int32(v.UserId),
  676. OriginalUserName: v.UserName,
  677. NewUserId: int32(adminInfo.AdminId),
  678. NewUserName: adminInfo.RealName,
  679. CreateTime: time.Now(),
  680. })
  681. }
  682. isMoveManual = true
  683. }
  684. case 3: //ETA指标库
  685. sourceStrList = append(sourceStrList, "ETA指标库")
  686. edbInfoType := 0
  687. tmpList, tmpErr := data_manage.GetEdbInfoListByUserId(oldUserIdList, edbInfoType)
  688. if tmpErr != nil {
  689. err = tmpErr
  690. return
  691. }
  692. if len(tmpList) > 0 {
  693. for _, v := range tmpList {
  694. // 操作记录
  695. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  696. DataPermissionMoveRecordId: 0,
  697. Source: int32(source),
  698. SubSource: int32(edbInfoType),
  699. OpUniqueCode: uniqueCode,
  700. DataId: fmt.Sprint(v.EdbInfoId),
  701. DataCode: v.EdbCode,
  702. DataName: v.EdbName,
  703. OriginalUserId: int32(v.SysUserId),
  704. OriginalUserName: v.SysUserRealName,
  705. NewUserId: int32(adminInfo.AdminId),
  706. NewUserName: adminInfo.RealName,
  707. CreateTime: time.Now(),
  708. })
  709. }
  710. isMoveEdb = true
  711. }
  712. case 4: //ETA预测指标
  713. sourceStrList = append(sourceStrList, "ETA预测指标")
  714. edbInfoType := 1
  715. tmpList, tmpErr := data_manage.GetEdbInfoListByUserId(oldUserIdList, edbInfoType)
  716. if tmpErr != nil {
  717. err = tmpErr
  718. return
  719. }
  720. if len(tmpList) > 0 {
  721. for _, v := range tmpList {
  722. // 操作记录
  723. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  724. DataPermissionMoveRecordId: 0,
  725. Source: int32(source),
  726. SubSource: int32(edbInfoType),
  727. OpUniqueCode: uniqueCode,
  728. DataId: fmt.Sprint(v.EdbInfoId),
  729. DataCode: v.EdbCode,
  730. DataName: v.EdbName,
  731. OriginalUserId: int32(v.SysUserId),
  732. OriginalUserName: v.SysUserRealName,
  733. NewUserId: int32(adminInfo.AdminId),
  734. NewUserName: adminInfo.RealName,
  735. CreateTime: time.Now(),
  736. })
  737. }
  738. isMovePredictEdb = true
  739. }
  740. case 5: //ETA图库
  741. sourceStrList = append(sourceStrList, "ETA图库")
  742. // 只展示eta图库
  743. subSource := utils.CHART_SOURCE_DEFAULT
  744. tmpList, tmpErr := data_manage.GetChartInfoListByUserId(oldUserIdList, subSource)
  745. if tmpErr != nil {
  746. err = tmpErr
  747. return
  748. }
  749. if len(tmpList) > 0 {
  750. for _, v := range tmpList {
  751. // 操作记录
  752. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  753. DataPermissionMoveRecordId: 0,
  754. Source: int32(source),
  755. SubSource: int32(subSource),
  756. OpUniqueCode: uniqueCode,
  757. DataId: fmt.Sprint(v.ChartInfoId),
  758. DataCode: fmt.Sprint(v.ChartInfoId),
  759. DataName: v.ChartName,
  760. OriginalUserId: int32(v.SysUserId),
  761. OriginalUserName: v.SysUserRealName,
  762. NewUserId: int32(adminInfo.AdminId),
  763. NewUserName: adminInfo.RealName,
  764. CreateTime: time.Now(),
  765. })
  766. }
  767. isMoveChart = true
  768. }
  769. case 6: // ETA表格
  770. sourceStrList = append(sourceStrList, "ETA表格")
  771. tmpList, tmpErr := excel.GetNoContentExcelListByUserId(oldUserIdList)
  772. if tmpErr != nil {
  773. err = tmpErr
  774. return
  775. }
  776. if len(tmpList) > 0 {
  777. for _, v := range tmpList {
  778. // 操作记录
  779. dataPermissionMoveRecordList = append(dataPermissionMoveRecordList, &data_manage_permission.DataPermissionMoveRecord{
  780. DataPermissionMoveRecordId: 0,
  781. Source: int32(source),
  782. SubSource: int32(v.Source),
  783. OpUniqueCode: uniqueCode,
  784. DataId: fmt.Sprint(v.ExcelInfoId),
  785. DataCode: fmt.Sprint(v.ExcelInfoId),
  786. DataName: v.ExcelName,
  787. OriginalUserId: int32(v.SysUserId),
  788. OriginalUserName: v.SysUserRealName,
  789. NewUserId: int32(adminInfo.AdminId),
  790. NewUserName: adminInfo.RealName,
  791. CreateTime: time.Now(),
  792. })
  793. if v.Source == utils.CUSTOM_ANALYSIS_TABLE {
  794. customAnalysisIds = append(customAnalysisIds, v.ExcelInfoId)
  795. }
  796. }
  797. isMoveExcel = true
  798. }
  799. default:
  800. }
  801. }
  802. // 修改创建人
  803. err = data_manage_permission.ModifyDataUserIdByOldUserId(oldUserIdList, adminInfo.AdminId, adminInfo.RealName, isMoveManual, isMoveMysteelChemical, isMoveEdb, isMovePredictEdb, isMoveChart, isMoveExcel)
  804. // 转移失败了那么就直接返回了
  805. if err != nil {
  806. return
  807. }
  808. // 由于自定义分析表分类私有化, 如果转移的含自定义分析表, 那么需要将excel转移至未分类下
  809. if len(customAnalysisIds) > 0 {
  810. err = MoveCustomAnalysisExcel2DefaultClassify(customAnalysisIds, adminInfo.AdminId, adminInfo.RealName)
  811. if err != nil {
  812. err = fmt.Errorf("MoveCustomAnalysisExcel2DefaultClassify, err: %v", err)
  813. return
  814. }
  815. }
  816. // 添加资产转移的记录
  817. content += fmt.Sprintf("(%s)", strings.Join(sourceStrList, ","))
  818. dataPermissionMessage := &data_manage_permission.DataPermissionMessage{
  819. DataPermissionMessageId: 0,
  820. SendUserId: int32(opUserId),
  821. ReceiveUserId: int32(adminInfo.AdminId),
  822. Content: title,
  823. Remark: content,
  824. OpType: 2,
  825. OpUniqueCode: uniqueCode,
  826. IsRead: 0,
  827. CreateTime: time.Now(),
  828. ModifyTime: time.Now(),
  829. }
  830. tmpErr := data_manage_permission.AddDataPermissionMoveRecord(dataPermissionMoveRecordList, dataPermissionMessage)
  831. // 失败的话,日志记录
  832. if tmpErr != nil {
  833. // 序列化结构体为 JSON 数据
  834. recordListJsonBytes, tmpErr2 := json.Marshal(dataPermissionMoveRecordList)
  835. if tmpErr2 != nil {
  836. utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;list转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage))
  837. return
  838. }
  839. // 序列化结构体为 JSON 数据
  840. messageJsonBytes, tmpErr2 := json.Marshal(dataPermissionMessage)
  841. if tmpErr2 != nil {
  842. utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;message转json失败:;操作记录:%+v;消息记录:%+v", tmpErr.Error(), dataPermissionMoveRecordList, dataPermissionMessage))
  843. return
  844. }
  845. utils.FileLog.Error(fmt.Sprintf("添加资产转移的记录失败:%s;操作记录:%s;消息记录:%s", tmpErr.Error(), string(recordListJsonBytes), string(messageJsonBytes)))
  846. }
  847. return
  848. }
  849. // GetMoveEdbChartCount 获取待转移的指标/图表列表
  850. // @param userId 用户
  851. // @param countType 来源 :1:设置资产权限;2:设置数据权限
  852. func GetMoveEdbChartCount(userId, countType int) (sourceMap map[int]int, err error) {
  853. sourceMap = make(map[int]int)
  854. {
  855. // 手工数据指标
  856. var condition string
  857. var pars []interface{}
  858. if userId > 0 {
  859. condition += ` AND a.user_id = ? `
  860. pars = append(pars, userId)
  861. }
  862. total, tmpErr := models.GetEdbinfoListCount(condition, pars, "", 0)
  863. if tmpErr != nil {
  864. err = tmpErr
  865. return
  866. }
  867. sourceMap[1] = total
  868. }
  869. {
  870. // 基础指标
  871. var condition string
  872. var pars []interface{}
  873. if userId > 0 {
  874. condition += ` AND sys_user_id = ? `
  875. pars = append(pars, userId)
  876. }
  877. condition += ` AND edb_info_type = ? AND edb_type = ? `
  878. pars = append(pars, 0, utils.EdbTypeBase)
  879. total, tmpErr := data_manage.GetEdbInfoByConditionCount(condition, pars)
  880. if tmpErr != nil {
  881. err = tmpErr
  882. return
  883. }
  884. sourceMap[3] = total
  885. }
  886. {
  887. // ETA预测指标库
  888. var condition string
  889. var pars []interface{}
  890. if userId > 0 {
  891. condition += ` AND sys_user_id = ? `
  892. pars = append(pars, userId)
  893. }
  894. //ETA预测指标
  895. condition += ` AND edb_info_type = ? `
  896. pars = append(pars, 1)
  897. total, tmpErr := data_manage.GetEdbInfoByConditionCount(condition, pars)
  898. if tmpErr != nil {
  899. err = tmpErr
  900. return
  901. }
  902. sourceMap[4] = total
  903. }
  904. {
  905. // 图库
  906. var condition string
  907. var pars []interface{}
  908. condition += ` AND source = ? `
  909. pars = append(pars, utils.CHART_SOURCE_DEFAULT)
  910. if userId > 0 {
  911. condition += ` AND sys_user_id = ? `
  912. pars = append(pars, userId)
  913. }
  914. total, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  915. if tmpErr != nil {
  916. err = tmpErr
  917. return
  918. }
  919. sourceMap[5] = total
  920. }
  921. {
  922. // ETA表格
  923. var condition string
  924. var pars []interface{}
  925. condition += ` AND is_delete = 0 `
  926. if userId > 0 {
  927. condition += ` AND sys_user_id = ? `
  928. pars = append(pars, userId)
  929. }
  930. switch countType {
  931. case 2:
  932. condition += ` AND source in (2,3,5) `
  933. default:
  934. condition += ` AND source in (2,3,4,5) `
  935. }
  936. // 平衡表
  937. condition += " AND (parent_id = 0 AND balance_type = 0)"
  938. total, tmpErr := excel.GetExcelInfoCountByCondition(condition, pars)
  939. if tmpErr != nil {
  940. err = tmpErr
  941. return
  942. }
  943. sourceMap[6] = total
  944. }
  945. {
  946. // 计算指标
  947. var condition string
  948. var pars []interface{}
  949. if userId > 0 {
  950. condition += ` AND sys_user_id = ? `
  951. pars = append(pars, userId)
  952. }
  953. condition += ` AND edb_info_type = ? AND edb_type = ? `
  954. pars = append(pars, 0, utils.EdbTypeCalculate)
  955. total, tmpErr := data_manage.GetEdbInfoByConditionCount(condition, pars)
  956. if tmpErr != nil {
  957. err = tmpErr
  958. return
  959. }
  960. sourceMap[7] = total
  961. }
  962. return
  963. }
  964. // afterMoveManualEdb
  965. // @Description: 手工数据移动后的操作
  966. // @author: Roc
  967. // @datetime 2024-08-01 13:55:59
  968. // @param tmpList []*models.Edbinfo
  969. // @param adminInfo *system.Admin
  970. // @param opUserId int
  971. // @param opUserName string
  972. func afterMoveManualEdb(tmpList []*models.Edbinfo, adminInfo *system.Admin, opUserId int, opUserName string) {
  973. recordList := make([]*models.EdbinfoOpRecord, 0)
  974. for _, tmpManualInfo := range tmpList {
  975. remark := fmt.Sprintf("数据资产由%s转移给%s", tmpManualInfo.UserName, adminInfo.RealName)
  976. recordList = append(recordList, &models.EdbinfoOpRecord{
  977. TradeCode: tmpManualInfo.TradeCode,
  978. Remark: remark,
  979. UserId: opUserId,
  980. UserName: opUserName,
  981. CreateTime: time.Now(),
  982. })
  983. }
  984. obj := models.EdbinfoOpRecord{}
  985. _ = obj.MulCreate(recordList)
  986. }
  987. // MoveCustomAnalysisExcel2DefaultClassify 移动表格至用户未分类目录下
  988. func MoveCustomAnalysisExcel2DefaultClassify(excelIds []int, adminId int, realName string) (err error) {
  989. if len(excelIds) == 0 {
  990. return
  991. }
  992. var moveClassifyId int
  993. // 查询默认分类
  994. cond := ` AND excel_classify_name = ? AND sys_user_id = ?`
  995. pars := make([]interface{}, 0)
  996. pars = append(pars, "未分类", adminId)
  997. classify, e := excel.GetExcelClassifyByCondition(cond, pars)
  998. if e != nil && !utils.IsErrNoRow(e) {
  999. err = fmt.Errorf("获取默认分类失败, %v", e)
  1000. return
  1001. }
  1002. if e == nil && classify.ExcelClassifyId > 0 {
  1003. moveClassifyId = classify.ExcelClassifyId
  1004. }
  1005. // 新增一个新的分类
  1006. if classify == nil || classify.ExcelClassifyId <= 0 {
  1007. classifyNew := &excel.ExcelClassify{
  1008. ExcelClassifyName: "未分类",
  1009. Source: utils.CUSTOM_ANALYSIS_TABLE,
  1010. SysUserId: adminId,
  1011. SysUserRealName: realName,
  1012. Level: 1,
  1013. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  1014. CreateTime: time.Now(),
  1015. ModifyTime: time.Now(),
  1016. }
  1017. if _, e = excel.AddExcelClassify(classifyNew); e != nil {
  1018. err = fmt.Errorf("新增默认分类失败, %v", e)
  1019. return
  1020. }
  1021. moveClassifyId = classifyNew.ExcelClassifyId
  1022. }
  1023. if moveClassifyId <= 0 {
  1024. err = fmt.Errorf("移动分类ID有误")
  1025. return
  1026. }
  1027. // 更新表格分类ID
  1028. if e = excel.UpdateExcelInfoClassifyIdByIds(moveClassifyId, excelIds); e != nil {
  1029. err = fmt.Errorf("移动多表格分类失败, %v", e)
  1030. return
  1031. }
  1032. return
  1033. }