chart_info_excel_balance.go 55 KB

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