chart_info_excel_balance.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. excelModel "eta/eta_mobile/models/data_manage/excel"
  7. "eta/eta_mobile/models/system"
  8. "eta/eta_mobile/services/data/data_manage_permission"
  9. "eta/eta_mobile/utils"
  10. "fmt"
  11. "math"
  12. "sort"
  13. "strings"
  14. "time"
  15. )
  16. 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) {
  17. // 图表数据权限
  18. {
  19. // 已授权分类id
  20. permissionChartIdList, permissionClassifyIdList, e := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  21. if e != nil {
  22. errMsg = "获取失败"
  23. err = fmt.Errorf("获取已授权分类id数据失败,Err:" + err.Error())
  24. return
  25. }
  26. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  27. }
  28. chartInfoId := chartInfo.ChartInfoId
  29. resp = new(data_manage.ChartInfoDetailResp)
  30. // 获取主题样式
  31. chartTheme, err := GetChartThemeConfig(chartInfo.ChartThemeId, 1, chartInfo.ChartType)
  32. if err != nil {
  33. errMsg = "获取失败"
  34. err = fmt.Errorf(" 获取主题信息失败 Err:%s", err.Error())
  35. return
  36. }
  37. chartInfo.ChartThemeStyle = chartTheme.Config
  38. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  39. dateType := chartInfo.DateType
  40. fmt.Println("dateType:", dateType)
  41. chartType := chartInfo.ChartType
  42. startDate := chartInfo.StartDate
  43. endDate := chartInfo.EndDate
  44. seasonStartDate := chartInfo.SeasonStartDate
  45. seasonEndDate := chartInfo.SeasonEndDate
  46. startYear := chartInfo.StartYear
  47. calendar := chartInfo.Calendar
  48. if calendar == "" {
  49. calendar = "公历"
  50. }
  51. mappingList, err := TransferChartEdbToEdbMappingFormat(chartInfoId, chartType, mappingListTmp, dataListMap)
  52. if err != nil {
  53. return
  54. }
  55. if chartType == 2 {
  56. startDate = seasonStartDate
  57. endDate = seasonEndDate
  58. if dateType <= 0 {
  59. if startDate != "" {
  60. dateType = 5
  61. } else {
  62. dateType = utils.DateTypeNYears
  63. }
  64. }
  65. } else {
  66. if dateType <= 0 {
  67. dateType = 3
  68. }
  69. }
  70. yearMax := 0
  71. if dateType == utils.DateTypeNYears {
  72. for _, v := range mappingList {
  73. if v.LatestDate != "" {
  74. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  75. if tErr != nil {
  76. errMsg = "获取失败"
  77. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  78. return
  79. }
  80. if lastDateT.Year() > yearMax {
  81. yearMax = lastDateT.Year()
  82. }
  83. }
  84. }
  85. }
  86. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  87. if chartInfo.ChartType == 2 {
  88. chartInfo.StartDate = startDate
  89. chartInfo.EndDate = endDate
  90. }
  91. // 图表额外数据参数
  92. extraConfigStr := chartInfo.ExtraConfig
  93. // 柱方图的一些配置
  94. var barConfig data_manage.BarChartInfoReq
  95. if chartInfo != nil && chartInfo.ChartType == 7 {
  96. if chartInfo.BarConfig == `` {
  97. err = fmt.Errorf("柱方图未配置")
  98. errMsg = "柱方图未配置"
  99. return
  100. }
  101. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  102. if err != nil {
  103. err = fmt.Errorf("柱方图配置异常 json.Unmarshal Err:%s", err.Error())
  104. errMsg = "柱方图配置异常"
  105. return
  106. }
  107. extraConfigStr = chartInfo.BarConfig
  108. }
  109. // 获取表格数据
  110. excelChartInfoDataShow := new(ExcelChartInfoDataShow)
  111. excelChartInfoDataShow.DataListMap = dataListMap
  112. if chartInfo.HaveOperaAuth {
  113. // 获取图表中的指标数据
  114. edbList, xEdbIdValue, yDataList, dataResp, e, msg := GetChartEdbDataV2(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, excelChartInfoDataShow)
  115. if e != nil {
  116. err = fmt.Errorf("获取图表,指标数据失败,Err:%s", e.Error())
  117. errMsg = msg
  118. return
  119. }
  120. // 单位
  121. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  122. chartInfo.Unit = yDataList[0].Unit
  123. chartInfo.UnitEn = yDataList[0].UnitEn
  124. }
  125. warnEdbList := make([]string, 0)
  126. for _, v := range edbList {
  127. if v.IsNullData {
  128. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  129. }
  130. }
  131. if len(warnEdbList) > 0 {
  132. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  133. }
  134. if chartInfoId > 0 && chartInfo != nil {
  135. //判断是否加入我的图库
  136. {
  137. var myChartCondition string
  138. var myChartPars []interface{}
  139. myChartCondition += ` AND a.admin_id=? `
  140. myChartPars = append(myChartPars, sysUser.AdminId)
  141. myChartCondition += ` AND a.chart_info_id=? `
  142. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  143. myChartList, e := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  144. if e != nil && e.Error() != utils.ErrNoRow() {
  145. errMsg = "获取失败"
  146. err = fmt.Errorf("获取我的图表信息失败,GetMyChartByCondition,Err:" + e.Error())
  147. return
  148. }
  149. if myChartList != nil && len(myChartList) > 0 {
  150. chartInfo.IsAdd = true
  151. chartInfo.MyChartId = myChartList[0].MyChartId
  152. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  153. }
  154. }
  155. }
  156. //判断是否需要展示英文标识
  157. chartInfo.IsEnChart = CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  158. // 图表的指标来源
  159. sourceNameList, sourceNameEnList := GetEdbSourceByEdbInfoIdList(edbList)
  160. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  161. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  162. // todo 指标权限
  163. {
  164. for _, item := range edbList {
  165. // 数据权限
  166. item.HaveOperaAuth = true
  167. }
  168. }
  169. resp.EdbInfoList = edbList
  170. resp.XEdbIdValue = xEdbIdValue
  171. resp.YDataList = yDataList
  172. resp.DataResp = dataResp
  173. } else {
  174. resp.EdbInfoList = mappingList
  175. }
  176. // todo 平衡表图表操作权限平衡表表格的操作权限保持一致,前端未用到该按钮权限,故不处理
  177. chartInfo.IsEdit, _ = GetBalanceExcelInfoOpButtonByChartInfoId(sysUser.AdminId, chartInfo.ChartInfoId, chartInfo.HaveOperaAuth)
  178. chartInfo.Button = data_manage.ChartViewButton{
  179. IsEdit: chartInfo.IsEdit,
  180. IsEnChart: chartInfo.IsEnChart,
  181. IsAdd: chartInfo.IsAdd,
  182. IsCopy: true,
  183. IsSetName: chartInfo.IsSetName,
  184. }
  185. resp.ChartInfo = chartInfo
  186. resp.BarChartInfo = barConfig
  187. return
  188. }
  189. // GetBalanceExcelEdbDataMapList 获取指标最后的基础数据
  190. 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) {
  191. // 指标对应的所有数据
  192. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  193. for _, v := range mappingList {
  194. //fmt.Println("v:", v.EdbInfoId)
  195. item := new(data_manage.ChartEdbInfoMapping)
  196. item.EdbInfoId = v.EdbInfoId
  197. item.SourceName = v.SourceName
  198. item.Source = v.Source
  199. item.EdbCode = v.EdbCode
  200. item.EdbName = v.EdbName
  201. item.EdbNameEn = v.EdbNameEn
  202. item.Frequency = v.Frequency
  203. item.EdbType = v.EdbType
  204. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  205. if v.Unit != `无` {
  206. item.Unit = v.Unit
  207. }
  208. item.UnitEn = v.UnitEn
  209. item.StartDate = v.StartDate
  210. item.EndDate = v.EndDate
  211. item.ModifyTime = v.ModifyTime
  212. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  213. item.PredictChartColor = v.PredictChartColor
  214. item.ClassifyId = v.ClassifyId
  215. if chartInfoId <= 0 {
  216. item.IsAxis = 1
  217. item.LeadValue = 0
  218. item.LeadUnit = ""
  219. item.ChartEdbMappingId = 0
  220. item.ChartInfoId = 0
  221. item.IsOrder = false
  222. item.EdbInfoType = 1
  223. item.ChartStyle = ""
  224. item.ChartColor = ""
  225. item.ChartWidth = 1
  226. item.MaxData = v.MaxValue
  227. item.MinData = v.MinValue
  228. } else {
  229. item.IsAxis = v.IsAxis
  230. item.EdbInfoType = v.EdbInfoType
  231. item.LeadValue = v.LeadValue
  232. item.LeadUnit = v.LeadUnit
  233. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  234. item.ChartEdbMappingId = v.ChartEdbMappingId
  235. item.ChartInfoId = v.ChartInfoId
  236. item.ChartStyle = v.ChartStyle
  237. item.ChartColor = v.ChartColor
  238. item.ChartWidth = v.ChartWidth
  239. item.IsOrder = v.IsOrder
  240. item.MaxData = v.MaxData
  241. item.MinData = v.MinData
  242. }
  243. item.LatestValue = v.LatestValue
  244. item.LatestDate = v.LatestDate
  245. item.UniqueCode = v.UniqueCode
  246. item.MoveLatestDate = v.LatestDate
  247. item.EdbAliasName = v.EdbAliasName
  248. item.IsConvert = v.IsConvert
  249. item.ConvertType = v.ConvertType
  250. item.ConvertValue = v.ConvertValue
  251. item.ConvertUnit = v.ConvertUnit
  252. item.ConvertEnUnit = v.ConvertEnUnit
  253. item.IsJoinPermission = v.IsJoinPermission
  254. var startDateReal string
  255. var diffSeconds int64
  256. if chartType == 2 { //季节性图
  257. startDateReal = startDate
  258. if len(mappingList) > 1 {
  259. item.IsAxis = v.IsAxis
  260. }
  261. } else {
  262. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  263. var startTimeRealTemp time.Time
  264. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  265. switch v.LeadUnit {
  266. case "天":
  267. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  268. case "月":
  269. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  270. case "季":
  271. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  272. case "周":
  273. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  274. case "年":
  275. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  276. }
  277. if startTimeRealTemp.Before(startDateParse) {
  278. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  279. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  280. } else {
  281. startDateReal = startDate
  282. diffSeconds = 0
  283. }
  284. // 预测指标的开始日期也要偏移
  285. {
  286. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  287. if tmpErr != nil {
  288. err = tmpErr
  289. return
  290. }
  291. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  292. if tmpErr != nil {
  293. err = tmpErr
  294. return
  295. }
  296. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  297. }
  298. } else {
  299. startDateReal = startDate
  300. }
  301. }
  302. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  303. calendarPreYear := 0
  304. if calendar == "农历" {
  305. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  306. if e != nil {
  307. err = fmt.Errorf("时间解析 time.Parse(%s, %s) error: %v", utils.FormatDate, startDateReal, e)
  308. return
  309. }
  310. calendarPreYear = newStartDateReal.Year() - 1
  311. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  312. startDateReal = newStartDateReal.Format(utils.FormatDate)
  313. }
  314. dataList := make([]*data_manage.EdbDataList, 0)
  315. dataListTmp, ok := dataListMap[v.EdbInfoId]
  316. if ok {
  317. dataList = dataListTmp
  318. // 对dataList 根据dataTimestamp 进行排序
  319. sort.Slice(dataList, func(i, j int) bool {
  320. return dataList[i].DataTimestamp < dataList[j].DataTimestamp
  321. })
  322. } else {
  323. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoId))
  324. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.EdbInfoId))
  325. }
  326. if v.IsConvert == 1 {
  327. switch v.ConvertType {
  328. case 1:
  329. for i, data := range dataList {
  330. dataList[i].Value = data.Value * v.ConvertValue
  331. }
  332. //item.MaxData = item.MaxData * v.ConvertValue
  333. //item.MinData = item.MinData * v.ConvertValue
  334. case 2:
  335. for i, data := range dataList {
  336. dataList[i].Value = data.Value / v.ConvertValue
  337. }
  338. //item.MaxData = item.MaxData / v.ConvertValue
  339. //item.MinData = item.MinData / v.ConvertValue
  340. case 3:
  341. for i, data := range dataList {
  342. if data.Value <= 0 {
  343. err = errors.New("数据中含有负数或0,无法对数运算")
  344. return
  345. }
  346. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  347. }
  348. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  349. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  350. }
  351. }
  352. edbDataListMap[v.EdbInfoId] = dataList
  353. if diffSeconds != 0 && v.EdbInfoType == 0 {
  354. dataListLen := len(dataList)
  355. for i := 0; i < dataListLen; i++ {
  356. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  357. }
  358. }
  359. if chartType == 2 && item.IsAxis == 1 {
  360. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  361. if tmpErr != nil {
  362. //item.DataList = dataList
  363. item.IsNullData = true
  364. edbList = append(edbList, item)
  365. continue
  366. }
  367. if calendar == "农历" {
  368. if len(dataList) <= 0 {
  369. result := new(data_manage.EdbDataResult)
  370. item.DataList = result
  371. } else {
  372. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  373. if tmpErr != nil {
  374. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  375. return
  376. }
  377. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  378. if tErr != nil {
  379. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  380. return
  381. }
  382. item.DataList = quarterDataList
  383. }
  384. } else {
  385. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  386. if tErr != nil {
  387. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  388. return
  389. }
  390. item.DataList = quarterDataList
  391. }
  392. } else if chartType == 2 && item.IsAxis == 0 {
  393. // 右轴数据处理
  394. xStartDate := "01-01"
  395. jumpYear := 0
  396. var seasonExtra data_manage.SeasonExtraItem
  397. if seasonExtraConfig != "" {
  398. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  399. if err != nil {
  400. return
  401. }
  402. }
  403. if seasonExtra.XStartDate != "" {
  404. xStartDate = seasonExtra.XStartDate
  405. jumpYear = seasonExtra.JumpYear
  406. }
  407. length := len(dataList)
  408. if length == 0 {
  409. return
  410. }
  411. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  412. if tmpErr != nil {
  413. //item.DataList = dataList
  414. item.IsNullData = true
  415. edbList = append(edbList, item)
  416. continue
  417. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  418. return
  419. }
  420. var rightAxisDate time.Time
  421. if jumpYear == 1 {
  422. latestDate = latestDate.AddDate(-1, 0, 0)
  423. latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(),xStartDate)
  424. rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
  425. if err != nil {
  426. return
  427. }
  428. }
  429. newDataList := make([]*data_manage.EdbDataList, 0)
  430. for _, v := range dataList {
  431. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  432. if e != nil {
  433. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  434. return
  435. }
  436. if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
  437. newDataList = append(newDataList, v)
  438. }
  439. }
  440. item.DataList = newDataList
  441. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  442. //item.DataList = dataList
  443. } else {
  444. item.DataList = dataList
  445. }
  446. edbList = append(edbList, item)
  447. }
  448. return
  449. }
  450. func CheckBalanceChartCacheAndPermission(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  451. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  452. adminId := sysUser.AdminId
  453. // todo 图表数据权限
  454. // 已授权分类id
  455. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  456. if err != nil {
  457. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  458. return
  459. }
  460. defer func() {
  461. if isOk {
  462. // 图表权限校验
  463. {
  464. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  465. //图表操作权限
  466. chartInfo.IsEdit = CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  467. chartInfo.Button = data_manage.ChartViewButton{
  468. IsEdit: chartInfo.IsEdit,
  469. IsEnChart: chartInfo.IsEnChart,
  470. IsAdd: chartInfo.IsAdd,
  471. IsCopy: true,
  472. IsSetName: chartInfo.IsSetName,
  473. }
  474. //if !resp.ChartInfo.HaveOperaAuth {
  475. // for _, v := range resp.EdbInfoList {
  476. // v.DataList = nil
  477. // }
  478. // resp.DataResp = nil
  479. // resp.XEdbIdValue = []int{}
  480. // resp.YDataList = []data_manage.YData{}
  481. //}
  482. }
  483. // todo 是否需要返回平衡表指标权限即判断平衡表的表格数据权限
  484. {
  485. // 查询图表关联的表格ID
  486. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfo.ChartInfoId)
  487. if e != nil {
  488. errMsg = "获取图表关联的表格ID失败,Err:" + e.Error()
  489. err = e
  490. return
  491. }
  492. checkExcelInfo := excelInfo
  493. if excelInfo.Source == utils.BALANCE_TABLE {
  494. checkExcelInfoId := excelInfo.ExcelInfoId
  495. if excelInfo.BalanceType == 1 {
  496. checkExcelInfoId = excelInfo.RelExcelInfoId
  497. } else {
  498. if excelInfo.ParentId > 0 {
  499. checkExcelInfoId = excelInfo.ParentId
  500. }
  501. }
  502. if checkExcelInfoId != excelInfo.ExcelInfoId {
  503. checkExcelInfo, err = excelModel.GetExcelInfoById(checkExcelInfoId)
  504. if err != nil {
  505. errMsg = "获取平衡表格信息失败"
  506. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  507. return
  508. }
  509. }
  510. }
  511. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, adminId)
  512. if e != nil {
  513. errMsg = "获取平衡表格数据权限失败"
  514. err = errors.New("获取表格权限信息失败,Err" + e.Error())
  515. return
  516. }
  517. // 数据权限
  518. for _, v := range resp.EdbInfoList {
  519. v.HaveOperaAuth = haveOperaAuth
  520. }
  521. }
  522. }
  523. }()
  524. key := GetChartInfoDataKey(chartInfo.ChartInfoId)
  525. if utils.Re == nil && isCache {
  526. if utils.Re == nil && utils.Rc.IsExist(key) {
  527. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  528. err := json.Unmarshal(redisData, &resp)
  529. if err != nil || resp == nil {
  530. return
  531. }
  532. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  533. var myCond string
  534. var myPars []interface{}
  535. myCond += ` AND a.admin_id=? `
  536. myPars = append(myPars, adminId)
  537. myCond += ` AND a.chart_info_id=? `
  538. myPars = append(myPars, chartInfo.ChartInfoId)
  539. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  540. if err != nil && err.Error() != utils.ErrNoRow() {
  541. msg = "获取失败"
  542. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  543. return
  544. }
  545. resp.ChartInfo.IsAdd = false
  546. resp.ChartInfo.MyChartId = 0
  547. resp.ChartInfo.MyChartClassifyId = ""
  548. if myList != nil && len(myList) > 0 {
  549. resp.ChartInfo.IsAdd = true
  550. resp.ChartInfo.MyChartId = myList[0].MyChartId
  551. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  552. }
  553. //判断是否加入我的图库
  554. {
  555. var myChartCondition string
  556. var myChartPars []interface{}
  557. myChartCondition += ` AND a.admin_id=? `
  558. myChartPars = append(myChartPars, adminId)
  559. myChartCondition += ` AND a.chart_info_id=? `
  560. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  561. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  562. if err != nil && err.Error() != utils.ErrNoRow() {
  563. msg = "获取失败"
  564. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  565. return
  566. }
  567. if myChartList != nil && len(myChartList) > 0 {
  568. chartInfo.IsAdd = true
  569. chartInfo.MyChartId = myChartList[0].MyChartId
  570. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  571. }
  572. }
  573. isOk = true
  574. fmt.Println("source redis")
  575. return
  576. }
  577. }
  578. }
  579. return
  580. }
  581. func getBalanceDataListStartDateAndValue(dataList []*data_manage.EdbDataList) (startDate, endDate string, startVal, endVal, maxVal, minVal float64) {
  582. if len(dataList) == 0 {
  583. return
  584. }
  585. startDate = dataList[0].DataTime
  586. startVal = dataList[0].Value
  587. maxVal = dataList[0].Value
  588. minVal = dataList[0].Value
  589. endDate = dataList[len(dataList)-1].DataTime
  590. endVal = dataList[len(dataList)-1].Value
  591. for _, v := range dataList {
  592. if v.DataTime < startDate {
  593. startDate = v.DataTime
  594. startVal = v.Value
  595. }
  596. if v.DataTime > endDate {
  597. endDate = v.DataTime
  598. endVal = v.Value
  599. }
  600. if v.Value > maxVal {
  601. maxVal = v.Value
  602. }
  603. if v.Value < minVal {
  604. minVal = v.Value
  605. }
  606. }
  607. return
  608. }
  609. func TransferChartEdbToEdbMappingFormat(chartInfoId, chartType int, mappingListTmp []*excelModel.ExcelChartEdb, dataListMap map[int][]*data_manage.EdbDataList) (mappingList []*data_manage.ChartEdbInfoMapping, err error) {
  610. mappingList = make([]*data_manage.ChartEdbInfoMapping, 0)
  611. //循环组装映射关系
  612. for _, v := range mappingListTmp {
  613. dataList := make([]*data_manage.EdbDataList, 0)
  614. dataListTmp, ok := dataListMap[v.ExcelChartEdbId]
  615. if ok {
  616. dataList = dataListTmp
  617. } else {
  618. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.ExcelChartEdbId))
  619. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.ExcelChartEdbId))
  620. }
  621. startDateStr, endDateStr, _, endVal, maxValue, minValue := getBalanceDataListStartDateAndValue(dataList)
  622. mapping := &data_manage.ChartEdbInfoMapping{
  623. EdbInfoId: v.ExcelChartEdbId,
  624. SourceName: "平衡表",
  625. Source: 0,
  626. SubSource: 0,
  627. EdbCode: v.EdbCode,
  628. EdbName: v.EdbName,
  629. EdbAliasName: v.EdbName,
  630. EdbNameEn: v.EdbNameEn,
  631. EdbAliasNameEn: "",
  632. EdbType: 0,
  633. Frequency: "",
  634. FrequencyEn: "",
  635. Unit: v.Unit,
  636. UnitEn: v.UnitEn,
  637. StartDate: startDateStr,
  638. EndDate: endDateStr,
  639. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  640. ChartEdbMappingId: v.ExcelChartEdbId,
  641. ChartInfoId: chartInfoId,
  642. MaxData: v.MaxData,
  643. MinData: v.MinData,
  644. IsOrder: v.IsOrder,
  645. IsAxis: v.IsAxis,
  646. EdbInfoType: v.EdbInfoType,
  647. //EdbInfoCategoryType: 0,
  648. LeadValue: v.LeadValue,
  649. LeadUnit: v.LeadUnit,
  650. LeadUnitEn: "",
  651. ChartStyle: "",
  652. ChartColor: "",
  653. PredictChartColor: "",
  654. ChartWidth: v.ChartWidth,
  655. ChartType: chartType,
  656. LatestDate: endDateStr,
  657. LatestValue: endVal,
  658. MoveLatestDate: "",
  659. UniqueCode: "",
  660. MinValue: minValue,
  661. MaxValue: maxValue,
  662. DataList: nil,
  663. IsNullData: false,
  664. MappingSource: 0,
  665. RegionType: "",
  666. ClassifyId: 0,
  667. IsConvert: 0,
  668. ConvertType: 0,
  669. ConvertValue: 0,
  670. ConvertUnit: "",
  671. ConvertEnUnit: "",
  672. IsJoinPermission: 0,
  673. HaveOperaAuth: true,
  674. }
  675. mappingList = append(mappingList, mapping)
  676. }
  677. return
  678. }
  679. // GetBalanceExcelInfoOpButtonByChartInfoId 获取图表权限
  680. func GetBalanceExcelInfoOpButtonByChartInfoId(sysUserId, chartInfoId int, chartHaveOperaAuth bool) (opButton bool, err error) {
  681. // 如果没有数据权限,那么直接返回
  682. if !chartHaveOperaAuth {
  683. return
  684. }
  685. // 获取表格信息
  686. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfoId)
  687. if e != nil {
  688. err = fmt.Errorf("获取ETA表格失败 Err %s", e.Error())
  689. return
  690. }
  691. checkExcelInfo := excelInfo
  692. if excelInfo.Source == utils.BALANCE_TABLE {
  693. checkExcelInfoId := excelInfo.ExcelInfoId
  694. if excelInfo.BalanceType == 1 {
  695. checkExcelInfoId = excelInfo.RelExcelInfoId
  696. } else {
  697. if excelInfo.ParentId > 0 {
  698. checkExcelInfoId = excelInfo.ParentId
  699. }
  700. }
  701. if checkExcelInfoId != excelInfo.ExcelInfoId {
  702. checkExcelInfo, e = excelModel.GetExcelInfoById(checkExcelInfoId)
  703. if e != nil {
  704. err = fmt.Errorf("获取平衡表格信息失败,Err:" + e.Error())
  705. return
  706. }
  707. }
  708. } else {
  709. return
  710. }
  711. // 数据权限
  712. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  713. if e != nil {
  714. err = fmt.Errorf("获取表格权限信息失败,Err" + e.Error())
  715. return
  716. }
  717. // 如果没有数据权限,那么直接返回
  718. if !haveOperaAuth {
  719. return
  720. }
  721. //button := GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  722. if sysUserId == checkExcelInfo.SysUserId {
  723. opButton = true
  724. } else {
  725. obj := new(excelModel.ExcelWorker)
  726. workerList, e := obj.GetByExcelInfoId(checkExcelInfo.ExcelInfoId)
  727. if e == nil {
  728. for _, v := range workerList {
  729. if v.SysUserId == sysUserId {
  730. opButton = true
  731. break
  732. }
  733. }
  734. }
  735. }
  736. return
  737. }