chart.go 24 KB

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