chart_info_excel_balance.go 60 KB

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