chart_info_excel_balance.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/data_manage/chart_theme"
  7. excelModel "eta_gn/eta_api/models/data_manage/excel"
  8. "eta_gn/eta_api/models/data_manage/excel/request"
  9. "eta_gn/eta_api/models/system"
  10. "eta_gn/eta_api/services/data/data_manage_permission"
  11. "eta_gn/eta_api/utils"
  12. "fmt"
  13. "math"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // AddBalanceExcelChart 添加平衡表图表
  20. func AddBalanceExcelChart(excelInfo *excelModel.ExcelInfo, req request.AddBalanceTableChartReq, sysUser *system.Admin, dataListMap map[int][]*data_manage.EdbDataList) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  21. if len(req.ChartEdbInfoList) == 0 {
  22. errMsg = "图表数据不能为空!"
  23. err = fmt.Errorf("图表数据不能为空!%s", err.Error())
  24. return
  25. }
  26. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  27. for k, chartEdb := range req.ChartEdbInfoList {
  28. excelEdb := new(excelModel.ExcelChartEdb)
  29. excelEdb.EdbName = chartEdb.EdbName
  30. excelEdb.EdbNameEn = excelEdb.EdbName
  31. excelEdb.Unit = chartEdb.Unit
  32. excelEdb.UnitEn = excelEdb.Unit
  33. randStr := utils.GetRandDigit(4)
  34. excelEdb.EdbCode = `T` + time.Now().Format("060102150405") + "_" + randStr
  35. excelEdb.ExcelInfoId = excelInfo.ExcelInfoId
  36. excelEdb.DateSequence = chartEdb.DateSequenceStr
  37. excelEdb.DataSequence = chartEdb.DataSequenceStr
  38. excelEdb.SysUserId = sysUser.AdminId
  39. excelEdb.SysUserRealName = sysUser.RealName
  40. excelEdb.MaxData = chartEdb.MaxData
  41. excelEdb.MinData = chartEdb.MinData
  42. excelEdb.IsOrder = chartEdb.IsOrder
  43. excelEdb.IsAxis = chartEdb.IsAxis
  44. excelEdb.FromTag = chartEdb.FromTag
  45. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  46. excelEdb.LeadValue = chartEdb.LeadValue
  47. excelEdb.LeadUnit = chartEdb.LeadUnit
  48. excelEdb.CreateTime = time.Now()
  49. excelEdb.ModifyTime = time.Now()
  50. excelEdb.ChartWidth = 1
  51. var dataList []*excelModel.ExcelChartData
  52. if excelInfo.BalanceType == 1 {
  53. tmpList, ok := dataListMap[k]
  54. if ok {
  55. for _, l := range tmpList {
  56. tmp := &excelModel.ExcelChartData{
  57. DataTime: l.DataTime,
  58. Value: l.Value,
  59. DataTimestamp: l.DataTimestamp,
  60. }
  61. dataList = append(dataList, tmp)
  62. }
  63. }
  64. }
  65. // 处理日期列表和值列表
  66. addItem := &excelModel.AddChartEdbAndDataItem{
  67. ChartEdb: excelEdb,
  68. DataList: dataList,
  69. }
  70. chartEdbList = append(chartEdbList, addItem)
  71. }
  72. chartInfo, err, errMsg, isSendEmail = addBalanceExcelChart(req, sysUser.AdminId, sysUser.RealName)
  73. if err != nil {
  74. if errMsg == "" {
  75. errMsg = "新增图表失败!"
  76. }
  77. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  78. return
  79. }
  80. obj := new(excelModel.ExcelChartEdb)
  81. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, []int{})
  82. if err != nil {
  83. errMsg = "新增图表失败!"
  84. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  85. return
  86. }
  87. return
  88. }
  89. func addBalanceExcelChart(req request.AddBalanceTableChartReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  90. isSendEmail = true // 默认错误的时候要发送邮件
  91. req.ChartName = strings.Trim(req.ChartName, " ")
  92. if req.ChartName == "" {
  93. errMsg = "请填写图表名称!"
  94. err = fmt.Errorf(errMsg)
  95. isSendEmail = false
  96. return
  97. }
  98. chartType := req.ChartType
  99. extraConfig := req.ExtraConfig
  100. // 查找默认主题设置
  101. // 查找主题类型id
  102. chartThemeType, err := chart_theme.GetChartThemeTypeByChartType(chartType)
  103. if err != nil {
  104. errMsg = "查找主题类型失败!"
  105. err = fmt.Errorf("%s, %s", errMsg, err.Error())
  106. return
  107. }
  108. chartThemeId := chartThemeType.DefaultChartThemeId
  109. // 季节性图表额外配置信息
  110. var seasonExtraConfig string
  111. if len(req.ChartEdbInfoList) <= 0 {
  112. errMsg = "请选择指标!"
  113. err = fmt.Errorf(errMsg)
  114. return
  115. }
  116. if chartType == 2 {
  117. // 处理季节性图表横轴配置
  118. {
  119. if req.SeasonExtraConfig.XEndDate != "" {
  120. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  121. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  122. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  123. return
  124. }
  125. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  126. if tErr != nil {
  127. errMsg = "季节性图表配置信息异常"
  128. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  129. return
  130. }
  131. seasonExtraConfig = string(seasonExtra)
  132. }
  133. }
  134. }
  135. // 图表额外配置
  136. extraConfig, _, err, errMsg = HandleExtraConfig(0, chartType, extraConfig)
  137. if err != nil {
  138. if errMsg == `` {
  139. errMsg = "指标异常!"
  140. }
  141. err = fmt.Errorf("指标异常!Err:" + err.Error())
  142. return
  143. }
  144. //判断图表是否存在
  145. var condition string
  146. var pars []interface{}
  147. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  148. condition += " AND chart_classify_id=0 "
  149. condition += " AND chart_name=? AND source = ? "
  150. pars = append(pars, req.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  151. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  152. if err != nil {
  153. errMsg = "判断图表名称是否存在失败"
  154. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  155. return
  156. }
  157. if count > 0 {
  158. errMsg = "图表名称已存在,请重新填写"
  159. err = fmt.Errorf("判断图表名称是否存在失败")
  160. isSendEmail = false
  161. return
  162. }
  163. chartInfo = new(data_manage.ChartInfo)
  164. chartInfo.ChartName = req.ChartName
  165. chartInfo.ChartNameEn = chartInfo.ChartName
  166. //chartInfo.EdbInfoIds = edbInfoIdStr
  167. //chartInfo.ChartClassifyId = req.ChartClassifyId
  168. chartInfo.SysUserId = sysUserId
  169. chartInfo.SysUserRealName = sysUserRealName
  170. chartInfo.CreateTime = time.Now()
  171. chartInfo.ModifyTime = time.Now()
  172. chartInfo.IsSetName = 0
  173. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  174. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  175. // todo 判断是否需要重新计算用户的start_date
  176. chartInfo.DateType = 3
  177. if chartType == 0 {
  178. chartType = 1
  179. }
  180. chartInfo.ChartType = chartType
  181. calendar := req.Calendar
  182. if calendar == "" {
  183. calendar = "公历"
  184. }
  185. chartInfo.Calendar = calendar
  186. chartInfo.LeftMin = req.LeftMin
  187. chartInfo.LeftMax = req.LeftMax
  188. chartInfo.RightMin = req.RightMin
  189. chartInfo.RightMax = req.RightMax
  190. chartInfo.Right2Min = req.Right2Min
  191. chartInfo.Right2Max = req.Right2Max
  192. chartInfo.MinMaxSave = req.MinMaxSave
  193. chartInfo.ExtraConfig = extraConfig
  194. chartInfo.SeasonExtraConfig = seasonExtraConfig
  195. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  196. chartInfo.ChartThemeId = chartThemeId
  197. chartInfo.SourcesFrom = req.SourcesFrom
  198. return
  199. }
  200. // EditBalanceExcelChart 添加平衡表图表
  201. func EditBalanceExcelChart(excelInfo *excelModel.ExcelInfo, req request.AddBalanceTableChartReq, sysUser *system.Admin, dataListMap map[int][]*data_manage.EdbDataList) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  202. if len(req.ChartEdbInfoList) == 0 {
  203. errMsg = "图表数据不能为空!"
  204. err = fmt.Errorf("图表数据不能为空!%s", err.Error())
  205. return
  206. }
  207. //查询已有的mapping
  208. mapping, err := excelModel.GetExcelChartEdbMappingByChartInfoId(req.ChartInfoId)
  209. if err != nil {
  210. errMsg = "查询图表mapping失败!"
  211. err = fmt.Errorf("查询图表mapping失败!%s", err.Error())
  212. return
  213. }
  214. deleteMap := make(map[int]int)
  215. for _, v := range mapping {
  216. deleteMap[v.ExcelChartEdbId] = v.ExcelChartEdbId
  217. }
  218. // 查询edbIds
  219. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  220. for k, chartEdb := range req.ChartEdbInfoList {
  221. if _, ok := deleteMap[chartEdb.ExcelChartEdbId]; ok {
  222. delete(deleteMap, chartEdb.ExcelChartEdbId)
  223. }
  224. excelChartEdbId := chartEdb.ExcelChartEdbId
  225. excelEdb := new(excelModel.ExcelChartEdb)
  226. if excelChartEdbId > 0 {
  227. excelEdb, err = excelModel.GetExcelChartEdbById(excelChartEdbId)
  228. if err != nil {
  229. if utils.IsErrNoRow(err) {
  230. errMsg = "指标不存在!"
  231. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(excelChartEdbId))
  232. return
  233. } else {
  234. errMsg = "获取指标信息失败!"
  235. err = errors.New("获取图表的指标信息失败,Err:" + err.Error())
  236. return
  237. }
  238. }
  239. } else {
  240. excelEdb.ExcelInfoId = excelInfo.ExcelInfoId
  241. randStr := utils.GetRandDigit(4)
  242. excelEdb.EdbCode = `T` + time.Now().Format("060102150405") + "_" + randStr
  243. excelEdb.SysUserId = sysUser.AdminId
  244. excelEdb.SysUserRealName = sysUser.RealName
  245. excelEdb.CreateTime = time.Now()
  246. }
  247. excelEdb.EdbName = chartEdb.EdbName
  248. excelEdb.EdbNameEn = excelEdb.EdbName
  249. excelEdb.Unit = chartEdb.Unit
  250. excelEdb.UnitEn = excelEdb.Unit
  251. excelEdb.DateSequence = chartEdb.DateSequenceStr
  252. excelEdb.DataSequence = chartEdb.DataSequenceStr
  253. excelEdb.MaxData = chartEdb.MaxData
  254. excelEdb.MinData = chartEdb.MinData
  255. excelEdb.IsOrder = chartEdb.IsOrder
  256. excelEdb.IsAxis = chartEdb.IsAxis
  257. excelEdb.FromTag = chartEdb.FromTag
  258. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  259. excelEdb.LeadValue = chartEdb.LeadValue
  260. excelEdb.LeadUnit = chartEdb.LeadUnit
  261. excelEdb.ChartWidth = 1
  262. excelEdb.ModifyTime = time.Now()
  263. var dataList []*excelModel.ExcelChartData
  264. if excelInfo.BalanceType == 1 {
  265. tmpList, ok := dataListMap[k]
  266. if ok {
  267. /*errMsg = "查询图表数据失败!"
  268. err = fmt.Errorf("查询图表数据失败!")
  269. return*/
  270. for _, l := range tmpList {
  271. tmp := &excelModel.ExcelChartData{
  272. DataTime: l.DataTime,
  273. Value: l.Value,
  274. DataTimestamp: l.DataTimestamp,
  275. }
  276. dataList = append(dataList, tmp)
  277. }
  278. }
  279. }
  280. // 处理日期列表和值列表
  281. addItem := &excelModel.AddChartEdbAndDataItem{
  282. ChartEdb: excelEdb,
  283. DataList: dataList,
  284. }
  285. chartEdbList = append(chartEdbList, addItem)
  286. }
  287. chartInfo, err, errMsg, isSendEmail = editBalanceExcelChart(req)
  288. if err != nil {
  289. //errMsg = "新增图表失败!"
  290. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  291. return
  292. }
  293. obj := new(excelModel.ExcelChartEdb)
  294. //删除原先多余的指标
  295. deleteEdbIds := make([]int, 0)
  296. for k, _ := range deleteMap {
  297. deleteEdbIds = append(deleteEdbIds, k)
  298. }
  299. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, deleteEdbIds)
  300. if err != nil {
  301. errMsg = "新增图表失败!"
  302. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  303. return
  304. }
  305. return
  306. }
  307. func editBalanceExcelChart(req request.AddBalanceTableChartReq) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  308. isSendEmail = true // 默认错误的时候要发送邮件
  309. chartType := req.ChartType
  310. extraConfig := req.ExtraConfig
  311. chartInfo, err = data_manage.GetChartInfoById(req.ChartInfoId)
  312. if err != nil {
  313. if utils.IsErrNoRow(err) {
  314. errMsg = "图表已被删除,请刷新页面"
  315. err = errors.New(errMsg)
  316. isSendEmail = false
  317. return
  318. }
  319. errMsg = "获取图表信息失败"
  320. err = errors.New("获取图表信息失败,Err:" + err.Error())
  321. return
  322. }
  323. // 查找主题类型id
  324. chartThemeType, err := chart_theme.GetChartThemeTypeByChartType(chartType)
  325. if err != nil {
  326. errMsg = "查找主题类型失败!"
  327. err = fmt.Errorf("%s, %s", errMsg, err.Error())
  328. return
  329. }
  330. chartThemeId := chartThemeType.DefaultChartThemeId
  331. // 季节性图表额外配置信息
  332. var seasonExtraConfig string
  333. if chartType == 2 {
  334. // 处理季节性图表横轴配置
  335. {
  336. if req.SeasonExtraConfig.XEndDate != "" {
  337. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  338. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  339. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  340. return
  341. }
  342. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  343. if tErr != nil {
  344. errMsg = "季节性图表配置信息异常"
  345. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  346. return
  347. }
  348. seasonExtraConfig = string(seasonExtra)
  349. }
  350. }
  351. }
  352. /*var extraConfigEdbInfoIdArr []int
  353. extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  354. if err != nil {
  355. err = errors.New("添加失败:" + err.Error())
  356. return
  357. }*/
  358. // 图表额外配置
  359. extraConfig, _, err, errMsg = HandleExtraConfig(0, chartType, extraConfig)
  360. if err != nil {
  361. if errMsg == `` {
  362. errMsg = "指标异常!"
  363. }
  364. err = fmt.Errorf("指标异常!Err:" + err.Error())
  365. return
  366. }
  367. //判断图表是否存在
  368. var condition string
  369. var pars []interface{}
  370. condition += " AND chart_info_id<>? "
  371. pars = append(pars, req.ChartInfoId)
  372. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  373. condition += " AND chart_classify_id=0 "
  374. condition += " AND chart_name=? AND source = ? "
  375. pars = append(pars, req.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  376. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  377. if err != nil {
  378. errMsg = "判断图表名称是否存在失败"
  379. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  380. return
  381. }
  382. if count > 0 {
  383. errMsg = "图表名称已存在,请重新填写"
  384. err = fmt.Errorf("判断图表名称是否存在失败")
  385. isSendEmail = false
  386. return
  387. }
  388. // todo 判断是否是禁用的图表
  389. // disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  390. chartInfo.ChartName = req.ChartName
  391. chartInfo.ChartNameEn = chartInfo.ChartName
  392. //chartInfo.EdbInfoIds = edbInfoIdStr
  393. //chartInfo.ChartClassifyId = req.ChartClassifyId
  394. chartInfo.ModifyTime = time.Now()
  395. chartInfo.IsSetName = 0
  396. // todo 判断是否需要重新计算用户的start_date
  397. chartInfo.DateType = 3
  398. if chartType == 0 {
  399. chartType = 1
  400. }
  401. chartInfo.ChartType = chartType
  402. calendar := req.Calendar
  403. if calendar == "" {
  404. calendar = "公历"
  405. }
  406. chartInfo.Calendar = calendar
  407. /* chartInfo.StartDate = req.StartDate
  408. chartInfo.EndDate = req.EndDate
  409. chartInfo.SeasonStartDate = req.StartDate
  410. chartInfo.SeasonEndDate = req.EndDate*/
  411. chartInfo.LeftMin = req.LeftMin
  412. chartInfo.LeftMax = req.LeftMax
  413. chartInfo.RightMin = req.RightMin
  414. chartInfo.RightMax = req.RightMax
  415. chartInfo.Right2Min = req.Right2Min
  416. chartInfo.Right2Max = req.Right2Max
  417. chartInfo.MinMaxSave = req.MinMaxSave
  418. //chartInfo.Disabled = disableVal
  419. //chartInfo.BarConfig = barChartConf
  420. chartInfo.ExtraConfig = extraConfig
  421. chartInfo.SeasonExtraConfig = seasonExtraConfig
  422. //chartInfo.StartYear = req.StartYear
  423. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  424. chartInfo.ChartThemeId = chartThemeId
  425. chartInfo.SourcesFrom = req.SourcesFrom
  426. /* chartInfo.Instructions = req.Instructions
  427. chartInfo.MarkersLines = req.MarkersLines
  428. chartInfo.MarkersAreas = req.MarkersAreas
  429. chartInfo.Unit = req.Unit
  430. chartInfo.UnitEn = req.UnitEn*/
  431. /*newId, err := data_manage.AddChartInfo(chartInfo)
  432. if err != nil {
  433. errMsg = `保存失败`
  434. err = fmt.Errorf("保存失败,Err:" + err.Error())
  435. return
  436. }
  437. chartInfo.ChartInfoId = int(newId)*/
  438. //添加es数据
  439. //go AddOrEditChartInfoToEs(chartInfo.ChartInfoId)
  440. return
  441. }
  442. func GetBalanceExcelChartDetail(chartInfo *data_manage.ChartInfoView, mappingListTmp []*excelModel.ExcelChartEdb, sysUser *system.Admin, dataListMap map[int][]*data_manage.EdbDataList) (resp *data_manage.ChartInfoDetailResp, err error, errMsg string) {
  443. defer func() {
  444. for _, v := range resp.EdbInfoList {
  445. v.ConvertToResp()
  446. }
  447. }()
  448. // 图表数据权限
  449. {
  450. // 已授权分类id
  451. permissionChartIdList, permissionClassifyIdList, e := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  452. if e != nil {
  453. errMsg = "获取失败"
  454. err = fmt.Errorf("获取已授权分类id数据失败,Err:" + err.Error())
  455. return
  456. }
  457. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  458. }
  459. chartInfoId := chartInfo.ChartInfoId
  460. resp = new(data_manage.ChartInfoDetailResp)
  461. // 获取主题样式
  462. chartTheme, err := GetChartThemeConfig(chartInfo.ChartThemeId, 1, chartInfo.ChartType)
  463. if err != nil {
  464. errMsg = "获取失败"
  465. err = fmt.Errorf(" 获取主题信息失败 Err:%s", err.Error())
  466. return
  467. }
  468. chartInfo.ChartThemeStyle = chartTheme.Config
  469. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  470. dateType := chartInfo.DateType
  471. fmt.Println("dateType:", dateType)
  472. chartType := chartInfo.ChartType
  473. startDate := chartInfo.StartDate
  474. endDate := chartInfo.EndDate
  475. seasonStartDate := chartInfo.SeasonStartDate
  476. seasonEndDate := chartInfo.SeasonEndDate
  477. startYear := chartInfo.StartYear
  478. calendar := chartInfo.Calendar
  479. if calendar == "" {
  480. calendar = "公历"
  481. }
  482. mappingList, err := TransferChartEdbToEdbMappingFormat(chartInfoId, chartType, mappingListTmp, dataListMap)
  483. if err != nil {
  484. return
  485. }
  486. if chartType == 2 {
  487. startDate = seasonStartDate
  488. endDate = seasonEndDate
  489. if dateType <= 0 {
  490. if startDate != "" {
  491. dateType = 5
  492. } else {
  493. dateType = utils.DateTypeNYears
  494. }
  495. }
  496. } else {
  497. if dateType <= 0 {
  498. dateType = 3
  499. }
  500. }
  501. yearMax := 0
  502. if dateType == utils.DateTypeNYears {
  503. for _, v := range mappingList {
  504. if v.LatestDate != "" {
  505. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  506. if tErr != nil {
  507. errMsg = "获取失败"
  508. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  509. return
  510. }
  511. if lastDateT.Year() > yearMax {
  512. yearMax = lastDateT.Year()
  513. }
  514. }
  515. }
  516. }
  517. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  518. if chartInfo.ChartType == 2 {
  519. chartInfo.StartDate = startDate
  520. chartInfo.EndDate = endDate
  521. }
  522. // 图表额外数据参数
  523. extraConfigStr := chartInfo.ExtraConfig
  524. // 柱方图的一些配置
  525. var barConfig data_manage.BarChartInfoReq
  526. if chartInfo != nil && chartInfo.ChartType == 7 {
  527. if chartInfo.BarConfig == `` {
  528. err = fmt.Errorf("柱方图未配置")
  529. errMsg = "柱方图未配置"
  530. return
  531. }
  532. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  533. if err != nil {
  534. err = fmt.Errorf("柱方图配置异常 json.Unmarshal Err:%s", err.Error())
  535. errMsg = "柱方图配置异常"
  536. return
  537. }
  538. extraConfigStr = chartInfo.BarConfig
  539. }
  540. // 获取表格数据
  541. excelChartInfoDataShow := new(ExcelChartInfoDataShow)
  542. excelChartInfoDataShow.DataListMap = dataListMap
  543. if chartInfo.HaveOperaAuth {
  544. // 获取图表中的指标数据
  545. edbList, xEdbIdValue, yDataList, dataResp, e, msg := GetChartEdbDataV2(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, excelChartInfoDataShow)
  546. if e != nil {
  547. err = fmt.Errorf("获取图表,指标数据失败,Err:%s", e.Error())
  548. errMsg = msg
  549. return
  550. }
  551. // 单位
  552. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  553. chartInfo.Unit = yDataList[0].Unit
  554. chartInfo.UnitEn = yDataList[0].UnitEn
  555. }
  556. warnEdbList := make([]string, 0)
  557. for _, v := range edbList {
  558. if v.IsNullData {
  559. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  560. }
  561. }
  562. if len(warnEdbList) > 0 {
  563. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  564. }
  565. if chartInfoId > 0 && chartInfo != nil {
  566. //判断是否加入我的图库
  567. {
  568. var myChartCondition string
  569. var myChartPars []interface{}
  570. myChartCondition += ` AND a.admin_id=? `
  571. myChartPars = append(myChartPars, sysUser.AdminId)
  572. myChartCondition += ` AND a.chart_info_id=? `
  573. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  574. myChartList, e := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  575. if e != nil && !utils.IsErrNoRow(e) {
  576. errMsg = "获取失败"
  577. err = fmt.Errorf("获取我的图表信息失败,GetMyChartByCondition,Err:" + e.Error())
  578. return
  579. }
  580. if myChartList != nil && len(myChartList) > 0 {
  581. chartInfo.IsAdd = true
  582. chartInfo.MyChartId = myChartList[0].MyChartId
  583. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  584. }
  585. }
  586. }
  587. //判断是否需要展示英文标识
  588. chartInfo.IsEnChart = CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  589. // 图表的指标来源
  590. sourceNameList, sourceNameEnList := GetEdbSourceByEdbInfoIdList(edbList)
  591. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  592. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  593. // todo 指标权限
  594. {
  595. for _, item := range edbList {
  596. // 数据权限
  597. item.HaveOperaAuth = true
  598. }
  599. }
  600. resp.EdbInfoList = edbList
  601. resp.XEdbIdValue = xEdbIdValue
  602. resp.YDataList = yDataList
  603. resp.DataResp = dataResp
  604. } else {
  605. resp.EdbInfoList = mappingList
  606. }
  607. // todo 平衡表图表操作权限平衡表表格的操作权限保持一致,前端未用到该按钮权限,故不处理
  608. chartInfo.IsEdit, _ = GetBalanceExcelInfoOpButtonByChartInfoId(sysUser.AdminId, chartInfo.ChartInfoId, chartInfo.HaveOperaAuth)
  609. chartInfo.Button = data_manage.ChartViewButton{
  610. IsEdit: chartInfo.IsEdit,
  611. IsEnChart: chartInfo.IsEnChart,
  612. IsAdd: chartInfo.IsAdd,
  613. IsCopy: true,
  614. IsSetName: chartInfo.IsSetName,
  615. }
  616. resp.ChartInfo = chartInfo
  617. resp.BarChartInfo = barConfig
  618. return
  619. }
  620. // GetBalanceExcelEdbDataMapList 获取指标最后的基础数据
  621. func GetBalanceExcelEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string, dataListMap map[int][]*data_manage.EdbDataList) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  622. // 指标对应的所有数据
  623. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  624. for _, v := range mappingList {
  625. //fmt.Println("v:", v.EdbInfoId)
  626. item := new(data_manage.ChartEdbInfoMapping)
  627. item.EdbInfoId = v.EdbInfoId
  628. item.SourceName = v.SourceName
  629. item.Source = v.Source
  630. item.EdbCode = v.EdbCode
  631. item.EdbName = v.EdbName
  632. item.EdbNameEn = v.EdbNameEn
  633. item.Frequency = v.Frequency
  634. item.EdbType = v.EdbType
  635. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  636. if v.Unit != `无` {
  637. item.Unit = v.Unit
  638. }
  639. item.UnitEn = v.UnitEn
  640. item.StartDate = v.StartDate
  641. item.EndDate = v.EndDate
  642. item.ModifyTime = v.ModifyTime
  643. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  644. item.PredictChartColor = v.PredictChartColor
  645. item.ClassifyId = v.ClassifyId
  646. if chartInfoId <= 0 {
  647. item.IsAxis = 1
  648. item.LeadValue = 0
  649. item.LeadUnit = ""
  650. item.ChartEdbMappingId = 0
  651. item.ChartInfoId = 0
  652. item.IsOrder = false
  653. item.EdbInfoType = 1
  654. item.ChartStyle = ""
  655. item.ChartColor = ""
  656. item.ChartWidth = 1
  657. item.MaxData = v.MaxValue
  658. item.MinData = v.MinValue
  659. } else {
  660. item.IsAxis = v.IsAxis
  661. item.EdbInfoType = v.EdbInfoType
  662. item.LeadValue = v.LeadValue
  663. item.LeadUnit = v.LeadUnit
  664. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  665. item.ChartEdbMappingId = v.ChartEdbMappingId
  666. item.ChartInfoId = v.ChartInfoId
  667. item.ChartStyle = v.ChartStyle
  668. item.ChartColor = v.ChartColor
  669. item.ChartWidth = v.ChartWidth
  670. item.IsOrder = v.IsOrder
  671. item.MaxData = v.MaxData
  672. item.MinData = v.MinData
  673. }
  674. item.LatestValue = v.LatestValue
  675. item.LatestDate = v.LatestDate
  676. item.UniqueCode = v.UniqueCode
  677. item.MoveLatestDate = v.LatestDate
  678. item.EdbAliasName = v.EdbAliasName
  679. item.IsConvert = v.IsConvert
  680. item.ConvertType = v.ConvertType
  681. item.ConvertValue = v.ConvertValue
  682. item.ConvertUnit = v.ConvertUnit
  683. item.ConvertEnUnit = v.ConvertEnUnit
  684. item.IsJoinPermission = v.IsJoinPermission
  685. var startDateReal string
  686. var diffSeconds int64
  687. if chartType == 2 { //季节性图
  688. startDateReal = startDate
  689. if len(mappingList) > 1 {
  690. item.IsAxis = v.IsAxis
  691. }
  692. } else {
  693. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  694. var startTimeRealTemp time.Time
  695. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  696. switch v.LeadUnit {
  697. case "天":
  698. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  699. case "月":
  700. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  701. case "季":
  702. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  703. case "周":
  704. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  705. case "年":
  706. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  707. }
  708. if startTimeRealTemp.Before(startDateParse) {
  709. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  710. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  711. } else {
  712. startDateReal = startDate
  713. diffSeconds = 0
  714. }
  715. // 预测指标的开始日期也要偏移
  716. {
  717. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  718. if tmpErr != nil {
  719. err = tmpErr
  720. return
  721. }
  722. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  723. if tmpErr != nil {
  724. err = tmpErr
  725. return
  726. }
  727. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  728. }
  729. } else {
  730. startDateReal = startDate
  731. }
  732. }
  733. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  734. calendarPreYear := 0
  735. if calendar == "农历" {
  736. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  737. if e != nil {
  738. err = fmt.Errorf("时间解析 time.Parse(%s, %s) error: %v", utils.FormatDate, startDateReal, e)
  739. return
  740. }
  741. calendarPreYear = newStartDateReal.Year() - 1
  742. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  743. startDateReal = newStartDateReal.Format(utils.FormatDate)
  744. }
  745. dataList := make([]*data_manage.EdbDataList, 0)
  746. dataListTmp, ok := dataListMap[v.EdbInfoId]
  747. if ok {
  748. dataList = dataListTmp
  749. // 对dataList 根据dataTimestamp 进行排序
  750. sort.Slice(dataList, func(i, j int) bool {
  751. return dataList[i].DataTimestamp < dataList[j].DataTimestamp
  752. })
  753. } else {
  754. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoId))
  755. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.EdbInfoId))
  756. }
  757. if v.IsConvert == 1 {
  758. switch v.ConvertType {
  759. case 1:
  760. for i, data := range dataList {
  761. dataList[i].Value = data.Value * v.ConvertValue
  762. }
  763. //item.MaxData = item.MaxData * v.ConvertValue
  764. //item.MinData = item.MinData * v.ConvertValue
  765. case 2:
  766. for i, data := range dataList {
  767. dataList[i].Value = data.Value / v.ConvertValue
  768. }
  769. //item.MaxData = item.MaxData / v.ConvertValue
  770. //item.MinData = item.MinData / v.ConvertValue
  771. case 3:
  772. for i, data := range dataList {
  773. if data.Value <= 0 {
  774. err = errors.New("数据中含有负数或0,无法对数运算")
  775. return
  776. }
  777. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  778. }
  779. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  780. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  781. }
  782. }
  783. edbDataListMap[v.EdbInfoId] = dataList
  784. if diffSeconds != 0 && v.EdbInfoType == 0 {
  785. dataListLen := len(dataList)
  786. for i := 0; i < dataListLen; i++ {
  787. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  788. }
  789. }
  790. if chartType == 2 && item.IsAxis == 1 {
  791. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  792. if tmpErr != nil {
  793. //item.DataList = dataList
  794. item.IsNullData = true
  795. edbList = append(edbList, item)
  796. continue
  797. }
  798. if calendar == "农历" {
  799. if len(dataList) <= 0 {
  800. result := new(data_manage.EdbDataResult)
  801. item.DataList = result
  802. } else {
  803. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  804. if tmpErr != nil {
  805. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  806. return
  807. }
  808. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  809. if tErr != nil {
  810. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  811. return
  812. }
  813. item.DataList = quarterDataList
  814. }
  815. } else {
  816. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  817. if tErr != nil {
  818. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  819. return
  820. }
  821. item.DataList = quarterDataList
  822. }
  823. } else if chartType == 2 && item.IsAxis == 0 {
  824. // 右轴数据处理
  825. xStartDate := "01-01"
  826. jumpYear := 0
  827. var seasonExtra data_manage.SeasonExtraItem
  828. if seasonExtraConfig != "" {
  829. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  830. if err != nil {
  831. return
  832. }
  833. }
  834. if seasonExtra.XStartDate != "" {
  835. xStartDate = seasonExtra.XStartDate
  836. jumpYear = seasonExtra.JumpYear
  837. }
  838. length := len(dataList)
  839. if length == 0 {
  840. return
  841. }
  842. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  843. if tmpErr != nil {
  844. //item.DataList = dataList
  845. item.IsNullData = true
  846. edbList = append(edbList, item)
  847. continue
  848. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  849. return
  850. }
  851. var rightAxisDate time.Time
  852. if jumpYear == 1 {
  853. latestDate = latestDate.AddDate(-1, 0, 0)
  854. latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
  855. rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
  856. if err != nil {
  857. return
  858. }
  859. }
  860. newDataList := make([]*data_manage.EdbDataList, 0)
  861. for _, v := range dataList {
  862. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  863. if e != nil {
  864. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  865. return
  866. }
  867. if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
  868. newDataList = append(newDataList, v)
  869. }
  870. }
  871. item.DataList = newDataList
  872. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  873. //item.DataList = dataList
  874. } else {
  875. item.DataList = dataList
  876. }
  877. edbList = append(edbList, item)
  878. }
  879. return
  880. }
  881. func CheckBalanceChartCacheAndPermission(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  882. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  883. adminId := sysUser.AdminId
  884. // todo 图表数据权限
  885. // 已授权分类id
  886. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  887. if err != nil {
  888. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  889. return
  890. }
  891. defer func() {
  892. if isOk {
  893. // 图表权限校验
  894. {
  895. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  896. //图表操作权限
  897. chartInfo.IsEdit = CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  898. chartInfo.Button = data_manage.ChartViewButton{
  899. IsEdit: chartInfo.IsEdit,
  900. IsEnChart: chartInfo.IsEnChart,
  901. IsAdd: chartInfo.IsAdd,
  902. IsCopy: true,
  903. IsSetName: chartInfo.IsSetName,
  904. }
  905. //if !resp.ChartInfo.HaveOperaAuth {
  906. // for _, v := range resp.EdbInfoList {
  907. // v.DataList = nil
  908. // }
  909. // resp.DataResp = nil
  910. // resp.XEdbIdValue = []int{}
  911. // resp.YDataList = []data_manage.YData{}
  912. //}
  913. }
  914. // todo 是否需要返回平衡表指标权限即判断平衡表的表格数据权限
  915. {
  916. // 查询图表关联的表格ID
  917. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfo.ChartInfoId)
  918. if e != nil {
  919. errMsg = "获取图表关联的表格ID失败,Err:" + e.Error()
  920. err = e
  921. return
  922. }
  923. checkExcelInfo := excelInfo
  924. if excelInfo.Source == utils.BALANCE_TABLE {
  925. checkExcelInfoId := excelInfo.ExcelInfoId
  926. if excelInfo.BalanceType == 1 {
  927. checkExcelInfoId = excelInfo.RelExcelInfoId
  928. } else {
  929. if excelInfo.ParentId > 0 {
  930. checkExcelInfoId = excelInfo.ParentId
  931. }
  932. }
  933. if checkExcelInfoId != excelInfo.ExcelInfoId {
  934. checkExcelInfo, err = excelModel.GetExcelInfoById(checkExcelInfoId)
  935. if err != nil {
  936. errMsg = "获取平衡表格信息失败"
  937. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  938. return
  939. }
  940. }
  941. }
  942. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, adminId)
  943. if e != nil {
  944. errMsg = "获取平衡表格数据权限失败"
  945. err = errors.New("获取表格权限信息失败,Err" + e.Error())
  946. return
  947. }
  948. // 数据权限
  949. for _, v := range resp.EdbInfoList {
  950. v.HaveOperaAuth = haveOperaAuth
  951. }
  952. }
  953. }
  954. }()
  955. key := GetChartInfoDataKey(chartInfo.ChartInfoId)
  956. if utils.Re == nil && isCache {
  957. if utils.Re == nil && utils.Rc.IsExist(key) {
  958. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  959. err := json.Unmarshal(redisData, &resp)
  960. if err != nil || resp == nil {
  961. return
  962. }
  963. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  964. var myCond string
  965. var myPars []interface{}
  966. myCond += ` AND a.admin_id=? `
  967. myPars = append(myPars, adminId)
  968. myCond += ` AND a.chart_info_id=? `
  969. myPars = append(myPars, chartInfo.ChartInfoId)
  970. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  971. if err != nil && !utils.IsErrNoRow(err) {
  972. msg = "获取失败"
  973. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  974. return
  975. }
  976. resp.ChartInfo.IsAdd = false
  977. resp.ChartInfo.MyChartId = 0
  978. resp.ChartInfo.MyChartClassifyId = ""
  979. if myList != nil && len(myList) > 0 {
  980. resp.ChartInfo.IsAdd = true
  981. resp.ChartInfo.MyChartId = myList[0].MyChartId
  982. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  983. }
  984. //判断是否加入我的图库
  985. {
  986. var myChartCondition string
  987. var myChartPars []interface{}
  988. myChartCondition += ` AND a.admin_id=? `
  989. myChartPars = append(myChartPars, adminId)
  990. myChartCondition += ` AND a.chart_info_id=? `
  991. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  992. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  993. if err != nil && !utils.IsErrNoRow(err) {
  994. msg = "获取失败"
  995. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  996. return
  997. }
  998. if myChartList != nil && len(myChartList) > 0 {
  999. chartInfo.IsAdd = true
  1000. chartInfo.MyChartId = myChartList[0].MyChartId
  1001. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1002. }
  1003. }
  1004. isOk = true
  1005. fmt.Println("source redis")
  1006. return
  1007. }
  1008. }
  1009. }
  1010. return
  1011. }
  1012. // GetBalanceExcelSeasonChartLegendPreview 获取预览的季节性图例
  1013. func GetBalanceExcelSeasonChartLegendPreview(dataList []*data_manage.EdbDataList, calendar string, seasonExtraConfigReq data_manage.SeasonExtraItem) (quarterDataList data_manage.QuarterDataList, err error, errMsg string) {
  1014. seasonExtraConfig := ""
  1015. if seasonExtraConfigReq.XEndDate != "" {
  1016. if seasonExtraConfigReq.XStartDate > seasonExtraConfigReq.XEndDate && seasonExtraConfigReq.JumpYear != 1 {
  1017. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  1018. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  1019. return
  1020. }
  1021. seasonExtra, tErr := json.Marshal(seasonExtraConfigReq)
  1022. if tErr != nil {
  1023. errMsg = "季节性图表配置信息异常"
  1024. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  1025. return
  1026. }
  1027. seasonExtraConfig = string(seasonExtra)
  1028. }
  1029. var latestDate time.Time //最新日期
  1030. startDate, endDate, _, _, _, _ := getBalanceDataListStartDateAndValue(dataList)
  1031. calendarPreYear := 0
  1032. startDateReal := startDate
  1033. latestDate, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1034. if calendar == "农历" {
  1035. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  1036. if e != nil {
  1037. err = fmt.Errorf("开始时间解析失败 time.Parse:" + e.Error())
  1038. return
  1039. }
  1040. calendarPreYear = newStartDateReal.Year() - 1
  1041. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  1042. startDateReal = newStartDateReal.Format(utils.FormatDate)
  1043. }
  1044. if calendar == "农历" {
  1045. if len(dataList) <= 0 {
  1046. return
  1047. } else {
  1048. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  1049. if tmpErr != nil {
  1050. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  1051. return
  1052. }
  1053. quarterDataList, err = GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  1054. if err != nil {
  1055. err = errors.New("获取季节性图表数据失败,Err:" + err.Error())
  1056. return
  1057. }
  1058. }
  1059. } else {
  1060. quarterDataList, err = GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  1061. if err != nil {
  1062. err = errors.New("获取季节性图表数据失败,Err:" + err.Error())
  1063. return
  1064. }
  1065. }
  1066. return
  1067. }
  1068. func getBalanceDataListStartDateAndValue(dataList []*data_manage.EdbDataList) (startDate, endDate string, startVal, endVal, maxVal, minVal float64) {
  1069. if len(dataList) == 0 {
  1070. return
  1071. }
  1072. startDate = dataList[0].DataTime
  1073. startVal = dataList[0].Value
  1074. maxVal = dataList[0].Value
  1075. minVal = dataList[0].Value
  1076. endDate = dataList[len(dataList)-1].DataTime
  1077. endVal = dataList[len(dataList)-1].Value
  1078. for _, v := range dataList {
  1079. if v.DataTime < startDate {
  1080. startDate = v.DataTime
  1081. startVal = v.Value
  1082. }
  1083. if v.DataTime > endDate {
  1084. endDate = v.DataTime
  1085. endVal = v.Value
  1086. }
  1087. if v.Value > maxVal {
  1088. maxVal = v.Value
  1089. }
  1090. if v.Value < minVal {
  1091. minVal = v.Value
  1092. }
  1093. }
  1094. return
  1095. }
  1096. // AddBalanceStaticExcel 另存为和存为静态表以及子表的复制都调用该接口
  1097. func AddBalanceStaticExcel(oldExcelInfo *excelModel.ExcelInfo, excelClassifyId int, versionName string, sysUser *system.Admin, parentId, relExcelInfoId, balanceType int, childExcelList []*excelModel.ExcelInfo, pingNameFlag bool, excelDataMap map[int]map[int][]*data_manage.EdbDataList) (excelInfo *excelModel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
  1098. isSendEmail = true
  1099. versionName = utils.TrimLRStr(versionName)
  1100. // 检验分类下是否存在该表格名称
  1101. // todo 检验动态表分类下表格名称是否存在
  1102. if balanceType == 1 && versionName != "" {
  1103. var condition string
  1104. var pars []interface{}
  1105. condition += " AND rel_excel_info_id=? AND balance_type=1 AND parent_id=0"
  1106. pars = append(pars, oldExcelInfo.ExcelInfoId)
  1107. condition += " AND version_name=? "
  1108. pars = append(pars, versionName)
  1109. count, tmpErr := excelModel.GetExcelInfoCountByCondition(condition, pars)
  1110. if tmpErr != nil {
  1111. errMsg = "查询版本名称失败"
  1112. err = tmpErr
  1113. return
  1114. }
  1115. if count > 0 {
  1116. errMsg = "表格版本名称已存在,请重新填写版本名称"
  1117. err = errors.New(errMsg)
  1118. isSendEmail = false
  1119. return
  1120. }
  1121. }
  1122. // 表格信息
  1123. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1124. excelName := oldExcelInfo.ExcelName
  1125. if pingNameFlag && versionName != "" && parentId == 0 {
  1126. excelName += "(" + versionName + ")"
  1127. }
  1128. // 处理表格内容, 静态表去除动态指标关联
  1129. content := oldExcelInfo.Content
  1130. excelInfo = &excelModel.ExcelInfo{
  1131. //ExcelInfoId: 0,
  1132. ExcelName: excelName,
  1133. Source: oldExcelInfo.Source,
  1134. ExcelType: oldExcelInfo.ExcelType,
  1135. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  1136. ExcelClassifyId: excelClassifyId,
  1137. SysUserId: sysUser.AdminId,
  1138. SysUserRealName: sysUser.RealName,
  1139. Content: content,
  1140. ExcelImage: oldExcelInfo.ExcelImage,
  1141. FileUrl: oldExcelInfo.FileUrl,
  1142. ParentId: parentId,
  1143. RelExcelInfoId: relExcelInfoId,
  1144. VersionName: versionName,
  1145. UpdateUserId: sysUser.AdminId,
  1146. UpdateUserRealName: sysUser.RealName,
  1147. BalanceType: balanceType,
  1148. Sort: oldExcelInfo.Sort,
  1149. IsDelete: 0,
  1150. ModifyTime: time.Now(),
  1151. CreateTime: time.Now(),
  1152. }
  1153. excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
  1154. if balanceType == 0 {
  1155. // 复制动态表才需要获取原excel与指标的关系表
  1156. excelEdbMappingListTmp, e := excelModel.GetAllExcelEdbMappingByExcelInfoId(oldExcelInfo.ExcelInfoId)
  1157. if e != nil {
  1158. err = e
  1159. errMsg = "获取失败"
  1160. return
  1161. }
  1162. for _, v := range excelEdbMappingListTmp {
  1163. tmp := new(excelModel.ExcelEdbMapping)
  1164. tmp.EdbInfoId = v.EdbInfoId
  1165. tmp.Source = utils.BALANCE_TABLE
  1166. tmp.CreateTime = time.Now()
  1167. tmp.ModifyTime = time.Now()
  1168. excelEdbMappingList = append(excelEdbMappingList, tmp)
  1169. }
  1170. }
  1171. var childExcel *excelModel.ExcelInfo
  1172. err = excelModel.AddExcelInfo(excelInfo, excelEdbMappingList, childExcel)
  1173. if err != nil {
  1174. errMsg = "保存失败"
  1175. return
  1176. }
  1177. if parentId == 0 && len(childExcelList) > 0 {
  1178. for _, childExcelInfo := range childExcelList {
  1179. _, err, errMsg, isSendEmail = AddBalanceStaticExcel(childExcelInfo, excelClassifyId, versionName, sysUser, excelInfo.ExcelInfoId, relExcelInfoId, balanceType, []*excelModel.ExcelInfo{}, pingNameFlag, excelDataMap)
  1180. if err != nil {
  1181. return
  1182. }
  1183. }
  1184. } else if parentId > 0 {
  1185. // 如果复制的是动态表的子表,则同步复制关联的图表
  1186. // 查出所有的chart_list, 同步复制图表和图表指标
  1187. // 相关联指标
  1188. mappingListTmp, e := excelModel.GetExcelChartEdbMappingByExcelInfoId(oldExcelInfo.ExcelInfoId)
  1189. if e != nil {
  1190. errMsg = "获取图表指标失败"
  1191. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  1192. return
  1193. }
  1194. mappingListMap := make(map[int][]*excelModel.ExcelChartEdb, 0)
  1195. charInfoIds := make([]int, 0)
  1196. for k, _ := range mappingListTmp {
  1197. mappingListTmp[k].ExcelInfoId = excelInfo.ExcelInfoId
  1198. mappingListMap[mappingListTmp[k].ChartInfoId] = append(mappingListMap[mappingListTmp[k].ChartInfoId], mappingListTmp[k])
  1199. }
  1200. for k, _ := range mappingListMap {
  1201. charInfoIds = append(charInfoIds, k)
  1202. }
  1203. if len(charInfoIds) > 0 {
  1204. chartInfoList, e := data_manage.GetChartInfoViewByIdList(charInfoIds)
  1205. if e != nil {
  1206. errMsg = "获取失败"
  1207. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  1208. return
  1209. }
  1210. for _, chartInfo := range chartInfoList {
  1211. mappingList, ok := mappingListMap[chartInfo.ChartInfoId]
  1212. if !ok {
  1213. err = fmt.Errorf("未找到图表关联的指标信息")
  1214. return
  1215. }
  1216. chartEdbDataMap, ok := excelDataMap[chartInfo.ChartInfoId]
  1217. if !ok && oldExcelInfo.BalanceType == 1 { //
  1218. err = fmt.Errorf("未找到图表关联的指标信息")
  1219. return
  1220. }
  1221. err, errMsg, isSendEmail = CopyBalanceExcelChart(chartInfo, mappingList, versionName, sysUser, pingNameFlag, chartEdbDataMap)
  1222. if err != nil {
  1223. err = fmt.Errorf("复制图表失败 Err:%s", err.Error())
  1224. return
  1225. }
  1226. }
  1227. }
  1228. }
  1229. return
  1230. }
  1231. // CopyBalanceExcelChart 复制平衡表图表
  1232. func CopyBalanceExcelChart(oldChartInfo *data_manage.ChartInfoView, oldChartEdbList []*excelModel.ExcelChartEdb, versionName string, sysUser *system.Admin, pingNameFlag bool, dataListMap map[int][]*data_manage.EdbDataList) (err error, errMsg string, isSendEmail bool) {
  1233. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  1234. if pingNameFlag && versionName != "" {
  1235. oldChartInfo.ChartName += "(" + versionName + ")"
  1236. }
  1237. for _, chartEdb := range oldChartEdbList {
  1238. excelEdb := new(excelModel.ExcelChartEdb)
  1239. excelEdb.EdbName = chartEdb.EdbName
  1240. if pingNameFlag && versionName != "" {
  1241. excelEdb.EdbName += "(" + versionName + ")"
  1242. }
  1243. excelEdb.EdbNameEn = excelEdb.EdbName
  1244. excelEdb.Unit = chartEdb.Unit
  1245. excelEdb.UnitEn = excelEdb.Unit
  1246. randStr := utils.GetRandDigit(4)
  1247. excelEdb.EdbCode = `T` + time.Now().Format("060102150405") + "_" + randStr
  1248. excelEdb.ExcelInfoId = chartEdb.ExcelInfoId
  1249. excelEdb.DateSequence = chartEdb.DateSequence
  1250. excelEdb.DataSequence = chartEdb.DataSequence
  1251. excelEdb.SysUserId = sysUser.AdminId
  1252. excelEdb.SysUserRealName = sysUser.RealName
  1253. excelEdb.MaxData = chartEdb.MaxData
  1254. excelEdb.MinData = chartEdb.MinData
  1255. excelEdb.IsOrder = chartEdb.IsOrder
  1256. excelEdb.IsAxis = chartEdb.IsAxis
  1257. excelEdb.FromTag = chartEdb.FromTag
  1258. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  1259. excelEdb.LeadValue = chartEdb.LeadValue
  1260. excelEdb.LeadUnit = chartEdb.LeadUnit
  1261. excelEdb.ChartWidth = 1
  1262. excelEdb.CreateTime = time.Now()
  1263. excelEdb.ModifyTime = time.Now()
  1264. var dataList []*excelModel.ExcelChartData
  1265. tmpList, ok := dataListMap[chartEdb.ExcelChartEdbId]
  1266. if ok {
  1267. for _, l := range tmpList {
  1268. tmp := &excelModel.ExcelChartData{
  1269. DataTime: l.DataTime,
  1270. Value: l.Value,
  1271. DataTimestamp: l.DataTimestamp,
  1272. }
  1273. dataList = append(dataList, tmp)
  1274. }
  1275. }
  1276. // 处理日期列表和值列表
  1277. addItem := &excelModel.AddChartEdbAndDataItem{
  1278. ChartEdb: excelEdb,
  1279. DataList: dataList,
  1280. }
  1281. chartEdbList = append(chartEdbList, addItem)
  1282. }
  1283. chartInfo, err, errMsg, isSendEmail := copyBalanceExcelChart(oldChartInfo, sysUser.AdminId, sysUser.RealName)
  1284. if err != nil {
  1285. errMsg = "新增图表失败!"
  1286. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  1287. return
  1288. }
  1289. obj := new(excelModel.ExcelChartEdb)
  1290. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, []int{})
  1291. if err != nil {
  1292. errMsg = "新增图表失败!"
  1293. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  1294. return
  1295. }
  1296. return
  1297. }
  1298. func copyBalanceExcelChart(oldChartInfo *data_manage.ChartInfoView, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1299. isSendEmail = true // 默认错误的时候要发送邮件
  1300. oldChartInfo.ChartName = strings.Trim(oldChartInfo.ChartName, " ")
  1301. if oldChartInfo.ChartName == "" {
  1302. errMsg = "请填写图表名称!"
  1303. err = fmt.Errorf(errMsg)
  1304. isSendEmail = false
  1305. return
  1306. }
  1307. //判断图表是否存在
  1308. var condition string
  1309. var pars []interface{}
  1310. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1311. condition += " AND chart_classify_id=0 "
  1312. condition += " AND chart_name=? AND source = ? "
  1313. pars = append(pars, oldChartInfo.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  1314. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1315. if err != nil {
  1316. errMsg = "判断图表名称是否存在失败"
  1317. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  1318. return
  1319. }
  1320. if count > 0 {
  1321. /*errMsg = "图表已存在,请重新填写"
  1322. err = fmt.Errorf("判断图表名称是否存在失败")
  1323. isSendEmail = false
  1324. return*/
  1325. oldChartInfo.ChartName += " " + strconv.Itoa(count)
  1326. }
  1327. chartInfo = new(data_manage.ChartInfo)
  1328. chartInfo.ChartName = oldChartInfo.ChartName
  1329. chartInfo.ChartNameEn = oldChartInfo.ChartName
  1330. //chartInfo.EdbInfoIds = edbInfoIdStr
  1331. //chartInfo.ChartClassifyId = req.ChartClassifyId
  1332. chartInfo.SysUserId = sysUserId
  1333. chartInfo.SysUserRealName = sysUserRealName
  1334. chartInfo.CreateTime = time.Now()
  1335. chartInfo.ModifyTime = time.Now()
  1336. chartInfo.IsSetName = 0
  1337. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1338. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1339. // todo 判断是否需要重新计算用户的start_date
  1340. chartInfo.DateType = oldChartInfo.DateType
  1341. chartInfo.ChartType = oldChartInfo.ChartType
  1342. chartInfo.Calendar = oldChartInfo.Calendar
  1343. /* chartInfo.StartDate = req.StartDate
  1344. chartInfo.EndDate = req.EndDate
  1345. chartInfo.SeasonStartDate = req.StartDate
  1346. chartInfo.SeasonEndDate = req.EndDate*/
  1347. chartInfo.LeftMin = oldChartInfo.LeftMin
  1348. chartInfo.LeftMax = oldChartInfo.LeftMax
  1349. chartInfo.RightMin = oldChartInfo.RightMin
  1350. chartInfo.RightMax = oldChartInfo.RightMax
  1351. chartInfo.Right2Min = oldChartInfo.Right2Min
  1352. chartInfo.Right2Max = oldChartInfo.Right2Max
  1353. chartInfo.MinMaxSave = oldChartInfo.MinMaxSave
  1354. //chartInfo.Disabled = disableVal
  1355. //chartInfo.BarConfig = barChartConf
  1356. chartInfo.ExtraConfig = oldChartInfo.ExtraConfig
  1357. chartInfo.SeasonExtraConfig = oldChartInfo.SeasonExtraConfig
  1358. //chartInfo.StartYear = req.StartYear
  1359. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  1360. chartInfo.ChartThemeId = oldChartInfo.ChartThemeId
  1361. chartInfo.SourcesFrom = oldChartInfo.SourcesFrom
  1362. /* chartInfo.Instructions = req.Instructions
  1363. chartInfo.MarkersLines = req.MarkersLines
  1364. chartInfo.MarkersAreas = req.MarkersAreas
  1365. chartInfo.Unit = req.Unit
  1366. chartInfo.UnitEn = req.UnitEn*/
  1367. /*newId, err := data_manage.AddChartInfo(chartInfo)
  1368. if err != nil {
  1369. errMsg = `保存失败`
  1370. err = fmt.Errorf("保存失败,Err:" + err.Error())
  1371. return
  1372. }
  1373. chartInfo.ChartInfoId = int(newId)*/
  1374. //添加es数据
  1375. //go AddOrEditChartInfoToEs(chartInfo.ChartInfoId)
  1376. return
  1377. }
  1378. // CopyBalanceExcel 动态平衡表另存为
  1379. func CopyBalanceExcel(oldExcelInfo *excelModel.ExcelInfo, excelClassifyId int, excelName string, sysUser *system.Admin) (excelInfo *excelModel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
  1380. if oldExcelInfo.ParentId != 0 && oldExcelInfo.BalanceType != 0 {
  1381. errMsg = "平衡表类型错误"
  1382. err = fmt.Errorf("平衡表类型错误 ")
  1383. return
  1384. }
  1385. // 查询excel表格名称是否已经存在
  1386. // 检验分类下是否存在该表格名称
  1387. {
  1388. var nameCondition string
  1389. var namePars []interface{}
  1390. nameCondition += " AND excel_classify_id=? AND parent_id=?"
  1391. namePars = append(namePars, excelClassifyId, 0)
  1392. nameCondition += " AND excel_name=? "
  1393. namePars = append(namePars, excelName)
  1394. count, tmpErr := excelModel.GetExcelInfoCountByCondition(nameCondition, namePars)
  1395. if tmpErr != nil {
  1396. errMsg = "判断表格名称是否存在失败"
  1397. err = tmpErr
  1398. return
  1399. }
  1400. if count > 0 {
  1401. errMsg = "表格名称已存在,请重新填写表格名称"
  1402. err = errors.New(errMsg)
  1403. isSendEmail = false
  1404. return
  1405. }
  1406. }
  1407. oldExcelInfo.ExcelName = excelName
  1408. // 先复制动态表,再复制动态子表,复制静态表,再复制静态子表
  1409. condition := " AND parent_id = ? AND balance_type = 0 "
  1410. var pars []interface{}
  1411. pars = append(pars, oldExcelInfo.ExcelInfoId)
  1412. childExcelList, err := excelModel.GetExcelInfoListByCondition(condition, pars)
  1413. if err != nil {
  1414. errMsg = "获取子表失败"
  1415. err = fmt.Errorf("获取子表失败 %s", err.Error())
  1416. return
  1417. }
  1418. // 查询出每个子表的内容,并将内容转为静态版本
  1419. excelDataMap := make(map[int]map[int][]*data_manage.EdbDataList)
  1420. excelInfo, err, errMsg, isSendEmail = AddBalanceStaticExcel(oldExcelInfo, excelClassifyId, "", sysUser, 0, 0, 0, childExcelList, false, excelDataMap)
  1421. if err != nil {
  1422. errMsg = "复制动态表失败"
  1423. err = fmt.Errorf("复制动态表失败 %s", err.Error())
  1424. return
  1425. }
  1426. //复制静态表
  1427. staticCondition := " AND parent_id = 0 AND balance_type = 1 AND rel_excel_info_id=? "
  1428. var staticPars []interface{}
  1429. staticPars = append(staticPars, oldExcelInfo.ExcelInfoId)
  1430. staticExcelList, err := excelModel.GetExcelInfoListByCondition(staticCondition, staticPars)
  1431. if err != nil {
  1432. errMsg = "获取子表失败"
  1433. err = fmt.Errorf("获取子表失败 %s", err.Error())
  1434. return
  1435. }
  1436. for _, staticExcelInfo := range staticExcelList {
  1437. cCondition := " AND parent_id = ? AND balance_type = 1"
  1438. var cPars []interface{}
  1439. cPars = append(cPars, staticExcelInfo.ExcelInfoId)
  1440. childList, e := excelModel.GetExcelInfoListByCondition(cCondition, cPars)
  1441. if e != nil {
  1442. errMsg = "获取子表失败"
  1443. err = fmt.Errorf("获取子表失败 %s", err.Error())
  1444. return
  1445. }
  1446. excelDataMap = make(map[int]map[int][]*data_manage.EdbDataList)
  1447. for _, childExcelInfo := range childList {
  1448. //得到表格数据并落库
  1449. tmpDataList, e := excelModel.GetExcelChartDataByExcelInfoId(childExcelInfo.ExcelInfoId)
  1450. if e != nil {
  1451. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  1452. return
  1453. }
  1454. if len(tmpDataList) > 0 {
  1455. for _, v := range tmpDataList {
  1456. child, ok := excelDataMap[v.ChartInfoId]
  1457. if !ok {
  1458. child = make(map[int][]*data_manage.EdbDataList)
  1459. }
  1460. tmp := &data_manage.EdbDataList{
  1461. DataTime: v.DataTime,
  1462. DataTimestamp: v.DataTimestamp,
  1463. Value: v.Value,
  1464. }
  1465. child[v.ExcelChartEdbId] = append(child[v.ExcelChartEdbId], tmp)
  1466. excelDataMap[v.ChartInfoId] = child
  1467. }
  1468. }
  1469. }
  1470. _, err, errMsg, isSendEmail = AddBalanceStaticExcel(staticExcelInfo, excelClassifyId, staticExcelInfo.VersionName, sysUser, 0, excelInfo.ExcelInfoId, 1, childList, false, excelDataMap)
  1471. if err != nil {
  1472. errMsg = "复制动态表失败"
  1473. err = fmt.Errorf("复制动态表失败 %s", err.Error())
  1474. return
  1475. }
  1476. }
  1477. return
  1478. }
  1479. func TransferChartEdbToEdbMappingFormat(chartInfoId, chartType int, mappingListTmp []*excelModel.ExcelChartEdb, dataListMap map[int][]*data_manage.EdbDataList) (mappingList []*data_manage.ChartEdbInfoMapping, err error) {
  1480. mappingList = make([]*data_manage.ChartEdbInfoMapping, 0)
  1481. //循环组装映射关系
  1482. for _, v := range mappingListTmp {
  1483. dataList := make([]*data_manage.EdbDataList, 0)
  1484. dataListTmp, ok := dataListMap[v.ExcelChartEdbId]
  1485. if ok {
  1486. dataList = dataListTmp
  1487. } else {
  1488. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.ExcelChartEdbId))
  1489. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.ExcelChartEdbId))
  1490. }
  1491. startDateStr, endDateStr, _, endVal, maxValue, minValue := getBalanceDataListStartDateAndValue(dataList)
  1492. mapping := &data_manage.ChartEdbInfoMapping{
  1493. EdbInfoId: v.ExcelChartEdbId,
  1494. SourceName: "平衡表",
  1495. Source: 0,
  1496. SubSource: 0,
  1497. EdbCode: v.EdbCode,
  1498. EdbName: v.EdbName,
  1499. EdbAliasName: v.EdbName,
  1500. EdbNameEn: v.EdbNameEn,
  1501. EdbAliasNameEn: "",
  1502. EdbType: 0,
  1503. Frequency: "",
  1504. FrequencyEn: "",
  1505. Unit: v.Unit,
  1506. UnitEn: v.UnitEn,
  1507. StartDate: startDateStr,
  1508. EndDate: endDateStr,
  1509. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  1510. ChartEdbMappingId: v.ExcelChartEdbId,
  1511. ChartInfoId: chartInfoId,
  1512. MaxData: v.MaxData,
  1513. MinData: v.MinData,
  1514. IsOrder: v.IsOrder,
  1515. IsAxis: v.IsAxis,
  1516. EdbInfoType: v.EdbInfoType,
  1517. //EdbInfoCategoryType: 0,
  1518. LeadValue: v.LeadValue,
  1519. LeadUnit: v.LeadUnit,
  1520. LeadUnitEn: "",
  1521. ChartStyle: "",
  1522. ChartColor: "",
  1523. PredictChartColor: "",
  1524. ChartWidth: v.ChartWidth,
  1525. ChartType: chartType,
  1526. LatestDate: endDateStr,
  1527. LatestValue: endVal,
  1528. MoveLatestDate: "",
  1529. UniqueCode: "",
  1530. MinValue: minValue,
  1531. MaxValue: maxValue,
  1532. DataList: nil,
  1533. IsNullData: false,
  1534. MappingSource: 0,
  1535. RegionType: "",
  1536. ClassifyId: 0,
  1537. IsConvert: 0,
  1538. ConvertType: 0,
  1539. ConvertValue: 0,
  1540. ConvertUnit: "",
  1541. ConvertEnUnit: "",
  1542. IsJoinPermission: 0,
  1543. HaveOperaAuth: true,
  1544. }
  1545. mappingList = append(mappingList, mapping)
  1546. }
  1547. return
  1548. }
  1549. // GetBalanceExcelInfoOpButtonByChartInfoId 获取图表权限
  1550. func GetBalanceExcelInfoOpButtonByChartInfoId(sysUserId, chartInfoId int, chartHaveOperaAuth bool) (opButton bool, err error) {
  1551. // 如果没有数据权限,那么直接返回
  1552. if !chartHaveOperaAuth {
  1553. return
  1554. }
  1555. // 获取表格信息
  1556. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfoId)
  1557. if e != nil {
  1558. err = fmt.Errorf("获取ETA表格失败 Err %s", e.Error())
  1559. return
  1560. }
  1561. checkExcelInfo := excelInfo
  1562. if excelInfo.Source == utils.BALANCE_TABLE {
  1563. checkExcelInfoId := excelInfo.ExcelInfoId
  1564. if excelInfo.BalanceType == 1 {
  1565. checkExcelInfoId = excelInfo.RelExcelInfoId
  1566. } else {
  1567. if excelInfo.ParentId > 0 {
  1568. checkExcelInfoId = excelInfo.ParentId
  1569. }
  1570. }
  1571. if checkExcelInfoId != excelInfo.ExcelInfoId {
  1572. checkExcelInfo, e = excelModel.GetExcelInfoById(checkExcelInfoId)
  1573. if e != nil {
  1574. err = fmt.Errorf("获取平衡表格信息失败,Err:" + e.Error())
  1575. return
  1576. }
  1577. }
  1578. } else {
  1579. return
  1580. }
  1581. // 数据权限
  1582. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  1583. if e != nil {
  1584. err = fmt.Errorf("获取表格权限信息失败,Err" + e.Error())
  1585. return
  1586. }
  1587. // 如果没有数据权限,那么直接返回
  1588. if !haveOperaAuth {
  1589. return
  1590. }
  1591. //button := GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  1592. if sysUserId == checkExcelInfo.SysUserId {
  1593. opButton = true
  1594. } else {
  1595. obj := new(excelModel.ExcelWorker)
  1596. workerList, e := obj.GetByExcelInfoId(checkExcelInfo.ExcelInfoId)
  1597. if e == nil {
  1598. for _, v := range workerList {
  1599. if v.SysUserId == sysUserId {
  1600. opButton = true
  1601. break
  1602. }
  1603. }
  1604. }
  1605. }
  1606. return
  1607. }