chart_info.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. package chart
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. "hongze/hongze_yb/global"
  8. "hongze/hongze_yb/models/request"
  9. "hongze/hongze_yb/models/response/chart_info"
  10. chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
  11. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  12. "hongze/hongze_yb/models/tables/chart_series"
  13. "hongze/hongze_yb/models/tables/chart_series_edb_mapping"
  14. company2 "hongze/hongze_yb/models/tables/company"
  15. edbDataModel "hongze/hongze_yb/models/tables/edb_data"
  16. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  17. "hongze/hongze_yb/models/tables/my_chart_classify"
  18. "hongze/hongze_yb/models/tables/yb_chart_daily_visit_log"
  19. "hongze/hongze_yb/models/tables/yb_chart_visit_log"
  20. "hongze/hongze_yb/services/alarm_msg"
  21. edbDataService "hongze/hongze_yb/services/edb_data"
  22. "hongze/hongze_yb/services/user"
  23. "hongze/hongze_yb/utils"
  24. "math"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "time"
  29. )
  30. // ModifyChartInfoAndMapping 修改图表信息
  31. func ModifyChartInfoAndMapping(edbInfoIdStr string, req *chartInfoModel.SaveChartInfoReq, chartType int, chartEdbInfoList []*chartInfoModel.ChartSaveItem) (err error) {
  32. // 开启事务
  33. tx := global.MYSQL["data"].Begin()
  34. defer func() {
  35. if err != nil {
  36. tx.Rollback()
  37. } else {
  38. tx.Commit()
  39. }
  40. }()
  41. // 更新图表信息
  42. if chartType != 2 {
  43. updateStr := `
  44. edb_info_ids = ?,
  45. modify_time = NOW(),
  46. date_type = ?,
  47. start_date = ?,
  48. end_date = ?,
  49. left_min = ?,
  50. left_max = ?,
  51. right_min = ?,
  52. right_max = ?`
  53. pars := []interface{}{edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax}
  54. if req.ExtraConfig != `` {
  55. updateStr += `,extra_config=? `
  56. pars = append(pars, req.ExtraConfig)
  57. }
  58. pars = append(pars, req.ChartInfoId)
  59. sql := ` UPDATE chart_info SET ` + updateStr + ` WHERE chart_info_id = ?`
  60. err = tx.Exec(sql, pars...).Error
  61. } else {
  62. updateStr := `edb_info_ids = ?,
  63. modify_time = NOW(),
  64. date_type = ?,
  65. calendar = ?,
  66. start_date = ?,
  67. end_date = ?,
  68. season_start_date = ?,
  69. season_end_date = ?,
  70. left_min = ?,
  71. left_max = ?,
  72. right_min = ?,
  73. right_max = ?`
  74. pars := []interface{}{edbInfoIdStr, req.DateType, req.Calendar, req.StartDate, req.EndDate, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax}
  75. if req.ExtraConfig != `` {
  76. updateStr += `,extra_config=? `
  77. pars = append(pars, req.ExtraConfig)
  78. }
  79. pars = append(pars, req.ChartInfoId)
  80. sql := ` UPDATE chart_info SET ` + updateStr + ` WHERE chart_info_id = ?`
  81. err = tx.Exec(sql, pars...).Error
  82. }
  83. if err != nil {
  84. fmt.Println("UPDATE chart_info Err:", err.Error())
  85. return
  86. }
  87. // 修改图表关联指标
  88. chartEdbMappingIdList := make([]string, 0)
  89. for _, v := range chartEdbInfoList {
  90. var tmpChartEdbMapping *chartEdbMappingModel.ChartEdbMapping
  91. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  92. err = tx.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_DEFAULT).First(&tmpChartEdbMapping).Error
  93. if err != nil && err != utils.ErrNoRow {
  94. fmt.Println("QueryRow Err:", err.Error())
  95. return err
  96. }
  97. if tmpChartEdbMapping == nil || tmpChartEdbMapping.ChartEdbMappingId == 0 {
  98. mapItem := new(chartEdbMappingModel.ChartEdbMapping)
  99. mapItem.ChartInfoId = req.ChartInfoId
  100. mapItem.EdbInfoId = v.EdbInfoId
  101. mapItem.CreateTime = time.Now()
  102. mapItem.ModifyTime = time.Now()
  103. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  104. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  105. mapItem.MaxData = v.MaxData
  106. mapItem.MinData = v.MinData
  107. mapItem.IsOrder = 0
  108. if v.IsOrder {
  109. mapItem.IsOrder = 1
  110. }
  111. mapItem.IsAxis = v.IsAxis
  112. mapItem.EdbInfoType = v.EdbInfoType
  113. mapItem.LeadValue = v.LeadValue
  114. mapItem.LeadUnit = v.LeadUnit
  115. mapItem.ChartStyle = v.ChartStyle
  116. mapItem.ChartColor = v.ChartColor
  117. mapItem.ChartWidth = v.ChartWidth
  118. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  119. err = tx.Create(mapItem).Error
  120. if err != nil {
  121. return
  122. }
  123. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  124. } else {
  125. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  126. tmpChartEdbMapping.ModifyTime = time.Now()
  127. tmpChartEdbMapping.MaxData = v.MaxData
  128. tmpChartEdbMapping.MinData = v.MinData
  129. tmpChartEdbMapping.IsOrder = 0
  130. if v.IsOrder {
  131. tmpChartEdbMapping.IsOrder = 1
  132. }
  133. tmpChartEdbMapping.IsAxis = v.IsAxis
  134. tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  135. tmpChartEdbMapping.LeadValue = v.LeadValue
  136. tmpChartEdbMapping.LeadUnit = v.LeadUnit
  137. tmpChartEdbMapping.ChartStyle = v.ChartStyle
  138. tmpChartEdbMapping.ChartColor = v.ChartColor
  139. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  140. tmpChartEdbMapping.ChartWidth = v.ChartWidth
  141. err = tx.Model(tmpChartEdbMapping).Select([]string{"ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth"}).Updates(*tmpChartEdbMapping).Error
  142. if err != nil {
  143. fmt.Println("chart_edb_mapping Err:" + err.Error())
  144. return err
  145. }
  146. }
  147. }
  148. // 清除原图表指标
  149. if len(chartEdbMappingIdList) > 0 {
  150. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  151. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  152. err = tx.Exec(dsql, req.ChartInfoId).Error
  153. if err != nil {
  154. fmt.Println("delete err:" + err.Error())
  155. return err
  156. }
  157. }
  158. return
  159. }
  160. // GetChartInfoRefreshData 获取需要刷新的指标
  161. func GetChartInfoRefreshData(chartInfoId int) (baseEdbInfoArr, calculateInfoArr []*edbInfoModel.EdbInfo, err error) {
  162. sql := ` SELECT b.* FROM chart_edb_mapping AS a
  163. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  164. WHERE a.chart_info_id=? `
  165. edbInfoList := make([]*edbInfoModel.EdbInfo, 0)
  166. err = global.MYSQL["data"].Raw(sql, chartInfoId).Scan(&edbInfoList).Error
  167. if err != nil {
  168. return
  169. }
  170. for _, v := range edbInfoList {
  171. fmt.Println(v.EdbInfoId, v.EdbType)
  172. if v.EdbType == 1 {
  173. baseEdbInfoArr = append(baseEdbInfoArr, v)
  174. } else {
  175. calculateInfoArr = append(calculateInfoArr, v)
  176. newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
  177. if err != nil {
  178. return baseEdbInfoArr, calculateInfoArr, err
  179. }
  180. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  181. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  182. }
  183. }
  184. return
  185. }
  186. // GetChartRefreshEdbInfo
  187. func GetChartRefreshEdbInfo(edbInfoId, source, n int) (baseEdbInfoArr, calculateInfoArr []*edbInfoModel.EdbInfo, err error) {
  188. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  189. if err != nil && err != utils.ErrNoRow {
  190. return
  191. }
  192. n++
  193. for _, item := range calculateList {
  194. fmt.Println(item.EdbInfoId)
  195. if item.EdbType == 1 {
  196. baseEdbInfoArr = append(baseEdbInfoArr, item)
  197. } else {
  198. calculateInfoArr = append(calculateInfoArr, item)
  199. if n > 10 {
  200. return
  201. }
  202. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetChartRefreshEdbInfo(item.EdbInfoId, item.Source, n)
  203. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  204. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  205. }
  206. }
  207. return
  208. }
  209. func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*edbInfoModel.EdbInfo, err error) {
  210. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  211. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  212. WHERE a.edb_info_id=? ORDER BY sort ASC `
  213. err = global.MYSQL["data"].Exec(sql, edbInfoId).Error
  214. return
  215. }
  216. // SaveChartVisitLog 保存图表访问记录
  217. func SaveChartVisitLog(userInfo user.UserInfo, chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int) (err error) {
  218. userId := int(userInfo.UserID)
  219. companyId := int(userInfo.CompanyID)
  220. chartInfoId := chartInfo.ChartInfoId
  221. // 访问记录
  222. visitLog := new(yb_chart_visit_log.YbChartVisitLog)
  223. visitLog.CompanyId = companyId
  224. companyInfo, err := company2.GetByCompanyId(userInfo.CompanyID)
  225. if err == nil {
  226. visitLog.CompanyName = companyInfo.CompanyName
  227. }
  228. visitLog.UserId = userId
  229. visitLog.RealName = userInfo.RealName
  230. visitLog.Mobile = userInfo.Mobile
  231. visitLog.Email = userInfo.Email
  232. visitLog.ChartInfoId = chartInfoId
  233. visitLog.ChartName = chartInfo.ChartName
  234. visitLog.CreateTime = time.Now().Local()
  235. visitLog.Create()
  236. // 每日访问记录,用户/天/图仅记录一次
  237. if myChartClassifyId > 0 {
  238. // 今日是否已访问
  239. nowTime := time.Now().Local()
  240. startTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  241. endTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 23, 59, 59, 0, time.Local)
  242. _, tmpErr := yb_chart_daily_visit_log.GetTodayVisitLog(userId, chartInfoId, myChartClassifyId, startTime, endTime)
  243. if tmpErr == nil {
  244. return
  245. }
  246. myChartClassify, tmpErr := my_chart_classify.GetClassifyById(myChartClassifyId)
  247. if tmpErr != nil {
  248. return
  249. }
  250. dailyLog := new(yb_chart_daily_visit_log.YbChartDailyVisitLog)
  251. dailyLog.CompanyId = companyId
  252. if companyInfo != nil {
  253. dailyLog.CompanyName = companyInfo.CompanyName
  254. }
  255. dailyLog.UserId = userId
  256. dailyLog.RealName = userInfo.RealName
  257. dailyLog.Mobile = userInfo.Mobile
  258. dailyLog.Email = userInfo.Email
  259. dailyLog.ChartInfoId = chartInfoId
  260. dailyLog.ChartName = chartInfo.ChartName
  261. dailyLog.MyChartClassifyId = myChartClassifyId
  262. dailyLog.MyChartClassifyName = myChartClassify.MyChartClassifyName
  263. dailyLog.CreateTime = time.Now().Local()
  264. dailyLog.Create()
  265. }
  266. return
  267. }
  268. // RefreshChart 图表刷新
  269. func RefreshChart(chartInfoId int) (err error) {
  270. var errMsg string
  271. defer func() {
  272. if err != nil {
  273. if global.CONFIG.Serve.RunMode == "release" {
  274. //go services.SendEmail(utils.APPNAME+"【"+global.CONFIG.Serve.RunMode+"】"+"失败提醒", "RefreshChart:"+errMsg, utils.EmailSendToUsers)
  275. go alarm_msg.SendAlarmMsg("RefreshChart Err:"+errMsg, 3)
  276. }
  277. fmt.Println("RefreshChart Err:" + errMsg)
  278. }
  279. }()
  280. // 获取需要刷新的指标
  281. baseEdbInfoArr, calculateInfoArr, err := GetChartInfoRefreshData(chartInfoId)
  282. if err != nil {
  283. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  284. return
  285. }
  286. newBaseEdbInfoArr := make([]*edbInfoModel.EdbInfo, 0)
  287. baseMap := make(map[int]*edbInfoModel.EdbInfo)
  288. for _, bv := range baseEdbInfoArr {
  289. // 如果不是普通指标,那么过滤
  290. if bv.EdbInfoType != 0 {
  291. continue
  292. }
  293. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  294. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  295. }
  296. baseMap[bv.EdbInfoId] = bv
  297. }
  298. newCalculateInfoArr := make([]*edbInfoModel.EdbInfo, 0)
  299. calculateMap := make(map[int]*edbInfoModel.EdbInfo)
  300. var calculateArr []int
  301. for _, bv := range calculateInfoArr {
  302. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  303. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  304. calculateArr = append(calculateArr, bv.EdbInfoId)
  305. }
  306. calculateMap[bv.EdbInfoId] = bv
  307. }
  308. sort.Ints(calculateArr)
  309. // 刷新指标
  310. var startDate string
  311. for _, bv := range newBaseEdbInfoArr {
  312. source := bv.Source
  313. subSource := bv.SubSource
  314. edbInfoId := bv.EdbInfoId
  315. edbCode := bv.EdbCode
  316. startDate = bv.StartDate.Format(utils.FormatDate)
  317. frequency := bv.Frequency
  318. if startDate == "0000-00-00" {
  319. continue
  320. }
  321. sTime := bv.EndDate
  322. var limitDay int
  323. startDate := ""
  324. switch frequency {
  325. case "日度":
  326. limitDay = utils.DATA_START_REFRESH_LIMIT
  327. case "周度":
  328. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  329. case "月度":
  330. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  331. case "季度":
  332. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  333. case "年度":
  334. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  335. default:
  336. limitDay = utils.DATA_START_REFRESH_LIMIT
  337. }
  338. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  339. fmt.Println("source:", source)
  340. respItem, err := RefreshEdbData(edbInfoId, source, subSource, edbCode, startDate)
  341. if err != nil {
  342. errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
  343. return err
  344. }
  345. if respItem.Ret != 200 {
  346. errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  347. return err
  348. }
  349. //maxAndMinItem, err := edbDataModel.GetEdbInfoMaxAndMinInfo(source, edbCode)
  350. //if err != nil {
  351. // return err
  352. //}
  353. //if maxAndMinItem != nil {
  354. // err = edbDataModel.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  355. // if err != nil {
  356. // return err
  357. // }
  358. //}
  359. }
  360. // 刷新计算指标
  361. for _, v := range calculateArr {
  362. edbInfo := calculateMap[v]
  363. if edbInfo == nil {
  364. return err
  365. }
  366. startDate = edbInfo.StartDate.Format(utils.FormatDate)
  367. source := edbInfo.Source
  368. if source == utils.DATA_SOURCE_CALCULATE {
  369. sTime := edbInfo.EndDate
  370. startDate = sTime.Format(utils.FormatDate)
  371. }
  372. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  373. if err != nil {
  374. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  375. errMsg = "RefreshEdbCalculateData Err:" + err.Error()
  376. return err
  377. }
  378. if result.Ret != 200 {
  379. fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  380. errMsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  381. return fmt.Errorf("刷新失败")
  382. }
  383. }
  384. return
  385. }
  386. // GetChartEdbData 获取图表的指标数据
  387. func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, xEdbIdValue []int, yDataList []chart_info.YData, sourceArr []string, dataResp interface{}, err error, errMsg string) {
  388. edbList = make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
  389. xEdbIdValue = make([]int, 0)
  390. yDataList = make([]chart_info.YData, 0)
  391. var extraConfig interface{}
  392. switch chartType {
  393. case 6: //时序组合图
  394. var tmpConfig chart_info.ChartTimeCombineExtraConf
  395. if extraConfigStr != `` {
  396. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  397. if err != nil {
  398. errMsg = "雷达图配置异常"
  399. err = errors.New(errMsg)
  400. return
  401. }
  402. }
  403. extraConfig = tmpConfig
  404. case 7: // 柱形图
  405. var barConfig request.BarChartInfoReq
  406. if extraConfigStr == `` {
  407. errMsg = "柱方图未配置"
  408. err = errors.New(errMsg)
  409. return
  410. }
  411. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  412. if err != nil {
  413. errMsg = "柱方图配置异常"
  414. err = errors.New(errMsg)
  415. return
  416. }
  417. extraConfig = barConfig
  418. case 10: // 截面散点图
  419. var tmpExtraConfig request.SectionScatterReq
  420. if extraConfigStr == `` {
  421. errMsg = "截面散点图未配置"
  422. err = errors.New(errMsg)
  423. return
  424. }
  425. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  426. if err != nil {
  427. errMsg = "截面散点配置异常"
  428. err = errors.New(errMsg)
  429. return
  430. }
  431. extraConfig = tmpExtraConfig
  432. case utils.CHART_TYPE_RADAR:
  433. var barConfig request.RadarChartInfoReq
  434. if extraConfigStr == `` {
  435. errMsg = "雷达图未配置"
  436. err = errors.New(errMsg)
  437. return
  438. }
  439. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  440. if err != nil {
  441. errMsg = "雷达图配置异常"
  442. err = errors.New(errMsg)
  443. return
  444. }
  445. extraConfig = barConfig
  446. case utils.CHART_TYPE_SECTION_COMBINE:
  447. // 预览和详情都走这个接口
  448. var sectionExtraConfig chart_info.ChartSectionAllExtraConf
  449. if extraConfigStr == `` {
  450. errMsg = "截面组合图未配置"
  451. err = errors.New(errMsg)
  452. return
  453. }
  454. if chartInfoId > 0 {
  455. var sectionExtraConfigTmp chart_info.ChartSectionExtraConf
  456. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfigTmp)
  457. if err != nil {
  458. errMsg = "截面组合图配置异常"
  459. err = errors.New(errMsg)
  460. return
  461. }
  462. sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
  463. sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
  464. sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
  465. sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
  466. sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
  467. sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
  468. // 查询所有的seriesEdb
  469. seriesEdbList, e := chart_series_edb_mapping.GetChartSeriesEdbByChartInfoId(chartInfoId)
  470. if e != nil {
  471. errMsg = "查询seriesEdb失败"
  472. err = errors.New(errMsg + e.Error())
  473. return
  474. }
  475. // todo 是否有必要返回单位信息
  476. // 组装成map
  477. seriesEdbMap := make(map[int][]*chart_info.ChartSectionSeriesEdbConf)
  478. for _, v := range seriesEdbList {
  479. var dateConf *chart_info.ChartSectionSeriesDateConfItem
  480. if v.DateConf != "" {
  481. err = json.Unmarshal([]byte(v.DateConf), &dateConf)
  482. if err != nil {
  483. errMsg = "截面组合图配置异常"
  484. err = errors.New(errMsg + err.Error())
  485. return
  486. }
  487. }
  488. tmp := &chart_info.ChartSectionSeriesEdbConf{
  489. ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
  490. ChartSeriesId: v.ChartSeriesId,
  491. //ChartInfoId: v.ChartInfoId,
  492. EdbInfoId: v.EdbInfoId,
  493. DateConf: dateConf,
  494. EdbName: "",
  495. EdbNameEn: "",
  496. Unit: "",
  497. UnitEn: "",
  498. DateConfName: v.DateConfName,
  499. DateConfType: v.DateConfType,
  500. }
  501. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
  502. }
  503. //查询series
  504. seriesListTmp, e := chart_series.GetChartSeriesByChartInfoId(chartInfoId)
  505. if e != nil {
  506. errMsg = "查询series失败"
  507. err = errors.New(errMsg + e.Error())
  508. return
  509. }
  510. seriesList := make([]*chart_info.ChartSectionSeriesItem, 0)
  511. for _, v := range seriesListTmp {
  512. tmpSeries := &chart_info.ChartSectionSeriesItem{
  513. ChartSeriesId: v.ChartSeriesId,
  514. SeriesName: v.SeriesName,
  515. SeriesNameEn: v.SeriesNameEn,
  516. ChartStyle: v.ChartStyle,
  517. ChartColor: v.ChartColor,
  518. ChartWidth: v.ChartWidth,
  519. IsPoint: v.IsPoint,
  520. IsNumber: v.IsNumber,
  521. IsAxis: v.IsAxis,
  522. MaxData: v.MaxData,
  523. MinData: v.MinData,
  524. //IsOrder: false,
  525. EdbInfoList: nil,
  526. DataList: nil,
  527. }
  528. edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
  529. if ok {
  530. tmpSeries.EdbInfoList = edbInfoList
  531. }
  532. seriesList = append(seriesList, tmpSeries)
  533. }
  534. sectionExtraConfig.SeriesList = seriesList
  535. } else {
  536. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  537. if err != nil {
  538. errMsg = "截面组合图配置异常"
  539. err = errors.New(errMsg)
  540. return
  541. }
  542. }
  543. //校验引用日期名称是否重复
  544. dateNameMap := make(map[string]int)
  545. dateNameEnMap := make(map[string]int)
  546. for _, v := range sectionExtraConfig.DateConfList {
  547. if _, ok := dateNameMap[v.DateConfName]; ok {
  548. errMsg = "截面组合图引用日期名称设置重复"
  549. err = errors.New(errMsg + v.DateConfName)
  550. return
  551. }
  552. if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
  553. errMsg = "截面组合图引用日期名称设置重复"
  554. err = errors.New(errMsg + v.DateConfNameEn)
  555. return
  556. }
  557. if v.DateConfName != "" {
  558. dateNameMap[v.DateConfName] = 1
  559. }
  560. if v.DateConfNameEn != "" {
  561. dateNameEnMap[v.DateConfNameEn] = 1
  562. }
  563. }
  564. //检查系列名称是否重复
  565. seriesNameMap := make(map[string]int)
  566. seriesNameEnMap := make(map[string]int)
  567. for _, v := range sectionExtraConfig.SeriesList {
  568. if _, ok := seriesNameMap[v.SeriesName]; ok {
  569. errMsg = "截面组合图系列名称设置重复"
  570. err = errors.New(errMsg + v.SeriesName)
  571. return
  572. }
  573. if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
  574. errMsg = "截面组合图系列名称设置重复"
  575. err = errors.New(errMsg + v.SeriesNameEn)
  576. return
  577. }
  578. if v.SeriesName != "" {
  579. seriesNameMap[v.SeriesName] = 1
  580. }
  581. if v.SeriesNameEn != "" {
  582. seriesNameEnMap[v.SeriesNameEn] = 1
  583. }
  584. }
  585. // 检查基准系列是否设置
  586. if sectionExtraConfig.BaseChartSeriesName == "" {
  587. errMsg = "截面组合图基准系列名称未设置"
  588. err = errors.New(errMsg)
  589. return
  590. }
  591. //todo 如果是详情接口,应该要从其他表里查询配置
  592. extraConfig = sectionExtraConfig
  593. default:
  594. xEdbIdValue = make([]int, 0)
  595. yDataList = make([]chart_info.YData, 0)
  596. }
  597. // 指标对应的所有数据
  598. edbDataListMap, edbList, sourceArr, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  599. if err != nil {
  600. return
  601. }
  602. // 特殊图形数据处理
  603. switch chartType {
  604. case 6: //时序组合图
  605. //判断是否堆积
  606. timeConf := extraConfig.(chart_info.ChartTimeCombineExtraConf)
  607. if extraConfigStr == "" { //历史数据,默认开启堆积
  608. timeConf = chart_info.ChartTimeCombineExtraConf{
  609. IsHeap: 1,
  610. }
  611. }
  612. dataResp = chart_info.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
  613. case 7: // 柱形图
  614. barChartConf := extraConfig.(request.BarChartInfoReq)
  615. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  616. for _, v := range edbList {
  617. // 指标别名
  618. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  619. for _, reqEdb := range barChartConf.EdbInfoIdList {
  620. if v.EdbInfoId == reqEdb.EdbInfoId {
  621. v.EdbAliasName = reqEdb.Name
  622. }
  623. }
  624. }
  625. }
  626. case 10: // 截面散点图
  627. sectionScatterConf := extraConfig.(request.SectionScatterReq)
  628. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  629. // 这个数据没有必要返回给前端
  630. for _, v := range edbList {
  631. v.DataList = nil
  632. }
  633. case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
  634. sectionConf := extraConfig.(chart_info.ChartSectionAllExtraConf)
  635. var chartInfo *chartInfoModel.ChartInfo
  636. if chartInfoId > 0 {
  637. chartInfo, err = chartInfoModel.GetChartInfoById(chartInfoId)
  638. if err != nil {
  639. errMsg = "获取图表信息失败"
  640. return
  641. }
  642. }
  643. xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
  644. // 这个数据没有必要返回给前端
  645. for _, v := range edbList {
  646. v.DataList = nil
  647. }
  648. case utils.CHART_TYPE_RADAR: //雷达图
  649. radarConf := extraConfig.(request.RadarChartInfoReq)
  650. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  651. }
  652. return
  653. }
  654. // GetEdbDataMapList 获取指标最后的基础数据
  655. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*edbDataModel.EdbDataList, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, sourceArr []string, err error) {
  656. edbDataListMap, edbList, sourceArr, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  657. return
  658. }
  659. // getEdbDataMapList 获取指标最后的基础数据
  660. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*edbDataModel.EdbDataList, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, sourceArr []string, err error) {
  661. // 关联指标来源
  662. sourceArr = make([]string, 0)
  663. // 指标对应的所有数据
  664. edbDataListMap = make(map[int][]*edbDataModel.EdbDataList)
  665. for _, v := range mappingList {
  666. //fmt.Println("v:", v.EdbInfoId)
  667. item := new(chartEdbMappingModel.ChartEdbInfoMappingList)
  668. item.EdbInfoId = v.EdbInfoId
  669. item.SourceName = v.SourceName
  670. item.Source = v.Source
  671. item.EdbCode = v.EdbCode
  672. item.EdbName = v.EdbName
  673. item.EdbNameEn = v.EdbNameEn
  674. item.Frequency = v.Frequency
  675. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  676. if v.Unit != `无` {
  677. item.Unit = v.Unit
  678. }
  679. item.UnitEn = v.UnitEn
  680. item.StartDate = v.StartDate
  681. item.EndDate = v.EndDate
  682. item.ModifyTime = v.ModifyTime
  683. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  684. item.PredictChartColor = v.PredictChartColor
  685. item.EdbAliasName = v.EdbAliasName
  686. if !utils.InArray(v.Source, utils.SystemSourceList) { //来源于系统的指标,都展示为弘则研究
  687. if !utils.InArray(v.SourceName, sourceArr) {
  688. sourceArr = append(sourceArr, v.SourceName)
  689. }
  690. }
  691. if chartInfoId <= 0 {
  692. item.IsAxis = 1
  693. item.LeadValue = 0
  694. item.LeadUnit = ""
  695. item.ChartEdbMappingId = 0
  696. item.ChartInfoId = 0
  697. item.IsOrder = false
  698. item.EdbInfoType = 1
  699. item.ChartStyle = ""
  700. item.ChartColor = ""
  701. item.ChartWidth = 0
  702. item.MaxData = v.MaxValue
  703. item.MinData = v.MinValue
  704. } else {
  705. item.IsAxis = v.IsAxis
  706. item.EdbInfoType = v.EdbInfoType
  707. item.LeadValue = v.LeadValue
  708. item.LeadUnit = v.LeadUnit
  709. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  710. item.ChartEdbMappingId = v.ChartEdbMappingId
  711. item.ChartInfoId = v.ChartInfoId
  712. item.ChartStyle = v.ChartStyle
  713. item.ChartColor = v.ChartColor
  714. item.ChartWidth = v.ChartWidth
  715. item.IsOrder = v.IsOrder
  716. item.MaxData = v.MaxData
  717. item.MinData = v.MinData
  718. }
  719. item.LatestValue = v.LatestValue
  720. item.LatestDate = v.LatestDate
  721. item.UniqueCode = v.UniqueCode
  722. item.MoveLatestDate = item.LatestDate
  723. item.EdbAliasName = v.EdbAliasName
  724. item.IsConvert = v.IsConvert
  725. item.ConvertType = v.ConvertType
  726. item.ConvertValue = v.ConvertValue
  727. item.ConvertUnit = v.ConvertUnit
  728. item.ConvertEnUnit = v.ConvertEnUnit
  729. var startDateReal string
  730. var diffSeconds int64
  731. if chartType == 2 { //季节性图
  732. startDateReal = startDate
  733. } else {
  734. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  735. var startTimeRealTemp time.Time
  736. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  737. switch v.LeadUnit {
  738. case "天":
  739. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  740. case "月":
  741. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  742. case "季":
  743. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  744. case "周":
  745. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  746. case "年":
  747. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  748. }
  749. if startTimeRealTemp.Before(startDateParse) {
  750. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  751. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  752. } else {
  753. startDateReal = startDate
  754. diffSeconds = 0
  755. }
  756. // 预测指标的开始日期也要偏移
  757. {
  758. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  759. if tmpErr != nil {
  760. err = tmpErr
  761. return
  762. }
  763. item.MoveLatestDate = item.MoveLatestDate.AddDate(0, 0, day)
  764. }
  765. } else {
  766. startDateReal = startDate
  767. }
  768. }
  769. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  770. calendarPreYear := 0
  771. if calendar == "农历" {
  772. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  773. if err != nil {
  774. fmt.Println("time.Parse:" + err.Error())
  775. }
  776. calendarPreYear = newStartDateReal.Year() - 1
  777. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  778. startDateReal = newStartDateReal.Format(utils.FormatDate)
  779. }
  780. dataList := make([]*edbDataModel.EdbDataList, 0)
  781. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate, ";EdbInfoCategoryType:", v.EdbInfoCategoryType)
  782. //var newEdbInfo *edbInfoModel.EdbInfo
  783. switch v.EdbInfoCategoryType {
  784. case 0:
  785. dataList, err = edbDataModel.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  786. case 1:
  787. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
  788. //if item.MaxValue < newEdbInfo.MaxValue {
  789. // item.MaxData = newEdbInfo.MaxValue
  790. //}
  791. //if item.MinData > newEdbInfo.MinValue {
  792. // item.MinData = newEdbInfo.MinValue
  793. //}
  794. default:
  795. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  796. }
  797. if err != nil {
  798. return
  799. }
  800. if v.IsConvert == 1 {
  801. switch v.ConvertType {
  802. case 1:
  803. for i, data := range dataList {
  804. dataList[i].Value = data.Value * v.ConvertValue
  805. }
  806. //item.MaxData = item.MaxData * v.ConvertValue
  807. //item.MinData = item.MinData * v.ConvertValue
  808. case 2:
  809. for i, data := range dataList {
  810. dataList[i].Value = data.Value / v.ConvertValue
  811. }
  812. //item.MaxData = item.MaxData / v.ConvertValue
  813. //item.MinData = item.MinData / v.ConvertValue
  814. case 3:
  815. for i, data := range dataList {
  816. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  817. }
  818. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  819. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  820. }
  821. }
  822. edbDataListMap[v.EdbInfoId] = dataList
  823. if diffSeconds != 0 && v.EdbInfoType == 0 {
  824. dataListLen := len(dataList)
  825. for i := 0; i < dataListLen; i++ {
  826. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  827. }
  828. }
  829. if chartType == 2 {
  830. //latestDateStr := v.LatestDate.Format(utils.FormatDate) //实际数据的截止日期
  831. latestDate := v.LatestDate
  832. if calendar == "农历" {
  833. if len(dataList) <= 0 {
  834. result := new(edbDataModel.EdbDataResult)
  835. item.DataList = result
  836. } else {
  837. result, tmpErr := edbDataService.AddCalculateQuarterV6(dataList)
  838. if tmpErr != nil {
  839. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  840. return
  841. }
  842. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  843. if tErr != nil {
  844. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  845. return
  846. }
  847. item.DataList = quarterDataList
  848. }
  849. } else {
  850. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  851. if tErr != nil {
  852. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  853. return
  854. }
  855. item.DataList = quarterDataList
  856. }
  857. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  858. //item.DataList = dataList
  859. } else {
  860. item.DataList = dataList
  861. }
  862. edbList = append(edbList, item)
  863. }
  864. return
  865. }
  866. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  867. func GetSeasonEdbInfoDataListByXDate(dataList []*edbDataModel.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort edbDataModel.QuarterDataList, err error) {
  868. xStartDate := "01-01"
  869. xEndDate := "12-31"
  870. jumpYear := 0
  871. legends := make([]edbDataModel.SeasonChartLegend, 0)
  872. var seasonExtra edbDataModel.SeasonExtraItem
  873. if seasonExtraConfig != "" {
  874. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  875. if err != nil {
  876. return
  877. }
  878. }
  879. if seasonExtra.XStartDate != "" {
  880. xStartDate = seasonExtra.XStartDate
  881. xEndDate = seasonExtra.XEndDate
  882. jumpYear = seasonExtra.JumpYear
  883. legends = seasonExtra.ChartLegend
  884. }
  885. length := len(dataList)
  886. if length == 0 {
  887. return
  888. }
  889. legendMap := make(map[string]string, 0)
  890. if len(legends) > 0 {
  891. for _, v := range legends {
  892. legendMap[v.Name] = v.Value
  893. }
  894. }
  895. latestDateStr := latestDate.Format(utils.FormatDate)
  896. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  897. //根据数据确定最早的年份,和最近年份
  898. //根据横轴的日期,汇总所有的年份
  899. startDate := dataList[0].DataTime
  900. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  901. if tmpErr != nil {
  902. err = tmpErr
  903. return
  904. }
  905. startYear := startDateT.Year()
  906. //获取数据的最新日期
  907. lastDate := dataList[length-1].DataTime
  908. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  909. if tmpErr != nil {
  910. err = tmpErr
  911. return
  912. }
  913. endYear := lastDateT.Year()
  914. nowYear := time.Now().Year()
  915. chartLegendMaxYear := 0
  916. dataMap := make(map[string]edbDataModel.QuarterXDateItem, 0)
  917. quarterDataList := make([]*edbDataModel.QuarterData, 0)
  918. quarterMap := make(map[string][]*edbDataModel.EdbDataList, 0)
  919. //整理出日期
  920. idx := 1
  921. chartLegendMap := make(map[string]int, 0)
  922. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  923. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  924. currentEndYear := currentStartYear
  925. if jumpYear == 1 {
  926. currentEndYear = currentStartYear + 1
  927. }
  928. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  929. name := fmt.Sprintf("%s_%s", startStr, endStr)
  930. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  931. startT, tEr := time.Parse(utils.FormatDate, startStr)
  932. if tEr != nil {
  933. err = tEr
  934. return
  935. }
  936. endT, tEr := time.Parse(utils.FormatDate, endStr)
  937. if tEr != nil {
  938. err = tEr
  939. return
  940. }
  941. if lastDateT.Before(startT) {
  942. //如果最新的日期在起始日之前,则跳出循环
  943. break
  944. }
  945. if endT.Year() > nowYear {
  946. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  947. nowYear = endT.Year()
  948. }
  949. item := edbDataModel.QuarterXDateItem{
  950. StartDate: startT,
  951. EndDate: endT,
  952. ShowName: showName,
  953. }
  954. chartLegendMaxYear = endT.Year()
  955. dataMap[name] = item
  956. chartLegendMap[name] = idx
  957. idx++
  958. if lastDateT.Before(endT) {
  959. //如果最新的日期在起始日之前,则跳出循环
  960. break
  961. }
  962. }
  963. lenYear := len(dataMap)
  964. if chartLegendMaxYear > endYear {
  965. chartLegendMaxYear = endYear
  966. }
  967. for k, v := range dataMap {
  968. if i, ok := chartLegendMap[k]; ok {
  969. //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  970. v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
  971. }
  972. dataMap[k] = v
  973. }
  974. for _, v := range dataList {
  975. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  976. year := dataTimeT.Year()
  977. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  978. for k, dateItem := range dataMap {
  979. tmpVal := edbDataModel.EdbDataList{
  980. EdbDataId: v.EdbDataId,
  981. EdbInfoId: v.EdbInfoId,
  982. DataTime: v.DataTime,
  983. DataTimestamp: v.DataTimestamp,
  984. Value: v.Value,
  985. }
  986. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  987. if jumpYear == 1 {
  988. //计算前一年最大的日期, 只补齐数据到去年
  989. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  990. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  991. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  992. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  993. } else {
  994. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  995. }
  996. } else {
  997. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  998. }
  999. timestamp := newItemDate.UnixNano() / 1e6
  1000. tmpVal.DataTimestamp = timestamp
  1001. tmpV := &tmpVal
  1002. if findVal, ok := quarterMap[k]; !ok {
  1003. findVal = append(findVal, tmpV)
  1004. quarterMap[k] = findVal
  1005. } else {
  1006. findVal = append(findVal, tmpV)
  1007. quarterMap[k] = findVal
  1008. }
  1009. if v.DataTime == latestDateStr {
  1010. dateItem.CuttingDataTimestamp = timestamp
  1011. dataMap[k] = dateItem
  1012. }
  1013. //break
  1014. }
  1015. }
  1016. }
  1017. for k, v := range dataMap {
  1018. itemList := quarterMap[k]
  1019. quarterItem := new(edbDataModel.QuarterData)
  1020. quarterItem.Years = v.ShowName
  1021. quarterItem.Year = v.ChartLegend
  1022. quarterItem.ChartLegend = v.ChartLegend
  1023. if le, ok := legendMap[v.ShowName]; ok {
  1024. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  1025. quarterItem.ChartLegend = le
  1026. }
  1027. }
  1028. quarterItem.DataList = itemList
  1029. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  1030. //如果等于最后的实际日期,那么将切割时间戳记录
  1031. if quarterItem.CuttingDataTimestamp == 0 {
  1032. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1033. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  1034. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1035. }
  1036. }
  1037. quarterDataList = append(quarterDataList, quarterItem)
  1038. }
  1039. if len(quarterDataList) > 0 {
  1040. quarterDataListSort = quarterDataList
  1041. sort.Sort(quarterDataListSort)
  1042. }
  1043. return
  1044. }
  1045. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  1046. func GetSeasonEdbInfoDataListByXDateNong(result *edbDataModel.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort edbDataModel.QuarterDataList, err error) {
  1047. xStartDate := "01-01"
  1048. xEndDate := "12-31"
  1049. jumpYear := 0
  1050. legends := make([]edbDataModel.SeasonChartLegend, 0)
  1051. var seasonExtra edbDataModel.SeasonExtraItem
  1052. if seasonExtraConfig != "" {
  1053. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  1054. if err != nil {
  1055. return
  1056. }
  1057. }
  1058. if seasonExtra.XStartDate != "" {
  1059. xStartDate = seasonExtra.XStartDate
  1060. xEndDate = seasonExtra.XEndDate
  1061. jumpYear = seasonExtra.JumpYear
  1062. legends = seasonExtra.ChartLegend
  1063. }
  1064. length := len(result.List)
  1065. if length == 0 {
  1066. return
  1067. }
  1068. legendMap := make(map[string]string, 0)
  1069. if len(legends) > 0 {
  1070. for _, v := range legends {
  1071. legendMap[v.Name] = v.Value
  1072. }
  1073. }
  1074. latestDateYear := latestDate.Year()
  1075. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  1076. //根据数据确定最早的年份,和最近年份
  1077. //根据横轴的日期,汇总所有的年份
  1078. startYear := result.List[0].Year
  1079. /*if jumpYear == 1 {
  1080. if startYear != calendarPreYear {
  1081. startYear = startYear - 1
  1082. }
  1083. }*/
  1084. itemLength := len(result.List[length-1].Items)
  1085. //获取数据的最新日期
  1086. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  1087. maxY := result.List[length-1].Year
  1088. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  1089. if tmpErr != nil {
  1090. err = tmpErr
  1091. return
  1092. }
  1093. endYear := lastDateT.Year()
  1094. nowYear := time.Now().Year()
  1095. chartLegendMaxYear := 0
  1096. dataMap := make(map[string]edbDataModel.QuarterXDateItem, 0)
  1097. quarterDataList := make([]*edbDataModel.QuarterData, 0)
  1098. resultData := make([]*edbDataModel.QuarterData, 0)
  1099. quarterMap := make(map[string][]*edbDataModel.EdbDataList, 0)
  1100. //整理出日期
  1101. var startTmpT, endTmpT time.Time
  1102. idx := 1
  1103. chartLegendMap := make(map[string]int, 0)
  1104. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  1105. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  1106. currentEndYear := currentStartYear
  1107. if jumpYear == 1 {
  1108. currentEndYear = currentStartYear + 1
  1109. }
  1110. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  1111. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  1112. startT, tEr := time.Parse(utils.FormatDate, startStr)
  1113. if tEr != nil {
  1114. err = tEr
  1115. return
  1116. }
  1117. endT, tEr := time.Parse(utils.FormatDate, endStr)
  1118. if tEr != nil {
  1119. err = tEr
  1120. return
  1121. }
  1122. if lastDateT.Before(startT) {
  1123. //如果最新的日期在起始日之前,则跳出循环
  1124. break
  1125. }
  1126. if endT.Year() > nowYear {
  1127. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  1128. nowYear = endT.Year()
  1129. }
  1130. item := edbDataModel.QuarterXDateItem{
  1131. StartDate: startT,
  1132. EndDate: endT,
  1133. ShowName: showName,
  1134. }
  1135. chartLegendMaxYear = endT.Year()
  1136. dataMap[showName] = item
  1137. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  1138. startTmpT = startT
  1139. endTmpT = endT
  1140. chartLegendMap[showName] = idx
  1141. idx++
  1142. if lastDateT.Before(endT) {
  1143. //如果最新的日期在起始日之前,则跳出循环
  1144. break
  1145. }
  1146. }
  1147. lenYear := len(dataMap)
  1148. if chartLegendMaxYear > endYear {
  1149. chartLegendMaxYear = endYear
  1150. }
  1151. for k, v := range dataMap {
  1152. if i, ok := chartLegendMap[k]; ok {
  1153. //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  1154. v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
  1155. }
  1156. dataMap[k] = v
  1157. }
  1158. yearDataListMap := make(map[int]*edbDataModel.EdbDataItems, 0)
  1159. for _, lv := range result.List {
  1160. yearDataListMap[lv.Year] = lv
  1161. }
  1162. fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
  1163. fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
  1164. fmt.Printf("maxY 为%d \n", maxY)
  1165. for name, dateItem := range dataMap {
  1166. tY := dateItem.EndDate.Year()
  1167. if lastDateT.Month() >= 11 {
  1168. if maxY > endTmpT.Year() {
  1169. tY = tY + 1
  1170. }
  1171. }
  1172. lv, ok1 := yearDataListMap[tY]
  1173. fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
  1174. if !ok1 {
  1175. continue
  1176. }
  1177. for _, item := range lv.Items {
  1178. tmpVal := edbDataModel.EdbDataList{
  1179. EdbDataId: item.EdbDataId,
  1180. EdbInfoId: item.EdbInfoId,
  1181. DataTime: item.DataTime,
  1182. DataTimestamp: item.DataTimestamp,
  1183. Value: item.Value,
  1184. }
  1185. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  1186. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  1187. tmpV := &tmpVal
  1188. if findVal, ok := quarterMap[name]; !ok {
  1189. findVal = append(findVal, tmpV)
  1190. quarterMap[name] = findVal
  1191. } else {
  1192. findVal = append(findVal, tmpV)
  1193. quarterMap[name] = findVal
  1194. }
  1195. if lv.Year >= latestDateYear {
  1196. // 切割的日期时间字符串
  1197. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  1198. if item.DataTime == cuttingDataTimeStr {
  1199. dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
  1200. dataMap[name] = dateItem
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }
  1206. for k, v := range dataMap {
  1207. itemList := quarterMap[k]
  1208. quarterItem := new(edbDataModel.QuarterData)
  1209. quarterItem.Years = v.ShowName
  1210. quarterItem.Year = v.ChartLegend
  1211. quarterItem.ChartLegend = v.ChartLegend
  1212. if le, ok := legendMap[v.ShowName]; ok {
  1213. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  1214. quarterItem.ChartLegend = le
  1215. }
  1216. }
  1217. quarterItem.DataList = itemList
  1218. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  1219. //如果等于最后的实际日期,那么将切割时间戳记录
  1220. if quarterItem.CuttingDataTimestamp == 0 {
  1221. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1222. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  1223. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1224. }
  1225. }
  1226. quarterDataList = append(quarterDataList, quarterItem)
  1227. }
  1228. if result.List[0].Year != calendarPreYear {
  1229. itemList := make([]*edbDataModel.EdbDataList, 0)
  1230. items := new(edbDataModel.QuarterData)
  1231. //items.Year = calendarPreYear
  1232. items.DataList = itemList
  1233. newResult := make([]*edbDataModel.QuarterData, 0)
  1234. newResult = append(newResult, items)
  1235. newResult = append(newResult, quarterDataList...)
  1236. resultData = newResult
  1237. } else {
  1238. resultData = quarterDataList
  1239. }
  1240. if len(quarterDataList) > 0 {
  1241. quarterDataListSort = resultData
  1242. sort.Sort(quarterDataListSort)
  1243. }
  1244. return
  1245. }
  1246. // BarChartData 柱方图的数据处理
  1247. func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, barChartInfoDateList []request.BarChartInfoDateReq, barChartInfoSort request.BarChartInfoSortReq) (edbIdList []int, yDataList []chart_info.YData, err error) {
  1248. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1249. edbDataMap := make(map[int]map[string]float64)
  1250. for edbInfoId, edbDataList := range edbDataListMap {
  1251. edbDateData := make(map[string]float64)
  1252. for _, edbData := range edbDataList {
  1253. edbDateData[edbData.DataTime] = edbData.Value
  1254. }
  1255. edbDataMap[edbInfoId] = edbDateData
  1256. }
  1257. // edbIdList 指标展示顺序;x轴的指标顺序
  1258. edbIdList = make([]int, 0)
  1259. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1260. dateData := make(map[int]float64)
  1261. if barChartInfoSort.Sort == 0 {
  1262. for _, v := range mappingList {
  1263. edbIdList = append(edbIdList, v.EdbInfoId)
  1264. }
  1265. } else {
  1266. lenBarChartInfoDateList := len(barChartInfoDateList)
  1267. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  1268. err = errors.New("排序日期异常")
  1269. return
  1270. }
  1271. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  1272. // 日期配置
  1273. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  1274. for edbInfoId, dataList := range edbDataListMap {
  1275. if len(dataList) <= 0 {
  1276. // 没有数据的指标id
  1277. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1278. continue
  1279. }
  1280. findDate := barChartInfoDate.Date
  1281. switch barChartInfoDate.Type {
  1282. case 1: //最新值
  1283. findDate = dataList[len(dataList)-1].DataTime
  1284. case 2: //近期几天
  1285. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1286. if tmpErr != nil {
  1287. err = tmpErr
  1288. return
  1289. }
  1290. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1291. lenData := len(dataList) - 1
  1292. for i := lenData; i >= 0; i-- {
  1293. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1294. if tmpErr != nil {
  1295. err = tmpErr
  1296. return
  1297. }
  1298. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1299. findDate = dataList[i].DataTime
  1300. break
  1301. }
  1302. }
  1303. case 3: // 固定日期
  1304. //最早的日期
  1305. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1306. if tmpErr != nil {
  1307. err = tmpErr
  1308. return
  1309. }
  1310. //寻找固定日期的数据
  1311. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1312. if tmpErr != nil {
  1313. err = tmpErr
  1314. return
  1315. }
  1316. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1317. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1318. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1319. findDate = tmpDate
  1320. break
  1321. }
  1322. }
  1323. default:
  1324. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1325. return
  1326. }
  1327. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1328. dateData[edbInfoId] = tmpValue
  1329. } else {
  1330. // 没有数据的指标id
  1331. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1332. }
  1333. }
  1334. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1335. // 排序
  1336. dateDataSort := utils.NewMapSorter(dateData)
  1337. sort.Sort(dateDataSort)
  1338. if barChartInfoSort.Sort == 1 {
  1339. // 先将没有数据的指标id放在最前面
  1340. if len(notDataEdbIdList) > 0 {
  1341. edbIdList = append(edbIdList, notDataEdbIdList...)
  1342. }
  1343. for _, v := range dateDataSort {
  1344. edbIdList = append(edbIdList, v.Key)
  1345. }
  1346. } else {
  1347. for i := len(dateDataSort) - 1; i >= 0; i-- {
  1348. edbIdList = append(edbIdList, dateDataSort[i].Key)
  1349. }
  1350. // 再将没有数据的指标id放在最后面
  1351. if len(notDataEdbIdList) > 0 {
  1352. edbIdList = append(edbIdList, notDataEdbIdList...)
  1353. }
  1354. }
  1355. }
  1356. yDataList = make([]chart_info.YData, 0) //y轴的数据列表
  1357. for _, barChartInfoDate := range barChartInfoDateList {
  1358. var maxDate time.Time
  1359. findDataList := make([]float64, 0) // 当前日期的数据值
  1360. for _, edbInfoId := range edbIdList {
  1361. findDate := barChartInfoDate.Date //需要的日期值
  1362. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1363. if len(dataList) <= 0 {
  1364. // 没有数据的指标id
  1365. findDataList = append(findDataList, 0)
  1366. continue
  1367. }
  1368. switch barChartInfoDate.Type {
  1369. case 1: //最新值
  1370. dataList := edbDataListMap[edbInfoId]
  1371. findDate = dataList[len(dataList)-1].DataTime
  1372. case 2: //近期几天
  1373. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1374. if tmpErr != nil {
  1375. err = tmpErr
  1376. return
  1377. }
  1378. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1379. lenData := len(dataList) - 1
  1380. for i := lenData; i >= 0; i-- {
  1381. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1382. if tmpErr != nil {
  1383. err = tmpErr
  1384. return
  1385. }
  1386. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1387. findDate = dataList[i].DataTime
  1388. break
  1389. }
  1390. }
  1391. case 3: // 固定日期
  1392. //最早的日期
  1393. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1394. if tmpErr != nil {
  1395. err = tmpErr
  1396. return
  1397. }
  1398. //寻找固定日期的数据
  1399. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1400. if tmpErr != nil {
  1401. err = tmpErr
  1402. return
  1403. }
  1404. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1405. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1406. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1407. findDate = tmpDate
  1408. break
  1409. }
  1410. }
  1411. default:
  1412. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1413. return
  1414. }
  1415. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1416. if maxDate.IsZero() {
  1417. maxDate = findDateTime
  1418. } else {
  1419. if findDateTime.After(maxDate) {
  1420. maxDate = findDateTime
  1421. }
  1422. }
  1423. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1424. findDataList = append(findDataList, tmpValue)
  1425. } else {
  1426. findDataList = append(findDataList, 0)
  1427. }
  1428. }
  1429. yDate := "0000-00-00"
  1430. if !maxDate.IsZero() {
  1431. yDate = maxDate.Format(utils.FormatDate)
  1432. }
  1433. yDataList = append(yDataList, chart_info.YData{
  1434. Date: yDate,
  1435. Value: findDataList,
  1436. Color: barChartInfoDate.Color,
  1437. Name: barChartInfoDate.Name,
  1438. })
  1439. }
  1440. return
  1441. }
  1442. // 获取频度的英文版
  1443. func GetFrequencyEn(frequency string) (frequencyEn string) {
  1444. switch frequency {
  1445. case "日度":
  1446. frequencyEn = "day"
  1447. return
  1448. case "周度":
  1449. frequencyEn = "week"
  1450. return
  1451. case "旬度":
  1452. frequencyEn = "ten days"
  1453. return
  1454. case "月度":
  1455. frequencyEn = "month"
  1456. return
  1457. case "季度":
  1458. frequencyEn = "quarter"
  1459. return
  1460. case "年度":
  1461. frequencyEn = "year"
  1462. return
  1463. }
  1464. return
  1465. }
  1466. func GetLeadUnitEn(unit string) (unitEn string) {
  1467. switch unit {
  1468. case "天":
  1469. unitEn = "day"
  1470. return
  1471. case "周":
  1472. unitEn = "week"
  1473. return
  1474. case "月":
  1475. unitEn = "month"
  1476. return
  1477. case "季":
  1478. unitEn = "quarter"
  1479. return
  1480. case "年":
  1481. unitEn = "year"
  1482. return
  1483. }
  1484. return
  1485. }
  1486. // ChartInfoRefreshV2 图表刷新
  1487. // @author Roc
  1488. // @datetime 2022-09-16 10:15:38
  1489. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1490. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  1491. var errmsg string
  1492. defer func() {
  1493. if err != nil {
  1494. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1495. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1496. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1497. }
  1498. }()
  1499. edbMappingList, err := chartEdbMappingModel.GetMappingListByChartInfoId(chartInfoId)
  1500. if err != nil {
  1501. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1502. return
  1503. }
  1504. edbIdList := make([]int, 0)
  1505. for _, v := range edbMappingList {
  1506. edbIdList = append(edbIdList, v.EdbInfoId)
  1507. }
  1508. // 批量刷新
  1509. err, errmsg = EdbInfoRefreshAllFromBase(edbIdList, false)
  1510. if err != nil {
  1511. return
  1512. }
  1513. return
  1514. }
  1515. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  1516. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  1517. switch chartType {
  1518. case 10: //截面散点
  1519. var extraConfig request.SectionScatterReq
  1520. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1521. if err != nil {
  1522. return
  1523. }
  1524. // 判断是否有配置日期序列
  1525. if len(extraConfig.SeriesList) <= 0 {
  1526. errMsg = `请配置序列`
  1527. err = errors.New(errMsg)
  1528. return
  1529. }
  1530. // 判断是否有填写指标
  1531. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  1532. errMsg = `请选择指标`
  1533. err = errors.New(errMsg)
  1534. return
  1535. }
  1536. // 遍历指标列表获取指标id
  1537. edbIdMap := make(map[int]int)
  1538. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  1539. // X 轴的指标id
  1540. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  1541. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  1542. edbIdList = append(edbIdList, v.XEdbInfoId)
  1543. }
  1544. // Y 轴的指标id
  1545. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  1546. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1547. edbIdList = append(edbIdList, v.YEdbInfoId)
  1548. }
  1549. }
  1550. }
  1551. return
  1552. }
  1553. // GetSectionScatterChartData 柱方图的数据处理
  1554. func GetSectionScatterChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, extraConfig request.SectionScatterReq) (edbIdList []int, chartDataResp request.SectionScatterInfoResp, err error) {
  1555. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1556. edbDataMap := make(map[int]map[string]float64)
  1557. for edbInfoId, edbDataList := range edbDataListMap {
  1558. edbDateData := make(map[string]float64)
  1559. for _, edbData := range edbDataList {
  1560. edbDateData[edbData.DataTime] = edbData.Value
  1561. }
  1562. edbDataMap[edbInfoId] = edbDateData
  1563. }
  1564. // edbIdList 指标展示顺序;x轴的指标顺序
  1565. edbIdList = make([]int, 0)
  1566. edbMappingMap := make(map[int]*chartEdbMappingModel.ChartEdbInfoMapping)
  1567. for _, v := range mappingList {
  1568. edbIdList = append(edbIdList, v.EdbInfoId)
  1569. edbMappingMap[v.EdbInfoId] = v
  1570. }
  1571. //SectionScatterSeriesInfoResp
  1572. dataListResp := make([]request.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1573. for _, seriesItem := range extraConfig.SeriesList {
  1574. var maxDate time.Time
  1575. // 系列中的指标数据
  1576. tmpSeriesEdbInfoList := make([]request.SectionScatterEdbItemResp, 0)
  1577. var minXVal, maxXVal, minYVal, maxYVal float64
  1578. for _, edbConf := range seriesItem.EdbInfoList {
  1579. tmpItem := request.SectionScatterEdbItemResp{
  1580. IsShow: edbConf.IsShow,
  1581. Name: edbConf.Name,
  1582. NameEn: edbConf.NameEn,
  1583. } //单个坐标点的数据
  1584. //X轴的数据
  1585. {
  1586. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1587. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1588. if !ok {
  1589. continue
  1590. }
  1591. findDate := edbConf.XDate //需要的日期值
  1592. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1593. if len(dataList) <= 0 {
  1594. // 没有数据的指标id
  1595. //findDataList = append(findDataList, 0)
  1596. continue
  1597. }
  1598. tmpItem.XEdbInfoId = edbInfoId
  1599. tmpItem.XName = edbMappingInfo.EdbName
  1600. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1601. switch edbConf.XDateType {
  1602. case 1: //最新值
  1603. dataList := edbDataListMap[edbInfoId]
  1604. findDate = dataList[len(dataList)-1].DataTime
  1605. case 2: //近期几天
  1606. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1607. if tmpErr != nil {
  1608. err = tmpErr
  1609. return
  1610. }
  1611. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1612. lenData := len(dataList) - 1
  1613. for i := lenData; i >= 0; i-- {
  1614. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1615. if tmpErr != nil {
  1616. err = tmpErr
  1617. return
  1618. }
  1619. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1620. findDate = dataList[i].DataTime
  1621. break
  1622. }
  1623. }
  1624. case 3: // 固定日期
  1625. //最早的日期
  1626. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1627. if tmpErr != nil {
  1628. err = tmpErr
  1629. return
  1630. }
  1631. //寻找固定日期的数据
  1632. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1633. if tmpErr != nil {
  1634. err = tmpErr
  1635. return
  1636. }
  1637. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1638. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1639. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1640. findDate = tmpDate
  1641. break
  1642. }
  1643. }
  1644. default:
  1645. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1646. return
  1647. }
  1648. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1649. if maxDate.IsZero() {
  1650. maxDate = findDateTime
  1651. } else {
  1652. if findDateTime.After(maxDate) {
  1653. maxDate = findDateTime
  1654. }
  1655. }
  1656. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1657. tmpItem.XDate = findDate
  1658. tmpItem.XValue = tmpValue
  1659. } else {
  1660. continue
  1661. }
  1662. }
  1663. //Y轴的数据
  1664. {
  1665. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1666. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1667. if !ok {
  1668. continue
  1669. }
  1670. findDate := edbConf.YDate //需要的日期值
  1671. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1672. if len(dataList) <= 0 {
  1673. // 没有数据的指标id
  1674. //findDataList = append(findDataList, 0)
  1675. continue
  1676. }
  1677. tmpItem.YEdbInfoId = edbInfoId
  1678. tmpItem.YName = edbMappingInfo.EdbName
  1679. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1680. switch edbConf.YDateType {
  1681. case 1: //最新值
  1682. dataList := edbDataListMap[edbInfoId]
  1683. findDate = dataList[len(dataList)-1].DataTime
  1684. case 2: //近期几天
  1685. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1686. if tmpErr != nil {
  1687. err = tmpErr
  1688. return
  1689. }
  1690. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1691. lenData := len(dataList) - 1
  1692. for i := lenData; i >= 0; i-- {
  1693. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1694. if tmpErr != nil {
  1695. err = tmpErr
  1696. return
  1697. }
  1698. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1699. findDate = dataList[i].DataTime
  1700. break
  1701. }
  1702. }
  1703. case 3: // 固定日期
  1704. //最早的日期
  1705. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1706. if tmpErr != nil {
  1707. err = tmpErr
  1708. return
  1709. }
  1710. //寻找固定日期的数据
  1711. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1712. if tmpErr != nil {
  1713. err = tmpErr
  1714. return
  1715. }
  1716. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1717. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1718. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1719. findDate = tmpDate
  1720. break
  1721. }
  1722. }
  1723. default:
  1724. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1725. return
  1726. }
  1727. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1728. if maxDate.IsZero() {
  1729. maxDate = findDateTime
  1730. } else {
  1731. if findDateTime.After(maxDate) {
  1732. maxDate = findDateTime
  1733. }
  1734. }
  1735. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1736. tmpItem.YDate = findDate
  1737. tmpItem.YValue = tmpValue
  1738. } else {
  1739. continue
  1740. }
  1741. }
  1742. // 获取当前系列的X轴的最大最小值
  1743. {
  1744. if tmpItem.XValue < minXVal {
  1745. minXVal = tmpItem.XValue
  1746. }
  1747. if tmpItem.XValue > maxXVal {
  1748. maxXVal = tmpItem.XValue
  1749. }
  1750. if tmpItem.YValue < minYVal {
  1751. minYVal = tmpItem.YValue
  1752. }
  1753. if tmpItem.YValue > maxYVal {
  1754. maxYVal = tmpItem.YValue
  1755. }
  1756. }
  1757. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1758. }
  1759. trendLimitData := make([]request.CoordinatePoint, 0) //趋势线的前后坐标点
  1760. var trendLine, rSquare string
  1761. // 生成线性方程式
  1762. var a, b float64
  1763. {
  1764. coordinateData := make([]utils.Coordinate, 0)
  1765. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1766. tmpCoordinate1 := utils.Coordinate{
  1767. X: tmpSeriesEdbInfo.XValue,
  1768. Y: tmpSeriesEdbInfo.YValue,
  1769. }
  1770. coordinateData = append(coordinateData, tmpCoordinate1)
  1771. }
  1772. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1773. if len(coordinateData) >= 2 {
  1774. a, b = utils.GetLinearResult(coordinateData)
  1775. if !math.IsNaN(a) && !math.IsNaN(b) {
  1776. if b > 0 {
  1777. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1778. } else {
  1779. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1780. }
  1781. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1782. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1783. }
  1784. // 计算R平方
  1785. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1786. }
  1787. trendLimitData = append(trendLimitData, request.CoordinatePoint{
  1788. X: minXVal,
  1789. Y: minYVal,
  1790. }, request.CoordinatePoint{
  1791. X: maxXVal,
  1792. Y: maxYVal,
  1793. })
  1794. }
  1795. dataListResp = append(dataListResp, request.SectionScatterSeriesItemResp{
  1796. Name: seriesItem.Name,
  1797. NameEn: seriesItem.NameEn,
  1798. Color: seriesItem.Color,
  1799. EdbInfoList: tmpSeriesEdbInfoList,
  1800. ShowTrendLine: seriesItem.ShowTrendLine,
  1801. ShowFitEquation: seriesItem.ShowFitEquation,
  1802. ShowRSquare: seriesItem.ShowRSquare,
  1803. TrendLine: trendLine,
  1804. RSquare: rSquare,
  1805. TrendLimitData: trendLimitData,
  1806. })
  1807. }
  1808. chartDataResp = request.SectionScatterInfoResp{
  1809. XName: extraConfig.XName,
  1810. XNameEn: extraConfig.XNameEn,
  1811. XUnitName: extraConfig.XUnitName,
  1812. XUnitNameEn: extraConfig.XUnitNameEn,
  1813. YName: extraConfig.YName,
  1814. YNameEn: extraConfig.YNameEn,
  1815. YUnitName: extraConfig.YUnitName,
  1816. YUnitNameEn: extraConfig.YUnitNameEn,
  1817. XMinValue: extraConfig.XMinValue,
  1818. XMaxValue: extraConfig.XMaxValue,
  1819. YMinValue: extraConfig.YMinValue,
  1820. YMaxValue: extraConfig.YMaxValue,
  1821. DataList: dataListResp,
  1822. }
  1823. return
  1824. }
  1825. // RadarChartData 雷达图的数据处理
  1826. func RadarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, extraConfig request.RadarChartInfoReq) (edbIdList []int, chartDataResp chart_info.RadarChartInfoResp, err error) {
  1827. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1828. edbDataMap := make(map[int]map[string]float64)
  1829. for edbInfoId, edbDataList := range edbDataListMap {
  1830. edbDateData := make(map[string]float64)
  1831. for _, edbData := range edbDataList {
  1832. edbDateData[edbData.DataTime] = edbData.Value
  1833. }
  1834. edbDataMap[edbInfoId] = edbDateData
  1835. }
  1836. // edbIdList 指标展示顺序;x轴的指标顺序
  1837. edbIdList = make([]int, 0)
  1838. for _, v := range mappingList {
  1839. edbIdList = append(edbIdList, v.EdbInfoId)
  1840. }
  1841. chartDateList := extraConfig.DateList
  1842. yDataList := make([]chart_info.RadarYData, 0) //y轴的数据列表
  1843. for _, chartDate := range chartDateList {
  1844. var maxDate time.Time
  1845. findDataList := make([]float64, 0) // 当前日期的数据值
  1846. for _, edbInfoId := range edbIdList {
  1847. findDate := chartDate.Date //需要的日期值
  1848. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1849. if len(dataList) <= 0 {
  1850. // 没有数据的指标id
  1851. findDataList = append(findDataList, 0)
  1852. continue
  1853. }
  1854. switch chartDate.Type {
  1855. case 1: //最新值
  1856. findDate = dataList[len(dataList)-1].DataTime
  1857. case 2: //近期几天
  1858. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1859. if tmpErr != nil {
  1860. err = tmpErr
  1861. return
  1862. }
  1863. findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
  1864. lenData := len(dataList) - 1
  1865. for i := lenData; i >= 0; i-- {
  1866. currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1867. if e != nil {
  1868. err = e
  1869. return
  1870. }
  1871. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1872. findDate = dataList[i].DataTime
  1873. break
  1874. }
  1875. }
  1876. case 3: // 固定日期
  1877. //最早的日期
  1878. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1879. if tmpErr != nil {
  1880. err = tmpErr
  1881. return
  1882. }
  1883. //寻找固定日期的数据
  1884. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
  1885. if tmpErr != nil {
  1886. err = tmpErr
  1887. return
  1888. }
  1889. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1890. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1891. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1892. findDate = tmpDate
  1893. break
  1894. }
  1895. }
  1896. default:
  1897. err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
  1898. return
  1899. }
  1900. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1901. if maxDate.IsZero() {
  1902. maxDate = findDateTime
  1903. } else {
  1904. if findDateTime.After(maxDate) {
  1905. maxDate = findDateTime
  1906. }
  1907. }
  1908. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1909. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  1910. findDataList = append(findDataList, tmpValue)
  1911. } else {
  1912. findDataList = append(findDataList, 0)
  1913. }
  1914. }
  1915. yDate := "0000-00-00"
  1916. if !maxDate.IsZero() {
  1917. yDate = maxDate.Format(utils.FormatDate)
  1918. }
  1919. yDataList = append(yDataList, chart_info.RadarYData{
  1920. Date: yDate,
  1921. Value: findDataList,
  1922. Color: chartDate.Color,
  1923. Name: chartDate.Name,
  1924. })
  1925. }
  1926. chartDataResp = chart_info.RadarChartInfoResp{
  1927. YDataList: yDataList,
  1928. XEdbIdValue: edbIdList,
  1929. }
  1930. return
  1931. }