chart.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_chart_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "time"
  9. "github.com/nosixtools/solarlunar"
  10. )
  11. type ChartInfo struct {
  12. ChartInfoId int `json:"-" orm:"column(chart_info_id);pk"`
  13. ChartName string `description:"来源名称"`
  14. ChartNameEn string `description:"英文图表名称"`
  15. ChartClassifyId int `json:"-" description:"图表分类id"`
  16. SysUserId int `json:"-"`
  17. SysUserRealName string `json:"-"`
  18. UniqueCode string `description:"图表唯一编码"`
  19. CreateTime time.Time `json:"-"`
  20. ModifyTime time.Time `json:"-"`
  21. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  22. StartDate string `description:"自定义开始日期"`
  23. EndDate string `description:"自定义结束日期"`
  24. IsSetName int `description:"设置名称"`
  25. EdbInfoIds string `description:"指标id"`
  26. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  27. Calendar string `description:"公历/农历"`
  28. SeasonStartDate string `description:"季节性图开始日期"`
  29. SeasonEndDate string `description:"季节性图开始日期"`
  30. ChartImage string `description:"图表图片"`
  31. BarConfig string `description:"柱方图的配置,json数据" json:"-"`
  32. LeftMin string `description:"图表左侧最小值"`
  33. LeftMax string `description:"图表左侧最大值"`
  34. RightMin string `description:"图表右侧最小值"`
  35. RightMax string `description:"图表右侧最大值"`
  36. Right2Min string `description:"图表右侧2最小值"`
  37. Right2Max string `description:"图表右侧2最大值"`
  38. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  39. Source int `description:"1:ETA图库;2:商品价格曲线"`
  40. Unit string `description:"中文单位名称"`
  41. UnitEn string `description:"英文单位名称"`
  42. ExtraConfig string `description:"图表额外配置,json数据" json:"-"`
  43. ChartSource string `description:"图表来源str"`
  44. ChartSourceEn string `description:"图表来源(英文)"`
  45. SeasonExtraConfig string `description:"季节性图表中的配置,json数据"`
  46. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  47. ChartThemeId int `description:"图表应用主题ID"`
  48. ChartThemeStyle string `description:"图表应用主题样式"`
  49. SourcesFrom string `description:"图表来源"`
  50. Instructions string `description:"图表说明"`
  51. MarkersLines string `description:"标识线"`
  52. MarkersAreas string `description:"标识区"`
  53. }
  54. func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfo, err error) {
  55. o := orm.NewOrmUsingDB("data")
  56. sql := ` SELECT * FROM chart_info WHERE unique_code=? `
  57. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  58. return
  59. }
  60. type ChartEdbInfoMapping struct {
  61. EdbInfoId int `description:"指标id"`
  62. SourceName string `description:"来源名称"`
  63. Source int `description:"来源id"`
  64. EdbCode string `description:"指标编码"`
  65. EdbName string `description:"指标名称"`
  66. EdbAliasName string `description:"指标名称(别名)"`
  67. EdbAliasNameEn string `description:"英文指标名称(别名)"`
  68. EdbNameEn string `description:"英文指标名称"`
  69. Frequency string `description:"频率"`
  70. FrequencyEn string `description:"英文频率"`
  71. Unit string `description:"单位"`
  72. UnitEn string `description:"英文单位"`
  73. StartDate string `description:"起始日期"`
  74. EndDate string `description:"终止日期"`
  75. ModifyTime string `description:"指标最后更新时间"`
  76. ChartEdbMappingId int `description:"图表指标id" json:"-"`
  77. ChartInfoId int `description:"图表id"`
  78. MaxData float64 `description:"上限"`
  79. MinData float64 `description:"下限"`
  80. IsOrder bool `description:"true:正序,false:逆序"`
  81. IsAxis int `description:"1:左轴,0:右轴"`
  82. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  83. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  84. LeadValue int `description:"领先值"`
  85. LeadUnit string `description:"领先单位"`
  86. LeadUnitEn string `description:"领先英文单位"`
  87. ChartStyle string `description:"图表类型"`
  88. ChartColor string `description:"颜色"`
  89. ChartWidth float64 `description:"线条大小"`
  90. DataList interface{}
  91. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  92. PredictChartColor string `description:"预测数据的颜色"`
  93. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图"`
  94. LatestDate string `description:"数据最新日期"`
  95. LatestValue float64 `description:"数据最新值"`
  96. MoveLatestDate string `description:"移动后的数据最新日期"`
  97. UniqueCode string `description:"指标唯一编码"`
  98. MinValue float64 `json:"-" description:"最小值"`
  99. MaxValue float64 `json:"-" description:"最大值"`
  100. IsNullData bool `json:"-" description:"是否空数据"`
  101. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  102. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  103. SubSourceName string `description:"子数据来源名称"`
  104. IndicatorCode string `description:"指标代码"`
  105. StockCode string `description:"证券代码"`
  106. IsConvert int `description:"是否数据转换 0不转 1转"`
  107. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  108. ConvertValue float64 `description:"数据转换值"`
  109. ConvertUnit string `description:"数据转换单位"`
  110. ConvertEnUnit string `description:"数据转换单位"`
  111. }
  112. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source,a.edb_alias_name`
  115. sql := ` SELECT ` + aField + `,b.source_name,b.source,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,b.end_date,b.modify_time,b.edb_type,b.latest_date,b.latest_value,b.unique_code,b.edb_info_type AS edb_info_category_type,b.sub_source,
  116. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  117. FROM chart_edb_mapping AS a
  118. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  119. WHERE chart_info_id=?
  120. ORDER BY chart_edb_mapping_id ASC `
  121. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  122. return
  123. }
  124. type EdbDataList struct {
  125. EdbDataId int `json:"-" description:" 指标数据ID"`
  126. EdbInfoId int `json:"-" description:"指标ID"`
  127. DataTime string `json:"-" description:"数据日期"`
  128. DataTimestamp int64 `description:"数据日期"`
  129. Value float64 `description:"数据值"`
  130. }
  131. // GetEdbDataList 获取指标的数据(日期正序返回)
  132. func GetEdbDataList(source, subSource, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  133. tableName := GetEdbDataTableName(source, subSource)
  134. if tableName == "" {
  135. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  136. list = make([]*EdbDataList, 0)
  137. return list, err
  138. }
  139. var pars []interface{}
  140. var condition string
  141. if startDate != "" {
  142. condition += ` AND data_time>=? `
  143. pars = append(pars, startDate)
  144. }
  145. if endDate != "" {
  146. condition += ` AND data_time<=? `
  147. pars = append(pars, endDate)
  148. }
  149. sql := `SELECT edb_data_id,edb_info_id,data_time,TRUNCATE(value,4) AS value,data_timestamp FROM %s WHERE edb_info_id=? `
  150. if condition != "" {
  151. sql += condition
  152. }
  153. sql += ` ORDER BY data_time ASC `
  154. sql = fmt.Sprintf(sql, tableName)
  155. o := orm.NewOrmUsingDB("data")
  156. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  157. return
  158. }
  159. type EdbDataItemList struct {
  160. EdbDataId int `json:"-" description:" 指标数据ID"`
  161. EdbInfoId int `json:"-" description:"指标ID"`
  162. DataTime string `description:"数据日期"`
  163. DataTimestamp int64 `description:"数据日期"`
  164. Value float64 `description:"数据值"`
  165. }
  166. type EdbDataItems struct {
  167. Items []*EdbDataList
  168. Year int
  169. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  170. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  171. }
  172. type EdbDataResult struct {
  173. List []*EdbDataItems
  174. }
  175. type QuarterData struct {
  176. Year string
  177. DataList []*EdbDataList
  178. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  179. ChartLegend string
  180. Years string
  181. }
  182. type QuarterXDateItem struct {
  183. StartDate time.Time
  184. EndDate time.Time
  185. ShowName string
  186. ChartLegend string
  187. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  188. }
  189. type ChartInfoDetailResp struct {
  190. ChartInfo *ChartInfo
  191. EdbInfoList []*ChartEdbInfoMapping
  192. XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
  193. YDataList []YData `description:"柱方图的y轴数据"`
  194. XDataList []XData `description:"商品价格曲线的X轴数据"`
  195. CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
  196. DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
  197. WaterMark string `description:"水印"`
  198. }
  199. // XData 商品价格曲线的的x轴数据
  200. type XData struct {
  201. Name string `description:"别名"`
  202. NameEn string `description:"英文别名"`
  203. }
  204. // YData 柱方图的y轴数据
  205. type YData struct {
  206. Date string `description:"数据日期"`
  207. ConfigDate time.Time `description:"配置的日期" json:"-"`
  208. Color string `description:"数据颜色"`
  209. Name string `description:"别名"`
  210. NameEn string `description:"英文别名"`
  211. Value []float64 `description:"每个指标的值"`
  212. NoDataEdbList []int `description:"没有数据的指标列表"`
  213. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  214. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  215. M []int `description:"对应开始日期的间隔值" json:"-"`
  216. NameList []string `description:"每个值对应的名称"`
  217. EnNameList []string `description:"每个值对应的英文名称"`
  218. }
  219. // RollingCorrelationChartDataResp 滚动相关性图
  220. type RollingCorrelationChartDataResp struct {
  221. XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
  222. YDataList []YData `description:"滚动相关性图的y轴数据"`
  223. }
  224. // 指标季度数据计算(公历转农历)
  225. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  226. var errMsg string
  227. defer func() {
  228. if errMsg != "" {
  229. fmt.Println("errMsg:", errMsg)
  230. }
  231. }()
  232. if len(dataList) <= 0 {
  233. result.List = make([]*EdbDataItems, 0)
  234. return
  235. }
  236. endDate := dataList[len(dataList)-1].DataTime
  237. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  238. if err != nil {
  239. return result, err
  240. }
  241. thisMonth := int(endDateForm.Month())
  242. result = new(EdbDataResult)
  243. var yearArr []int
  244. yearMap := make(map[int]int)
  245. var cureentDate time.Time
  246. if thisMonth < 11 {
  247. for k, v := range dataList {
  248. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  249. if err != nil {
  250. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  251. return result, err
  252. }
  253. if k == len(dataList)-1 {
  254. cureentDate = dateTime
  255. }
  256. year := dateTime.Year()
  257. if _, ok := yearMap[year]; !ok {
  258. yearArr = append(yearArr, year)
  259. }
  260. yearMap[year] = year
  261. }
  262. } else {
  263. for k, v := range dataList {
  264. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  265. if err != nil {
  266. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  267. return result, err
  268. }
  269. if k == len(dataList)-1 {
  270. cureentDate = dateTime
  271. }
  272. year := dateTime.Year() + 1
  273. if _, ok := yearMap[year]; !ok {
  274. yearArr = append(yearArr, year)
  275. }
  276. yearMap[year] = year
  277. }
  278. }
  279. //排序
  280. fmt.Println("yearArr:", yearArr)
  281. thisYear := cureentDate.Year()
  282. //thisMonth := int(cureentDate.Month())
  283. fmt.Println("thisMonth:", thisMonth)
  284. for ky, vy := range yearArr {
  285. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  286. if thisMonth < 11 {
  287. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  288. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  289. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  290. if err != nil {
  291. errMsg = "生成当前春节失败,Err:" + err.Error()
  292. return result, err
  293. }
  294. preYear := vy
  295. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  296. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  297. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  298. if err != nil {
  299. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  300. return result, err
  301. }
  302. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  303. items := new(EdbDataItems)
  304. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  305. items.Year = preYear
  306. for _, v := range dataList {
  307. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  308. if err != nil {
  309. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  310. return result, err
  311. }
  312. newDate := dateTime.AddDate(0, 0, int(day))
  313. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  314. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  315. if newDate.Before(selectDate) || newDate == selectDate {
  316. timestamp := newDate.UnixNano() / 1e6
  317. item := new(EdbDataList)
  318. item.DataTime = newDate.Format(utils.FormatDate)
  319. item.EdbInfoId = v.EdbInfoId
  320. item.Value = v.Value
  321. item.EdbDataId = v.EdbDataId
  322. item.DataTimestamp = timestamp
  323. items.Items = append(items.Items, item)
  324. }
  325. }
  326. result.List = append(result.List, items)
  327. } else {
  328. nextYear := thisYear + 1
  329. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  330. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  331. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  332. if err != nil {
  333. errMsg = "生成当前春节失败,Err:" + err.Error()
  334. return result, err
  335. }
  336. preYear := vy
  337. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  338. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  339. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  340. if err != nil {
  341. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  342. return result, err
  343. }
  344. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  345. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  346. items := new(EdbDataItems)
  347. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  348. items.Year = preYear - 1
  349. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  350. if ky+1 < len(yearArr) {
  351. for _, v := range dataList {
  352. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  353. if err != nil {
  354. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  355. return result, err
  356. }
  357. newDate := dateTime.AddDate(0, 0, int(day))
  358. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  359. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  360. if newDate.Before(selectDate) || newDate == selectDate {
  361. timestamp := newDate.UnixNano() / 1e6
  362. item := new(EdbDataList)
  363. item.DataTime = newDate.Format(utils.FormatDate)
  364. item.EdbInfoId = v.EdbInfoId
  365. item.Value = v.Value
  366. item.EdbDataId = v.EdbDataId
  367. item.DataTimestamp = timestamp
  368. items.Items = append(items.Items, item)
  369. }
  370. }
  371. result.List = append(result.List, items)
  372. } else {
  373. for _, v := range dataList {
  374. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  375. if err != nil {
  376. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  377. return result, err
  378. }
  379. timestamp := dateTime.UnixNano() / 1e6
  380. item := new(EdbDataList)
  381. item.DataTime = dateTime.Format(utils.FormatDate)
  382. item.EdbInfoId = v.EdbInfoId
  383. item.Value = v.Value
  384. item.EdbDataId = v.EdbDataId
  385. item.DataTimestamp = timestamp
  386. items.Items = append(items.Items, item)
  387. }
  388. result.List = append(result.List, items)
  389. }
  390. }
  391. }
  392. return
  393. }
  394. // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
  395. func AddCalculateQuarterV6(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  396. var errMsg string
  397. defer func() {
  398. if errMsg != "" {
  399. fmt.Println("errMsg:", errMsg)
  400. }
  401. }()
  402. endDate := dataList[len(dataList)-1].DataTime
  403. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  404. if err != nil {
  405. return result, err
  406. }
  407. thisMonth := int(endDateForm.Month())
  408. result = new(EdbDataResult)
  409. var yearArr []int
  410. yearMap := make(map[int]int)
  411. var cureentDate time.Time
  412. if thisMonth < 11 {
  413. for k, v := range dataList {
  414. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  415. if err != nil {
  416. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  417. return result, err
  418. }
  419. if k == len(dataList)-1 {
  420. cureentDate = dateTime
  421. }
  422. year := dateTime.Year()
  423. if _, ok := yearMap[year]; !ok {
  424. yearArr = append(yearArr, year)
  425. }
  426. yearMap[year] = year
  427. }
  428. } else {
  429. for k, v := range dataList {
  430. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  431. if err != nil {
  432. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  433. return result, err
  434. }
  435. if k == len(dataList)-1 {
  436. cureentDate = dateTime
  437. }
  438. year := dateTime.Year() + 1
  439. if _, ok := yearMap[year]; !ok {
  440. yearArr = append(yearArr, year)
  441. }
  442. yearMap[year] = year
  443. }
  444. }
  445. //排序
  446. fmt.Println("yearArr:", yearArr)
  447. thisYear := cureentDate.Year()
  448. //thisMonth := int(cureentDate.Month())
  449. fmt.Println("thisMonth:", thisMonth)
  450. for ky, vy := range yearArr {
  451. fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
  452. fmt.Println("")
  453. if thisMonth < 11 {
  454. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  455. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  456. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  457. if err != nil {
  458. errMsg = "生成当前春节失败,Err:" + err.Error()
  459. return result, err
  460. }
  461. preYear := vy
  462. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  463. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  464. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  465. if err != nil {
  466. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  467. return result, err
  468. }
  469. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  470. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  471. items := new(EdbDataItems)
  472. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  473. items.Year = preYear
  474. for _, v := range dataList {
  475. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  476. if err != nil {
  477. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  478. return result, err
  479. }
  480. newDate := dateTime.AddDate(0, 0, int(day))
  481. timestamp := newDate.UnixNano() / 1e6
  482. item := new(EdbDataList)
  483. item.DataTime = newDate.Format(utils.FormatDate)
  484. item.EdbInfoId = v.EdbInfoId
  485. item.Value = v.Value
  486. item.EdbDataId = v.EdbDataId
  487. item.DataTimestamp = timestamp
  488. items.Items = append(items.Items, item)
  489. }
  490. result.List = append(result.List, items)
  491. } else {
  492. nextYear := thisYear + 1
  493. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  494. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  495. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  496. if err != nil {
  497. errMsg = "生成当前春节失败,Err:" + err.Error()
  498. return result, err
  499. }
  500. preYear := vy
  501. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  502. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  503. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  504. if err != nil {
  505. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  506. return result, err
  507. }
  508. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  509. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  510. items := new(EdbDataItems)
  511. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  512. items.Year = preYear
  513. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  514. //if ky+1 < len(yearArr) {
  515. for _, v := range dataList {
  516. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  517. if err != nil {
  518. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  519. return result, err
  520. }
  521. newDate := dateTime.AddDate(0, 0, int(day))
  522. timestamp := newDate.UnixNano() / 1e6
  523. item := new(EdbDataList)
  524. item.DataTime = newDate.Format(utils.FormatDate)
  525. item.EdbInfoId = v.EdbInfoId
  526. item.Value = v.Value
  527. item.EdbDataId = v.EdbDataId
  528. item.DataTimestamp = timestamp
  529. items.Items = append(items.Items, item)
  530. }
  531. result.List = append(result.List, items)
  532. /*} else {
  533. for _, v := range dataList {
  534. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  535. if err != nil {
  536. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  537. return result, err
  538. }
  539. timestamp := dateTime.UnixNano() / 1e6
  540. item := new(EdbDataList)
  541. item.DataTime = dateTime.Format(utils.FormatDate)
  542. item.EdbInfoId = v.EdbInfoId
  543. item.Value = v.Value
  544. item.EdbDataId = v.EdbDataId
  545. item.DataTimestamp = timestamp
  546. items.Items = append(items.Items, item)
  547. }
  548. result.List = append(result.List, items)
  549. }*/
  550. }
  551. }
  552. return
  553. }
  554. type CorrelationInfo struct {
  555. LeadValue int `description:"领先值"`
  556. LeadUnit string `description:"领先单位"`
  557. StartDate string `description:"开始日期"`
  558. EndDate string `description:"结束日期"`
  559. EdbInfoIdFirst int `description:"A指标ID"`
  560. EdbInfoIdSecond int `description:"B指标ID"`
  561. PeriodData string `description:"X轴-期数数据"`
  562. CorrelationData string `description:"Y轴-相关性系数"`
  563. }
  564. type SeasonExtraItem struct {
  565. ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
  566. XStartDate string `description:"横坐标显示的起始日"`
  567. XEndDate string `description:"横坐标显示的截止日"`
  568. JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
  569. }
  570. type SeasonChartLegend struct {
  571. Name string
  572. Value string
  573. }
  574. type QuarterDataList []*QuarterData
  575. func (m QuarterDataList) Len() int {
  576. return len(m)
  577. }
  578. func (m QuarterDataList) Less(i, j int) bool {
  579. return m[i].Years < m[j].Years
  580. }
  581. func (m QuarterDataList) Swap(i, j int) {
  582. m[i], m[j] = m[j], m[i]
  583. }