chart_info_excel_balance.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. seasonXStartDateWithYear := ""
  194. seasonXEndDateWithYear := ""
  195. // 如果是季节性图则进入特殊排序
  196. if chartType == 2 {
  197. // 根据设置的左右轴,对mappingList进行排序,1左轴排在前面,0右轴排在前面
  198. sort.Slice(mappingList, func(i, j int) bool {
  199. return mappingList[i].IsAxis > mappingList[j].IsAxis
  200. })
  201. }
  202. for _, v := range mappingList {
  203. //fmt.Println("v:", v.EdbInfoId)
  204. item := new(data_manage.ChartEdbInfoMapping)
  205. item.EdbInfoId = v.EdbInfoId
  206. item.SourceName = v.SourceName
  207. item.Source = v.Source
  208. item.EdbCode = v.EdbCode
  209. item.EdbName = v.EdbName
  210. item.EdbNameEn = v.EdbNameEn
  211. item.Frequency = v.Frequency
  212. item.EdbType = v.EdbType
  213. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  214. if v.Unit != `无` {
  215. item.Unit = v.Unit
  216. }
  217. item.UnitEn = v.UnitEn
  218. item.StartDate = v.StartDate
  219. item.EndDate = v.EndDate
  220. item.ModifyTime = v.ModifyTime
  221. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  222. item.PredictChartColor = v.PredictChartColor
  223. item.ClassifyId = v.ClassifyId
  224. if chartInfoId <= 0 {
  225. item.IsAxis = 1
  226. item.LeadValue = 0
  227. item.LeadUnit = ""
  228. item.ChartEdbMappingId = 0
  229. item.ChartInfoId = 0
  230. item.IsOrder = false
  231. item.EdbInfoType = 1
  232. item.ChartStyle = ""
  233. item.ChartColor = ""
  234. item.ChartWidth = 1
  235. item.MaxData = v.MaxValue
  236. item.MinData = v.MinValue
  237. } else {
  238. item.IsAxis = v.IsAxis
  239. item.EdbInfoType = v.EdbInfoType
  240. item.LeadValue = v.LeadValue
  241. item.LeadUnit = v.LeadUnit
  242. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  243. item.ChartEdbMappingId = v.ChartEdbMappingId
  244. item.ChartInfoId = v.ChartInfoId
  245. item.ChartStyle = v.ChartStyle
  246. item.ChartColor = v.ChartColor
  247. item.ChartWidth = v.ChartWidth
  248. item.IsOrder = v.IsOrder
  249. item.MaxData = v.MaxData
  250. item.MinData = v.MinData
  251. }
  252. item.LatestValue = v.LatestValue
  253. item.LatestDate = v.LatestDate
  254. item.UniqueCode = v.UniqueCode
  255. item.MoveLatestDate = v.LatestDate
  256. item.EdbAliasName = v.EdbAliasName
  257. item.IsConvert = v.IsConvert
  258. item.ConvertType = v.ConvertType
  259. item.ConvertValue = v.ConvertValue
  260. item.ConvertUnit = v.ConvertUnit
  261. item.ConvertEnUnit = v.ConvertEnUnit
  262. item.IsJoinPermission = v.IsJoinPermission
  263. var startDateReal string
  264. var diffSeconds int64
  265. if chartType == 2 { //季节性图
  266. startDateReal = startDate
  267. if len(mappingList) > 1 {
  268. item.IsAxis = v.IsAxis
  269. }
  270. } else {
  271. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  272. var startTimeRealTemp time.Time
  273. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  274. switch v.LeadUnit {
  275. case "天":
  276. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  277. case "月":
  278. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  279. case "季":
  280. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  281. case "周":
  282. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  283. case "年":
  284. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  285. }
  286. if startTimeRealTemp.Before(startDateParse) {
  287. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  288. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  289. } else {
  290. startDateReal = startDate
  291. diffSeconds = 0
  292. }
  293. // 预测指标的开始日期也要偏移
  294. {
  295. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  296. if tmpErr != nil {
  297. err = tmpErr
  298. return
  299. }
  300. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  301. if tmpErr != nil {
  302. err = tmpErr
  303. return
  304. }
  305. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  306. }
  307. } else {
  308. startDateReal = startDate
  309. }
  310. }
  311. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  312. calendarPreYear := 0
  313. if calendar == "农历" {
  314. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  315. if e != nil {
  316. err = fmt.Errorf("时间解析 time.Parse(%s, %s) error: %v", utils.FormatDate, startDateReal, e)
  317. return
  318. }
  319. calendarPreYear = newStartDateReal.Year() - 1
  320. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  321. startDateReal = newStartDateReal.Format(utils.FormatDate)
  322. }
  323. dataList := make([]*data_manage.EdbDataList, 0)
  324. dataListTmp, ok := dataListMap[v.EdbInfoId]
  325. if ok {
  326. dataList = dataListTmp
  327. // 对dataList 根据dataTimestamp 进行排序
  328. sort.Slice(dataList, func(i, j int) bool {
  329. return dataList[i].DataTimestamp < dataList[j].DataTimestamp
  330. })
  331. } else {
  332. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoId))
  333. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.EdbInfoId))
  334. }
  335. if v.IsConvert == 1 {
  336. switch v.ConvertType {
  337. case 1:
  338. for i, data := range dataList {
  339. dataList[i].Value = data.Value * v.ConvertValue
  340. }
  341. //item.MaxData = item.MaxData * v.ConvertValue
  342. //item.MinData = item.MinData * v.ConvertValue
  343. case 2:
  344. for i, data := range dataList {
  345. dataList[i].Value = data.Value / v.ConvertValue
  346. }
  347. //item.MaxData = item.MaxData / v.ConvertValue
  348. //item.MinData = item.MinData / v.ConvertValue
  349. case 3:
  350. for i, data := range dataList {
  351. if data.Value <= 0 {
  352. err = errors.New("数据中含有负数或0,无法对数运算")
  353. return
  354. }
  355. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  356. }
  357. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  358. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  359. }
  360. }
  361. edbDataListMap[v.EdbInfoId] = dataList
  362. if diffSeconds != 0 && v.EdbInfoType == 0 {
  363. dataListLen := len(dataList)
  364. for i := 0; i < dataListLen; i++ {
  365. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  366. }
  367. }
  368. if chartType == 2 && item.IsAxis == 1 {
  369. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  370. if tmpErr != nil {
  371. //item.DataList = dataList
  372. item.IsNullData = true
  373. edbList = append(edbList, item)
  374. continue
  375. }
  376. if calendar == "农历" {
  377. if len(dataList) <= 0 {
  378. result := new(data_manage.EdbDataResult)
  379. item.DataList = result
  380. } else {
  381. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  382. if tmpErr != nil {
  383. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  384. return
  385. }
  386. quarterDataList, seasonXStartDateWithYearTmp, seasonXEndDateWithYearTmp, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  387. if tErr != nil {
  388. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  389. return
  390. }
  391. item.DataList = quarterDataList
  392. seasonXStartDateWithYear = seasonXStartDateWithYearTmp
  393. seasonXEndDateWithYear = seasonXEndDateWithYearTmp
  394. }
  395. } else {
  396. quarterDataList, seasonXStartDateWithYearTmp, seasonXEndDateWithYearTmp, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  397. if tErr != nil {
  398. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  399. return
  400. }
  401. item.DataList = quarterDataList
  402. seasonXStartDateWithYear = seasonXStartDateWithYearTmp
  403. seasonXEndDateWithYear = seasonXEndDateWithYearTmp
  404. }
  405. } else if chartType == 2 && item.IsAxis == 0 {
  406. newDataList := make([]*data_manage.EdbDataList, 0)
  407. seasonXStartDateWithYearT, _ := time.Parse(utils.FormatDate, seasonXStartDateWithYear)
  408. seasonXEndDateWithYearT, _ := time.Parse(utils.FormatDate, seasonXEndDateWithYear)
  409. for _, v := range dataList {
  410. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  411. if e != nil {
  412. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  413. return
  414. }
  415. // 如果数据时间在横轴的开始日期和结束日期之间,则加入到数据列表中
  416. if (dataTime.After(seasonXStartDateWithYearT) && dataTime.Before(seasonXEndDateWithYearT)) || dataTime.Equal(seasonXStartDateWithYearT) || dataTime.Equal(seasonXEndDateWithYearT) {
  417. newDataList = append(newDataList, v)
  418. }
  419. }
  420. item.DataList = newDataList
  421. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  422. //item.DataList = dataList
  423. } else {
  424. item.DataList = dataList
  425. }
  426. edbList = append(edbList, item)
  427. }
  428. return
  429. }
  430. func CheckBalanceChartCacheAndPermission(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  431. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  432. adminId := sysUser.AdminId
  433. // todo 图表数据权限
  434. // 已授权分类id
  435. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  436. if err != nil {
  437. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  438. return
  439. }
  440. defer func() {
  441. if isOk {
  442. // 图表权限校验
  443. {
  444. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  445. //图表操作权限
  446. chartInfo.IsEdit = CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  447. chartInfo.Button = data_manage.ChartViewButton{
  448. IsEdit: chartInfo.IsEdit,
  449. IsEnChart: chartInfo.IsEnChart,
  450. IsAdd: chartInfo.IsAdd,
  451. IsCopy: true,
  452. IsSetName: chartInfo.IsSetName,
  453. }
  454. //if !resp.ChartInfo.HaveOperaAuth {
  455. // for _, v := range resp.EdbInfoList {
  456. // v.DataList = nil
  457. // }
  458. // resp.DataResp = nil
  459. // resp.XEdbIdValue = []int{}
  460. // resp.YDataList = []data_manage.YData{}
  461. //}
  462. }
  463. // todo 是否需要返回平衡表指标权限即判断平衡表的表格数据权限
  464. {
  465. // 查询图表关联的表格ID
  466. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfo.ChartInfoId)
  467. if e != nil {
  468. errMsg = "获取图表关联的表格ID失败,Err:" + e.Error()
  469. err = e
  470. return
  471. }
  472. checkExcelInfo := excelInfo
  473. if excelInfo.Source == utils.BALANCE_TABLE {
  474. checkExcelInfoId := excelInfo.ExcelInfoId
  475. if excelInfo.BalanceType == 1 {
  476. checkExcelInfoId = excelInfo.RelExcelInfoId
  477. } else {
  478. if excelInfo.ParentId > 0 {
  479. checkExcelInfoId = excelInfo.ParentId
  480. }
  481. }
  482. if checkExcelInfoId != excelInfo.ExcelInfoId {
  483. checkExcelInfo, err = excelModel.GetExcelInfoById(checkExcelInfoId)
  484. if err != nil {
  485. errMsg = "获取平衡表格信息失败"
  486. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  487. return
  488. }
  489. }
  490. }
  491. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, adminId)
  492. if e != nil {
  493. errMsg = "获取平衡表格数据权限失败"
  494. err = errors.New("获取表格权限信息失败,Err" + e.Error())
  495. return
  496. }
  497. // 数据权限
  498. for _, v := range resp.EdbInfoList {
  499. v.HaveOperaAuth = haveOperaAuth
  500. }
  501. }
  502. }
  503. }()
  504. key := GetChartInfoDataKey(chartInfo.ChartInfoId)
  505. if utils.Re == nil && isCache {
  506. if utils.Re == nil && utils.Rc.IsExist(key) {
  507. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  508. err := json.Unmarshal(redisData, &resp)
  509. if err != nil || resp == nil {
  510. return
  511. }
  512. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  513. var myCond string
  514. var myPars []interface{}
  515. myCond += ` AND a.admin_id=? `
  516. myPars = append(myPars, adminId)
  517. myCond += ` AND a.chart_info_id=? `
  518. myPars = append(myPars, chartInfo.ChartInfoId)
  519. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  520. if err != nil && err.Error() != utils.ErrNoRow() {
  521. msg = "获取失败"
  522. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  523. return
  524. }
  525. resp.ChartInfo.IsAdd = false
  526. resp.ChartInfo.MyChartId = 0
  527. resp.ChartInfo.MyChartClassifyId = ""
  528. if myList != nil && len(myList) > 0 {
  529. resp.ChartInfo.IsAdd = true
  530. resp.ChartInfo.MyChartId = myList[0].MyChartId
  531. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  532. }
  533. //判断是否加入我的图库
  534. {
  535. var myChartCondition string
  536. var myChartPars []interface{}
  537. myChartCondition += ` AND a.admin_id=? `
  538. myChartPars = append(myChartPars, adminId)
  539. myChartCondition += ` AND a.chart_info_id=? `
  540. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  541. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  542. if err != nil && err.Error() != utils.ErrNoRow() {
  543. msg = "获取失败"
  544. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  545. return
  546. }
  547. if myChartList != nil && len(myChartList) > 0 {
  548. chartInfo.IsAdd = true
  549. chartInfo.MyChartId = myChartList[0].MyChartId
  550. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  551. }
  552. }
  553. isOk = true
  554. fmt.Println("source redis")
  555. return
  556. }
  557. }
  558. }
  559. return
  560. }
  561. func getBalanceDataListStartDateAndValue(dataList []*data_manage.EdbDataList) (startDate, endDate string, startVal, endVal, maxVal, minVal float64) {
  562. if len(dataList) == 0 {
  563. return
  564. }
  565. startDate = dataList[0].DataTime
  566. startVal = dataList[0].Value
  567. maxVal = dataList[0].Value
  568. minVal = dataList[0].Value
  569. endDate = dataList[len(dataList)-1].DataTime
  570. endVal = dataList[len(dataList)-1].Value
  571. for _, v := range dataList {
  572. if v.DataTime < startDate {
  573. startDate = v.DataTime
  574. startVal = v.Value
  575. }
  576. if v.DataTime > endDate {
  577. endDate = v.DataTime
  578. endVal = v.Value
  579. }
  580. if v.Value > maxVal {
  581. maxVal = v.Value
  582. }
  583. if v.Value < minVal {
  584. minVal = v.Value
  585. }
  586. }
  587. return
  588. }
  589. func TransferChartEdbToEdbMappingFormat(chartInfoId, chartType int, mappingListTmp []*excelModel.ExcelChartEdb, dataListMap map[int][]*data_manage.EdbDataList) (mappingList []*data_manage.ChartEdbInfoMapping, err error) {
  590. mappingList = make([]*data_manage.ChartEdbInfoMapping, 0)
  591. //循环组装映射关系
  592. for _, v := range mappingListTmp {
  593. dataList := make([]*data_manage.EdbDataList, 0)
  594. dataListTmp, ok := dataListMap[v.ExcelChartEdbId]
  595. if ok {
  596. dataList = dataListTmp
  597. } else {
  598. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.ExcelChartEdbId))
  599. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.ExcelChartEdbId))
  600. }
  601. startDateStr, endDateStr, _, endVal, maxValue, minValue := getBalanceDataListStartDateAndValue(dataList)
  602. mapping := &data_manage.ChartEdbInfoMapping{
  603. EdbInfoId: v.ExcelChartEdbId,
  604. SourceName: "平衡表",
  605. Source: 0,
  606. SubSource: 0,
  607. EdbCode: v.EdbCode,
  608. EdbName: v.EdbName,
  609. EdbAliasName: v.EdbName,
  610. EdbNameEn: v.EdbNameEn,
  611. EdbAliasNameEn: "",
  612. EdbType: 0,
  613. Frequency: "",
  614. FrequencyEn: "",
  615. Unit: v.Unit,
  616. UnitEn: v.UnitEn,
  617. StartDate: startDateStr,
  618. EndDate: endDateStr,
  619. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  620. ChartEdbMappingId: v.ExcelChartEdbId,
  621. ChartInfoId: chartInfoId,
  622. MaxData: v.MaxData,
  623. MinData: v.MinData,
  624. IsOrder: v.IsOrder,
  625. IsAxis: v.IsAxis,
  626. EdbInfoType: v.EdbInfoType,
  627. //EdbInfoCategoryType: 0,
  628. LeadValue: v.LeadValue,
  629. LeadUnit: v.LeadUnit,
  630. LeadUnitEn: "",
  631. ChartStyle: "",
  632. ChartColor: "",
  633. PredictChartColor: "",
  634. ChartWidth: v.ChartWidth,
  635. ChartType: chartType,
  636. LatestDate: endDateStr,
  637. LatestValue: endVal,
  638. MoveLatestDate: "",
  639. UniqueCode: "",
  640. MinValue: minValue,
  641. MaxValue: maxValue,
  642. DataList: nil,
  643. IsNullData: false,
  644. MappingSource: 0,
  645. RegionType: "",
  646. ClassifyId: 0,
  647. IsConvert: 0,
  648. ConvertType: 0,
  649. ConvertValue: 0,
  650. ConvertUnit: "",
  651. ConvertEnUnit: "",
  652. IsJoinPermission: 0,
  653. HaveOperaAuth: true,
  654. }
  655. mappingList = append(mappingList, mapping)
  656. }
  657. return
  658. }
  659. // GetBalanceExcelInfoOpButtonByChartInfoId 获取图表权限
  660. func GetBalanceExcelInfoOpButtonByChartInfoId(sysUserId, chartInfoId int, chartHaveOperaAuth bool) (opButton bool, err error) {
  661. // 如果没有数据权限,那么直接返回
  662. if !chartHaveOperaAuth {
  663. return
  664. }
  665. // 获取表格信息
  666. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfoId)
  667. if e != nil {
  668. err = fmt.Errorf("获取ETA表格失败 Err %s", e.Error())
  669. return
  670. }
  671. checkExcelInfo := excelInfo
  672. if excelInfo.Source == utils.BALANCE_TABLE {
  673. checkExcelInfoId := excelInfo.ExcelInfoId
  674. if excelInfo.BalanceType == 1 {
  675. checkExcelInfoId = excelInfo.RelExcelInfoId
  676. } else {
  677. if excelInfo.ParentId > 0 {
  678. checkExcelInfoId = excelInfo.ParentId
  679. }
  680. }
  681. if checkExcelInfoId != excelInfo.ExcelInfoId {
  682. checkExcelInfo, e = excelModel.GetExcelInfoById(checkExcelInfoId)
  683. if e != nil {
  684. err = fmt.Errorf("获取平衡表格信息失败,Err:" + e.Error())
  685. return
  686. }
  687. }
  688. } else {
  689. return
  690. }
  691. // 数据权限
  692. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  693. if e != nil {
  694. err = fmt.Errorf("获取表格权限信息失败,Err" + e.Error())
  695. return
  696. }
  697. // 如果没有数据权限,那么直接返回
  698. if !haveOperaAuth {
  699. return
  700. }
  701. //button := GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  702. if sysUserId == checkExcelInfo.SysUserId {
  703. opButton = true
  704. } else {
  705. obj := new(excelModel.ExcelWorker)
  706. workerList, e := obj.GetByExcelInfoId(checkExcelInfo.ExcelInfoId)
  707. if e == nil {
  708. for _, v := range workerList {
  709. if v.SysUserId == sysUserId {
  710. opButton = true
  711. break
  712. }
  713. }
  714. }
  715. }
  716. return
  717. }