data_move.go 35 KB

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