chart_info.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/company"
  6. "eta/eta_mobile/models/data_manage"
  7. "eta/eta_mobile/models/system"
  8. "eta/eta_mobile/services/alarm_msg"
  9. "eta/eta_mobile/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "math"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type ChartInfoReq struct {
  19. ChartInfoId int `description:"图表id,新增时传0"`
  20. }
  21. // DeleteChartInfoDataRedis 清除图表缓存
  22. func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
  23. var req ChartInfoReq
  24. err = json.Unmarshal(bodyByte, &req)
  25. if err != nil {
  26. return
  27. }
  28. if req.ChartInfoId > 0 {
  29. err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
  30. }
  31. return
  32. }
  33. // DeleteChartClassifyRedis 清除图表分类缓存
  34. func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
  35. err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
  36. return
  37. }
  38. // GetChartInfoDataKey 获取图表缓存的key
  39. func GetChartInfoDataKey(chartInfoId int) string {
  40. key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
  41. return key
  42. }
  43. // CheckOpChartPermission 判断图表操作权限
  44. func CheckOpChartPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  45. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  46. ok = true
  47. }
  48. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  49. if ok == false && createUserId == sysUser.AdminId {
  50. ok = true
  51. }
  52. // 如果图表权限id 是 1 ,那么允许编辑
  53. if ok == false && sysUser.ChartPermission == 1 {
  54. ok = true
  55. }
  56. return
  57. }
  58. // GetChartEdbData 获取图表的指标数据
  59. func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
  60. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  61. xEdbIdValue = make([]int, 0)
  62. yDataList = make([]data_manage.YData, 0)
  63. var extraConfig interface{}
  64. switch chartType {
  65. case 7: // 柱形图
  66. var barConfig data_manage.BarChartInfoReq
  67. if extraConfigStr == `` {
  68. errMsg = "柱方图未配置"
  69. err = errors.New(errMsg)
  70. return
  71. }
  72. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  73. if err != nil {
  74. errMsg = "柱方图配置异常"
  75. err = errors.New(errMsg)
  76. return
  77. }
  78. extraConfig = barConfig
  79. case 10: // 截面散点图
  80. var tmpExtraConfig data_manage.SectionScatterReq
  81. if extraConfigStr == `` {
  82. errMsg = "截面散点图未配置"
  83. err = errors.New(errMsg)
  84. return
  85. }
  86. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  87. if err != nil {
  88. errMsg = "截面散点配置异常"
  89. err = errors.New(errMsg)
  90. return
  91. }
  92. extraConfig = tmpExtraConfig
  93. case utils.CHART_TYPE_RADAR:
  94. var barConfig data_manage.RadarChartInfoReq
  95. if extraConfigStr == `` {
  96. errMsg = "雷达图未配置"
  97. err = errors.New(errMsg)
  98. return
  99. }
  100. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  101. if err != nil {
  102. errMsg = "雷达图配置异常"
  103. err = errors.New(errMsg)
  104. return
  105. }
  106. extraConfig = barConfig
  107. default:
  108. xEdbIdValue = make([]int, 0)
  109. yDataList = make([]data_manage.YData, 0)
  110. }
  111. // 指标对应的所有数据
  112. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  113. if err != nil {
  114. return
  115. }
  116. // 特殊图形数据处理
  117. switch chartType {
  118. case 7: // 柱形图
  119. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  120. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  121. for _, v := range edbList {
  122. // 指标别名
  123. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  124. for _, reqEdb := range barChartConf.EdbInfoIdList {
  125. if v.EdbInfoId == reqEdb.EdbInfoId {
  126. v.EdbAliasName = reqEdb.Name
  127. }
  128. }
  129. }
  130. }
  131. for k := range yDataList {
  132. yDataList[k].Unit = barChartConf.Unit
  133. yDataList[k].UnitEn = barChartConf.UnitEn
  134. }
  135. case 10: // 截面散点图
  136. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  137. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  138. // 这个数据没有必要返回给前端
  139. for _, v := range edbList {
  140. v.DataList = nil
  141. }
  142. case utils.CHART_TYPE_RADAR: //雷达图
  143. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  144. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  145. }
  146. return
  147. }
  148. // GetEdbDataMapList 获取指标最后的基础数据
  149. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  150. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  151. return
  152. }
  153. // getEdbDataMapList 获取指标最后的基础数据
  154. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  155. // 指标对应的所有数据
  156. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  157. for _, v := range mappingList {
  158. //fmt.Println("v:", v.EdbInfoId)
  159. item := new(data_manage.ChartEdbInfoMapping)
  160. item.EdbInfoId = v.EdbInfoId
  161. item.SourceName = v.SourceName
  162. item.Source = v.Source
  163. item.EdbCode = v.EdbCode
  164. item.EdbName = v.EdbName
  165. item.EdbNameEn = v.EdbNameEn
  166. item.Frequency = v.Frequency
  167. item.EdbType = v.EdbType
  168. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  169. if v.Unit != `无` {
  170. item.Unit = v.Unit
  171. }
  172. item.UnitEn = v.UnitEn
  173. item.StartDate = v.StartDate
  174. item.EndDate = v.EndDate
  175. item.ModifyTime = v.ModifyTime
  176. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  177. item.PredictChartColor = v.PredictChartColor
  178. item.ClassifyId = v.ClassifyId
  179. item.EdbAliasName = v.EdbAliasName
  180. if chartInfoId <= 0 {
  181. item.IsAxis = 1
  182. item.LeadValue = 0
  183. item.LeadUnit = ""
  184. item.ChartEdbMappingId = 0
  185. item.ChartInfoId = 0
  186. item.IsOrder = false
  187. item.EdbInfoType = 1
  188. item.ChartStyle = ""
  189. item.ChartColor = ""
  190. item.ChartWidth = 0
  191. item.MaxData = v.MaxValue
  192. item.MinData = v.MinValue
  193. } else {
  194. item.IsAxis = v.IsAxis
  195. item.EdbInfoType = v.EdbInfoType
  196. item.LeadValue = v.LeadValue
  197. item.LeadUnit = v.LeadUnit
  198. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  199. item.ChartEdbMappingId = v.ChartEdbMappingId
  200. item.ChartInfoId = v.ChartInfoId
  201. item.ChartStyle = v.ChartStyle
  202. item.ChartColor = v.ChartColor
  203. item.ChartWidth = v.ChartWidth
  204. item.IsOrder = v.IsOrder
  205. item.MaxData = v.MaxData
  206. item.MinData = v.MinData
  207. }
  208. item.LatestValue = v.LatestValue
  209. item.LatestDate = v.LatestDate
  210. item.UniqueCode = v.UniqueCode
  211. item.MoveLatestDate = v.LatestDate
  212. var startDateReal string
  213. var diffSeconds int64
  214. if chartType == 2 { //季节性图
  215. startDateReal = startDate
  216. } else {
  217. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  218. var startTimeRealTemp time.Time
  219. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  220. switch v.LeadUnit {
  221. case "天":
  222. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  223. case "月":
  224. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  225. case "季":
  226. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  227. case "周":
  228. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  229. case "年":
  230. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  231. }
  232. if startTimeRealTemp.Before(startDateParse) {
  233. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  234. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  235. } else {
  236. startDateReal = startDate
  237. diffSeconds = 0
  238. }
  239. // 预测指标的开始日期也要偏移
  240. {
  241. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  242. if tmpErr != nil {
  243. err = tmpErr
  244. return
  245. }
  246. latestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.LatestDate, time.Local)
  247. if tmpErr != nil {
  248. err = tmpErr
  249. return
  250. }
  251. item.LatestDate = latestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  252. }
  253. } else {
  254. startDateReal = startDate
  255. }
  256. }
  257. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  258. calendarPreYear := 0
  259. if calendar == "农历" {
  260. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  261. if err != nil {
  262. fmt.Println("time.Parse:" + err.Error())
  263. }
  264. calendarPreYear = newStartDateReal.Year() - 1
  265. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  266. startDateReal = newStartDateReal.Format(utils.FormatDate)
  267. }
  268. dataList := make([]*data_manage.EdbDataList, 0)
  269. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  270. //var newEdbInfo *data_manage.EdbInfo
  271. switch v.EdbInfoCategoryType {
  272. case 0:
  273. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  274. case 1:
  275. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
  276. default:
  277. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  278. }
  279. if err != nil {
  280. return
  281. }
  282. edbDataListMap[v.EdbInfoId] = dataList
  283. if diffSeconds != 0 && v.EdbInfoType == 0 {
  284. dataListLen := len(dataList)
  285. for i := 0; i < dataListLen; i++ {
  286. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  287. }
  288. }
  289. if chartType == 2 {
  290. //latestDateStr := v.LatestDate //实际数据的截止日期
  291. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  292. if tmpErr != nil {
  293. //item.DataList = dataList
  294. item.IsNullData = true
  295. edbList = append(edbList, item)
  296. continue
  297. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  298. return
  299. }
  300. if calendar == "农历" {
  301. if len(dataList) <= 0 {
  302. result := new(data_manage.EdbDataResult)
  303. item.DataList = result
  304. } else {
  305. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  306. if tmpErr != nil {
  307. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  308. return
  309. }
  310. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  311. if tErr != nil {
  312. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  313. return
  314. }
  315. item.DataList = quarterDataList
  316. }
  317. } else {
  318. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  319. if tErr != nil {
  320. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  321. return
  322. }
  323. item.DataList = quarterDataList
  324. }
  325. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  326. //item.DataList = dataList
  327. } else {
  328. item.DataList = dataList
  329. }
  330. edbList = append(edbList, item)
  331. }
  332. return
  333. }
  334. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  335. func GetSeasonEdbInfoDataListByXDate(dataList []*data_manage.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort data_manage.QuarterDataList, err error) {
  336. xStartDate := "01-01"
  337. xEndDate := "12-31"
  338. jumpYear := 0
  339. legends := make([]data_manage.SeasonChartLegend, 0)
  340. var seasonExtra data_manage.SeasonExtraItem
  341. if seasonExtraConfig != "" {
  342. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  343. if err != nil {
  344. return
  345. }
  346. }
  347. if seasonExtra.XStartDate != "" {
  348. xStartDate = seasonExtra.XStartDate
  349. xEndDate = seasonExtra.XEndDate
  350. jumpYear = seasonExtra.JumpYear
  351. legends = seasonExtra.ChartLegend
  352. }
  353. length := len(dataList)
  354. if length == 0 {
  355. return
  356. }
  357. legendMap := make(map[string]string, 0)
  358. if len(legends) > 0 {
  359. for _, v := range legends {
  360. legendMap[v.Name] = v.Value
  361. }
  362. }
  363. latestDateStr := latestDate.Format(utils.FormatDate)
  364. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  365. //根据数据确定最早的年份,和最近年份
  366. //根据横轴的日期,汇总所有的年份
  367. startDate := dataList[0].DataTime
  368. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  369. if tmpErr != nil {
  370. err = tmpErr
  371. return
  372. }
  373. startYear := startDateT.Year()
  374. //获取数据的最新日期
  375. lastDate := dataList[length-1].DataTime
  376. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  377. if tmpErr != nil {
  378. err = tmpErr
  379. return
  380. }
  381. endYear := lastDateT.Year()
  382. nowYear := time.Now().Year()
  383. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  384. quarterDataList := make([]*data_manage.QuarterData, 0)
  385. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  386. //整理出日期
  387. idx := 1
  388. chartLegendMap := make(map[string]int, 0)
  389. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  390. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  391. currentEndYear := currentStartYear
  392. if jumpYear == 1 {
  393. currentEndYear = currentStartYear + 1
  394. }
  395. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  396. name := fmt.Sprintf("%s_%s", startStr, endStr)
  397. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  398. startT, tEr := time.Parse(utils.FormatDate, startStr)
  399. if tEr != nil {
  400. err = tEr
  401. return
  402. }
  403. endT, tEr := time.Parse(utils.FormatDate, endStr)
  404. if tEr != nil {
  405. err = tEr
  406. return
  407. }
  408. if lastDateT.Before(startT) {
  409. //如果最新的日期在起始日之前,则跳出循环
  410. break
  411. }
  412. if endT.Year() > nowYear {
  413. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  414. nowYear = endT.Year()
  415. }
  416. item := data_manage.QuarterXDateItem{
  417. StartDate: startT,
  418. EndDate: endT,
  419. ShowName: showName,
  420. }
  421. dataMap[name] = item
  422. chartLegendMap[name] = idx
  423. idx++
  424. if lastDateT.Before(endT) {
  425. //如果最新的日期在起始日之前,则跳出循环
  426. break
  427. }
  428. }
  429. lenYear := len(dataMap)
  430. for k, v := range dataMap {
  431. if i, ok := chartLegendMap[k]; ok {
  432. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  433. }
  434. dataMap[k] = v
  435. }
  436. for _, v := range dataList {
  437. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  438. year := dataTimeT.Year()
  439. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  440. for k, dateItem := range dataMap {
  441. tmpVal := data_manage.EdbDataList{
  442. EdbDataId: v.EdbDataId,
  443. EdbInfoId: v.EdbInfoId,
  444. DataTime: v.DataTime,
  445. DataTimestamp: v.DataTimestamp,
  446. Value: v.Value,
  447. }
  448. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  449. if jumpYear == 1 {
  450. //计算前一年最大的日期, 只补齐数据到去年
  451. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  452. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  453. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  454. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  455. } else {
  456. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  457. }
  458. } else {
  459. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  460. }
  461. timestamp := newItemDate.UnixNano() / 1e6
  462. tmpVal.DataTimestamp = timestamp
  463. tmpV := &tmpVal
  464. if findVal, ok := quarterMap[k]; !ok {
  465. findVal = append(findVal, tmpV)
  466. quarterMap[k] = findVal
  467. } else {
  468. findVal = append(findVal, tmpV)
  469. quarterMap[k] = findVal
  470. }
  471. if v.DataTime == latestDateStr {
  472. dateItem.CuttingDataTimestamp = timestamp
  473. dataMap[k] = dateItem
  474. }
  475. //break
  476. }
  477. }
  478. }
  479. for k, v := range dataMap {
  480. itemList := quarterMap[k]
  481. quarterItem := new(data_manage.QuarterData)
  482. quarterItem.Years = v.ShowName
  483. quarterItem.ChartLegend = v.ChartLegend
  484. if le, ok := legendMap[v.ShowName]; ok {
  485. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  486. quarterItem.ChartLegend = le
  487. }
  488. }
  489. quarterItem.DataList = itemList
  490. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  491. //如果等于最后的实际日期,那么将切割时间戳记录
  492. if quarterItem.CuttingDataTimestamp == 0 {
  493. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  494. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  495. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  496. }
  497. }
  498. quarterDataList = append(quarterDataList, quarterItem)
  499. }
  500. if len(quarterDataList) > 0 {
  501. quarterDataListSort = quarterDataList
  502. sort.Sort(quarterDataListSort)
  503. }
  504. return
  505. }
  506. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  507. func GetSeasonEdbInfoDataListByXDateNong(result *data_manage.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort data_manage.QuarterDataList, err error) {
  508. xStartDate := "01-01"
  509. xEndDate := "12-31"
  510. jumpYear := 0
  511. legends := make([]data_manage.SeasonChartLegend, 0)
  512. var seasonExtra data_manage.SeasonExtraItem
  513. if seasonExtraConfig != "" {
  514. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  515. if err != nil {
  516. return
  517. }
  518. }
  519. if seasonExtra.XStartDate != "" {
  520. xStartDate = seasonExtra.XStartDate
  521. xEndDate = seasonExtra.XEndDate
  522. jumpYear = seasonExtra.JumpYear
  523. legends = seasonExtra.ChartLegend
  524. }
  525. length := len(result.List)
  526. if length == 0 {
  527. return
  528. }
  529. legendMap := make(map[string]string, 0)
  530. if len(legends) > 0 {
  531. for _, v := range legends {
  532. legendMap[v.Name] = v.Value
  533. }
  534. }
  535. latestDateYear := latestDate.Year()
  536. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  537. //根据数据确定最早的年份,和最近年份
  538. //根据横轴的日期,汇总所有的年份
  539. startYear := result.List[0].Year
  540. /*if jumpYear == 1 {
  541. if startYear != calendarPreYear {
  542. startYear = startYear - 1
  543. }
  544. }*/
  545. itemLength := len(result.List[length-1].Items)
  546. //获取数据的最新日期
  547. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  548. maxY := result.List[length-1].Year
  549. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  550. if tmpErr != nil {
  551. err = tmpErr
  552. return
  553. }
  554. endYear := lastDateT.Year()
  555. nowYear := time.Now().Year()
  556. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  557. quarterDataList := make([]*data_manage.QuarterData, 0)
  558. resultData := make([]*data_manage.QuarterData, 0)
  559. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  560. //整理出日期
  561. var startTmpT, endTmpT time.Time
  562. idx := 1
  563. chartLegendMap := make(map[string]int, 0)
  564. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  565. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  566. currentEndYear := currentStartYear
  567. if jumpYear == 1 {
  568. currentEndYear = currentStartYear + 1
  569. }
  570. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  571. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  572. startT, tEr := time.Parse(utils.FormatDate, startStr)
  573. if tEr != nil {
  574. err = tEr
  575. return
  576. }
  577. endT, tEr := time.Parse(utils.FormatDate, endStr)
  578. if tEr != nil {
  579. err = tEr
  580. return
  581. }
  582. if lastDateT.Before(startT) {
  583. //如果最新的日期在起始日之前,则跳出循环
  584. break
  585. }
  586. if endT.Year() > nowYear {
  587. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  588. nowYear = endT.Year()
  589. }
  590. item := data_manage.QuarterXDateItem{
  591. StartDate: startT,
  592. EndDate: endT,
  593. ShowName: showName,
  594. }
  595. dataMap[showName] = item
  596. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  597. startTmpT = startT
  598. endTmpT = endT
  599. chartLegendMap[showName] = idx
  600. idx++
  601. if lastDateT.Before(endT) {
  602. //如果最新的日期在起始日之前,则跳出循环
  603. break
  604. }
  605. }
  606. lenYear := len(dataMap)
  607. for k, v := range dataMap {
  608. if i, ok := chartLegendMap[k]; ok {
  609. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  610. }
  611. dataMap[k] = v
  612. }
  613. yearDataListMap := make(map[int]*data_manage.EdbDataItems, 0)
  614. for _, lv := range result.List {
  615. yearDataListMap[lv.Year] = lv
  616. }
  617. //判断哪些点应该落在同一条时间线上
  618. /*maxY := lastDateT.Year()
  619. changeFlag := false
  620. if lastDateT.Month() >= 11 {
  621. maxY = maxY + 1
  622. }
  623. if maxY < nowYear {
  624. changeFlag = true
  625. maxY = nowYear
  626. }*/
  627. /*endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
  628. endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
  629. minY := maxY
  630. if jumpYear == 1 {
  631. minY = maxY - 1
  632. }
  633. startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
  634. startTmpT, _ := time.Parse(utils.FormatDate, startTmp)*/
  635. fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
  636. fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
  637. fmt.Printf("maxY 为%d \n", maxY)
  638. for name, dateItem := range dataMap {
  639. tY := dateItem.EndDate.Year()
  640. if lastDateT.Month() >= 11 {
  641. if maxY > endTmpT.Year() {
  642. tY = tY + 1
  643. }
  644. }
  645. lv, ok1 := yearDataListMap[tY]
  646. fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
  647. if !ok1 {
  648. continue
  649. }
  650. for _, item := range lv.Items {
  651. tmpVal := data_manage.EdbDataList{
  652. EdbDataId: item.EdbDataId,
  653. EdbInfoId: item.EdbInfoId,
  654. DataTime: item.DataTime,
  655. DataTimestamp: item.DataTimestamp,
  656. Value: item.Value,
  657. }
  658. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  659. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  660. tmpV := &tmpVal
  661. if findVal, ok := quarterMap[name]; !ok {
  662. findVal = append(findVal, tmpV)
  663. quarterMap[name] = findVal
  664. } else {
  665. findVal = append(findVal, tmpV)
  666. quarterMap[name] = findVal
  667. }
  668. if lv.Year >= latestDateYear {
  669. // 切割的日期时间字符串
  670. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  671. if item.DataTime == cuttingDataTimeStr {
  672. dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
  673. dataMap[name] = dateItem
  674. }
  675. }
  676. }
  677. }
  678. }
  679. for k, v := range dataMap {
  680. itemList := quarterMap[k]
  681. quarterItem := new(data_manage.QuarterData)
  682. quarterItem.Years = v.ShowName
  683. quarterItem.ChartLegend = v.ChartLegend
  684. if le, ok := legendMap[v.ShowName]; ok {
  685. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  686. quarterItem.ChartLegend = le
  687. }
  688. }
  689. quarterItem.DataList = itemList
  690. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  691. //如果等于最后的实际日期,那么将切割时间戳记录
  692. if quarterItem.CuttingDataTimestamp == 0 {
  693. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  694. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  695. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  696. }
  697. }
  698. quarterDataList = append(quarterDataList, quarterItem)
  699. }
  700. if result.List[0].Year != calendarPreYear {
  701. itemList := make([]*data_manage.EdbDataList, 0)
  702. items := new(data_manage.QuarterData)
  703. //items.Year = calendarPreYear
  704. items.DataList = itemList
  705. newResult := make([]*data_manage.QuarterData, 0)
  706. newResult = append(newResult, items)
  707. newResult = append(newResult, quarterDataList...)
  708. resultData = newResult
  709. } else {
  710. resultData = quarterDataList
  711. }
  712. if len(quarterDataList) > 0 {
  713. quarterDataListSort = resultData
  714. sort.Sort(quarterDataListSort)
  715. }
  716. return
  717. }
  718. // BarChartData 柱方图的数据处理
  719. func BarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []data_manage.YData, err error) {
  720. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  721. edbDataMap := make(map[int]map[string]float64)
  722. for edbInfoId, edbDataList := range edbDataListMap {
  723. edbDateData := make(map[string]float64)
  724. for _, edbData := range edbDataList {
  725. edbDateData[edbData.DataTime] = edbData.Value
  726. }
  727. edbDataMap[edbInfoId] = edbDateData
  728. }
  729. // edbIdList 指标展示顺序;x轴的指标顺序
  730. edbIdList = make([]int, 0)
  731. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  732. dateData := make(map[int]float64)
  733. if barChartInfoSort.Sort == 0 {
  734. for _, v := range mappingList {
  735. edbIdList = append(edbIdList, v.EdbInfoId)
  736. }
  737. } else {
  738. lenBarChartInfoDateList := len(barChartInfoDateList)
  739. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  740. err = errors.New("排序日期异常")
  741. return
  742. }
  743. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  744. // 日期配置
  745. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  746. for edbInfoId, dataList := range edbDataListMap {
  747. if len(dataList) <= 0 {
  748. // 没有数据的指标id
  749. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  750. continue
  751. }
  752. findDate := barChartInfoDate.Date
  753. switch barChartInfoDate.Type {
  754. case 1: //最新值
  755. findDate = dataList[len(dataList)-1].DataTime
  756. case 2: //近期几天
  757. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  758. if tmpErr != nil {
  759. err = tmpErr
  760. return
  761. }
  762. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  763. lenData := len(dataList) - 1
  764. for i := lenData; i >= 0; i-- {
  765. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  766. if tmpErr != nil {
  767. err = tmpErr
  768. return
  769. }
  770. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  771. findDate = dataList[i].DataTime
  772. break
  773. }
  774. }
  775. case 3: // 固定日期
  776. //最早的日期
  777. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  778. if tmpErr != nil {
  779. err = tmpErr
  780. return
  781. }
  782. //寻找固定日期的数据
  783. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  784. if tmpErr != nil {
  785. err = tmpErr
  786. return
  787. }
  788. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  789. tmpDate := tmpDateTime.Format(utils.FormatDate)
  790. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  791. findDate = tmpDate
  792. break
  793. }
  794. }
  795. default:
  796. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  797. return
  798. }
  799. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  800. dateData[edbInfoId] = tmpValue
  801. } else {
  802. // 没有数据的指标id
  803. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  804. }
  805. }
  806. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  807. // 排序
  808. dateDataSort := utils.NewMapSorter(dateData)
  809. sort.Sort(dateDataSort)
  810. if barChartInfoSort.Sort == 1 {
  811. // 先将没有数据的指标id放在最前面
  812. if len(notDataEdbIdList) > 0 {
  813. edbIdList = append(edbIdList, notDataEdbIdList...)
  814. }
  815. for _, v := range dateDataSort {
  816. edbIdList = append(edbIdList, v.Key)
  817. }
  818. } else {
  819. for i := len(dateDataSort) - 1; i >= 0; i-- {
  820. edbIdList = append(edbIdList, dateDataSort[i].Key)
  821. }
  822. // 再将没有数据的指标id放在最后面
  823. if len(notDataEdbIdList) > 0 {
  824. edbIdList = append(edbIdList, notDataEdbIdList...)
  825. }
  826. }
  827. }
  828. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  829. for _, barChartInfoDate := range barChartInfoDateList {
  830. var maxDate time.Time
  831. findDataList := make([]float64, 0) // 当前日期的数据值
  832. for _, edbInfoId := range edbIdList {
  833. findDate := barChartInfoDate.Date //需要的日期值
  834. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  835. if len(dataList) <= 0 {
  836. // 没有数据的指标id
  837. findDataList = append(findDataList, 0)
  838. continue
  839. }
  840. switch barChartInfoDate.Type {
  841. case 1: //最新值
  842. dataList := edbDataListMap[edbInfoId]
  843. findDate = dataList[len(dataList)-1].DataTime
  844. case 2: //近期几天
  845. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  846. if tmpErr != nil {
  847. err = tmpErr
  848. return
  849. }
  850. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  851. lenData := len(dataList) - 1
  852. for i := lenData; i >= 0; i-- {
  853. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  854. if tmpErr != nil {
  855. err = tmpErr
  856. return
  857. }
  858. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  859. findDate = dataList[i].DataTime
  860. break
  861. }
  862. }
  863. case 3: // 固定日期
  864. //最早的日期
  865. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  866. if tmpErr != nil {
  867. err = tmpErr
  868. return
  869. }
  870. //寻找固定日期的数据
  871. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  872. if tmpErr != nil {
  873. err = tmpErr
  874. return
  875. }
  876. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  877. tmpDate := tmpDateTime.Format(utils.FormatDate)
  878. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  879. findDate = tmpDate
  880. break
  881. }
  882. }
  883. default:
  884. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  885. return
  886. }
  887. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  888. if maxDate.IsZero() {
  889. maxDate = findDateTime
  890. } else {
  891. if findDateTime.After(maxDate) {
  892. maxDate = findDateTime
  893. }
  894. }
  895. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  896. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  897. findDataList = append(findDataList, tmpValue)
  898. } else {
  899. findDataList = append(findDataList, 0)
  900. }
  901. }
  902. yDate := "0000-00-00"
  903. if !maxDate.IsZero() {
  904. yDate = maxDate.Format(utils.FormatDate)
  905. }
  906. yDataList = append(yDataList, data_manage.YData{
  907. Date: yDate,
  908. Value: findDataList,
  909. Color: barChartInfoDate.Color,
  910. Name: barChartInfoDate.Name,
  911. })
  912. }
  913. return
  914. }
  915. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  916. // 相关性图表不判断指标
  917. if source == utils.CHART_SOURCE_CORRELATION && chartNameEn != "" {
  918. return true
  919. }
  920. if chartNameEn == "" {
  921. return false
  922. }
  923. // 截面散点图的话,肯定是有英文配置的
  924. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  925. return true
  926. }
  927. for _, v := range edbList {
  928. if v.EdbNameEn == "" {
  929. return false
  930. }
  931. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  932. return false
  933. }
  934. }
  935. return true
  936. }
  937. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  938. if edbNameEn == "" {
  939. return false
  940. }
  941. if unit != "无" && unit != "" && unitEn == "" {
  942. return false
  943. }
  944. return true
  945. }
  946. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  947. // return int 0:启用,1:禁用
  948. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  949. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  950. if err != nil {
  951. return
  952. }
  953. //没有配置,立马返回
  954. if crmConfig.ConfigValue == `` {
  955. return
  956. }
  957. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  958. for _, edbInfoId := range edbInfoIdArr {
  959. for _, confEdbInfoId := range confEdbInfoIdList {
  960. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  961. isDisable = 1
  962. return
  963. }
  964. }
  965. }
  966. return
  967. }
  968. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  969. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  970. switch chartType {
  971. case 10: //截面散点
  972. var extraConfig data_manage.SectionScatterReq
  973. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  974. if err != nil {
  975. return
  976. }
  977. // 判断是否有配置日期序列
  978. if len(extraConfig.SeriesList) <= 0 {
  979. errMsg = `请配置序列`
  980. err = errors.New(errMsg)
  981. return
  982. }
  983. // 判断是否有填写指标
  984. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  985. errMsg = `请选择指标`
  986. err = errors.New(errMsg)
  987. return
  988. }
  989. // 遍历指标列表获取指标id
  990. edbIdMap := make(map[int]int)
  991. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  992. // X 轴的指标id
  993. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  994. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  995. edbIdList = append(edbIdList, v.XEdbInfoId)
  996. }
  997. // Y 轴的指标id
  998. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  999. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1000. edbIdList = append(edbIdList, v.YEdbInfoId)
  1001. }
  1002. }
  1003. }
  1004. return
  1005. }
  1006. // GetSectionScatterChartData 截面散点图的数据处理
  1007. func GetSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  1008. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1009. edbDataMap := make(map[int]map[string]float64)
  1010. for edbInfoId, edbDataList := range edbDataListMap {
  1011. edbDateData := make(map[string]float64)
  1012. for _, edbData := range edbDataList {
  1013. edbDateData[edbData.DataTime] = edbData.Value
  1014. }
  1015. edbDataMap[edbInfoId] = edbDateData
  1016. }
  1017. // edbIdList 指标展示顺序;x轴的指标顺序
  1018. edbIdList = make([]int, 0)
  1019. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  1020. for _, v := range mappingList {
  1021. edbIdList = append(edbIdList, v.EdbInfoId)
  1022. edbMappingMap[v.EdbInfoId] = v
  1023. }
  1024. //SectionScatterSeriesInfoResp
  1025. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1026. for _, seriesItem := range extraConfig.SeriesList {
  1027. var maxDate time.Time
  1028. // 系列中的指标数据
  1029. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1030. var minXVal, maxXVal, minYVal, maxYVal float64
  1031. for _, edbConf := range seriesItem.EdbInfoList {
  1032. tmpItem := data_manage.SectionScatterEdbItemResp{
  1033. IsShow: edbConf.IsShow,
  1034. Name: edbConf.Name,
  1035. NameEn: edbConf.NameEn,
  1036. } //单个坐标点的数据
  1037. //X轴的数据
  1038. {
  1039. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1040. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1041. if !ok {
  1042. continue
  1043. }
  1044. findDate := edbConf.XDate //需要的日期值
  1045. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1046. if len(dataList) <= 0 {
  1047. // 没有数据的指标id
  1048. //findDataList = append(findDataList, 0)
  1049. continue
  1050. }
  1051. tmpItem.XEdbInfoId = edbInfoId
  1052. tmpItem.XName = edbMappingInfo.EdbName
  1053. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1054. switch edbConf.XDateType {
  1055. case 1: //最新值
  1056. findDate = dataList[len(dataList)-1].DataTime
  1057. case 2: //近期几天
  1058. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1059. if tmpErr != nil {
  1060. err = tmpErr
  1061. return
  1062. }
  1063. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1064. lenData := len(dataList) - 1
  1065. for i := lenData; i >= 0; i-- {
  1066. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1067. if tmpErr != nil {
  1068. err = tmpErr
  1069. return
  1070. }
  1071. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1072. findDate = dataList[i].DataTime
  1073. break
  1074. }
  1075. }
  1076. case 3: // 固定日期
  1077. //最早的日期
  1078. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1079. if tmpErr != nil {
  1080. err = tmpErr
  1081. return
  1082. }
  1083. //寻找固定日期的数据
  1084. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1085. if tmpErr != nil {
  1086. err = tmpErr
  1087. return
  1088. }
  1089. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1090. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1091. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1092. findDate = tmpDate
  1093. break
  1094. }
  1095. }
  1096. default:
  1097. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1098. return
  1099. }
  1100. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1101. if maxDate.IsZero() {
  1102. maxDate = findDateTime
  1103. } else {
  1104. if findDateTime.After(maxDate) {
  1105. maxDate = findDateTime
  1106. }
  1107. }
  1108. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1109. tmpItem.XDate = findDate
  1110. tmpItem.XValue = tmpValue
  1111. } else {
  1112. continue
  1113. }
  1114. }
  1115. //Y轴的数据
  1116. {
  1117. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1118. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1119. if !ok {
  1120. continue
  1121. }
  1122. findDate := edbConf.YDate //需要的日期值
  1123. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1124. if len(dataList) <= 0 {
  1125. // 没有数据的指标id
  1126. //findDataList = append(findDataList, 0)
  1127. continue
  1128. }
  1129. tmpItem.YEdbInfoId = edbInfoId
  1130. tmpItem.YName = edbMappingInfo.EdbName
  1131. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1132. switch edbConf.YDateType {
  1133. case 1: //最新值
  1134. findDate = dataList[len(dataList)-1].DataTime
  1135. case 2: //近期几天
  1136. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1137. if tmpErr != nil {
  1138. err = tmpErr
  1139. return
  1140. }
  1141. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1142. lenData := len(dataList) - 1
  1143. for i := lenData; i >= 0; i-- {
  1144. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1145. if tmpErr != nil {
  1146. err = tmpErr
  1147. return
  1148. }
  1149. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1150. findDate = dataList[i].DataTime
  1151. break
  1152. }
  1153. }
  1154. case 3: // 固定日期
  1155. //最早的日期
  1156. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1157. if tmpErr != nil {
  1158. err = tmpErr
  1159. return
  1160. }
  1161. //寻找固定日期的数据
  1162. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1163. if tmpErr != nil {
  1164. err = tmpErr
  1165. return
  1166. }
  1167. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1168. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1169. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1170. findDate = tmpDate
  1171. break
  1172. }
  1173. }
  1174. default:
  1175. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1176. return
  1177. }
  1178. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1179. if maxDate.IsZero() {
  1180. maxDate = findDateTime
  1181. } else {
  1182. if findDateTime.After(maxDate) {
  1183. maxDate = findDateTime
  1184. }
  1185. }
  1186. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1187. tmpItem.YDate = findDate
  1188. tmpItem.YValue = tmpValue
  1189. } else {
  1190. continue
  1191. }
  1192. }
  1193. // 获取当前系列的X轴的最大最小值
  1194. {
  1195. if tmpItem.XValue < minXVal {
  1196. minXVal = tmpItem.XValue
  1197. }
  1198. if tmpItem.XValue > maxXVal {
  1199. maxXVal = tmpItem.XValue
  1200. }
  1201. if tmpItem.YValue < minYVal {
  1202. minYVal = tmpItem.YValue
  1203. }
  1204. if tmpItem.YValue > maxYVal {
  1205. maxYVal = tmpItem.YValue
  1206. }
  1207. }
  1208. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1209. }
  1210. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1211. var trendLine, rSquare string
  1212. // 生成线性方程式
  1213. var a, b float64
  1214. {
  1215. coordinateData := make([]utils.Coordinate, 0)
  1216. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1217. tmpCoordinate1 := utils.Coordinate{
  1218. X: tmpSeriesEdbInfo.XValue,
  1219. Y: tmpSeriesEdbInfo.YValue,
  1220. }
  1221. coordinateData = append(coordinateData, tmpCoordinate1)
  1222. }
  1223. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1224. if len(coordinateData) >= 2 {
  1225. a, b = utils.GetLinearResult(coordinateData)
  1226. if !math.IsNaN(a) && !math.IsNaN(b) {
  1227. if b > 0 {
  1228. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1229. } else {
  1230. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1231. }
  1232. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1233. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1234. }
  1235. // 计算R平方
  1236. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1237. }
  1238. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1239. X: minXVal,
  1240. Y: minYVal,
  1241. }, data_manage.CoordinatePoint{
  1242. X: maxXVal,
  1243. Y: maxYVal,
  1244. })
  1245. }
  1246. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1247. Name: seriesItem.Name,
  1248. NameEn: seriesItem.NameEn,
  1249. Color: seriesItem.Color,
  1250. EdbInfoList: tmpSeriesEdbInfoList,
  1251. ShowTrendLine: seriesItem.ShowTrendLine,
  1252. ShowFitEquation: seriesItem.ShowFitEquation,
  1253. ShowRSquare: seriesItem.ShowRSquare,
  1254. TrendLine: trendLine,
  1255. RSquare: rSquare,
  1256. TrendLimitData: trendLimitData,
  1257. })
  1258. }
  1259. chartDataResp = data_manage.SectionScatterInfoResp{
  1260. XName: extraConfig.XName,
  1261. XNameEn: extraConfig.XNameEn,
  1262. XUnitName: extraConfig.XUnitName,
  1263. XUnitNameEn: extraConfig.XUnitNameEn,
  1264. YName: extraConfig.YName,
  1265. YNameEn: extraConfig.YNameEn,
  1266. YUnitName: extraConfig.YUnitName,
  1267. YUnitNameEn: extraConfig.YUnitNameEn,
  1268. XMinValue: extraConfig.XMinValue,
  1269. XMaxValue: extraConfig.XMaxValue,
  1270. YMinValue: extraConfig.YMinValue,
  1271. YMaxValue: extraConfig.YMaxValue,
  1272. DataList: dataListResp,
  1273. }
  1274. return
  1275. }
  1276. // ChartInfoRefreshV2 图表刷新
  1277. // @author Roc
  1278. // @datetime 2022-09-16 10:15:38
  1279. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1280. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  1281. var errmsg string
  1282. defer func() {
  1283. if err != nil {
  1284. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1285. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1286. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1287. }
  1288. }()
  1289. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1290. if err != nil {
  1291. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1292. return
  1293. }
  1294. edbIdList := make([]int, 0)
  1295. for _, v := range edbMappingList {
  1296. edbIdList = append(edbIdList, v.EdbInfoId)
  1297. }
  1298. // 批量刷新
  1299. err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true)
  1300. if err != nil {
  1301. return
  1302. }
  1303. return
  1304. }
  1305. // BatchChartInfoRefreshV2 图表批量刷新
  1306. // @author Roc
  1307. // @datetime 2022-09-16 10:15:38
  1308. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1309. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo, redisKey string) (err error, isAsync bool) {
  1310. if len(chartInfoList) <= 0 {
  1311. return
  1312. }
  1313. chartInfoIdSlice := make([]int, 0)
  1314. for _, chartInfo := range chartInfoList {
  1315. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  1316. }
  1317. var errMsg string
  1318. defer func() {
  1319. if err != nil {
  1320. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errMsg, 3)
  1321. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1322. fmt.Println("ChartInfoRefresh Err:" + errMsg)
  1323. }
  1324. }()
  1325. // 是否删除缓存
  1326. isDeleteCache := true
  1327. if redisKey != `` {
  1328. //设置最多10分钟缓存
  1329. utils.Rc.SetNX(redisKey, 1, time.Minute*10)
  1330. defer func() {
  1331. if isDeleteCache {
  1332. utils.Rc.Delete(redisKey)
  1333. }
  1334. }()
  1335. }
  1336. // 获取需要刷新的指标信息
  1337. edbIdList := make([]int, 0)
  1338. edbInfoIdMap := make(map[int]int)
  1339. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  1340. for _, v := range edbMappingList {
  1341. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  1342. edbIdList = append(edbIdList, v.EdbInfoId)
  1343. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  1344. }
  1345. }
  1346. // 获取需要刷新的指标列表
  1347. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
  1348. // 需要刷新的指标数量
  1349. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  1350. if totalEdbInfo > 20 { // 关联指标过多的时候,异步刷新
  1351. isAsync = true
  1352. isDeleteCache = false // 不删除缓存
  1353. go refreshChartEdbInfo(chartInfoList, redisKey, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1354. } else {
  1355. err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1356. //清除图表缓存
  1357. for _, v := range chartInfoList {
  1358. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1359. _ = utils.Rc.Delete(key)
  1360. }
  1361. }
  1362. return
  1363. }
  1364. // refreshChartEdbInfo
  1365. // @Description: 刷新图表中的质保列表
  1366. // @author: Roc
  1367. // @datetime 2023-12-01 16:05:39
  1368. // @param chartInfoList []*data_manage.ChartInfo
  1369. // @param redisKey string
  1370. // @param newBaseEdbInfoArr []*data_manage.EdbInfo
  1371. // @param newBasePredictEdbInfoArr []*data_manage.EdbInfo
  1372. // @param newCalculateMap map[int]*data_manage.EdbInfo
  1373. // @param newPredictCalculateMap map[int]*data_manage.EdbInfo
  1374. // @param calculateArr []int
  1375. // @param predictCalculateArr []int
  1376. func refreshChartEdbInfo(chartInfoList []*data_manage.ChartInfo, redisKey string, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  1377. edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1378. //清除图表缓存
  1379. for _, v := range chartInfoList {
  1380. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1381. _ = utils.Rc.Delete(key)
  1382. }
  1383. // 如果是有插入redisKey,那么刷新结束后就清除缓存
  1384. if redisKey != `` {
  1385. utils.Rc.Delete(redisKey)
  1386. }
  1387. }
  1388. // GetBatchChartRefreshKey
  1389. // @Description: 获取批量刷新key
  1390. // @author: Roc
  1391. // @datetime 2023-11-30 13:30:26
  1392. // @param source string
  1393. // @param reportId int
  1394. // @param reportChapterId int
  1395. // @return string
  1396. func GetBatchChartRefreshKey(source string, reportId, reportChapterId int) string {
  1397. if source == `` {
  1398. return ``
  1399. }
  1400. return fmt.Sprint("batch_chart_refresh:", source, ":", reportId, ":", reportChapterId)
  1401. }
  1402. // CheckBatchChartRefreshResult
  1403. // @Description: 判断报告中的图表是否刷新完成
  1404. // @author: Roc
  1405. // @datetime 2023-11-30 13:30:26
  1406. // @param source string
  1407. // @param reportId int
  1408. // @param reportChapterId int
  1409. // @return string
  1410. func CheckBatchChartRefreshResult(source string, reportId, reportChapterId int) (refreshResult bool) {
  1411. refreshResult = true
  1412. redisKey := GetBatchChartRefreshKey(source, reportId, reportChapterId)
  1413. if redisKey != `` {
  1414. // 如果找到了key,那么就是还在更新中
  1415. isOk := utils.Rc.IsExist(redisKey)
  1416. if isOk {
  1417. refreshResult = false
  1418. }
  1419. }
  1420. return
  1421. }
  1422. // RadarChartData 雷达图的数据处理
  1423. func RadarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.RadarChartInfoReq) (edbIdList []int, chartDataResp data_manage.RadarChartInfoResp, err error) {
  1424. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1425. edbDataMap := make(map[int]map[string]float64)
  1426. for edbInfoId, edbDataList := range edbDataListMap {
  1427. edbDateData := make(map[string]float64)
  1428. for _, edbData := range edbDataList {
  1429. edbDateData[edbData.DataTime] = edbData.Value
  1430. }
  1431. edbDataMap[edbInfoId] = edbDateData
  1432. }
  1433. // edbIdList 指标展示顺序;x轴的指标顺序
  1434. edbIdList = make([]int, 0)
  1435. for _, v := range mappingList {
  1436. edbIdList = append(edbIdList, v.EdbInfoId)
  1437. }
  1438. chartDateList := extraConfig.DateList
  1439. yDataList := make([]data_manage.RadarYData, 0) //y轴的数据列表
  1440. for _, chartDate := range chartDateList {
  1441. var maxDate time.Time
  1442. findDataList := make([]float64, 0) // 当前日期的数据值
  1443. for _, edbInfoId := range edbIdList {
  1444. findDate := chartDate.Date //需要的日期值
  1445. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1446. if len(dataList) <= 0 {
  1447. // 没有数据的指标id
  1448. findDataList = append(findDataList, 0)
  1449. continue
  1450. }
  1451. switch chartDate.Type {
  1452. case 1: //最新值
  1453. findDate = dataList[len(dataList)-1].DataTime
  1454. case 2: //近期几天
  1455. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1456. if tmpErr != nil {
  1457. err = tmpErr
  1458. return
  1459. }
  1460. findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
  1461. lenData := len(dataList) - 1
  1462. for i := lenData; i >= 0; i-- {
  1463. currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1464. if e != nil {
  1465. err = e
  1466. return
  1467. }
  1468. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1469. findDate = dataList[i].DataTime
  1470. break
  1471. }
  1472. }
  1473. case 3: // 固定日期
  1474. //最早的日期
  1475. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1476. if tmpErr != nil {
  1477. err = tmpErr
  1478. return
  1479. }
  1480. //寻找固定日期的数据
  1481. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
  1482. if tmpErr != nil {
  1483. err = tmpErr
  1484. return
  1485. }
  1486. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1487. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1488. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1489. findDate = tmpDate
  1490. break
  1491. }
  1492. }
  1493. default:
  1494. err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
  1495. return
  1496. }
  1497. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1498. if maxDate.IsZero() {
  1499. maxDate = findDateTime
  1500. } else {
  1501. if findDateTime.After(maxDate) {
  1502. maxDate = findDateTime
  1503. }
  1504. }
  1505. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1506. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  1507. findDataList = append(findDataList, tmpValue)
  1508. } else {
  1509. findDataList = append(findDataList, 0)
  1510. }
  1511. }
  1512. yDate := "0000-00-00"
  1513. if !maxDate.IsZero() {
  1514. yDate = maxDate.Format(utils.FormatDate)
  1515. }
  1516. yDataList = append(yDataList, data_manage.RadarYData{
  1517. Date: yDate,
  1518. Value: findDataList,
  1519. Color: chartDate.Color,
  1520. Name: chartDate.Name,
  1521. })
  1522. }
  1523. chartDataResp = data_manage.RadarChartInfoResp{
  1524. YDataList: yDataList,
  1525. XEdbIdValue: edbIdList,
  1526. }
  1527. return
  1528. }