chart_info.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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) (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. default:
  94. xEdbIdValue = make([]int, 0)
  95. yDataList = make([]data_manage.YData, 0)
  96. }
  97. // 指标对应的所有数据
  98. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList)
  99. if err != nil {
  100. return
  101. }
  102. // 特殊图形数据处理
  103. switch chartType {
  104. case 7: // 柱形图
  105. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  106. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  107. for _, v := range edbList {
  108. // 指标别名
  109. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  110. for _, reqEdb := range barChartConf.EdbInfoIdList {
  111. if v.EdbInfoId == reqEdb.EdbInfoId {
  112. v.EdbAliasName = reqEdb.Name
  113. }
  114. }
  115. }
  116. }
  117. for k := range yDataList {
  118. yDataList[k].Unit = barChartConf.Unit
  119. yDataList[k].UnitEn = barChartConf.UnitEn
  120. }
  121. case 10: // 截面散点图
  122. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  123. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  124. // 这个数据没有必要返回给前端
  125. for _, v := range edbList {
  126. v.DataList = nil
  127. }
  128. }
  129. return
  130. }
  131. // GetEdbDataMapList 获取指标最后的基础数据
  132. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  133. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList)
  134. return
  135. }
  136. // getEdbDataMapList 获取指标最后的基础数据
  137. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  138. // 指标对应的所有数据
  139. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  140. for _, v := range mappingList {
  141. //fmt.Println("v:", v.EdbInfoId)
  142. item := new(data_manage.ChartEdbInfoMapping)
  143. item.EdbInfoId = v.EdbInfoId
  144. item.SourceName = v.SourceName
  145. item.Source = v.Source
  146. item.EdbCode = v.EdbCode
  147. item.EdbName = v.EdbName
  148. item.EdbNameEn = v.EdbNameEn
  149. item.Frequency = v.Frequency
  150. item.EdbType = v.EdbType
  151. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  152. if v.Unit != `无` {
  153. item.Unit = v.Unit
  154. }
  155. item.UnitEn = v.UnitEn
  156. item.StartDate = v.StartDate
  157. item.EndDate = v.EndDate
  158. item.ModifyTime = v.ModifyTime
  159. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  160. item.PredictChartColor = v.PredictChartColor
  161. item.ClassifyId = v.ClassifyId
  162. if chartInfoId <= 0 {
  163. item.IsAxis = 1
  164. item.LeadValue = 0
  165. item.LeadUnit = ""
  166. item.ChartEdbMappingId = 0
  167. item.ChartInfoId = 0
  168. item.IsOrder = false
  169. item.EdbInfoType = 1
  170. item.ChartStyle = ""
  171. item.ChartColor = ""
  172. item.ChartWidth = 0
  173. item.MaxData = v.MaxValue
  174. item.MinData = v.MinValue
  175. } else {
  176. item.IsAxis = v.IsAxis
  177. item.EdbInfoType = v.EdbInfoType
  178. item.LeadValue = v.LeadValue
  179. item.LeadUnit = v.LeadUnit
  180. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  181. item.ChartEdbMappingId = v.ChartEdbMappingId
  182. item.ChartInfoId = v.ChartInfoId
  183. item.ChartStyle = v.ChartStyle
  184. item.ChartColor = v.ChartColor
  185. item.ChartWidth = v.ChartWidth
  186. item.IsOrder = v.IsOrder
  187. item.MaxData = v.MaxData
  188. item.MinData = v.MinData
  189. }
  190. item.LatestValue = v.LatestValue
  191. item.LatestDate = v.LatestDate
  192. item.UniqueCode = v.UniqueCode
  193. item.MoveLatestDate = v.LatestDate
  194. var startDateReal string
  195. var diffSeconds int64
  196. if chartType == 2 { //季节性图
  197. startDateReal = startDate
  198. } else {
  199. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  200. var startTimeRealTemp time.Time
  201. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  202. switch v.LeadUnit {
  203. case "天":
  204. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  205. case "月":
  206. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  207. case "季":
  208. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  209. case "周":
  210. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  211. case "年":
  212. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  213. }
  214. if startTimeRealTemp.Before(startDateParse) {
  215. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  216. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  217. } else {
  218. startDateReal = startDate
  219. diffSeconds = 0
  220. }
  221. // 预测指标的开始日期也要偏移
  222. {
  223. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  224. if tmpErr != nil {
  225. err = tmpErr
  226. return
  227. }
  228. latestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.LatestDate, time.Local)
  229. if tmpErr != nil {
  230. err = tmpErr
  231. return
  232. }
  233. item.LatestDate = latestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  234. }
  235. } else {
  236. startDateReal = startDate
  237. }
  238. }
  239. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  240. calendarPreYear := 0
  241. if calendar == "农历" {
  242. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  243. if err != nil {
  244. fmt.Println("time.Parse:" + err.Error())
  245. }
  246. calendarPreYear = newStartDateReal.Year() - 1
  247. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  248. startDateReal = newStartDateReal.Format(utils.FormatDate)
  249. }
  250. dataList := make([]*data_manage.EdbDataList, 0)
  251. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  252. //var newEdbInfo *data_manage.EdbInfo
  253. switch v.EdbInfoCategoryType {
  254. case 0:
  255. dataList, err = data_manage.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  256. case 1:
  257. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
  258. default:
  259. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  260. }
  261. if err != nil {
  262. return
  263. }
  264. edbDataListMap[v.EdbInfoId] = dataList
  265. if diffSeconds != 0 && v.EdbInfoType == 0 {
  266. dataListLen := len(dataList)
  267. for i := 0; i < dataListLen; i++ {
  268. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  269. }
  270. }
  271. if chartType == 2 {
  272. latestDateStr := v.LatestDate //实际数据的截止日期
  273. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  274. if tmpErr != nil {
  275. //item.DataList = dataList
  276. item.IsNullData = true
  277. edbList = append(edbList, item)
  278. continue
  279. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  280. return
  281. }
  282. latestDateYear := latestDate.Year() //实际数据截止年份
  283. if calendar == "农历" {
  284. if len(dataList) <= 0 {
  285. result := new(data_manage.EdbDataResult)
  286. item.DataList = result
  287. } else {
  288. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  289. if tmpErr != nil {
  290. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  291. return
  292. }
  293. // 处理季节图的截止日期
  294. for k, edbDataItems := range result.List {
  295. var cuttingDataTimestamp int64
  296. // 切割的日期时间字符串
  297. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  298. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  299. if edbDataItems.Year >= latestDateYear {
  300. for _, tmpData := range edbDataItems.Items {
  301. if tmpData.DataTime == cuttingDataTimeStr {
  302. cuttingDataTimestamp = tmpData.DataTimestamp
  303. break
  304. }
  305. }
  306. }
  307. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  308. result.List[k] = edbDataItems
  309. }
  310. if result.List[0].Year != calendarPreYear {
  311. itemList := make([]*data_manage.EdbDataList, 0)
  312. items := new(data_manage.EdbDataItems)
  313. //items.Year = calendarPreYear
  314. items.Items = itemList
  315. newResult := new(data_manage.EdbDataResult)
  316. newResult.List = append(newResult.List, items)
  317. newResult.List = append(newResult.List, result.List...)
  318. item.DataList = newResult
  319. } else {
  320. item.DataList = result
  321. }
  322. }
  323. } else {
  324. currentYear := time.Now().Year()
  325. quarterDataList := make([]*data_manage.QuarterData, 0)
  326. quarterMap := make(map[int][]*data_manage.EdbDataList)
  327. var quarterArr []int
  328. for _, v := range dataList {
  329. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  330. if tmpErr != nil {
  331. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  332. return
  333. }
  334. year := itemDate.Year()
  335. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  336. timestamp := newItemDate.UnixNano() / 1e6
  337. v.DataTimestamp = timestamp
  338. if findVal, ok := quarterMap[year]; !ok {
  339. quarterArr = append(quarterArr, year)
  340. findVal = append(findVal, v)
  341. quarterMap[year] = findVal
  342. } else {
  343. findVal = append(findVal, v)
  344. quarterMap[year] = findVal
  345. }
  346. }
  347. for _, v := range quarterArr {
  348. itemList := quarterMap[v]
  349. quarterItem := new(data_manage.QuarterData)
  350. quarterItem.Year = v
  351. quarterItem.DataList = itemList
  352. //如果等于最后的实际日期,那么将切割时间戳记录
  353. if v == latestDateYear {
  354. var cuttingDataTimestamp int64
  355. for _, tmpData := range itemList {
  356. if tmpData.DataTime == latestDateStr {
  357. cuttingDataTimestamp = tmpData.DataTimestamp
  358. break
  359. }
  360. }
  361. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  362. } else if v > latestDateYear {
  363. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  364. if len(itemList) > 0 {
  365. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  366. }
  367. }
  368. quarterDataList = append(quarterDataList, quarterItem)
  369. }
  370. item.DataList = quarterDataList
  371. }
  372. } else if chartType == 7 { //柱方图
  373. //item.DataList = dataList
  374. } else {
  375. item.DataList = dataList
  376. }
  377. edbList = append(edbList, item)
  378. }
  379. return
  380. }
  381. // BarChartData 柱方图的数据处理
  382. 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) {
  383. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  384. edbDataMap := make(map[int]map[string]float64)
  385. for edbInfoId, edbDataList := range edbDataListMap {
  386. edbDateData := make(map[string]float64)
  387. for _, edbData := range edbDataList {
  388. edbDateData[edbData.DataTime] = edbData.Value
  389. }
  390. edbDataMap[edbInfoId] = edbDateData
  391. }
  392. // edbIdList 指标展示顺序;x轴的指标顺序
  393. edbIdList = make([]int, 0)
  394. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  395. dateData := make(map[int]float64)
  396. if barChartInfoSort.Sort == 0 {
  397. for _, v := range mappingList {
  398. edbIdList = append(edbIdList, v.EdbInfoId)
  399. }
  400. } else {
  401. lenBarChartInfoDateList := len(barChartInfoDateList)
  402. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  403. err = errors.New("排序日期异常")
  404. return
  405. }
  406. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  407. // 日期配置
  408. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  409. for edbInfoId, dataList := range edbDataListMap {
  410. if len(dataList) <= 0 {
  411. // 没有数据的指标id
  412. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  413. continue
  414. }
  415. findDate := barChartInfoDate.Date
  416. switch barChartInfoDate.Type {
  417. case 1: //最新值
  418. findDate = dataList[len(dataList)-1].DataTime
  419. case 2: //近期几天
  420. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  421. if tmpErr != nil {
  422. err = tmpErr
  423. return
  424. }
  425. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  426. lenData := len(dataList) - 1
  427. for i := lenData; i >= 0; i-- {
  428. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  429. if tmpErr != nil {
  430. err = tmpErr
  431. return
  432. }
  433. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  434. findDate = dataList[i].DataTime
  435. break
  436. }
  437. }
  438. case 3: // 固定日期
  439. //最早的日期
  440. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  441. if tmpErr != nil {
  442. err = tmpErr
  443. return
  444. }
  445. //寻找固定日期的数据
  446. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  447. if tmpErr != nil {
  448. err = tmpErr
  449. return
  450. }
  451. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  452. tmpDate := tmpDateTime.Format(utils.FormatDate)
  453. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  454. findDate = tmpDate
  455. break
  456. }
  457. }
  458. default:
  459. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  460. return
  461. }
  462. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  463. dateData[edbInfoId] = tmpValue
  464. } else {
  465. // 没有数据的指标id
  466. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  467. }
  468. }
  469. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  470. // 排序
  471. dateDataSort := utils.NewMapSorter(dateData)
  472. sort.Sort(dateDataSort)
  473. if barChartInfoSort.Sort == 1 {
  474. // 先将没有数据的指标id放在最前面
  475. if len(notDataEdbIdList) > 0 {
  476. edbIdList = append(edbIdList, notDataEdbIdList...)
  477. }
  478. for _, v := range dateDataSort {
  479. edbIdList = append(edbIdList, v.Key)
  480. }
  481. } else {
  482. for i := len(dateDataSort) - 1; i >= 0; i-- {
  483. edbIdList = append(edbIdList, dateDataSort[i].Key)
  484. }
  485. // 再将没有数据的指标id放在最后面
  486. if len(notDataEdbIdList) > 0 {
  487. edbIdList = append(edbIdList, notDataEdbIdList...)
  488. }
  489. }
  490. }
  491. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  492. for _, barChartInfoDate := range barChartInfoDateList {
  493. var maxDate time.Time
  494. findDataList := make([]float64, 0) // 当前日期的数据值
  495. for _, edbInfoId := range edbIdList {
  496. findDate := barChartInfoDate.Date //需要的日期值
  497. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  498. if len(dataList) <= 0 {
  499. // 没有数据的指标id
  500. findDataList = append(findDataList, 0)
  501. continue
  502. }
  503. switch barChartInfoDate.Type {
  504. case 1: //最新值
  505. dataList := edbDataListMap[edbInfoId]
  506. findDate = dataList[len(dataList)-1].DataTime
  507. case 2: //近期几天
  508. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  509. if tmpErr != nil {
  510. err = tmpErr
  511. return
  512. }
  513. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  514. lenData := len(dataList) - 1
  515. for i := lenData; i >= 0; i-- {
  516. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  517. if tmpErr != nil {
  518. err = tmpErr
  519. return
  520. }
  521. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  522. findDate = dataList[i].DataTime
  523. break
  524. }
  525. }
  526. case 3: // 固定日期
  527. //最早的日期
  528. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  529. if tmpErr != nil {
  530. err = tmpErr
  531. return
  532. }
  533. //寻找固定日期的数据
  534. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  535. if tmpErr != nil {
  536. err = tmpErr
  537. return
  538. }
  539. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  540. tmpDate := tmpDateTime.Format(utils.FormatDate)
  541. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  542. findDate = tmpDate
  543. break
  544. }
  545. }
  546. default:
  547. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  548. return
  549. }
  550. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  551. if maxDate.IsZero() {
  552. maxDate = findDateTime
  553. } else {
  554. if findDateTime.After(maxDate) {
  555. maxDate = findDateTime
  556. }
  557. }
  558. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  559. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  560. findDataList = append(findDataList, tmpValue)
  561. } else {
  562. findDataList = append(findDataList, 0)
  563. }
  564. }
  565. yDate := "0000-00-00"
  566. if !maxDate.IsZero() {
  567. yDate = maxDate.Format(utils.FormatDate)
  568. }
  569. yDataList = append(yDataList, data_manage.YData{
  570. Date: yDate,
  571. Value: findDataList,
  572. Color: barChartInfoDate.Color,
  573. Name: barChartInfoDate.Name,
  574. })
  575. }
  576. return
  577. }
  578. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  579. // 相关性图表不判断指标
  580. if source == utils.CHART_SOURCE_CORRELATION && chartNameEn != "" {
  581. return true
  582. }
  583. if chartNameEn == "" {
  584. return false
  585. }
  586. // 截面散点图的话,肯定是有英文配置的
  587. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  588. return true
  589. }
  590. for _, v := range edbList {
  591. if v.EdbNameEn == "" {
  592. return false
  593. }
  594. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  595. return false
  596. }
  597. }
  598. return true
  599. }
  600. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  601. if edbNameEn == "" {
  602. return false
  603. }
  604. if unit != "无" && unit != "" && unitEn == "" {
  605. return false
  606. }
  607. return true
  608. }
  609. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  610. // return int 0:启用,1:禁用
  611. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  612. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  613. if err != nil {
  614. return
  615. }
  616. //没有配置,立马返回
  617. if crmConfig.ConfigValue == `` {
  618. return
  619. }
  620. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  621. for _, edbInfoId := range edbInfoIdArr {
  622. for _, confEdbInfoId := range confEdbInfoIdList {
  623. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  624. isDisable = 1
  625. return
  626. }
  627. }
  628. }
  629. return
  630. }
  631. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  632. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  633. switch chartType {
  634. case 10: //截面散点
  635. var extraConfig data_manage.SectionScatterReq
  636. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  637. if err != nil {
  638. return
  639. }
  640. // 判断是否有配置日期序列
  641. if len(extraConfig.SeriesList) <= 0 {
  642. errMsg = `请配置序列`
  643. err = errors.New(errMsg)
  644. return
  645. }
  646. // 判断是否有填写指标
  647. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  648. errMsg = `请选择指标`
  649. err = errors.New(errMsg)
  650. return
  651. }
  652. // 遍历指标列表获取指标id
  653. edbIdMap := make(map[int]int)
  654. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  655. // X 轴的指标id
  656. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  657. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  658. edbIdList = append(edbIdList, v.XEdbInfoId)
  659. }
  660. // Y 轴的指标id
  661. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  662. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  663. edbIdList = append(edbIdList, v.YEdbInfoId)
  664. }
  665. }
  666. }
  667. return
  668. }
  669. // GetSectionScatterChartData 截面散点图的数据处理
  670. func GetSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  671. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  672. edbDataMap := make(map[int]map[string]float64)
  673. for edbInfoId, edbDataList := range edbDataListMap {
  674. edbDateData := make(map[string]float64)
  675. for _, edbData := range edbDataList {
  676. edbDateData[edbData.DataTime] = edbData.Value
  677. }
  678. edbDataMap[edbInfoId] = edbDateData
  679. }
  680. // edbIdList 指标展示顺序;x轴的指标顺序
  681. edbIdList = make([]int, 0)
  682. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  683. for _, v := range mappingList {
  684. edbIdList = append(edbIdList, v.EdbInfoId)
  685. edbMappingMap[v.EdbInfoId] = v
  686. }
  687. //SectionScatterSeriesInfoResp
  688. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  689. for _, seriesItem := range extraConfig.SeriesList {
  690. var maxDate time.Time
  691. // 系列中的指标数据
  692. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  693. var minXVal, maxXVal, minYVal, maxYVal float64
  694. for _, edbConf := range seriesItem.EdbInfoList {
  695. tmpItem := data_manage.SectionScatterEdbItemResp{
  696. IsShow: edbConf.IsShow,
  697. Name: edbConf.Name,
  698. NameEn: edbConf.NameEn,
  699. } //单个坐标点的数据
  700. //X轴的数据
  701. {
  702. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  703. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  704. if !ok {
  705. continue
  706. }
  707. findDate := edbConf.XDate //需要的日期值
  708. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  709. if len(dataList) <= 0 {
  710. // 没有数据的指标id
  711. //findDataList = append(findDataList, 0)
  712. continue
  713. }
  714. tmpItem.XEdbInfoId = edbInfoId
  715. tmpItem.XName = edbMappingInfo.EdbName
  716. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  717. switch edbConf.XDateType {
  718. case 1: //最新值
  719. findDate = dataList[len(dataList)-1].DataTime
  720. case 2: //近期几天
  721. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  722. if tmpErr != nil {
  723. err = tmpErr
  724. return
  725. }
  726. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  727. lenData := len(dataList) - 1
  728. for i := lenData; i >= 0; i-- {
  729. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  730. if tmpErr != nil {
  731. err = tmpErr
  732. return
  733. }
  734. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  735. findDate = dataList[i].DataTime
  736. break
  737. }
  738. }
  739. case 3: // 固定日期
  740. //最早的日期
  741. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  742. if tmpErr != nil {
  743. err = tmpErr
  744. return
  745. }
  746. //寻找固定日期的数据
  747. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  748. if tmpErr != nil {
  749. err = tmpErr
  750. return
  751. }
  752. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  753. tmpDate := tmpDateTime.Format(utils.FormatDate)
  754. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  755. findDate = tmpDate
  756. break
  757. }
  758. }
  759. default:
  760. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  761. return
  762. }
  763. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  764. if maxDate.IsZero() {
  765. maxDate = findDateTime
  766. } else {
  767. if findDateTime.After(maxDate) {
  768. maxDate = findDateTime
  769. }
  770. }
  771. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  772. tmpItem.XDate = findDate
  773. tmpItem.XValue = tmpValue
  774. } else {
  775. continue
  776. }
  777. }
  778. //Y轴的数据
  779. {
  780. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  781. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  782. if !ok {
  783. continue
  784. }
  785. findDate := edbConf.YDate //需要的日期值
  786. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  787. if len(dataList) <= 0 {
  788. // 没有数据的指标id
  789. //findDataList = append(findDataList, 0)
  790. continue
  791. }
  792. tmpItem.YEdbInfoId = edbInfoId
  793. tmpItem.YName = edbMappingInfo.EdbName
  794. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  795. switch edbConf.YDateType {
  796. case 1: //最新值
  797. findDate = dataList[len(dataList)-1].DataTime
  798. case 2: //近期几天
  799. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  800. if tmpErr != nil {
  801. err = tmpErr
  802. return
  803. }
  804. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  805. lenData := len(dataList) - 1
  806. for i := lenData; i >= 0; i-- {
  807. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  808. if tmpErr != nil {
  809. err = tmpErr
  810. return
  811. }
  812. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  813. findDate = dataList[i].DataTime
  814. break
  815. }
  816. }
  817. case 3: // 固定日期
  818. //最早的日期
  819. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  820. if tmpErr != nil {
  821. err = tmpErr
  822. return
  823. }
  824. //寻找固定日期的数据
  825. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  826. if tmpErr != nil {
  827. err = tmpErr
  828. return
  829. }
  830. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  831. tmpDate := tmpDateTime.Format(utils.FormatDate)
  832. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  833. findDate = tmpDate
  834. break
  835. }
  836. }
  837. default:
  838. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  839. return
  840. }
  841. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  842. if maxDate.IsZero() {
  843. maxDate = findDateTime
  844. } else {
  845. if findDateTime.After(maxDate) {
  846. maxDate = findDateTime
  847. }
  848. }
  849. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  850. tmpItem.YDate = findDate
  851. tmpItem.YValue = tmpValue
  852. } else {
  853. continue
  854. }
  855. }
  856. // 获取当前系列的X轴的最大最小值
  857. {
  858. if tmpItem.XValue < minXVal {
  859. minXVal = tmpItem.XValue
  860. }
  861. if tmpItem.XValue > maxXVal {
  862. maxXVal = tmpItem.XValue
  863. }
  864. if tmpItem.YValue < minYVal {
  865. minYVal = tmpItem.YValue
  866. }
  867. if tmpItem.YValue > maxYVal {
  868. maxYVal = tmpItem.YValue
  869. }
  870. }
  871. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  872. }
  873. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  874. var trendLine, rSquare string
  875. // 生成线性方程式
  876. var a, b float64
  877. {
  878. coordinateData := make([]utils.Coordinate, 0)
  879. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  880. tmpCoordinate1 := utils.Coordinate{
  881. X: tmpSeriesEdbInfo.XValue,
  882. Y: tmpSeriesEdbInfo.YValue,
  883. }
  884. coordinateData = append(coordinateData, tmpCoordinate1)
  885. }
  886. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  887. if len(coordinateData) >= 2 {
  888. a, b = utils.GetLinearResult(coordinateData)
  889. if !math.IsNaN(a) && !math.IsNaN(b) {
  890. if b > 0 {
  891. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  892. } else {
  893. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  894. }
  895. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  896. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  897. }
  898. // 计算R平方
  899. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  900. }
  901. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  902. X: minXVal,
  903. Y: minYVal,
  904. }, data_manage.CoordinatePoint{
  905. X: maxXVal,
  906. Y: maxYVal,
  907. })
  908. }
  909. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  910. Name: seriesItem.Name,
  911. NameEn: seriesItem.NameEn,
  912. Color: seriesItem.Color,
  913. EdbInfoList: tmpSeriesEdbInfoList,
  914. ShowTrendLine: seriesItem.ShowTrendLine,
  915. ShowFitEquation: seriesItem.ShowFitEquation,
  916. ShowRSquare: seriesItem.ShowRSquare,
  917. TrendLine: trendLine,
  918. RSquare: rSquare,
  919. TrendLimitData: trendLimitData,
  920. })
  921. }
  922. chartDataResp = data_manage.SectionScatterInfoResp{
  923. XName: extraConfig.XName,
  924. XNameEn: extraConfig.XNameEn,
  925. XUnitName: extraConfig.XUnitName,
  926. XUnitNameEn: extraConfig.XUnitNameEn,
  927. YName: extraConfig.YName,
  928. YNameEn: extraConfig.YNameEn,
  929. YUnitName: extraConfig.YUnitName,
  930. YUnitNameEn: extraConfig.YUnitNameEn,
  931. XMinValue: extraConfig.XMinValue,
  932. XMaxValue: extraConfig.XMaxValue,
  933. YMinValue: extraConfig.YMinValue,
  934. YMaxValue: extraConfig.YMaxValue,
  935. DataList: dataListResp,
  936. }
  937. return
  938. }
  939. // ChartInfoRefreshV2 图表刷新
  940. // @author Roc
  941. // @datetime 2022-09-16 10:15:38
  942. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  943. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  944. var errmsg string
  945. defer func() {
  946. if err != nil {
  947. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  948. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  949. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  950. }
  951. }()
  952. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  953. if err != nil {
  954. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  955. return
  956. }
  957. edbIdList := make([]int, 0)
  958. for _, v := range edbMappingList {
  959. edbIdList = append(edbIdList, v.EdbInfoId)
  960. }
  961. // 批量刷新
  962. err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true)
  963. if err != nil {
  964. return
  965. }
  966. return
  967. }
  968. // BatchChartInfoRefreshV2 图表批量刷新
  969. // @author Roc
  970. // @datetime 2022-09-16 10:15:38
  971. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  972. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo) (err error) {
  973. if len(chartInfoList) <= 0 {
  974. return
  975. }
  976. chartInfoIdSlice := make([]int, 0)
  977. for _, chartInfo := range chartInfoList {
  978. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  979. }
  980. var errmsg string
  981. defer func() {
  982. if err != nil {
  983. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  984. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  985. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  986. }
  987. }()
  988. edbIdList := make([]int, 0)
  989. edbInfoIdMap := make(map[int]int)
  990. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  991. for _, v := range edbMappingList {
  992. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  993. edbIdList = append(edbIdList, v.EdbInfoId)
  994. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  995. }
  996. }
  997. // 批量刷新
  998. err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true)
  999. if err != nil {
  1000. return
  1001. }
  1002. return
  1003. }